def extract_type_names_and_ids() -> dict: """ Extracts type_name to type id mapping from CC3DXML :return {dict}: """ cc3d_xml2_obj_converter = CompuCellSetup.persistent_globals.cc3d_xml_2_obj_converter if cc3d_xml2_obj_converter is None: return {} plugin_elements = cc3d_xml2_obj_converter.root.getElements("Plugin") list_plugin = CC3DXMLListPy(plugin_elements) type_id_type_name_dict = {} for element in list_plugin: if element.getAttribute("Name") == "CellType": cell_types_elements = 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") type_id_type_name_dict[type_id] = type_name return type_id_type_name_dict
def extract_type_names_and_ids() -> dict: """ Extracts type_name to type id mapping from CC3DXML :return {dict}: """ cc3d_xml2_obj_converter = CompuCellSetup.persistent_globals.cc3d_xml_2_obj_converter if cc3d_xml2_obj_converter is None: return {} type_id_type_name_dict = {} plugin_elements = cc3d_xml2_obj_converter.root.getElements("Plugin") if len(plugin_elements): # handle situation where we have plugins and we are looking for CellType plugin to extract elements list_plugin = CC3DXMLListPy(plugin_elements) for element in list_plugin: if element.getAttribute("Name") == "CellType": cell_types_elements = element.getElements("CellType") type_id_type_name_dict = extract_type_id_type_name_dict( cell_types_elements=cell_types_elements) else: # try if <CompuCell3DLatticeData> is available - it will be stored in cc3d_xml2_obj_converter.root if cc3d_xml2_obj_converter.root.name == 'CompuCell3DLatticeData': cell_types_elements = cc3d_xml2_obj_converter.root.getElements( "CellType") type_id_type_name_dict = extract_type_id_type_name_dict( cell_types_elements=cell_types_elements) return type_id_type_name_dict
def extract_type_id_type_name_dict(cell_types_elements): """ Extract dictionary mapping cell type id to cell type name from a sequence of xml elements that look as follows: <CellType TypeId="0" TypeName="Medium"/> <CellType TypeId="1" TypeName="Condensing"/> <CellType TypeId="2" TypeName="NonCondensing"/> Note that this sequence of elements can be found int he CellTypePlugin or in the <CompuCell3DLatticeData> in the dml.files :param cell_types_elements: :return: """ type_id_type_name_dict = {} 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") type_id_type_name_dict[type_id] = type_name return type_id_type_name_dict
def read_cc3_d_file_format(self, file_name): """ This function reads the CompuCell3D (.cc3d -XML)file. Which contains the file paths to all the resources in used in the project. 'cc3dSimulationData' object in this class holds all file paths and read data. :param file_name: file path for the :return: """ # Import XML utils to read the .cc3d xml file xml2_obj_converter = Xml2Obj() # Get the full file path .cc3d xml file file_full_path = os.path.abspath(file_name) self.cc3dSimulationData.basePath = os.path.dirname(file_full_path) self.cc3dSimulationData.path = file_full_path bp = self.cc3dSimulationData.basePath # Read the .cc3d xml and get the root element root_element = xml2_obj_converter.Parse( file_full_path) # this is simulation element # Check if custom settings file (Simulation/_settings.xml) exists. custom_settings_flag = os.path.isfile( os.path.join(self.cc3dSimulationData.basePath, 'Simulation', settings_data.SETTINGS_FILE_NAME)) if custom_settings_flag: # If setting file is there load it to resources as PlayerSettings self.cc3dSimulationData.playerSettingsResource = CC3DResource() self.cc3dSimulationData.playerSettingsResource.path = os.path.abspath( os.path.join(self.cc3dSimulationData.basePath, 'Simulation', settings_data.SETTINGS_FILE_NAME)) self.cc3dSimulationData.playerSettingsResource.type = "PlayerSettings" print('GOT SUSTOM SETTINGS : ', self.cc3dSimulationData.playerSettingsResource.path) # Get the version of the file if root_element.findAttribute('version'): version = root_element.getAttribute('version') self.cc3dSimulationData.version = version # Get the model xml file if root_element.getFirstElement("XMLScript"): # If XML file exists load in resources as XMLScript xml_script_relative = root_element.getFirstElement( "XMLScript").getText() self.cc3dSimulationData.xmlScriptResource.path = os.path.abspath( os.path.join( bp, xml_script_relative)) # normalizing path to xml script self.cc3dSimulationData.xmlScriptResource.type = "XMLScript" # Get the python script for the model if root_element.getFirstElement("PythonScript"): # If python file exists load in resources as PythonScript python_script_relative = root_element.getFirstElement( "PythonScript").getText() self.cc3dSimulationData.pythonScriptResource.path = os.path.abspath( os.path.join(bp, python_script_relative) ) # normalizing path to python script self.cc3dSimulationData.pythonScriptResource.type = "PythonScript" # Get the PIF file resource for the model if root_element.getFirstElement("PIFFile"): # If PIF file exists load in resources as PIFFile pif_file_relative = root_element.getFirstElement( "PIFFile").getText() self.cc3dSimulationData.pifFileResource.path = os.path.abspath( os.path.join(bp, pif_file_relative)) # normalizing path self.cc3dSimulationData.pifFileResource.type = "PIFFile" # Read the SerializeSimulation element which have the data on serialization of the resources. # todo - remove this section - we no longer need serializer resource if root_element.getFirstElement("SerializeSimulation"): serialize_elem = root_element.getFirstElement( "SerializeSimulation") self.cc3dSimulationData.serializerResource = CC3DSerializerResource( ) if serialize_elem: if serialize_elem.findAttribute("OutputFrequency"): self.cc3dSimulationData.serializerResource.outputFrequency = serialize_elem.getAttributeAsInt( "OutputFrequency") if serialize_elem.findAttribute( "AllowMultipleRestartDirectories"): self.cc3dSimulationData.serializerResource.allowMultipleRestartDirectories = serialize_elem.getAttributeAsBool( "AllowMultipleRestartDirectories") if serialize_elem.findAttribute("FileFormat"): self.cc3dSimulationData.serializerResource.fileFormat = serialize_elem.getAttribute( "FileFormat") if root_element.getFirstElement("RestartSimulation"): restart_elem = root_element.getFirstElement("RestartSimulation") if not self.cc3dSimulationData.serializerResource: self.cc3dSimulationData.serializerResource = CC3DSerializerResource( ) if restart_elem.findAttribute("RestartDirectory"): self.cc3dSimulationData.serializerResource.restartDirectory = restart_elem.getAttribute( "RestartDirectory") # Reading parameter scan resources in the .cc3d file if root_element.getFirstElement("ParameterScan"): ps_file = root_element.getFirstElement("ParameterScan").getText() self.cc3dSimulationData.parameterScanResource = CC3DParameterScanResource( ) self.cc3dSimulationData.parameterScanResource.path = os.path.abspath( os.path.join(bp, ps_file)) # normalizing path to python script self.cc3dSimulationData.parameterScanResource.type = 'ParameterScan' # setting same base path for parameter scan as for the project # - necessary to get relative paths in the parameterSpec file self.cc3dSimulationData.parameterScanResource.basePath = self.cc3dSimulationData.basePath # reading content of XML parameter scan specs # ------------------------------------------------------------------ IMPORTANT IMPOTRANT ---------------- # WE HAVE TO CALL MANUALLY readParameterScanSpecs because # if it is called each time CC3DSiulationDataHandler calls readCC3DFileFormat # it may cause problems with parameter scan # namely one process will attempt to read parameter scan specs while another might try # to write to it and error will get thrown and synchronization gets lost # plus readCC3DFileFormat should read .cc3d only , not files which are included from .cc3d # ------------------------------------------------------------------ IMPORTANT IMPOTRANT ---------------- # Reading the remaining resources in the .cc3d file resourceList = CC3DXMLListPy(root_element.getElements("Resource")) for resourceElem in resourceList: cc3dResource = CC3DResource() cc3dResource.path = os.path.abspath( os.path.join(bp, resourceElem.getText())) if resourceElem.findAttribute("Type"): cc3dResource.type = resourceElem.getAttribute("Type") if resourceElem.findAttribute("Module"): cc3dResource.module = resourceElem.getAttribute("Module") if resourceElem.findAttribute("Origin"): cc3dResource.origin = resourceElem.getAttribute("Origin") if resourceElem.findAttribute("Copy"): copyAttr = resourceElem.getAttribute("Copy") if copyAttr.lower() == "no": cc3dResource.copy = False self.cc3dSimulationData.resources[cc3dResource.path] = cc3dResource
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 extract_cell_type_info_from_file_name(_lds_file: str) -> dict: lds_xml = _LatticeDataXMLReader(_lds_file) cte = CC3DXMLListPy(lds_xml.cell_types_elements) return {el.getAttributeAsInt("TypeId"): el.getAttribute("TypeName") for el in cte}
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))
from cc3d.twedit5.twedit.utils.global_imports import *
def extractElementListProperties(self, _root_element, _elementFormat): ''' _elementFormat=[ElementName,TypeOfElementValue,[AttribName,Type,Optional,MultiDictKey],[AttribName,Type,Optional,MultiDictKey],...] e.g. [VolumeEnergyParameters,None,[CellType,String,False,True],[LambdaVolume,Double,False,False],[TargetVolume,Double,False,False]] _returnObjectFormat=[MultiDimDict={(Key1,Key2):[ElementValue,Attrib1,Attrib2]}] ''' # parsing _elementFormat to determine type of the return object try: elementName = _elementFormat[0] elementValueFormat = _elementFormat[1] multiDictKeys = [] attributes = [] elementFormatLength = len(_elementFormat) attributeFormatList = _elementFormat[2:] for attributeFormat in attributeFormatList: if attributeFormat[3]: multiDictKeys.append(attributeFormat[0]) except IndexError as e: print('wrong description of element format:', _elementFormat) return None multiDictConstructionCode = '' closingPart = '' multiDictAssignmentCode = '' print('THIS IS multiDictConstructionCode=', multiDictConstructionCode) for number, key in enumerate(multiDictKeys): if multiDictConstructionCode == '': multiDictConstructionCode = 'MultiDimDict(' closingPart = 'dict)' multiDictAssignmentCode = 'moduleDataDict[keyList[%s]]' % str( number) else: multiDictConstructionCode += 'lambda:MultiDimDict(' closingPart += ')' multiDictAssignmentCode += '[keyList[%s]]' % str(number) if multiDictConstructionCode == '': return None else: multiDictConstructionCode += closingPart print('THIS IS multiDictConstructionCode=', multiDictConstructionCode) print('multiDictAssignmentCode=', multiDictAssignmentCode) # Constructing multiDict moduleDataDict = eval(multiDictConstructionCode) print('moduleDataDict=', moduleDataDict) # parsing XML element and extracting data to put them into object defined by multiDictConstructionCode print('elementName=', elementName) elementVec = CC3DXMLListPy(_root_element.getElements(elementName)) print('elementVec.size()=', elementVec.getBaseClass().size()) print('elementVec=', elementVec) print('attributeFormatList=', attributeFormatList) for element in elementVec: print('processing element') keyList = [] valueList = [] if elementValueFormat is not None: functionName = 'get' if elementValueFormat == '': functionName += 'Text' else: functionName += elementValueFormat elementValueFetcherFunction = getattr(element, functionName) elementValue = elementValueFetcherFunction() valueList.append(elementValue) for attributeFormat in attributeFormatList: attributeName = attributeFormat[0] attributeType = attributeFormat[1] attributeOptionalFlag = attributeFormat[2] attributeKeyFlag = attributeFormat[3] attributeValue = None print('attributeFormat=', attributeFormat) if element.findAttribute(attributeName): attributeFetcherFunction = getattr( element, 'getAttribute' + attributeType) attributeValue = attributeFetcherFunction(attributeName) # adding key or value if attributeKeyFlag: keyList.append(attributeValue) else: valueList.append(attributeValue) else: if not attributeOptionalFlag: keyList = [] valueList = [] break print('keyList=', keyList) print('valueList=', valueList) if len(keyList): exec(multiDictAssignmentCode + '=valueList') print('moduleDataDict=', moduleDataDict) return moduleDataDict
# Start-Of-Header