def init_modules(sim, _cc3dXML2ObjConverter): """ :param sim: :param _cc3dXML2ObjConverter: :return: """ plugin_data_list = XMLUtils.CC3DXMLListPy(_cc3dXML2ObjConverter.root.getElements("Plugin")) for pluginData in plugin_data_list: sim.ps.addPluginDataCC3D(pluginData) steppable_data_list = XMLUtils.CC3DXMLListPy(_cc3dXML2ObjConverter.root.getElements("Steppable")) for steppableData in steppable_data_list: sim.ps.addSteppableDataCC3D(steppableData) potts_data_list = XMLUtils.CC3DXMLListPy(_cc3dXML2ObjConverter.root.getElements("Potts")) assert potts_data_list.getBaseClass().size() <= 1, 'You have more than 1 definition of the Potts section' if potts_data_list.getBaseClass().size() == 1: for pottsData in potts_data_list: sim.ps.addPottsDataCC3D(pottsData) metadata_data_list = XMLUtils.CC3DXMLListPy(_cc3dXML2ObjConverter.root.getElements("Metadata")) assert metadata_data_list.getBaseClass().size() <= 1, 'You have more than 1 definition of the Metadata section' if metadata_data_list.getBaseClass().size() == 1: for metadataData in metadata_data_list: sim.ps.addMetadataDataCC3D(metadataData)
def extract_fields_used_from_file_name(_lds_file: str) -> dict: lds_xml = _LatticeDataXMLReader(_lds_file) fields_used = dict() if lds_xml.fields_element: field_list = XMLUtils.CC3DXMLListPy(lds_xml.fields_element.getElements("Field")) fields_used.update({el.getAttribute("Name"): el.getAttribute("Type") for el in field_list}) return fields_used
def extract_custom_vis_scripts_from_file_name(_lds_file: str) -> dict: lds_xml = _LatticeDataXMLReader(_lds_file) o = dict() # ToDo: if a "CustomVis" Type was provided, # require that a "Script" was also provided; else warn user if lds_xml.fields_element: fl = XMLUtils.CC3DXMLListPy(lds_xml.fields_element.getElements("Field")) o.update({el.getAttribute("Name"): el.getAttribute("Script") for el in fl if el.findAttribute("Script")}) return o
def parseXML( xml_fname): """ :param xml_fname: :return: """ cc3dXML2ObjConverter = XMLUtils.Xml2Obj() root_element = cc3dXML2ObjConverter.Parse(xml_fname) return cc3dXML2ObjConverter
def __init__(self, lds_file: str): self.lds_file = os.path.abspath(lds_file) assert os.path.isfile(self.lds_file) self.xml2_obj_converter = XMLUtils.Xml2Obj() self.root_element = self.xml2_obj_converter.Parse(lds_file) self.dim_element = self.root_element.getFirstElement("Dimensions") self.output_element = self.root_element.getFirstElement("Output") self.lattice_element = self.root_element.getFirstElement("Lattice") self.cell_types_elements = self.root_element.getElements("CellType") self.fields_element = self.root_element.getFirstElement("Fields")
def extractCurrentModule( self, _editor, _moduleType, _moduleName ): # editor,_moduleType='Plugin',_moduleName=['Name','Contact'] moduleName = [] try: if _moduleName[0] == '' and _moduleName[1] == '': moduleName = [] else: moduleName = _moduleName except: moduleName = _moduleName # print 'ecm _moduleType,moduleName=',(_moduleType,moduleName) moduleBegin, moduleEnd = self.cc3dmlHelper.findModuleLine( _editor=_editor, _moduleType=_moduleType, _moduleName=moduleName) # moduleBegin,moduleEnd=self.cc3dmlHelper.findModuleLine(_editor=_editor,_moduleType=_moduleType,_moduleName=_moduleName) # print 'ecm moduleBegin,moduleEnd=',(moduleBegin,moduleEnd) snippet = self.extractSnippet(_editor, moduleBegin, moduleEnd) # print 'EXTRACT moduleBegin,moduleEnd=',(moduleBegin,moduleEnd) if moduleBegin < 0 and moduleEnd < 0: return None, moduleBegin, moduleEnd cc3dXML2ObjConverter = XMLUtils.Xml2Obj() try: root_element = cc3dXML2ObjConverter.ParseString(snippet) except xml.parsers.expat.ExpatError as e: QMessageBox.critical(self.__ui, "Error Parsing CC3DML String ", e.__str__()) raise UserWarning(e.__str__()) # return root_element,moduleBegin,moduleEnd return cc3dXML2ObjConverter, moduleBegin, moduleEnd # have to return cc3dXML2ObjConverter instead of root_element because if we return root_element then cc3dXML2ObjConverter
def get_results_reader_no_ui(lds_file): """ Generate CML Results Reader instance for a results lattice description file :param lds_file: Path to lattice description file :return: CMLResultsReader instance """ xml2_obj_converter = XMLUtils.Xml2Obj() root_element = xml2_obj_converter.Parse(lds_file) dim_element = root_element.getFirstElement("Dimensions") field_dim = Dim3D() field_dim.x = int(dim_element.getAttribute("x")) field_dim.y = int(dim_element.getAttribute("y")) field_dim.z = int(dim_element.getAttribute("z")) ui_dummy = CC3DUIDummy(field_dim) return CMLResultReader(ui_dummy), ui_dummy
def extract_lattice_description_info(self, file_name: str) -> None: """ Reads the xml that summarizes serialized simulation files :param file_name:{str} xml metadata file name :return: None """ # lattice data simulation file lds_file = os.path.abspath(file_name) self.ldsDir = os.path.dirname(lds_file) xml2_obj_converter = XMLUtils.Xml2Obj() root_element = xml2_obj_converter.Parse(lds_file) dim_element = root_element.getFirstElement("Dimensions") self.fieldDim = CompuCell.Dim3D() self.fieldDim.x = int(dim_element.getAttribute("x")) self.fieldDim.y = int(dim_element.getAttribute("y")) self.fieldDim.z = int(dim_element.getAttribute("z")) output_element = root_element.getFirstElement("Output") self.ldsCoreFileName = output_element.getAttribute("CoreFileName") self.frequency = int(output_element.getAttribute("Frequency")) self.numberOfSteps = int(output_element.getAttribute("NumberOfSteps")) # obtaining list of files in the ldsDir lattice_element = root_element.getFirstElement("Lattice") self.latticeType = lattice_element.getAttribute("Type") # getting information about cell type names and cell ids. # It is necessary during generation of the PIF files from VTK output cell_types_elements = root_element.getElements("CellType") list_cell_type_elements = CC3DXMLListPy(cell_types_elements) for cell_type_element in list_cell_type_elements: type_name = cell_type_element.getAttribute("TypeName") type_id = cell_type_element.getAttributeAsInt("TypeId") self.typeIdTypeNameDict[type_id] = type_name # now will convert python dictionary into C++ map<int, string> self.typeIdTypeNameCppMap = CC3DXML.MapIntStr() for type_id in list(self.typeIdTypeNameDict.keys()): self.typeIdTypeNameCppMap[int( type_id)] = self.typeIdTypeNameDict[type_id] lds_file_list = os.listdir(self.ldsDir) for fName in lds_file_list: if re.match(".*\.vtk$", fName): self.ldsFileList.append(fName) self.ldsFileList.sort() # extracting information about fields in the lds file fields_element = root_element.getFirstElement("Fields") if fields_element: field_list = XMLUtils.CC3DXMLListPy( fields_element.getElements("Field")) for field_elem in field_list: field_name = field_elem.getAttribute("Name") self.fieldsUsed[field_elem.getAttribute( "Name")] = field_elem.getAttribute("Type") if field_elem.findAttribute( "Script"): # True or False if present # ToDo: if a "CustomVis" Type was provided, # require that a "Script" was also provided; else warn user custom_vis_script = field_elem.getAttribute("Script") self.customVis = CompuCellSetup.createCustomVisPy( field_name) self.customVis.registerVisCallbackFunction( CompuCellSetup.vtkScriptCallback) self.customVis.addActor(field_name, "vtkActor") # we'll piggyback off the actorsDict self.customVis.addActor("customVTKScript", custom_vis_script)
def treeNode(itemNode, _superParent=None): # itemNode can only be Element! # itemNode is of type CC3DXMLElement if not itemNode: return None try: itemNode.name node = itemNode except AttributeError: node = itemNode.root # node = itemNode t_node = TreeItem(node.name, node.cdata) # Setting item values for Potts, Metadata, Plugins and Steppables. # Those settings are for display purposes only and do not affect CC3DXML element that # Compucell3d uses to configure the data # all we do is to label Potts tree element as Posst and Plugin and Steppables are named # using their names extracted from xml file. # we are using _itemValue to label label those elements and do not change cdata of the CC3DXMLelement if node.name == "Potts": t_node.setItemValueOnly("Potts") if node.name == "Metadata": t_node.setItemValueOnly("Metadata") # handling opening elements for Plugin and Steppables if node.name == "Plugin": t_node.setItemValueOnly(node.getAttribute("Name")) if node.name == "Steppable": t_node.setItemValueOnly(node.getAttribute("Type")) t_node.setCC3DXMLElement( node) # domNode holds reference to current CC3DXMLElement # setting superParents -they are either Potts, Plugin or Steppable. SuperParents are used in steering. # Essentially when we change any TreeItem in the TreeView # we can quickly extract name of the superParent and tell CC3D to reinitialize module that superParent describes. # Otherwise if we dont keep track of super parents we would either have to do: # 1. time consuming and messy tracking back of which module needs to be changed in response # to change in one of the parameter # or # 2. reinitialize all modules each time a single parameter is changed superParent = _superParent if not _superParent: if node.name in ("Plugin", "Steppable", "Potts", "Metadata"): superParent = t_node t_node.setSuperParent(superParent) # FOR AN UNKNOWN REASON "regular" map iteration does not work so i had to implement by hand iterators # in C++ and Python to walk through all the elements in the map<string,string> in python if node.attributes.size(): for attr_combo in node.attributes.items(): attribute_name = attr_combo[0] attribute_value = attr_combo[1] if node.name == "Plugin" and attribute_name == "Name": continue if node.name == "Steppable" and attribute_name == "Type": continue tree_child = TreeItem( attribute_name, attribute_value) # attribute name, attribute value pair tree_child.setCC3DXMLElement(node) tree_child.setSuperParent(superParent) tree_child.setElementType("attribute") t_node.addChild(tree_child) children = XMLUtils.CC3DXMLListPy(node.children) for child in children: t_child = treeNode(child, superParent) t_node.addChild(t_child) return t_node
def __insertSnippet(self, _snippetName): # print "GOT REQUEST FOR SNIPPET ",_snippetName snippetNameStr = str(_snippetName) self.handlerDict = self.snippetUtils.getHandlersDict() text = self.snippetDictionary[str(_snippetName)] editor = self.__ui.getCurrentEditor() curFileName = str(self.__ui.getCurrentDocumentName()) basename, ext = os.path.splitext(curFileName) if ext != ".xml" and ext != ".cc3dml": QMessageBox.warning(self.__ui, "CC3DML files only", "CC3DML code snippets work only for xml/cc3dml files") return # here we parse cell type plugin if found cc3dXML2ObjConverter = XMLUtils.Xml2Obj() # root_element=cc3dXML2ObjConverter.ParseString(str(editor.text())) # print 'root_element=',root_element cellTypeData = self.getCellTypeData() gpd = self.getPottsData() print('cellTypeData=', cellTypeData) # self.findModuleLine(editor,_moduleType='Plugin',_moduleName=['Name','CellType']) # self.findModuleLine(editor,_moduleType='Steppable',_moduleName=['Type','PIFInitializer']) # self.findModuleLine(editor,_moduleType='Potts',_moduleName=[]) # self.findModuleLine(editor,_moduleType='Plugin',_moduleName=['Name','CenterOfMass']) if cellTypeData is None: # could not obtain data by parsing xml file - most likely due to parsing error return hiding_comments = self.configuration.setting("SkipCommentsInXMLSnippets") if not len(cellTypeData): print('self.handlerDict=', self.handlerDict) print('self.handlerDict[str(_snippetName)]=', self.handlerDict["Plugins CellType"]) pottsBegin, pottsEnd = self.findModuleLine(editor, _moduleType='Potts', _moduleName=[]) if pottsEnd < 0: editor.setCursorPosition(0, 0) else: editor.setCursorPosition(pottsEnd + 1, 0) self.handlerDict["Plugins CellType"](data=cellTypeData, editor=editor, generalPropertiesData=gpd, hiding_comments=hiding_comments) QMessageBox.warning(self.__ui, "Fresh Cell Type Plugin", "Please check newly inserted code and call %s again" % snippetNameStr) return # read freshly inseerted cell type plugin else: self.handlerDict[snippetNameStr](data=cellTypeData, editor=editor, generalPropertiesData=gpd, hiding_comments=hiding_comments) return
def getCellTypeData(self): editor = self.__ui.getCurrentEditor() cc3dXML2ObjConverter = XMLUtils.Xml2Obj() try: root_element = cc3dXML2ObjConverter.ParseString(str(editor.text())) except xml.parsers.expat.ExpatError as e: QMessageBox.critical(self.__ui, "Error Parsing CC3DML file", e.__str__()) print('GOT PARSING ERROR:', e) return root_element.getFirstElement('Plugin') # print('root_element=', root_element) cellTypeElement = root_element.getFirstElement("Plugin", d2mss({"Name": "CellType"})) cellTypeDict = {} if not cellTypeElement or cellTypeElement is None: return cellTypeDict cellTypeElementVec = cellTypeElement.getElements("CellType") cellTypeElementVec = CC3DXMLListPy(cellTypeElement.getElements("CellType")) for element in cellTypeElementVec: typeName = '' typeId = -1 typeFreeze = False if element.findAttribute('TypeName'): typeName = element.getAttribute('TypeName') else: continue if element.findAttribute('TypeId'): typeId = element.getAttributeAsInt('TypeId') else: continue if element.findAttribute('Freeze'): typeFreeze = True cellTypeDict[typeId] = [typeName, typeFreeze] return cellTypeDict print('cellTypeElementVec=', cellTypeElementVec) print('cellTypeElement=', dir(cellTypeElement))
def getPottsData(self): editor = self.__ui.getCurrentEditor() gpd = {} # default values gpd['Dim'] = [0, 0, 0] gpd['Temperature'] = 0 gpd['NeighborOrder'] = 3 gpd['MCS'] = 10000 gpd['SimulationName'] = 'PLEASE_PUT_SIMULATION_FILE_NAME_HERE' gpd['LatticeType'] = 'Square' cc3dXML2ObjConverter = XMLUtils.Xml2Obj() try: root_element = cc3dXML2ObjConverter.ParseString(str(editor.text())) except xml.parsers.expat.ExpatError as e: QMessageBox.critical(self.__ui, "Error Parsing CC3DML file", e.__str__()) print('GOT PARSING ERROR:', e) return gpd root_element.getFirstElement('Plugin') # print('root_element=', root_element) pottsElement = root_element.getFirstElement('Potts') if not pottsElement or pottsElement is None: return gpd # extract dimension dimElement = pottsElement.getFirstElement('Dimensions') if dimElement: if dimElement.findAttribute('x'): gpd['Dim'][0] = dimElement.getAttributeAsUInt('x') if dimElement.findAttribute('y'): gpd['Dim'][1] = dimElement.getAttributeAsUInt('y') if dimElement.findAttribute('z'): gpd['Dim'][2] = dimElement.getAttributeAsUInt('z') tempElement = pottsElement.getFirstElement('Temperature') if tempElement: gpd['MembraneFluctuations'] = float(tempElement.getText()) nElement = pottsElement.getFirstElement('NeighborOrder') if nElement: gpd['NeighborOrder'] = float(nElement.getText()) sElement = pottsElement.getFirstElement('Steps') if sElement: gpd['MCS'] = float(sElement.getText()) return gpd
from cc3d.twedit5.twedit.utils.global_imports import *
# Start-Of-Header