def __genMetaOptions(childDict): """ Recursive method used by createNew() for generating the "options" part of the metaFile. Returns a DataDict which becomes one branch/level of the metaFile """ print "AcdOptiSolverSetupManager::__genMetaOptions()" ret = DataDict() for item in childDict: # Make a copy of the old items in the childDict and add it to the metFile thisItem = ret.pushBack(item[0], item[1].copy()) # Add some new meta-information fields if thisItem.getValSingle("type") == "dict": # Recursively modify this dicts children also thisItem.setValSingle( "children", AcdOptiSolverManager.__genMetaOptions(item[1].getValSingle("children")) ) else: # Ordinary data field, set its value to whatever is default thisItem.pushBack("value", thisItem.getValSingle("default")) if DataDict.boolconv(item[1].getValSingle("must")): thisItem.pushBack("enabled", "True") else: thisItem.pushBack("enabled", "False") return ret
def __init__(self,settingsDict=None): if settingsDict == None: settingsDict = DataDict() settingsDict.pushBack("key", "") settingsDict.pushBack("val", "") settingsDict.pushBack("numFiltered", "0") AcdOptiDataExtractorFilter.__init__(self, settingsDict)
def __makePBS(self): """ Creates the run.pbs file used by Hopper's batch system """ #Calculate number of mpi nodes needed, and build the commands jobs = self.__paramFile.dataDict["jobs"] commands = [] numNodes = 0 for jobName, job in jobs: command = None if DataDict.boolconv(job["aprun"]): if job["tasksNode"] == "-1": nodesThis = int(math.ceil(int(job["tasks"])/float(self.CPUsPerNode))) else: assert int(job["tasksNode"]) <= self.CPUsPerNode nodesThis = int(math.ceil(int(job["tasks"])/float(job["tasksNode"]))) if nodesThis > numNodes: numNodes = nodesThis def makeOption(optionName, key, optional): get = job[key] if get == "-1" and optional: return "" return optionName + " " + get + " " command = "aprun " + makeOption("-n", "tasks", False)\ + makeOption("-N", "tasksNode", True)\ + makeOption("-S", "tasksNuma", True)\ + job["command"] + " " + job["commandArgs"] else: command = job["command"] commands.append(command) if len(commands) == 0: raise AcdOptiException_optiRunner_stageError("No commands built") if not numNodes > 0: raise AcdOptiException_optiRunner_stageError("Got numNodes="+str(numNodes)) #Write PBS file header runpbs = open(os.path.join(self.runConfig.stageFolder, "run.pbs"), 'w') runpbs.write("#!/bin/bash\n") torqueMeta = self.getTorqueMeta() runpbs.write("#PBS -q " + torqueMeta["queue"] + "\n") runpbs.write("#PBS -l mppwidth=" + str(numNodes*self.CPUsPerNode) + "\n") runpbs.write("#PBS -l walltime=" + torqueMeta["walltime"] + "\n") runpbs.write("#PBS -N " + self.runConfig.stageName + "\n") runpbs.write("#PBS -A " + torqueMeta["repo"] + "\n") if DataDict.boolconv(torqueMeta["importVars"]): runpbs.write("#PBS -V\n") runpbs.write("\n\n") #Write PBS script runpbs.write("## Commands:\n") for command in commands: runpbs.write(command + "\n") runpbs.close()
def __init__(self,folder,collection): self.folder = folder self.collection = collection #Construct the instance name from folder instName = folder if instName[-1] == "/": instName = instName[0:-1] self.instName = instName = os.path.split(instName)[1] #Load paramFile self.__paramfile = AcdOptiFileParser_simple(os.path.join(folder, "paramFile.set"), 'rw') if self.__paramfile.dataDict["fileID"] != "AcdOptiMetaAnalysis": raise AcdOptiException_metaAnalysis_loadFail("Got wrong fileID='" + self.__paramfile.dataDict["fileID"] + "'") if self.__paramfile.dataDict.getValSingle("instName") != self.instName: raise AcdOptiException_metaAnalysis_loadFail("instName doesn't match folder name") self.lockdown = DataDict.boolconv(self.__paramfile.dataDict.getValSingle("lockdown")) self.xVariable = self.__paramfile.dataDict["xVariable"] self.yVariable = self.__paramfile.dataDict["yVariable"] if len(self.__paramfile.dataDict.getVals("fVariable")) == 0: self.__paramfile.dataDict.pushBack("fVariable", "") self.__paramfile.dataDict.pushBack("fEquals", "") self.__paramfile.dataDict.pushBack("fLT", "") self.__paramfile.dataDict.pushBack("fGT", "") self.__paramfile.write() self.fVariable = self.__paramfile.dataDict["fVariable"] def floatOrNone(strIn): if strIn == "": return None else: return float(strIn) self.fEquals = floatOrNone(self.__paramfile.dataDict["fEquals"]) self.fGT = floatOrNone(self.__paramfile.dataDict["fGT"]) self.fLT = floatOrNone(self.__paramfile.dataDict["fLT"]) if len(self.__paramfile.dataDict.getVals("targetValue")) > 0: self.targetValue = float(self.__paramfile.dataDict["targetValue"]) else: self.targetValue = None anaData = self.__paramfile.dataDict["anaData"] self.xArray = [] self.yArray = [] for (x,y) in anaData: self.xArray.append(float(x)) self.yArray.append(float(y)) #Final init & lockdown checks if self.lockdown == True: if self.yVariable == None or self.xVariable == None: #The x,yArray may still be empty, don't check for this raise AcdOptiException_metaAnalysis_loadFail("Lockdown, but xVariable='" + self.xVariable + "', yVariable='" + self.yVariable) self.xArray = np.asarray(self.xArray) self.yArray = np.asarray(self.yArray)
def write(self): self.__paramfile.dataDict.setValSingle("lockdown", str(self.lockdown)) self.__paramfile.dataDict.setValSingle("extractFname", self.extractFname) knfile = self.__paramfile.dataDict["keyNames"] knfile.clear() for kn in self.keyNames: knfile.pushBack("keyName", kn) edfile = self.__paramfile.dataDict["extractedData"] edfile.clear() for row in self.dataExtracted: rowDict = DataDict() for (colKey,col) in row.iteritems(): rowDict.pushBack(colKey, col) edfile.pushBack("dataPoint",rowDict) fifile = self.__paramfile.dataDict["filters"] fifile.clear() for f in self.filters: f.settingsDict["numFiltered"] = str(f.numFiltered) fifile.pushBack(f.filterType, f.settingsDict) kkfile = self.__paramfile.dataDict["keepKeys"] kkfile.clear() for k in self.keepKeys: if len(self.keyNames) > 0 and not k in self.keyNames: print "WARNING: Invalid key '" + k + "' in keepKeys" kkfile.pushBack("key", k) plfile = self.__paramfile.dataDict["plots"] plfile.clear() for p in self.plots: plfile.pushBack(p.plotType, p.settingsDict) self.__paramfile.write()
def __generateSetup_recursiveHelper(setupDict): print "AcdOptiSolverManager::__generateSetup_recursiveHelper()" # , setupDict=", setupDict ret = DataDict() for item in setupDict: if not DataDict.boolconv(item[1]["enabled"]): continue if item[1]["type"] == "dict": ret.pushBack(item[0], AcdOptiSolverManager.__generateSetup_recursiveHelper(item[1]["children"])) else: ret.pushBack(item[0], item[1]["value"]) return ret
def __init__(self, dataExtractor, settingsDictOrInstName): assert settingsDictOrInstName != None settingsDict = None print settingsDictOrInstName if type(settingsDictOrInstName) == str: settingsDict = DataDict() settingsDict.pushBack("plotType", self.plotType) settingsDict.pushBack("instName", settingsDictOrInstName) settingsDict.pushBack("varX", "") settingsDict.pushBack("varY", "") else: settingsDict = settingsDictOrInstName print settingsDict super(DataExtractorPlot2D,self).__init__(dataExtractor,settingsDict) self.varX = settingsDict["varX"] self.varY = settingsDict["varY"]
def __init__(self, folder, collection): self.folder = folder self.collection = collection #Construct the instance name from folder instName = folder if instName[-1] == "/": instName = instName[0:-1] self.instName = instName = os.path.split(instName)[1] #Load paramFile self.__paramfile = AcdOptiFileParser_simple(os.path.join(folder, "paramFile.set"), 'rw') if self.__paramfile.dataDict["fileID"] != "AcdOptiDataExtractor": raise AcdOptiException_dataExtractor_loadFail("Got wrong fileID='" + self.__paramfile.dataDict["fileID"] + "'") if self.__paramfile.dataDict.getValSingle("instName") != self.instName: raise AcdOptiException_dataExtractor_loadFail("instName doesn't match folder name") self.lockdown = DataDict.boolconv(self.__paramfile.dataDict.getValSingle("lockdown")) try: self.extractFname = self.__paramfile.dataDict["extractFname"] except AcdOptiException_dataDict_getValsSingle: print "AcdOptiDataExtractor::__init__(): Adding extractFname to paramFile" self.__paramfile.dataDict.pushBack("extractFname", "") self.__paramfile.write() self.extractFname = "" #KeepKeys self.keepKeys = [] try: fileKeepKeys = self.__paramfile.dataDict["keepKeys"] for (k,v) in fileKeepKeys: assert k == "key" self.keepKeys.append(v) except AcdOptiException_dataDict_getValsSingle: print "AcdOptiDataExtractor::__init__(): Adding keepKeys to paramFile" self.__paramfile.dataDict.pushBack("keepKeys", DataDict()) self.__paramfile.write() #Parse keyNames self.keyNames = [] for (k,v) in self.__paramfile.dataDict["keyNames"]: assert k == "keyName" self.keyNames.append(v) #Parse extractedData self.dataExtracted = [] for (k,v) in self.__paramfile.dataDict["extractedData"]: assert k == "dataPoint" assert isinstance(v,DataDict) pb = {} for (k,v) in v: pb[k] = v if not k in self.keyNames: raise AcdOptiException_dataExtractor_loadFail("Key name '" + k + "' not found in self.keyNames") self.dataExtracted.append(pb) #Find, load, and attach filters self.filters = [] for (k,v) in self.__paramfile.dataDict["filters"]: self.filters.append(AcdOptiDataExtractorFilter.getFilterClass(k,v)) #Plots self.plots = [] try: filePlots = self.__paramfile.dataDict["plots"] for (k,v) in filePlots: self.plots.append(AcdOptiDataExtractorPlot.getPlotClass(k, self, v)) except AcdOptiException_dataDict_getValsSingle: self.__paramfile.dataDict.pushBack("plots", DataDict()) self.__paramfile.write()
def __init__(self, folder, scanCollection): self.folder = folder self.scanCollection = scanCollection #Construct the instance name from folder instName = folder if instName[-1] == "/": instName = instName[0:-1] self.instName = instName = os.path.split(instName)[1] #Load paramFile self.__paramfile = AcdOptiFileParser_simple(os.path.join(folder, "paramFile.set"), 'rw') if self.__paramfile.dataDict["fileID"] != "AcdOptiScan": raise AcdOptiException_scan_loadFail("Got wrong fileID='" + self.__paramfile.dataDict["fileID"] + "'") if self.__paramfile.dataDict.getValSingle("instName") != self.instName: raise AcdOptiException_scan_loadFail("instName doesn't match folder name") self.lockdown = DataDict.boolconv(self.__paramfile.dataDict.getValSingle("lockdown")) self.staged = DataDict.boolconv(self.__paramfile.dataDict.getValSingle("staged")) self.run = DataDict.boolconv(self.__paramfile.dataDict.getValSingle("run")) self.slaveGeomsDict = self.__paramfile.dataDict["slaveGeoms"] self.slaveGeoms = [] baseGeomInstance_name = self.__paramfile.dataDict["baseGeomInstance_name"] if baseGeomInstance_name == "": self.baseGeomInstance = None assert len(self.slaveGeomsDict) == 0 else: self.baseGeomInstance = self.scanCollection.project.geomCollection.geomInstances[baseGeomInstance_name] if self.__paramfile.dataDict["scanParameter_name"] != "": #assert self.baseGeomInstance != None #Why? self.scanParameter_name = self.__paramfile.dataDict["scanParameter_name"] assert self.scanParameter_name in self.getValidParamNames() self.scanParameter_range_max = float(self.__paramfile.dataDict["scanParameter_max"]) self.scanParameter_range_min = float(self.__paramfile.dataDict["scanParameter_min"]) self.scanParameter_range_step = float(self.__paramfile.dataDict["scanParameter_step"]) self.generateRange() try: self.predict_anaVariable = self.__paramfile.dataDict["predict_anaVariable"] self.predict_targetValue = self.__paramfile.dataDict["predict_targetValue"] except AcdOptiException_dataDict_getValsSingle: self.predict_anaVariable = "" self.predict_targetValue = "" self.__paramfile.dataDict.pushBack("predict_anaVariable", "") self.__paramfile.dataDict.pushBack("predict_targetValue", "") self.__paramfile.write() try: self.predict_a = self.__paramfile.dataDict["predict_a"] self.predict_b = self.__paramfile.dataDict["predict_b"] self.predict_x = self.__paramfile.dataDict["predict_x"] self.predict_r = self.__paramfile.dataDict["predict_r"] except AcdOptiException_dataDict_getValsSingle: self.predict_a = "" self.predict_b = "" self.predict_x = "" self.predict_r = "" self.__paramfile.dataDict.pushBack("predict_a", "") self.__paramfile.dataDict.pushBack("predict_b", "") self.__paramfile.dataDict.pushBack("predict_x", "") self.__paramfile.dataDict.pushBack("predict_r", "") self.__paramfile.write() try: self.predict_ndof=self.__paramfile.dataDict["predict_ndof"] except AcdOptiException_dataDict_getValsSingle: self.predict_ndof = "" self.__paramfile.dataDict.pushBack("predict_ndof","") self.__paramfile.write() for (geomName, nothingOfInterest) in self.slaveGeomsDict: #Mutal referencing self.slaveGeoms.append(self.scanCollection.project.geomCollection.geomInstances[geomName]) self.slaveGeoms[-1].scanInstances.append(self)
def __init__(self, dataExtractor, settingsDictOrInstName): assert settingsDictOrInstName != None settingsDict = None print settingsDictOrInstName if type(settingsDictOrInstName) == str: settingsDict = DataDict() settingsDict.pushBack("plotType", self.plotType) settingsDict.pushBack("instName", settingsDictOrInstName) settingsDict.pushBack("varX", "GEOM.e") settingsDict.pushBack("varY", "GEOM.sFrac") settingsDict.pushBack("constE", "220") #(MV/m)**6 * 200 ns, will be taken to the power 6 settingsDict.pushBack("constE_2", "250") #(MV/m)**6 * 200 ns, will be taken to the power 6 settingsDict.pushBack("varNormE", DataDict()) settingsDict["varNormE"].pushBack("var", "ANA.RFpost_local.maxFieldsOnSurface[0].surf[0].mode[0].Emax_norm[0]") settingsDict.pushBack("constSC", "4.0") #(MW/mm**2)**3 * 200 ns, will be taken to the power 3 settingsDict.pushBack("constSC_2", "5.0") #(MW/mm**2)**3 * 200 ns, will be taken to the power 3 settingsDict.pushBack("varNormSC", DataDict()) settingsDict["varNormSC"].pushBack("var", "ANA.RFpost_local.maxFieldsOnSurface[0].surf[0].mode[0].SCmax_norm[0]") settingsDict.pushBack("constPC", "2.3") #(MW/mm)**3 * 200 ns, will be taken to the power 3 settingsDict.pushBack("constPC_2", "2.9") #(MW/mm)**3 * 200 ns, will be taken to the power 3 settingsDict.pushBack("varFrequency", "ANA.Omega3P_modeInfo.Mode[0].FrequencyReal[0]") settingsDict.pushBack("varRQ", "ANA.RFpost_local.RoverQ[0].mode[0].RoQ_norm[0]") settingsDict.pushBack("varVg", "ANA.GroupVelocity.VG[0]") settingsDict.pushBack("varRadius", "GEOM.a") else: settingsDict = settingsDictOrInstName if not "constE_2" in settingsDict.getKeys(): print "Adding optimistic constants!" print settingsDict settingsDict.pushBack("constE_2", "250") #(MV/m)**6 * 200 ns, will be taken to the power 6 settingsDict.pushBack("constSC_2", "5.0") #(MW/mm**2)**3 * 200 ns, will be taken to the power 3 settingsDict.pushBack("constPC_2", "2.9") #(MW/mm)**3 * 200 ns, will be taken to the power 3 if type(settingsDict["varNormE"]) == str: assert type(settingsDict["varNormSC"]) == str print "Changing varNormE and varNormSC to a DataDict" print settingsDict oldVarNormE = settingsDict["varNormE"] newVarNormE = DataDict() newVarNormE.pushBack("var", oldVarNormE) settingsDict.delItem("varNormE") settingsDict.pushBack("varNormE", newVarNormE) oldVarNormSC = settingsDict["varNormSC"] newVarNormSC = DataDict() newVarNormSC.pushBack("var", oldVarNormSC) settingsDict.delItem("varNormSC") settingsDict.pushBack("varNormSC", newVarNormSC) # print settingsDict super(DataExtractorPlotsScaleOptim,self).__init__(dataExtractor,settingsDict) self.varX = settingsDict["varX"] self.varY = settingsDict["varY"] self.constE = settingsDict["constE"] self.constE_2 = settingsDict["constE_2"] self.varNormE = settingsDict["varNormE"].vals self.constSC = settingsDict["constSC"] self.constSC_2 = settingsDict["constSC_2"] self.varNormSC = settingsDict["varNormSC"].vals self.constPC = settingsDict["constPC"] self.constPC_2 = settingsDict["constPC_2"] self.varFrequency = settingsDict["varFrequency"] self.varRQ = settingsDict["varRQ"] self.varVg = settingsDict["varVg"] self.varRadius = settingsDict["varRadius"]
def __init__(self, dataExtractor, settingsDictOrInstName): assert settingsDictOrInstName != None settingsDict = None print settingsDictOrInstName if type(settingsDictOrInstName) == str: settingsDict = DataDict() settingsDict.pushBack("plotType", self.plotType) settingsDict.pushBack("instName", settingsDictOrInstName) settingsDict.pushBack("varX", "") settingsDict.pushBack("varY", "") settingsDict.pushBack("varZ", "") settingsDict.pushBack("limit", "") settingsDict.pushBack("useLimit", "False") settingsDict.pushBack("numContours", ""); else: settingsDict = settingsDictOrInstName print settingsDict super(DataExtractorPlot3D,self).__init__(dataExtractor,settingsDict) self.varX = settingsDict["varX"] self.varY = settingsDict["varY"] self.varZ = settingsDict["varZ"] if type(self.varZ) == str: print "DataExtractorPlot3D: Converting varZ..." oldVarZ = self.varZ self.varZ = DataDict() self.varZ.pushBack("var", oldVarZ) try: self.limit = settingsDict["limit"] self.useLimit = settingsDict["useLimit"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("limit", "") self.limit = "" settingsDict.pushBack("useLimit", "False") self.useLimit = "False" try: self.numContours = settingsDict["numContours"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("numContours", ""); self.numContours = "" try: self.extractionMode = settingsDict["extractionMode"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("extractionMode", "all"); self.extractionMode = "all" try: self.ZmultFactor = settingsDict["ZmultFactor"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("ZmultFactor", "1.0") self.ZmultFactor = "1.0"
class DataExtractorPlot3D(AcdOptiDataExtractorPlot): plotType = "DataExtractorPlot3D" varX = None varY = None varZ = None limit = None useLimit = None numContours = None extractionMode = None ZmultFactor = None def __init__(self, dataExtractor, settingsDictOrInstName): assert settingsDictOrInstName != None settingsDict = None print settingsDictOrInstName if type(settingsDictOrInstName) == str: settingsDict = DataDict() settingsDict.pushBack("plotType", self.plotType) settingsDict.pushBack("instName", settingsDictOrInstName) settingsDict.pushBack("varX", "") settingsDict.pushBack("varY", "") settingsDict.pushBack("varZ", "") settingsDict.pushBack("limit", "") settingsDict.pushBack("useLimit", "False") settingsDict.pushBack("numContours", ""); else: settingsDict = settingsDictOrInstName print settingsDict super(DataExtractorPlot3D,self).__init__(dataExtractor,settingsDict) self.varX = settingsDict["varX"] self.varY = settingsDict["varY"] self.varZ = settingsDict["varZ"] if type(self.varZ) == str: print "DataExtractorPlot3D: Converting varZ..." oldVarZ = self.varZ self.varZ = DataDict() self.varZ.pushBack("var", oldVarZ) try: self.limit = settingsDict["limit"] self.useLimit = settingsDict["useLimit"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("limit", "") self.limit = "" settingsDict.pushBack("useLimit", "False") self.useLimit = "False" try: self.numContours = settingsDict["numContours"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("numContours", ""); self.numContours = "" try: self.extractionMode = settingsDict["extractionMode"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("extractionMode", "all"); self.extractionMode = "all" try: self.ZmultFactor = settingsDict["ZmultFactor"] except AcdOptiException_dataDict_getValsSingle: settingsDict.pushBack("ZmultFactor", "1.0") self.ZmultFactor = "1.0" def doExport(self,fname): (X, Y, Z) = self.getData() raise NotImplementedError def getData(self): assert self.dataExtractor.lockdown assert self.varX in self.dataExtractor.keyNames assert self.varY in self.dataExtractor.keyNames #assert self.varZ in self.dataExtractor.keyNames for k in self.varZ.vals: assert k in self.dataExtractor.keyNames assert self.extractionMode=="all" or self.extractionMode=="min" \ or self.extractionMode=="max" or self.extractionMode=="mean" X = [] Y = [] Z = [] for (row, rcount) in zip(self.dataExtractor.dataExtracted, xrange(len(self.dataExtractor.dataExtracted))): try: x = float(row[self.varX]) y = float(row[self.varY]) except KeyError: continue z = [] for zk in self.varZ.vals: try: z.append( float(row[zk]) ) except KeyError: pass except ValueError: print "Warning in DataExtractorPlot2D::getData(): Could not convert value in row", rcount, "to float, skipping!" #print "x=",x,"y=",y,"z=",z if len(z) == 0: continue if self.extractionMode == "all": X += [x,]*len(z) Y += [y,]*len(z) Z += z elif self.extractionMode == "mean": X.append(x) Y.append(y) Z.append(np.mean(z)) elif self.extractionMode == "min": X.append(x) Y.append(y) Z.append(np.min(z)) elif self.extractionMode == "max": X.append(x) Y.append(y) Z.append(np.max(z)) ZmultFactor = float(self.ZmultFactor) #input checking is present if ZmultFactor != 1.0: print "Multiplying with ZmultFactor =", ZmultFactor Z = map(lambda z: z*ZmultFactor, Z) return (X,Y,Z) def deduplicate(self,X,Y,Z,mode="mean"): """ Deduplicate by finding all unique pairs (x,y), for each such pair set Z = average Z over all pair occurences, or find min or max value """ assert len(X) == len(Y) and len(Y) == len(Z) assert mode=="mean" or mode=="max" or mode=="min" #Dedup'ed variables x = [] y = [] z = [] n = [] def comparePair(x1,y1, x2,y2): return x1==x2 and y1==y2 #Find all unique paris for i in xrange(len(X)): prevFound = False for j in xrange(len(x)): if comparePair(X[i],Y[i],x[j],y[j]): prevFound = True break if not prevFound: x.append(X[i]) y.append(Y[i]) #Average over z for j in xrange(len(x)): #Loop over unique (x,y) -> z n.append(0) if mode == "mean": z.append(0.0) else: z.append(None) for i in xrange(len(X)): #Loop over all (X,Y,Z)) if comparePair(X[i],Y[i],x[j],y[j]): n[-1] += 1 if mode == "mean": z[-1] += Z[i] elif mode == "min" and (z[-1] == None or z[-1] > Z[i]): z[-1] = Z[i] elif mode == "max" and (z[-1] == None or z[-1] < Z[i]): z[-1] = Z[i] if mode == "mean": z[-1] /= float(n[-1]) return(x,y,z,n) def fitPlane(self): """Fit Z=a0+a1*x+a2*y""" (X,Y,Z) = self.getData() assert len(X) == len(Y) and len(X) == len(Z) ndof = len(Z)-3 assert ndof > 0 import numpy as np A = [np.ones(len(Z))] A.append(np.asarray(X)) A.append(np.asarray(Y)) A = np.vstack(A).T model = np.linalg.lstsq(A,Z)[0] #Calculate R^2 R2 = 0.0 for i in xrange(len(Z)): R2 += ( (model[0] + model[1]*X[i] + model[2]*Y[i]) - Z[i])**2 return (model, ndof, R2) def fitQuad(self): """Fit Z=a0 + a1*x+a2*y + a3*x^2+a4*y^2 + a5*x*y""" (X,Y,Z) = self.getData() assert len(X) == len(Y) and len(X) == len(Z) ndof = len(Z)-6 assert ndof > 0 import numpy as np A = [np.ones(len(Z))] A.append(np.asarray(X)) A.append(np.asarray(Y)) A.append(np.asarray(X)**2) A.append(np.asarray(Y)**2) A.append(np.asarray(X)*np.asarray(Y)) A = np.vstack(A).T model = np.linalg.lstsq(A,Z)[0] #Calculate R^2 R2 = 0.0 for i in xrange(len(Z)): R2 += ( (model[0] + model[1]*X[i] + model[2]*Y[i] + model[3]*X[i]**2 + model[4]*Y[i]**2 + model[5]*X[i]*Y[i]) - Z[i])**2 return (model, ndof, R2) def getBelowLimit(self,limit): "Remove points with z-values larger than limit" (X,Y,Z) = self.getData() x = [] y = [] z = [] for i in xrange(len(X)): if (Z[i] < limit): x.append(X[i]) y.append(Y[i]) z.append(Z[i]) return (x,y,z); def updateSettingsDict(self): self.settingsDict["varX"] = self.varX self.settingsDict["varY"] = self.varY self.settingsDict["varZ"] = self.varZ self.settingsDict["limit"] = self.limit self.settingsDict["useLimit"] = self.useLimit self.settingsDict["numContours"] = self.numContours self.settingsDict["extractionMode"] = self.extractionMode self.settingsDict["ZmultFactor"] = self.ZmultFactor
def __init__(self, folder, geometryInstance, meshTemplateCollection): """ Loads the meshInstance from the data from folder and connects it to its template and geometryInstance. Raises AcdOptiException_meshInstance_loadFail if something (such as "not a meshInstance folder") goes wrong. """ print "AcdOptiMeshInstance::__init__()" self.folder = folder self.geometryInstance = geometryInstance self.meshTemplateCollection = meshTemplateCollection if not os.path.isdir(folder): raise AcdOptiException_meshInstance_loadFail(\ "Folder \"" + folder + "\" does not exist") #Construct the instance name from folder name instname = folder if instname[-1] == "/": instname = instname[0:-1] instname = os.path.split(instname)[1] self.instName = instname #Load paramFile.set try: self.__paramFile = AcdOptiFileParser_simple(\ os.path.join(folder, "paramFile.set"), 'rw') except IOError: raise AcdOptiException_meshInstance_loadFail(\ "Problem loading file \"paramFile.set\" in folder \"" +\ folder + "\", got an IOError") if self.__paramFile.dataDict.getValSingle("fileID") != "meshInstance": raise AcdOptiException_meshInstance_loadFail( "Wrong fileID in meshInstance.set, got \"" + self.__paramFile.dataDict.getValSingle("fileID") + "\"") lock = self.__paramFile.dataDict.getValSingle("lockdown") if lock == "True": self.lockdown = True elif lock == "False": self.lockdown = False else: raise AcdOptiException_meshInstance_loadFail(\ "Invalid content in field \"lockdown\" of file paramFile.set") meshBad = self.__paramFile.dataDict.getVals("meshBad") if len(meshBad) == 0: self.meshBad = False else: assert len(meshBad) == 1 self.meshBad = DataDict.boolconv(meshBad[0]) #Check that the geometryInstance is correct geometryInstance_expectedName = self.__paramFile.dataDict.getValSingle("geomInstance_name") if geometryInstance.instName != geometryInstance_expectedName: raise AcdOptiException_meshInstance_loadFail(\ "Excpected name of geometryInstance does not match the one passed") #Find the MeshTemplate meshTemplate_expectedName = self.__paramFile.dataDict.getValSingle("meshTemplate_name") try: self.meshTemplate = self.meshTemplateCollection.meshTemplates[meshTemplate_expectedName] self.meshTemplate.registerInstance(self) except KeyError: raise AcdOptiException_meshInstance_loadFail("Could not find the meshTemplate \"" + meshTemplate_expectedName + "\"") #Find and load template overrides self.__templateOverrides = {} try: templateOverrides_data = self.__paramFile.dataDict.getValSingle("templateOverrides") except AcdOptiException_dataDict_getValsSingle: raise AcdOptiException_meshInstance_loadFail\ ("Couldn't load templateOverrides from paramFile.set") if not isinstance(templateOverrides_data,DataDict): raise AcdOptiException_meshInstance_loadFail\ ("templateOverrides from paramFile.set is not a DataDict!") for (k,v) in zip(templateOverrides_data.keys, templateOverrides_data.vals): if k in self.__templateOverrides: raise AcdOptiException_geomCollection_loadFail\ ("Double occurrence of key \"" + k + "\" in templateOverrides on file") if not k in self.meshTemplate.paramDefaults_getKeys(): raise AcdOptiException_meshInstance_loadFail( "Entry \"" + k + "\" in templateOverrides on file has no match in the template") self.__templateOverrides[k] = v #Find subfolders and check if they are runConfigs if not os.path.isdir(os.path.join(self.folder, "runConfigs")): raise AcdOptiException_meshInstance_loadFail("Could not find runConfigs folder") self.runConfigs = {} for d in os.listdir(os.path.join(self.folder,"runConfigs")): dAbs = os.path.abspath(os.path.join(self.folder,"runConfigs",d)) if not os.path.isdir(dAbs): #Skip files etc. continue #try: self.runConfigs[d] = AcdOptiRunConfig(dAbs,self)