sim_data.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. import re
  3. import xml.dom.minidom
  4. import os
  5. try:
  6. import pygraphviz as pgv
  7. except ImportError:
  8. pgv = None
  9. def parse_multi_value_node(node):
  10. 'convert multi value node like <LayerDimensions N="10000" Nx="100" Ny="100"/> to dictionary {N:10000, Nx:100, Ny:100}'
  11. value_dict = dict()
  12. for key in node.attributes.keys():
  13. value_dict[key] = try_convert_value_string(node.getAttribute(key))
  14. return value_dict
  15. def parse_value_node(element_value_node):
  16. value = element_value_node.getAttribute("value")
  17. if value != "":
  18. # simple value node like <NormPos value="1"/>
  19. return try_convert_value_string(value)
  20. else:
  21. # value node with multiple attributes like <LayerDimensions N="10000" Nx="100" Ny="100"/>
  22. return parse_multi_value_node(element_value_node)
  23. def try_convert_value_string(value_string):
  24. v = value_string
  25. try:
  26. v = int(value_string)
  27. except:
  28. try:
  29. v = float(value_string)
  30. except:
  31. pass
  32. return v
  33. class SimElement:
  34. def __init__(self, name="", id=0, type=0, xml_node=None, dir_name=None):
  35. self.id = id
  36. self.type = type
  37. self.name = name
  38. self.dir_name = dir_name
  39. if xml_node is not None:
  40. self.name = xml_node.getAttribute("Name")
  41. self.type = try_convert_value_string(xml_node.getAttribute("Type"))
  42. self.id = try_convert_value_string(xml_node.getAttribute("id"))
  43. childs = xml_node.childNodes
  44. for child in childs:
  45. if child.localName is not None:
  46. value = parse_value_node(child)
  47. setattr(self, child.localName, value)
  48. class Layer(SimElement):
  49. def __init__(self, name="", id=0, type=0, xml_node=None, dir_name=None):
  50. SimElement.__init__(self, name=name, id=id, type=type, xml_node=xml_node, dir_name=dir_name)
  51. class Connection(SimElement):
  52. def __init__(self, name="", id=0, type=0, xml_node=None, dir_name=None):
  53. SimElement.__init__(self, name=name, id=id, type=type, xml_node=xml_node, dir_name=dir_name)
  54. class Normalize(SimElement):
  55. def __init__(self, name="", id=0, type=0, xml_node=None, dir_name=None):
  56. SimElement.__init__(self, name=name, id=id, type=type, xml_node=xml_node, dir_name=dir_name)
  57. class Input(SimElement):
  58. def __init__(self, name="", id=0, type=0, xml_node=None, dir_name=None):
  59. SimElement.__init__(self, name=name, id=id, type=type, xml_node=xml_node, dir_name=dir_name)
  60. def get_SimElement_list_from_dom(dom, tag_name=None, class_name=None, dir_name=None):
  61. element_list = []
  62. dom_elements = dom.getElementsByTagName(tag_name)
  63. for node in dom_elements:
  64. element_list.append(class_name(xml_node=node, dir_name=dir_name))
  65. return element_list
  66. class SimInfo:
  67. "read Sim.SimInfo file from simulation data directory"
  68. def __init__(self, file_name):
  69. file = open(file_name, "r")
  70. dat = file.read()
  71. file.close()
  72. self.dom = xml.dom.minidom.parseString(dat)
  73. self.dir_name = os.path.dirname(file_name)
  74. self.layers = get_SimElement_list_from_dom(self.dom, "Layer", Layer)
  75. self.normalizes = get_SimElement_list_from_dom(self.dom, "Normalize", Normalize)
  76. self.connections = get_SimElement_list_from_dom(self.dom, "Connection", Connection)
  77. self.inputs = get_SimElement_list_from_dom(self.dom, "Input", Input)
  78. def get_layers(self):
  79. return self.layers
  80. def get_normalizes(self):
  81. return self.normalizes
  82. def get_connections(self):
  83. return self.connections
  84. def get_inputs(self):
  85. return self.inputs
  86. def find_layer_with_id(self, id):
  87. for layer in self.layers:
  88. if layer.id == id:
  89. return layer
  90. def write_layout_graph(self, file_name="layout.png"):
  91. if pgv is None:
  92. raise ImportError("pygraphviz not available")
  93. G = pgv.AGraph(strict=False,directed=True)
  94. G.edge_attr['color'] = 'green'
  95. for layer in self.layers:
  96. G.add_node(layer.name)
  97. for con in self.connections:
  98. source = self.find_layer_with_id(con.Source['id'])
  99. target = self.find_layer_with_id(con.Target['id'])
  100. if source is not None and target is not None:
  101. G.add_edge(source.name, target.name)
  102. edge = G.get_edge(source.name, target.name)
  103. if con.InputNumber in [1,3]:
  104. edge.attr['arrowhead'] = 'tee'
  105. edge.attr['color'] = 'red'
  106. G.layout(prog='dot')
  107. G.draw(file_name)
  108. class ConfigLineParser:
  109. "parse line of a config file"
  110. def __init__(self):
  111. self.re_comment = re.compile("#")
  112. self.re_key_value = re.compile("([a-zA-Z0-9_]+) : (.*)")
  113. self.re_flag = re.compile("([a-zA-Z0-9_]+)")
  114. self.re_flag_false = re.compile("No([a-zA-Z0-9_]+)")
  115. def parse_line(self, line):
  116. if self.re_comment.match(line) is not None:
  117. return None, None
  118. match = self.re_key_value.match(line)
  119. if match is not None:
  120. key = match.group(1)
  121. value = match.group(2)
  122. return key, try_convert_value_string(value)
  123. match = self.re_flag_false.match(line)
  124. if match is not None:
  125. key = match.group(1)
  126. return key, False
  127. match = self.re_flag.match(line)
  128. if match is not None:
  129. key = match.group(1)
  130. return key, True
  131. return None, None
  132. def read_config(file_path):
  133. "read simulation settings file (settings_<simname>.cfg or <sim_data_path>/backup_settings)"
  134. config_dict = dict()
  135. file = open(file_path, 'r')
  136. parser = ConfigLineParser()
  137. for line in file:
  138. key, value = parser.parse_line(line)
  139. if key is not None:
  140. config_dict[key] = value
  141. file.close()
  142. return config_dict
  143. if __name__=="__main__":
  144. pass