def execNameFound(self, execName):
        if hasattr(self, "oldExecName"):
            if execName == self.oldExecName:
                return

        self.automaticCustom = []
        self.oldExecName = execName
        from PyFoam import configuration as conf
        from PyFoam.Basics.CustomPlotInfo import CustomPlotInfo

        solvers = set([execName])

        for name, lst in conf().items("SolverBase"):
            if execName.lower().startswith(name):
                solvers |= set(eval(lst))

        import re
        autoplots = [
            a.lower() for a in conf().getList("Plotting", "autoplots")
        ]

        for name, dataString in conf().items("Autoplots"):
            found = False
            try:
                data = eval(dataString)
                solverPatterns = data["solvers"]
                info = data["plotinfo"]
                for s in solverPatterns:
                    try:
                        for eName in solvers:
                            if re.compile(s).fullmatch(eName):
                                found = True
                                break
                        if found:
                            break
                    except AttributeError:
                        # python 2 has no fullmatch
                        for eName in solvers:
                            if re.compile("(?:" + s + r")\Z").match(eName):
                                found = True
                                break
                        if found:
                            break
            except KeyError:
                import sys
                e = sys.exc_info()[1]
                warning("Miss-configured automatic expression", name,
                        "Missing key", e.args[0])
            except re.error:
                import sys
                warning("Problem with regular expression", s, "of", name, ":",
                        sys.exc_info()[1])
            except SyntaxError:
                warning("Syntax error in", dataString)

            if found or name.lower() in autoplots:
                self.automaticCustom.append(CustomPlotInfo(raw=info,
                                                           name=name))
Exemplo n.º 2
0
 def testCustomCounter(self):
     data = FoamStringParser(newStyle2)
     ci = CustomPlotInfo(data["testit"])
     self.assertEqual(ci.nr, 1)
     ci = CustomPlotInfo(data["time"])
     self.assertEqual(ci.nr, 2)
Exemplo n.º 3
0
 def testNewStyle(self):
     data = FoamStringParser(newStyle)
     ci = CustomPlotInfo(data["testit"])
     self.assertEqual(ci.expr, "Integral of CO2 = (%f%)")
     self.assertEqual(ci.nr, 1)
     self.assertEqual(ci.accumulation, "first")
Exemplo n.º 4
0
 def testPyDictRegexp(self):
     ci = CustomPlotInfo(pyDictRegexp)
     self.assertEqual(ci.expr, "Integral of CO2 = (%f%)")
     self.assertEqual(ci.nr, 1)
     self.assertEqual(ci.accumulation, "first")
Exemplo n.º 5
0
 def testPureRegex(self):
     ci = CustomPlotInfo(pureRegexp)
     self.assertEqual(ci.expr, pureRegexp)
     self.assertEqual(ci.nr, 1)
     self.assertEqual(ci.accumulation, "first")
Exemplo n.º 6
0
    def run(self):
        if not self.opts.server and not self.opts.pickle:
            error("No mode selected")
        if self.opts.server and self.opts.pickle:
            error("Both modes selected")

        doPandas = self.opts.pandasData or self.opts.pandasSeries

        if self.opts.server:
            if len(self.parser.getArgs()) != 2:
                error("Need a server and a port to be specified")

            host = self.parser.getArgs()[0]
            port = int(self.parser.getArgs()[1])

            try:
                self.server = ServerProxy("http://%s:%d" % (host, port))
                methods = self.server.system.listMethods()
            except socket.error:
                reason = sys.exc_info()[
                    1]  # Needed because python 2.5 does not support 'as e'
                self.error("Socket error while connecting:", reason)
            except ProtocolError:
                reason = sys.exc_info()[
                    1]  # Needed because python 2.5 does not support 'as e'
                self.error("XMLRPC-problem", reason)

            plotInfo = self.executeCommand("getPlots()")
            lineInfo = self.executeCommand("getPlotData()")
        else:
            if len(self.parser.getArgs()) != 1:
                warning("Only the first parameter is used")

            fName = self.parser.getArgs()[0]
            unpick = pickle.Unpickler(open(fName))

            lineInfo = unpick.load()
            plotInfo = unpick.load()

        if not self.quiet:
            print_("Found", len(plotInfo), "plots and", len(lineInfo),
                   "data sets")

        registry = TimeLinesRegistry()
        for nr, line in iteritems(lineInfo):
            if not self.quiet:
                print_("Adding line", nr)
            TimeLineCollection(preloadData=line, registry=registry)

        registry.resolveSlaves()

        if (self.opts.csvFiles or self.opts.excelFiles or doPandas
                or self.opts.numpyData) and self.opts.rawLines:
            rawData = {}
            rawSeries = {}
            rawNumpy = {}

            for k, l in iteritems(registry.lines):
                name = str(k)
                if type(k) == int:
                    name = "Line%d" % k
                csvName = self.opts.filePrefix + name + ".csv"
                if self.opts.csvFiles:
                    if not self.quiet:
                        print_("Writing", k, "to", csvName)
                    l.getData().writeCSV(csvName)
                if self.opts.excelFiles:
                    xlsName = self.opts.filePrefix + name + ".xls"
                    if not self.quiet:
                        print_("Writing", k, "to", xlsName)
                    l.getData().getData().to_excel(xlsName)
                if self.opts.pandasData:
                    rawData[k] = l.getData().getData()
                if self.opts.numpyData:
                    rawNumpy[k] = l.getData().data.copy()
                if self.opts.pandasSeries:
                    rawSeries[k] = l.getData().getSeries()

            if self.opts.numpyData:
                self.setData({"rawNumpy": rawNumpy})
            if self.opts.pandasData:
                self.setData({"rawData": rawData})
            if self.opts.pandasSeries:
                self.setData({"rawSeries": rawSeries})

            if self.opts.csvFiles or self.opts.excelFiles:
                return

        pRegistry = PlotLinesRegistry()

        plotNumpy = {}
        plotData = {}
        plotSeries = {}

        for i, p in iteritems(plotInfo):
            theId = p["id"]
            if not self.quiet:
                print_("Plotting", i, ":", theId, end=" ")
            spec = CustomPlotInfo(raw=p["spec"])
            if len(registry.get(p["data"]).getTimes()) > 0 and registry.get(
                    p["data"]).getValueNames() > 0:
                if self.opts.csvFiles or self.opts.excelFiles or doPandas or self.opts.numpyData:
                    dataSet = registry.get(p["data"]).getData()
                    if self.opts.csvFiles:
                        dataSet.writeCSV(self.opts.filePrefix + theId + ".csv")
                    if self.opts.excelFiles:
                        dataSet.getData().to_excel(self.opts.filePrefix +
                                                   theId + ".xls")
                    if self.opts.numpyData:
                        plotNumpy[theId] = dataSet.data.copy()
                    if self.opts.pandasData:
                        plotData[theId] = dataSet.getData()
                    if self.opts.pandasSeries:
                        plotSeries[theId] = dataSet.getSeries()
                else:
                    if self.opts.start or self.opts.end:
                        # rewrite CustomPlotInfo one of these days
                        if "start" in spec.getDict():
                            self.warning("Overriding plot start",
                                         spec["start"], "with",
                                         self.opts.start)
                        spec.set("start", self.opts.start)
                        if "end" in spec.getDict():
                            self.warning("Overriding plot end", spec["end"],
                                         "with", self.opts.end)
                        spec.set("end", self.opts.end)

                    mp = createPlotTimelines(
                        registry.get(p["data"]),
                        spec,
                        implementation=self.opts.implementation,
                        showWindow=self.opts.showWindow,
                        registry=pRegistry)
                    if self.opts.insertTitles:
                        mp.actualSetTitle(p["spec"]["theTitle"])
                    if self.opts.writePictures:
                        if mp.hasData():
                            mp.doHardcopy(self.opts.prefix + theId, "png")
                        else:
                            if not self.quiet:
                                print_("has no data", end=" ")
                if not self.quiet:
                    print_()
            else:
                if not self.quiet:
                    print_("No data - skipping")

            if not (self.opts.csvFiles or doPandas):
                sleep(self.opts.sleepTime
                      )  # there seems to be a timing issue with Gnuplot

        if self.opts.numpyData:
            self.setData({"plotNumpy": plotNumpy})
        if self.opts.pandasData:
            self.setData({"plotData": plotData})
        if self.opts.pandasSeries:
            self.setData({"plotSeries": plotSeries})
    def run(self):
        if not self.opts.server and not self.opts.pickle:
            error("No mode selected")
        if self.opts.server and self.opts.pickle:
            error("Both modes selected")

        doPandas=self.opts.pandasData or self.opts.pandasSeries

        if self.opts.server:
            if len(self.parser.getArgs())!=2:
                error("Need a server and a port to be specified")

            host=self.parser.getArgs()[0]
            port=int(self.parser.getArgs()[1])

            try:
                self.server=ServerProxy("http://%s:%d" % (host,port))
                methods=self.server.system.listMethods()
            except socket.error:
                reason = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                self.error("Socket error while connecting:",reason)
            except ProtocolError:
                reason = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e'
                self.error("XMLRPC-problem",reason)

            plotInfo=self.executeCommand("getPlots()")
            lineInfo=self.executeCommand("getPlotData()")
        else:
            if len(self.parser.getArgs())!=1:
                warning("Only the first parameter is used")

            fName=self.parser.getArgs()[0]
            unpick=pickle.Unpickler(open(fName,"rb"))

            lineInfo=unpick.load()
            plotInfo=unpick.load()

        if not self.quiet:
            print_("Found",len(plotInfo),"plots and",len(lineInfo),"data sets")

        registry=TimeLinesRegistry()
        for nr,line in iteritems(lineInfo):
            if not self.quiet:
                print_("Adding line",nr)
            TimeLineCollection(preloadData=line,registry=registry)

        registry.resolveSlaves()

        if (self.opts.csvFiles or self.opts.excelFiles or doPandas or self.opts.numpyData) and self.opts.rawLines:
            rawData={}
            rawSeries={}
            rawNumpy={}

            for k,l in iteritems(registry.lines):
                name=str(k)
                if type(k)==int:
                    name="Line%d" % k
                csvName=self.opts.filePrefix+name+".csv"
                if self.opts.csvFiles:
                    if not self.quiet:
                        print_("Writing",k,"to",csvName)
                    l.getData().writeCSV(csvName)
                if self.opts.excelFiles:
                    xlsName=self.opts.filePrefix+name+".xls"
                    if not self.quiet:
                        print_("Writing",k,"to",xlsName)
                    l.getData().getData().to_excel(xlsName)
                if self.opts.pandasData:
                    rawData[k]=l.getData().getData()
                if self.opts.numpyData:
                    rawNumpy[k]=l.getData().data.copy()
                if self.opts.pandasSeries:
                    rawSeries[k]=l.getData().getSeries()

            if self.opts.numpyData:
                self.setData({"rawNumpy":rawNumpy})
            if self.opts.pandasData:
                self.setData({"rawData":rawData})
            if self.opts.pandasSeries:
                self.setData({"rawSeries":rawSeries})

            if self.opts.csvFiles or self.opts.excelFiles:
                return

        pRegistry=PlotLinesRegistry()

        plotNumpy={}
        plotData={}
        plotSeries={}

        for i,p in iteritems(plotInfo):
            theId=p["id"]
            if not self.quiet:
                print_("Plotting",i,":",theId,end=" ")
            spec=CustomPlotInfo(raw=p["spec"])
            if len(registry.get(p["data"]).getTimes())>0 and registry.get(p["data"]).getValueNames()>0:
                if self.opts.csvFiles or self.opts.excelFiles or doPandas or self.opts.numpyData:
                    dataSet=registry.get(p["data"]).getData()
                    if self.opts.csvFiles:
                        dataSet.writeCSV(self.opts.filePrefix+theId+".csv")
                    if self.opts.excelFiles:
                        dataSet.getData().to_excel(self.opts.filePrefix+theId+".xls")
                    if self.opts.numpyData:
                        plotNumpy[theId]=dataSet.data.copy()
                    if self.opts.pandasData:
                        plotData[theId]=dataSet.getData()
                    if self.opts.pandasSeries:
                        plotSeries[theId]=dataSet.getSeries()
                else:
                    if self.opts.start or self.opts.end:
                        # rewrite CustomPlotInfo one of these days
                        if "start" in spec.getDict():
                            self.warning("Overriding plot start",spec["start"],
                                         "with",self.opts.start)
                        spec.set("start",self.opts.start)
                        if "end" in spec.getDict():
                            self.warning("Overriding plot end",spec["end"],
                                         "with",self.opts.end)
                        spec.set("end",self.opts.end)

                    mp=createPlotTimelines(registry.get(p["data"]),
                                           spec,
                                           implementation=self.opts.implementation,
                                           showWindow=self.opts.showWindow,
                                           registry=pRegistry)
                    if self.opts.insertTitles:
                        mp.actualSetTitle(p["spec"]["theTitle"])
                    if self.opts.writePictures:
                        if mp.hasData():
                            mp.doHardcopy(self.opts.prefix+theId,"png")
                        else:
                            if not self.quiet:
                                print_("has no data",end=" ")
                if not self.quiet:
                    print_()
            else:
                if not self.quiet:
                    print_("No data - skipping")

            if not(self.opts.csvFiles or doPandas):
                sleep(self.opts.sleepTime) # there seems to be a timing issue with Gnuplot

        if self.opts.numpyData:
            self.setData({"plotNumpy":plotNumpy})
        if self.opts.pandasData:
            self.setData({"plotData":plotData})
        if self.opts.pandasSeries:
            self.setData({"plotSeries":plotSeries})