Beispiel #1
0
 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)'])
Beispiel #2
0
 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)'])
Beispiel #3
0
    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)
Beispiel #4
0
    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)
Beispiel #5
0
    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)