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 __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 __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 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 __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, 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