def readVesselDataFromCSV(filename= None,delimiter=';',networkPath = str(cur+"/../NetworkFiles/")): ''' ''' if filename == None: print "ERROR: moduleCSV.readVesselDataFromCSV(): no filename passed to csv-writer!" return {} networkDirectory = filename.split('.')[0] if not os.path.exists(str(networkPath+networkDirectory)): print "" print 'ERROR: moduleCSV.readVesselDataFromCSV(): file not in the correct directory / directory does not exists' return {} # load data reader = csv.DictReader(open(networkPath+networkDirectory+'/'+filename,'rb'),delimiter=delimiter) # hash data with in dictionary and separate units columUnits = {} vesselData = {} for row in reader: Id = row.pop('Id') if Id == 'unit': columUnits = row else: Id = int(Id) vesselData[Id] = row variablesToDiscard = [] for Id,data in vesselData.iteritems(): polyChaos = {} for variable,variableValueStr in data.iteritems(): # check if value is defined if variableValueStr not in ['', None]: #find units if '#' in columUnits[variable]: # '#' in variable or nothing,variableUnit = columUnits[variable].split('#',1) # check for polyChaos variables if '-pC' not in variable: # convert variables to corret unit and type data[variable] = loadVariablesConversion(variable, variableValueStr, variableUnit) else: variable,number = variable.split('-pC') if variable not in polyChaos.keys(): polyChaos[variable] = variableValueStr else: polyChaos[variable] = ' '.join([polyChaos[variable],variableValueStr]) else: variablesToDiscard.append([Id,variable]) # find out variables which have no values # convert polynomial chaos variables to corret unit and type for variable,variableValueStr in polyChaos.iteritems(): variableUnit = columUnits[variable].split('#',1) polyChaos[variable] = loadVariablesConversion(variable, variableValueStr, variableUnit, polychaos = True) data['polyChaos'] = polyChaos for variable in data.iterkeys(): if '-pC' in variable: variablesToDiscard.append([Id,variable]) # remove variables which have no values for Id,variableToDiscard in variablesToDiscard: del vesselData[Id][variableToDiscard] return {'vesselData':vesselData}
def readRandomInputsfromCSV(networkName, randomInputManager, delimiter=';'): ''' Function reads the random variable data from a *.csv file all existing rv definitions in the randomInputManager will be erased ''' # prepare randomInputManager randomInputManager.deleteAllRandomInputs() # create reader randomVariableCSVFile = mFPH.getFilePath('randomVariableCSVFile', networkName, 'xxx', 'read', exception='Warning') reader = csv.DictReader(open(randomVariableCSVFile, 'rb'), delimiter=delimiter) # evaluate tags variablesToLoadTags = [] variablesToLoadTags.extend(nxml.generalRandomInputsAttributes) for listValue in nxml.randomInputsReference.itervalues(): variablesToLoadTags.extend(listValue) variablesToLoadTags.extend(['randomInputType', 'location']) # add random variables for row in reader: dataDict = {} for variable in variablesToLoadTags: if variable in row.keys(): # TODO: add units variableUnit = None # save converted CSV-value dataDict[variable] = mXML.loadVariablesConversion( variable, row[variable], variableUnit) else: print "WARNING: mCSV.readRandomInputsfromCSV(), no variable {} defined in the csv file but needed to create proper working random input".format( variable) randomInputManager.addRandomInput(dataDict)
def readVesselDataFromCSV(networkName, delimiter=';'): """ Functions loads vessel data from *.csv file inclusive polynomial chaos definitions input: networkName delimiter (default = ';') return: dict := {'vesselData': vesselData} which is used by vascular network to update its vessel data with the function vascularNetwork.updateNetwork(dataDict) """ vesselCSVFile = mFPH.getFilePath('vesselCSVFile', networkName, 'xxx', 'read', exception='Warning') if vesselCSVFile == None: return None with open(vesselCSVFile, 'rb') as csvfile: dialect = csv.Sniffer().sniff(csvfile.read(1024), delimiters=";,") csvfile.seek(0) if dialect.delimiter == ',': print """\n WARNING: mCSV92 detected delimiter ',': This delimiter might lead to wrong data as it is used as well as decimal place separator in some languages! Check the loaded values carefully!""" #reader = ccBC.csv.DictReader(csvfile,dialect) reader = csv.DictReader(csvfile, delimiter=dialect.delimiter) # hash data with in dictionary and separate units columUnits = {} vesselData = {} for row in reader: Id = row.pop('Id') if Id == 'unit': columUnits = row else: Id = int(Id) vesselData[Id] = row variablesToDiscard = [] for Id, data in vesselData.iteritems(): # polyChaos = {} for variable, variableValueStr in data.iteritems(): # check if value is defined if variableValueStr not in ['', None]: #find units if '#' in columUnits[variable]: # '#' in variable or nothing, variableUnit = columUnits[variable].split( '#', 1) # convert variables to corret unit and type data[variable] = mXML.loadVariablesConversion( variable, variableValueStr, variableUnit) else: variablesToDiscard.append( [Id, variable]) # find out variables which have no values # remove variables which have no values for Id, variableToDiscard in variablesToDiscard: del vesselData[Id][variableToDiscard] return {'vesselData': vesselData}
def readBCFromCSV(networkName, delimiter=';'): """ Functions loads boundaryCondition data from \*.csv file inclusive polynomial chaos definitions Args: networkName (str): The name of the network delimiter (str): Delimiter (default = ';') Returns: VascularNetwork.boundaryConditionDict A description of the VascNw.BCD instance returned VascularNetwork.boundaryConditionPolyChaos A description of the VascNw.BCPC instance returned """ boundaryCSVFile = mFPH.getFilePath('boundaryCSVFile', networkName, 'xxx', 'read', exception='Warning') reader = ccBC.csv.DictReader(open(boundaryCSVFile, 'rb'), delimiter=delimiter) # hash data with in dictionary and separate units columUnits = {} boundaryData = [] for row in reader: if row['Id'] == 'unit': columUnits = row else: boundaryData.append(row) boundaryConditionPolyChaos = {} BCconditionData = {} for bcData in boundaryData: Id = int(bcData['Id']) # TODO: read write polynomial chaos variables # create class instance boundaryType = bcData['boundaryType'] try: boundaryInstance = eval(nxml.bcTagsClassReferences[boundaryType])() except: 'ERROR moduleCSV.readBCFromCSV: boundaryType <<{}>> does not exist'.format( boundaryType) boundaryDataDict = {'name': boundaryType} polyChaos = {} for variable, variableValueStr in bcData.iteritems(): if variable in nxml.boundaryConditionElements[boundaryType]: try: variableUnit = columUnits[variable] except: variableUnit = None # save converted XML-value if variable == 'filePathName': path = ''.join([boundaryCSVFile]) if path not in variableValueStr: variableValueStr = variableValueStr.join([path, '']) print variableValueStr if variableValueStr != '': try: boundaryDataDict[ variable] = mXML.loadVariablesConversion( variable, variableValueStr, variableUnit) except: pass if '-pC' in variable and variableValueStr != '': polyChaos['name'] = boundaryType variable, number = variable.split('-pC') if variable in polyChaos.keys(): polyChaos[variable] = ' '.join( [polyChaos[variable], variableValueStr]) else: polyChaos[variable] = variableValueStr # convert polynomial chaos variables to corret unit and type if polyChaos != {}: for variable, variableValueStr in polyChaos.iteritems(): try: variableUnit = columUnits[variable].split('#', 1) polyChaos[variable] = mXML.loadVariablesConversion( variable, variableValueStr, variableUnit, polychaos=True) except: pass if Id not in boundaryConditionPolyChaos.keys(): boundaryConditionPolyChaos[Id] = [polyChaos] else: boundaryConditionPolyChaos[Id].append(polyChaos) boundaryInstance.update(boundaryDataDict) if Id not in BCconditionData.keys(): BCconditionData[Id] = [boundaryInstance] else: BCconditionData[Id].append(boundaryInstance) return BCconditionData, boundaryConditionPolyChaos
def readBCFromCSV(delimiter=';',filename = None, networkPath = str(cur+"/../NetworkFiles/")): ''' ''' if filename == None: print "ERROR: moduleCSV.readBCFromCSV() no filename passed to csv-writer!" return {} networkDirectory = filename.split('BC')[0] if os.path.isfile(networkPath+networkDirectory+'/'+filename) == False: print "ERROR: moduleCSV.readBCFromCSV() file does not exists!" return {} reader = csv.DictReader(open(networkPath+networkDirectory+'/'+filename,'rb'),delimiter=delimiter) # hash data with in dictionary and separate units columUnits = {} boundaryData = [] for row in reader: if row['Id'] == 'unit': columUnits = row else: boundaryData.append(row) boundaryConditionPolyChaos = {} BCconditionData = {} for bcData in boundaryData: Id = int(bcData['Id']) # create class instance boundaryType = bcData['boundaryType'] try: boundaryInstance = eval(bcTagsClassReferences[boundaryType])() except: 'ERROR moduleCSV.readBCFromCSV: boundaryType <<{}>> does not exist'.format(boundaryType) boundaryDataDict = {'name':boundaryType} polyChaos = {} for variable,variableValueStr in bcData.iteritems(): if variable in boundaryConditionElements[boundaryType]: try: variableUnit = columUnits[variable] except: variableUnit = None # save converted XML-value if variable == 'filePathName': path = ''.join([networkDirectory,'/']) if path not in variableValueStr: variableValueStr = variableValueStr.join([path,'']) boundaryDataDict[variable] = loadVariablesConversion(variable, variableValueStr, variableUnit) if '-pC' in variable and variableValueStr != '': polyChaos['name'] = boundaryType variable,number = variable.split('-pC') if variable in polyChaos.keys(): polyChaos[variable] = ' '.join([polyChaos[variable],variableValueStr]) else: polyChaos[variable] = variableValueStr # convert polynomial chaos variables to corret unit and type if polyChaos != {}: for variable,variableValueStr in polyChaos.iteritems(): try: variableUnit = columUnits[variable].split('#',1) polyChaos[variable] = loadVariablesConversion(variable, variableValueStr, variableUnit, polychaos = True) except: pass if Id not in boundaryConditionPolyChaos.keys(): boundaryConditionPolyChaos[Id] =[polyChaos] else: boundaryConditionPolyChaos[Id].append(polyChaos) boundaryInstance.update(boundaryDataDict) if Id not in BCconditionData.keys(): BCconditionData[Id] = [boundaryInstance] else: BCconditionData[Id].append(boundaryInstance) return BCconditionData,boundaryConditionPolyChaos