def testGetSpreadsheet(self): csvName = path.join(self.theDir, "nix.csv") sd = TimelineDirectory(self.theDir) st = sd["p"] spread = st() spread.writeCSV(csvName) self.assertEqual(len(st.positions) + 1, len(spread.names())) rereadSpread = SpreadsheetData(csvName=csvName) self.assertEqual(len(spread.names()), len(rereadSpread.names())) self.assertEqual(len(spread.data), len(rereadSpread.data)) sd = TimelineDirectory(self.theDir, "timeline") st = sd["p"] spread = st() spread.writeCSV(path.join(self.theDir, "nix2.csv"))
def testGetSpreadsheet(self): csvName = path.join(self.theDir, "nix.csv") sd = TimelineDirectory(self.theDir) st = sd["p"] spread = st() assert len(spread.names()) == 5 assert spread.size() == 3 spread.writeCSV(csvName) assert len(st.positions) + 1 == len(spread.names()) rereadSpread = SpreadsheetData(csvName=csvName) assert len(spread.names()) == len(rereadSpread.names()) assert len(spread.data) == len(rereadSpread.data) sd = TimelineDirectory(self.theDir, "timeline") st = sd["p"] spread = st() spread.writeCSV(path.join(self.theDir, "nix2.csv"))
def testFindCorrectNames(self): sd = TimelineDirectory(self.theDir, writeTime=0.1) self.assertEqual(len(sd), 3) self.assertEqual(sd.timeRange(), (0., 3.)) self.assertEqual(len(sd.getData([0])), 3) self.assertEqual(len(sd.getData([0])[0][2]), 4) self.assertEqual(len(sd.getDataLocation(vectorMode="mag")), 12) self.assertEqual(sd.positions(), ['(0 0 1)', '(1 0 1)', '(2 0 1)', '(3 1 1)'])
def testGetTimesTimeline(self): sd = TimelineDirectory(self.theDir, dirName="timeline") st = sd["p"] self.assertEqual(st.getData([0.5, 1, 2, 4]), [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [1.0, 1.0, 1.0]]) self.assert_(not st.isProbe()) st = sd["h"] self.assertEqual(st.getData( [0.5, 1, 2, 4]), [[0.0, 0.0, 0.0], [0.0, 1.0], [0.0, 1.0], [1.0, 1.0, 1.0]]) self.assertRaises(KeyError, sd.__getitem__, "ha")
def testGetTimes(self): sd = TimelineDirectory(self.theDir) st = sd["p"] self.assertEqual(st.getData([0.5, 1, 2, 4]), [[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0]]) self.assert_(st.isProbe()) st = sd["h"] # this should be checked, but I don't want to self.assertEqual( st.getData([0.5, 1, 2, 4]), [[0.0, 0.0, 0.0, 0.0], [0.0, 1.0], [0.0, 1.0], [1.0, 1.0, 1.0]]) self.assertRaises(KeyError, sd.__getitem__, "ha")
def testFindCorrectNames(self): sd=TimelineDirectory(theDir,writeTime=0.1) self.assertEqual(len(sd),2) self.assertEqual(sd.timeRange(),(0.,3.)) self.assertEqual(len(sd.getData([0])),2) self.assertEqual(len(sd.getData([0])[0][2]),4) self.assertEqual(len(sd.getDataLocation()),8) self.assertEqual(sd.positions(), ['(0 0 1)', '(1 0 1)', '(2 0 1)', '(3 1 1)'])
def testGetTimesTimelineWithStringNoTitle(self): sd = TimelineDirectory(self.theDir, dirName="state") st = sd["state"] spread = st(False) self.assertEqual(spread(-1, "state"), "") self.assertEqual(spread(1, "state"), b("da")) self.assertEqual(spread(2.7, "state"), b("hier")) self.assertAlmostEqual(spread(2.7, "code"), 2.85) data = spread(2.7) self.assertEqual(len(data), 2) self.assertEqual(data["state"], b("hier")) self.assertAlmostEqual(data["code"], 2.85) import numpy as np self.assertEqual(spread.data.dtype[1].kind, "S")
def testGetTimesTimelineWithString(self): sd = TimelineDirectory(self.theDir, dirName="state") st = sd["state"] self.assertEqual(st.getData( [0.5, 1, 2, 4]), [['nix', 1.0], ['da', 2.0], ['da', 2.0], ['hier', 3.0]]) self.assert_(not st.isProbe()) spread = st() self.assertEqual(spread(-1, "state_t=0 state"), "") self.assertEqual(spread(1, "state_t=0 state"), b("da")) self.assertEqual(spread(2.7, "state_t=0 state"), b("hier")) self.assertAlmostEqual(spread(2.7, "state_t=0 code"), 2.85) data = spread(2.7) self.assertEqual(len(data), 2) self.assertEqual(data["state_t=0 state"], b("hier")) self.assertAlmostEqual(data["state_t=0 code"], 2.85)
def run(self): # remove trailing slashif present if self.opts.dirName[-1] == path.sep: self.opts.dirName = self.opts.dirName[:-1] usedDirName = self.opts.dirName.replace("/", "_") timelines = TimelineDirectory(self.parser.getArgs()[0], dirName=self.opts.dirName, writeTime=self.opts.writeTime) reference = None if self.opts.reference and self.opts.referenceCase: self.error( "Options --reference-directory and --reference-case are mutual exclusive" ) if (self.opts.csvFile or self.opts.excelFile or self.opts.pandasData or self.opts.numpyData) and (self.opts.compare or self.opts.metrics): self.error( "Options --csv-file/excel-file/--pandas-data/--numpy-data and --compare/--metrics are mutual exclusive" ) if self.opts.reference: reference = TimelineDirectory(self.parser.getArgs()[0], dirName=self.opts.reference, writeTime=self.opts.writeTime) elif self.opts.referenceCase: reference = TimelineDirectory(self.opts.referenceCase, dirName=self.opts.dirName, writeTime=self.opts.writeTime) if self.opts.info: self.setData({ 'writeTimes': timelines.writeTimes, 'usedTimes': timelines.usedTime, 'fields': timelines.values, 'positions': timelines.positions(), 'timeRange': timelines.timeRange() }) if not self.opts.silent: print_("Write Times : ", timelines.writeTimes) print_("Used Time : ", timelines.usedTime) print_("Fields : ", timelines.values, end="") if len(timelines.vectors) > 0: if not self.opts.silent: print_(" Vectors: ", timelines.vectors) self.setData({'vectors': timelines.vectors}) else: if not self.opts.silent: print_() if not self.opts.silent: print_("Positions : ", timelines.positions()) print_("Time range : ", timelines.timeRange()) if reference: refData = { 'writeTimes': reference.writeTimes, 'fields': reference.values, 'positions': reference.positions(), 'timeRange': reference.timeRange() } if not self.opts.silent: print_("\nReference Data") print_("Write Times : ", reference.writeTimes) print_("Fields : ", reference.values, end="") if len(reference.vectors) > 0: if not self.opts.silent: print_(" Vectors: ", reference.vectors) refData["vectors"] = reference.vectors else: if not self.opts.silent: print_() if not self.opts.silent: print_("Positions : ", reference.positions()) print_("Time range : ", reference.timeRange()) self.setData({"reference": refData}) return 0 if self.opts.fields == None: self.opts.fields = timelines.values else: for v in self.opts.fields: if v not in timelines.values: self.error("The requested value", v, "not in possible values", timelines.values) if self.opts.positions == None: self.opts.positions = timelines.positions() else: pos = self.opts.positions self.opts.positions = [] for p in pos: try: p = int(p) if p < 0 or p >= len(timelines.positions()): self.error("Time index", p, "out of range for positons", timelines.positions()) else: self.opts.positions.append(timelines.positions()[p]) except ValueError: if p not in timelines.positions(): self.error("Position", p, "not in", timelines.positions()) else: self.opts.positions.append(p) if len(self.opts.positions) == 0: self.error("No valid positions") result = "set term png nocrop enhanced \n" if self.opts.basicMode == None: self.error("No mode selected. Do so with '--basic-mode'") elif self.opts.basicMode == 'bars': if self.opts.time == None: self.error("No times specified for bar-plots") self.opts.time.sort() if self.opts.referenceTime and reference != None: minTime, maxTime = reference.timeRange() else: minTime, maxTime = timelines.timeRange() usedTimes = [] hasMin = False for t in self.opts.time: if t < minTime: if not hasMin: usedTimes.append(minTime) hasMin = True elif t > maxTime: usedTimes.append(maxTime) break else: usedTimes.append(t) data = timelines.getData(usedTimes, value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) # print_(data) result += "set style data histogram\n" result += "set style histogram cluster gap 1\n" result += "set style fill solid border -1\n" result += "set boxwidth 0.9\n" result += "set xtics border in scale 1,0.5 nomirror rotate by 90 offset character 0, 0, 0\n" # set xtic rotate by -45\n" result += "set xtics (" for i, p in enumerate(self.opts.positions): if i > 0: result += " , " result += '"%s" %d' % (p, i) result += ")\n" for tm in usedTimes: if abs(float(tm)) > 1e20: continue result += self.setFile("%s_writeTime_%s_Time_%s.png" % (usedDirName, timelines.usedTime, tm)) result += 'set title "Directory: %s WriteTime: %s Time: %s"\n' % ( self.opts.dirName.replace("_", "\\\\_"), timelines.usedTime, tm) result += "plot " first = True for val in self.opts.fields: if first: first = False else: result += ", " result += '"-" title "%s" ' % val.replace("_", "\\\\_") result += "\n" for v, t, vals in data: if t == tm: for v in vals: result += "%g\n" % v result += "e\n" elif self.opts.basicMode == 'lines': # print_(self.opts.positions) oPlots = timelines.getDataLocation(value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) plots = oPlots[:] rPlots = None if reference: rPlots = reference.getDataLocation( value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) for gp, pos, val, comp, tv in rPlots: plots.append( (gp, pos, self.opts.refprefix + " " + val, comp, tv)) if self.opts.referenceTime and reference != None: minTime, maxTime = reference.timeRange() else: minTime, maxTime = timelines.timeRange() if self.opts.minTime: minTime = self.opts.minTime if self.opts.maxTime: maxTime = self.opts.maxTime result += "set xrange [%g:%g]\n" % (minTime, maxTime) if self.opts.collectLines == "fields": for val in self.opts.fields: vname = val if val in timelines.vectors: vname += "_" + self.opts.vectorMode result += self.setFile( "%s_writeTime_%s_Value_%s.png" % (usedDirName, timelines.usedTime, vname)) result += 'set title "Directory: %s WriteTime: %s Value: %s"\n' % ( self.opts.dirName.replace("_", "\\\\_"), timelines.usedTime, vname.replace("_", "\\\\\\_")) result += "plot " first = True for f, v, p, i, tl in plots: if v == val: if first: first = False else: result += " , " if type(i) == int: result += ' "%s" using 1:%d title "%s" with lines ' % ( f, i + 2, p.replace("_", "\\\\_")) else: result += ' "%s" using 1:%s title "%s" with lines ' % ( f, i, p.replace("_", "\\\\_")) result += "\n" elif self.opts.collectLines == "positions": for pos in self.opts.positions: result += self.setFile( "%s_writeTime_%s_Position_%s.png" % (usedDirName, timelines.usedTime, pos)) result += 'set title "Directory: %s WriteTime: %s Position: %s"\n' % ( self.opts.dirName.replace("_", "\\\\_"), timelines.usedTime, pos.replace("_", "\\\\_")) result += "plot " first = True for f, v, p, i, tl in plots: if p == pos: if first: first = False else: result += " , " if type(i) == int: result += ' "%s" using 1:%d title "%s" with lines ' % ( f, i + 2, v.replace("_", "\\\\_")) else: result += ' "%s" using 1:%s title "%s" with lines ' % ( f, i, v.replace("_", "\\\\_")) result += "\n" else: self.error("Unimplemented collection of lines:", self.opts.collectLines) else: self.error("Not implemented basicMode", self.opts.basicMode) if self.opts.csvFile or self.opts.excelFile or self.opts.pandasData or self.opts.numpyData: if self.opts.basicMode != 'lines': self.error( "CSV and Excel-files currently only supported for lines-mode (also Pandas and Numpy-data)" ) spread = plots[0][-1]() usedFiles = set([plots[0][0]]) for line in plots[1:]: if line[0] not in usedFiles: usedFiles.add(line[0]) sp = line[-1]() try: spread += sp except WrongDataSize: if self.opts.resample: for n in sp.names()[1:]: data = spread.resample( sp, n, extendData=self.opts.extendData) try: spread.append(n, data) except ValueError: spread.append( self.opts.refprefix + " " + n, data) else: self.warning("Try the --resample-option") raise if self.opts.csvFile: spread.writeCSV(self.opts.csvFile) if self.opts.excelFile: spread.getData().to_excel(self.opts.excelFile) if self.opts.pandasData: self.setData({ "series": spread.getSeries(), "dataFrame": spread.getData() }) if self.opts.numpyData: self.setData({"data": spread.data.copy()}) elif self.opts.compare or self.opts.metrics: statData = {} if self.opts.compare: statData["compare"] = {} if self.opts.metrics: statData["metrics"] = {} for p in self.opts.positions: if self.opts.compare: statData["compare"][p] = {} if self.opts.metrics: statData["metrics"][p] = {} if self.opts.basicMode != 'lines': self.error("Compare currently only supported for lines-mode") if self.opts.compare: if rPlots == None: self.error("No reference data specified. Can't compare") elif len(rPlots) != len(oPlots): self.error("Number of original data sets", len(oPlots), "is not equal to the reference data sets", len(rPlots)) for i, p in enumerate(oPlots): pth, val, loc, ind, tl = p if self.opts.compare: rpth, rval, rloc, rind, rtl = rPlots[i] if val != rval or loc != rloc or ind != rind: self.error("Original data", p, "and reference", rPlots[i], "do not match") data = tl() try: dataIndex = 1 + ind if self.opts.metrics: if not self.opts.silent: print_("Metrics for", val, "on", loc, "index", ind, "(Path:", pth, ")") result = data.metrics(data.names()[dataIndex], minTime=self.opts.minTime, maxTime=self.opts.maxTime) statData["metrics"][loc][val] = result if not self.opts.silent: print_(" Min :", result["min"]) print_(" Max :", result["max"]) print_(" Average :", result["average"]) print_(" Weighted average :", result["wAverage"]) if not self.opts.compare: print_("Data size:", data.size()) print_(" Time Range :", result["tMin"], result["tMax"]) if self.opts.compare: if not self.opts.silent: print_("Comparing", val, "on", loc, "index", ind, "(path:", pth, ")", end="") ref = rtl() if self.opts.compareOnOriginal: if not self.opts.silent: print_("on original data points") result = data.compare(ref, data.names()[dataIndex], minTime=self.opts.minTime, maxTime=self.opts.maxTime) else: if not self.opts.silent: print_("on reference data points") result = ref.compare(data, data.names()[dataIndex], minTime=self.opts.minTime, maxTime=self.opts.maxTime) statData["compare"][loc][val] = result if not self.opts.silent: print_(" Max difference :", result["max"]) print_(" Average difference :", result["average"]) print_(" Weighted average :", result["wAverage"]) print_("Data size:", data.size(), "Reference:", ref.size()) if not self.opts.metrics: print_(" Time Range :", result["tMin"], result["tMax"]) if not self.opts.silent: print_() except TypeError: if self.opts.vectorMode == "mag": self.error( "Vector-mode 'mag' not supported for --compare and --metrics" ) else: raise self.setData(statData) else: dest = sys.stdout if self.opts.gnuplotFile: dest = open(self.opts.gnuplotFile, "w") dest.write(result)
def run(self): # remove trailing slashif present if self.opts.dirName[-1]==path.sep: self.opts.dirName=self.opts.dirName[:-1] usedDirName=self.opts.dirName.replace("/","_") timelines=TimelineDirectory(self.parser.getArgs()[0], dirName=self.opts.dirName, writeTime=self.opts.writeTime) reference=None if self.opts.reference and self.opts.referenceCase: self.error("Options --reference-directory and --reference-case are mutual exclusive") if (self.opts.csvFile or self.opts.excelFile or self.opts.pandasData or self.opts.numpyData) and (self.opts.compare or self.opts.metrics): self.error("Options --csv-file/excel-file/--pandas-data/--numpy-data and --compare/--metrics are mutual exclusive") if self.opts.reference: reference=TimelineDirectory(self.parser.getArgs()[0], dirName=self.opts.reference, writeTime=self.opts.writeTime) elif self.opts.referenceCase: reference=TimelineDirectory(self.opts.referenceCase, dirName=self.opts.dirName, writeTime=self.opts.writeTime) if self.opts.info: self.setData({'writeTimes' : timelines.writeTimes, 'usedTimes' : timelines.usedTime, 'fields' : timelines.values, 'positions' : timelines.positions(), 'timeRange' : timelines.timeRange()}) if not self.opts.silent: print_("Write Times : ",timelines.writeTimes) print_("Used Time : ",timelines.usedTime) print_("Fields : ",timelines.values,end="") if len(timelines.vectors)>0: if not self.opts.silent: print_(" Vectors: ",timelines.vectors) self.setData({'vectors':timelines.vectors}) else: if not self.opts.silent: print_() if not self.opts.silent: print_("Positions : ",timelines.positions()) print_("Time range : ",timelines.timeRange()) if reference: refData={'writeTimes' : reference.writeTimes, 'fields' : reference.values, 'positions' : reference.positions(), 'timeRange' : reference.timeRange()} if not self.opts.silent: print_("\nReference Data") print_("Write Times : ",reference.writeTimes) print_("Fields : ",reference.values,end="") if len(reference.vectors)>0: if not self.opts.silent: print_(" Vectors: ",reference.vectors) refData["vectors"]=reference.vectors else: if not self.opts.silent: print_() if not self.opts.silent: print_("Positions : ",reference.positions()) print_("Time range : ",reference.timeRange()) self.setData({"reference":refData}) return 0 if self.opts.fields==None: self.opts.fields=timelines.values else: for v in self.opts.fields: if v not in timelines.values: self.error("The requested value",v,"not in possible values",timelines.values) if self.opts.positions==None: self.opts.positions=timelines.positions() else: pos=self.opts.positions self.opts.positions=[] for p in pos: try: p=int(p) if p<0 or p>=len(timelines.positions()): self.error("Time index",p,"out of range for positons",timelines.positions()) else: self.opts.positions.append(timelines.positions()[p]) except ValueError: if p not in timelines.positions(): self.error("Position",p,"not in",timelines.positions()) else: self.opts.positions.append(p) if len(self.opts.positions)==0: self.error("No valid positions") result="set term png nocrop enhanced \n" if self.opts.basicMode==None: self.error("No mode selected. Do so with '--basic-mode'") elif self.opts.basicMode=='bars': if self.opts.time==None: self.error("No times specified for bar-plots") self.opts.time.sort() if self.opts.referenceTime and reference!=None: minTime,maxTime=reference.timeRange() else: minTime,maxTime=timelines.timeRange() usedTimes=[] hasMin=False for t in self.opts.time: if t<minTime: if not hasMin: usedTimes.append(minTime) hasMin=True elif t>maxTime: usedTimes.append(maxTime) break else: usedTimes.append(t) data=timelines.getData(usedTimes, value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) # print_(data) result+="set style data histogram\n" result+="set style histogram cluster gap 1\n" result+="set style fill solid border -1\n" result+="set boxwidth 0.9\n" result+="set xtics border in scale 1,0.5 nomirror rotate by 90 offset character 0, 0, 0\n" # set xtic rotate by -45\n" result+="set xtics (" for i,p in enumerate(self.opts.positions): if i>0: result+=" , " result+='"%s" %d' % (p,i) result+=")\n" for tm in usedTimes: if abs(float(tm))>1e20: continue result+=self.setFile("%s_writeTime_%s_Time_%s.png" % (usedDirName,timelines.usedTime,tm)) result+='set title "Directory: %s WriteTime: %s Time: %s"\n' % (self.opts.dirName.replace("_","\\\\_"),timelines.usedTime,tm) result+= "plot " first=True for val in self.opts.fields: if first: first=False else: result+=", " result+='"-" title "%s" ' % val.replace("_","\\\\_") result+="\n" for v,t,vals in data: if t==tm: for v in vals: result+="%g\n" % v result+="e\n" elif self.opts.basicMode=='lines': # print_(self.opts.positions) oPlots=timelines.getDataLocation(value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) plots=oPlots[:] rPlots=None if reference: rPlots=reference.getDataLocation(value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) for gp,pos,val,comp,tv in rPlots: plots.append((gp, pos, self.opts.refprefix+" "+val, comp, tv)) if self.opts.referenceTime and reference!=None: minTime,maxTime=reference.timeRange() else: minTime,maxTime=timelines.timeRange() if self.opts.minTime: minTime=self.opts.minTime if self.opts.maxTime: maxTime=self.opts.maxTime result+= "set xrange [%g:%g]\n" % (minTime,maxTime) if self.opts.collectLines=="fields": for val in self.opts.fields: vname=val if val in timelines.vectors: vname+="_"+self.opts.vectorMode result+=self.setFile("%s_writeTime_%s_Value_%s.png" % (usedDirName,timelines.usedTime,vname)) result+='set title "Directory: %s WriteTime: %s Value: %s"\n' % (self.opts.dirName.replace("_","\\\\_"),timelines.usedTime,vname.replace("_","\\\\\\_")) result+= "plot " first=True for f,v,p,i,tl in plots: if v==val: if first: first=False else: result+=" , " if type(i)==int: result+= ' "%s" using 1:%d title "%s" with lines ' % (f,i+2,p.replace("_","\\\\_")) else: result+= ' "%s" using 1:%s title "%s" with lines ' % (f,i,p.replace("_","\\\\_")) result+="\n" elif self.opts.collectLines=="positions": for pos in self.opts.positions: result+=self.setFile("%s_writeTime_%s_Position_%s.png" % (usedDirName,timelines.usedTime,pos)) result+='set title "Directory: %s WriteTime: %s Position: %s"\n' % (self.opts.dirName.replace("_","\\\\_"),timelines.usedTime,pos.replace("_","\\\\_")) result+= "plot " first=True for f,v,p,i,tl in plots: if p==pos: if first: first=False else: result+=" , " if type(i)==int: result+= ' "%s" using 1:%d title "%s" with lines ' % (f,i+2,v.replace("_","\\\\_")) else: result+= ' "%s" using 1:%s title "%s" with lines ' % (f,i,v.replace("_","\\\\_")) result+="\n" else: self.error("Unimplemented collection of lines:",self.opts.collectLines) else: self.error("Not implemented basicMode",self.opts.basicMode) if self.opts.csvFile or self.opts.excelFile or self.opts.pandasData or self.opts.numpyData: if self.opts.basicMode!='lines': self.error("CSV and Excel-files currently only supported for lines-mode (also Pandas and Numpy-data)") spread=plots[0][-1]() usedFiles=set([plots[0][0]]) for line in plots[1:]: if line[0] not in usedFiles: usedFiles.add(line[0]) sp=line[-1]() try: spread+=sp except WrongDataSize: if self.opts.resample: for n in sp.names()[1:]: data=spread.resample(sp, n, extendData=self.opts.extendData) try: spread.append(n,data) except ValueError: spread.append(self.opts.refprefix+" "+n,data) else: self.warning("Try the --resample-option") raise if self.opts.csvFile: spread.writeCSV(self.opts.csvFile) if self.opts.excelFile: spread.getData().to_excel(self.opts.excelFile) if self.opts.pandasData: self.setData({"series":spread.getSeries(), "dataFrame":spread.getData()}) if self.opts.numpyData: self.setData({"data":spread.data.copy()}) elif self.opts.compare or self.opts.metrics: statData={} if self.opts.compare: statData["compare"]={} if self.opts.metrics: statData["metrics"]={} for p in self.opts.positions: if self.opts.compare: statData["compare"][p]={} if self.opts.metrics: statData["metrics"][p]={} if self.opts.basicMode!='lines': self.error("Compare currently only supported for lines-mode") if self.opts.compare: if rPlots==None: self.error("No reference data specified. Can't compare") elif len(rPlots)!=len(oPlots): self.error("Number of original data sets",len(oPlots), "is not equal to the reference data sets", len(rPlots)) for i,p in enumerate(oPlots): pth,val,loc,ind,tl=p if self.opts.compare: rpth,rval,rloc,rind,rtl=rPlots[i] if val!=rval or loc!=rloc or ind!=rind: self.error("Original data",p,"and reference",rPlots[i], "do not match") data=tl() try: dataIndex=1+ind if self.opts.metrics: if not self.opts.silent: print_("Metrics for",val,"on",loc,"index",ind,"(Path:",pth,")") result=data.metrics(data.names()[dataIndex], minTime=self.opts.minTime, maxTime=self.opts.maxTime) statData["metrics"][loc][val]=result if not self.opts.silent: print_(" Min :",result["min"]) print_(" Max :",result["max"]) print_(" Average :",result["average"]) print_(" Weighted average :",result["wAverage"]) if not self.opts.compare: print_("Data size:",data.size()) print_(" Time Range :",result["tMin"],result["tMax"]) if self.opts.compare: if not self.opts.silent: print_("Comparing",val,"on",loc,"index",ind,"(path:",pth,")",end="") ref=rtl() if self.opts.compareOnOriginal: if not self.opts.silent: print_("on original data points") result=data.compare(ref, data.names()[dataIndex], minTime=self.opts.minTime, maxTime=self.opts.maxTime) else: if not self.opts.silent: print_("on reference data points") result=ref.compare(data, data.names()[dataIndex], minTime=self.opts.minTime, maxTime=self.opts.maxTime) statData["compare"][loc][val]=result if not self.opts.silent: print_(" Max difference :",result["max"]) print_(" Average difference :",result["average"]) print_(" Weighted average :",result["wAverage"]) print_("Data size:",data.size(),"Reference:",ref.size()) if not self.opts.metrics: print_(" Time Range :",result["tMin"],result["tMax"]) if not self.opts.silent: print_() except TypeError: if self.opts.vectorMode=="mag": self.error("Vector-mode 'mag' not supported for --compare and --metrics") else: raise self.setData(statData) else: dest=sys.stdout if self.opts.gnuplotFile: dest=open(self.opts.gnuplotFile,"w") dest.write(result)
def run(self): # remove trailing slashif present if self.opts.dirName[-1]==path.sep: self.opts.dirName=self.opts.dirName[:-1] timelines=TimelineDirectory(self.parser.getArgs()[0], dirName=self.opts.dirName, writeTime=self.opts.writeTime) reference=None if self.opts.reference and self.opts.referenceCase: self.error("Options --reference-directory and --reference-case are mutual exclusive") if self.opts.csvFile and (self.opts.compare or self.opts.metrics): self.error("Options --csv-file and --compare/--metrics are mutual exclusive") if self.opts.reference: reference=TimelineDirectory(self.parser.getArgs()[0], dirName=self.opts.reference, writeTime=self.opts.writeTime) elif self.opts.referenceCase: reference=TimelineDirectory(self.opts.referenceCase, dirName=self.opts.dirName, writeTime=self.opts.writeTime) if self.opts.info: self.setData({'writeTimes' : timelines.writeTimes, 'usedTimes' : timelines.usedTime, 'fields' : timelines.values, 'positions' : timelines.positions(), 'timeRange' : timelines.timeRange()}) if not self.opts.silent: print "Write Times : ",timelines.writeTimes print "Used Time : ",timelines.usedTime print "Fields : ",timelines.values, if len(timelines.vectors)>0: if not self.opts.silent: print " Vectors: ",timelines.vectors self.setData({'vectors':timelines.vectors}) else: if not self.opts.silent: print if not self.opts.silent: print "Positions : ",timelines.positions() print "Time range : ",timelines.timeRange() if reference: refData={'writeTimes' : reference.writeTimes, 'fields' : reference.values, 'positions' : reference.positions(), 'timeRange' : reference.timeRange()} if not self.opts.silent: print "\nReference Data" print "Write Times : ",reference.writeTimes print "Fields : ",reference.values, if len(reference.vectors)>0: if not self.opts.silent: print " Vectors: ",reference.vectors refData["vectors"]=reference.vectors else: if not self.opts.silent: print if not self.opts.silent: print "Positions : ",reference.positions() print "Time range : ",reference.timeRange() self.setData({"reference":refData}) return 0 if self.opts.fields==None: self.opts.fields=timelines.values else: for v in self.opts.fields: if v not in timelines.values: self.error("The requested value",v,"not in possible values",timelines.values) if self.opts.positions==None: self.opts.positions=timelines.positions() else: pos=self.opts.positions self.opts.positions=[] for p in pos: try: p=int(p) if p<0 or p>=len(timelines.positions()): self.error("Time index",p,"out of range for positons",timelines.positions()) else: self.opts.positions.append(timelines.positions()[p]) except ValueError: if p not in timelines.positions(): self.error("Position",p,"not in",timelines.positions()) else: self.opts.positions.append(p) result="set term png nocrop enhanced \n" if self.opts.basicMode==None: self.error("No mode selected. Do so with '--basic-mode'") elif self.opts.basicMode=='bars': if self.opts.time==None: self.error("No times specified for bar-plots") self.opts.time.sort() if self.opts.referenceTime and reference!=None: minTime,maxTime=reference.timeRange() else: minTime,maxTime=timelines.timeRange() usedTimes=[] hasMin=False for t in self.opts.time: if t<minTime: if not hasMin: usedTimes.append(minTime) hasMin=True elif t>maxTime: usedTimes.append(maxTime) break else: usedTimes.append(t) data=timelines.getData(usedTimes, value=self.opts.fields, position=self.opts.positions) # print data result+="set style data histogram\n" result+="set style histogram cluster gap 1\n" result+="set style fill solid border -1\n" result+="set boxwidth 0.9\n" result+="set xtics border in scale 1,0.5 nomirror rotate by 90 offset character 0, 0, 0\n" # set xtic rotate by -45\n" result+="set xtics (" for i,p in enumerate(self.opts.positions): if i>0: result+=" , " result+='"%s" %d' % (p,i) result+=")\n" for tm in usedTimes: if abs(float(tm))>1e20: continue result+=self.setFile("%s_writeTime_%s_Time_%s.png" % (self.opts.dirName,timelines.usedTime,tm)) result+='set title "Directory: %s WriteTime: %s Time: %s"\n' % (self.opts.dirName.replace("_","\\\\_"),timelines.usedTime,tm) result+= "plot " first=True for val in self.opts.fields: if first: first=False else: result+=", " result+='"-" title "%s" ' % val.replace("_","\\\\_") result+="\n" for v,t,vals in data: if t==tm: for v in vals: result+="%g\n" % v result+="e\n" elif self.opts.basicMode=='lines': # print self.opts.positions oPlots=timelines.getDataLocation(value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) plots=oPlots[:] rPlots=None if reference: rPlots=reference.getDataLocation(value=self.opts.fields, position=self.opts.positions, vectorMode=self.opts.vectorMode) for gp,pos,val,comp,tv in rPlots: plots.append((gp, pos, self.opts.refprefix+" "+val, comp, tv)) if self.opts.referenceTime and reference!=None: minTime,maxTime=reference.timeRange() else: minTime,maxTime=timelines.timeRange() if self.opts.minTime: minTime=self.opts.minTime if self.opts.maxTime: maxTime=self.opts.maxTime result+= "set xrange [%g:%g]\n" % (minTime,maxTime) if self.opts.collectLines=="fields": for val in self.opts.fields: vname=val if val in timelines.vectors: vname+="_"+self.opts.vectorMode result+=self.setFile("%s_writeTime_%s_Value_%s.png" % (self.opts.dirName,timelines.usedTime,vname)) result+='set title "Directory: %s WriteTime: %s Value: %s"\n' % (self.opts.dirName.replace("_","\\\\_"),timelines.usedTime,vname.replace("_","\\\\\\_")) result+= "plot " first=True for f,v,p,i,tl in plots: if v==val: if first: first=False else: result+=" , " if type(i)==int: result+= ' "%s" using 1:%d title "%s" with lines ' % (f,i+2,p.replace("_","\\\\_")) else: result+= ' "%s" using 1:%s title "%s" with lines ' % (f,i,p.replace("_","\\\\_")) result+="\n" elif self.opts.collectLines=="positions": for pos in self.opts.positions: result+=self.setFile("%s_writeTime_%s_Position_%s.png" % (self.opts.dirName,timelines.usedTime,pos)) result+='set title "Directory: %s WriteTime: %s Position: %s"\n' % (self.opts.dirName.replace("_","\\\\_"),timelines.usedTime,pos.replace("_","\\\\_")) result+= "plot " first=True for f,v,p,i in plots: if p==pos: if first: first=False else: result+=" , " if type(i)==int: result+= ' "%s" using 1:%d title "%s" with lines ' % (f,i+2,v.replace("_","\\\\_")) else: result+= ' "%s" using 1:%s title "%s" with lines ' % (f,i,v.replace("_","\\\\_")) result+="\n" else: self.error("Unimplemented collection of lines:",self.opts.collectLines) else: self.error("Not implemented basicMode",self.opts.basicMode) if self.opts.csvFile: if self.opts.basicMode!='lines': self.error("CSV-files currently only supported for lines-mode") spread=plots[0][-1]() for line in plots[1:]: if line[3]==0: sp=line[-1]() try: spread+=sp except WrongDataSize,e: if self.opts.resample: for n in sp.names()[1:]: data=spread.resample(sp, n, extendData=self.opts.extendData) try: spread.append(n,data) except ValueError: spread.append(self.opts.refprefix+" "+n,data) else: self.warning("Try the --resample-option") raise spread.writeCSV(self.opts.csvFile)