def gfit(self, func, eqn, params, list1, list2=[], list3=[], ** options): """ all syntax should be valid GNUPLOT syntax func - a string of the function call i.e. "f(x)" eqn - a string of a GNUPLOT equation "a*x**b" params - a dictionary of parameters in eqn and their initial values ex: {"a": 1, "b": 3} """ from rasmus import util self.set(** options) print len(list1), len(list2), len(list3) if not self.enable: raise Exception("must be output must be enabled for fitting") list1, list2, list3 = self.prepareData(list1, list2, list3) # add data to graph self.data.append(self.Plot(list1, list2, list3, copy.copy(self.options))) # perform fitting self.stream = os.popen("gnuplot", "w") print >>self.stream, "%s = %s" % (func, eqn) for param, value in params.items(): print >>self.stream, "%s = %f" % (param, value) print >>self.stream, "fit %s '-' via %s" % \ (func, ",".join(params.keys())) self.outputData(list1, list2, list3) # save and read parameters outfile = util.tempfile(".", "plot", ".txt") print >>self.stream, "save var '%s'" % outfile print >>self.stream, "print 'done'" self.stream.flush() # wait for variable file while not os.path.isfile(outfile): pass params = self.readParams(outfile) os.remove(outfile) # build eqn for plotting paramlist = "" for param, value in params.items(): paramlist += "%s = %s, " % (param, value) self.options["eqn"] = paramlist + "%s = %s, %s" % \ (func, eqn, func) self.options["style"] = "lines" # add fitted eqn to graph self.data.append(self.Plot([], [], [], copy.copy(self.options))) self.replot()
def gfit(self, func, eqn, params, list1, list2=[], list3=[], **options): """ all syntax should be valid GNUPLOT syntax func - a string of the function call i.e. "f(x)" eqn - a string of a GNUPLOT equation "a*x**b" params - a dictionary of parameters in eqn and their initial values ex: {"a": 1, "b": 3} """ from rasmus import util self.set(**options) print len(list1), len(list2), len(list3) if not self.enable: raise Exception("must be output must be enabled for fitting") list1, list2, list3 = self.prepareData(list1, list2, list3) # add data to graph self.data.append( self.Plot(list1, list2, list3, copy.copy(self.options))) # perform fitting self.stream = os.popen("gnuplot", "w") print >> self.stream, "%s = %s" % (func, eqn) for param, value in params.items(): print >> self.stream, "%s = %f" % (param, value) print >>self.stream, "fit %s '-' via %s" % \ (func, ",".join(params.keys())) self.outputData(list1, list2, list3) # save and read parameters outfile = util.tempfile(".", "plot", ".txt") print >> self.stream, "save var '%s'" % outfile print >> self.stream, "print 'done'" self.stream.flush() # wait for variable file while not os.path.isfile(outfile): pass params = self.readParams(outfile) os.remove(outfile) # build eqn for plotting paramlist = "" for param, value in params.items(): paramlist += "%s = %s, " % (param, value) self.options["eqn"] = paramlist + "%s = %s, %s" % \ (func, eqn, func) self.options["style"] = "lines" # add fitted eqn to graph self.data.append(self.Plot([], [], [], copy.copy(self.options))) self.replot()
def wait(self): """Wait until all commands are known to be excuted""" from rasmus import util tmpfile = util.tempfile(".", "gnuplot", ".ps") print >>self.stream, "set output '%s'" % tmpfile print >>self.stream, "set terminal postscript color" print >>self.stream, "plot '-'\n0 0\ne\n" self.stream.flush() while not os.path.isfile(tmpfile): pass os.remove(tmpfile)
def wait(self): """Wait until all commands are known to be excuted""" from rasmus import util tmpfile = util.tempfile(".", "gnuplot", ".ps") print >> self.stream, "set output '%s'" % tmpfile print >> self.stream, "set terminal postscript color" print >> self.stream, "plot '-'\n0 0\ne\n" self.stream.flush() while not os.path.isfile(tmpfile): pass os.remove(tmpfile)
def setTerminal(self, filename = "", format="x11"): if not self.enable: return from rasmus import util # auto detect format from filename if filename != "": print >>self.stream, "set output \"%s\"" % filename # determine format if filename.endswith(".ps"): format = "ps" if filename.endswith(".pdf"): format = "pdf" if filename.endswith(".gif"): format = "gif" if filename.endswith(".png"): format = "png" if filename.endswith(".jpg"): format = "jpg" else: tmpfile = util.tempfile(".", "gnuplot", ".ps") print >>self.stream, "set output \"%s\"" % tmpfile return tmpfile # set terminal format if format == "ps": print >>self.stream, "set terminal postscript color" elif format == "pdf": print >>self.stream, "set terminal pdf" elif format == "gif": print >>self.stream, "set terminal gif" elif format == "jpg": print >>self.stream, "set terminal jpeg" else: print >>self.stream, "set terminal %s" % format
def setTerminal(self, filename="", format="x11"): if not self.enable: return from rasmus import util # auto detect format from filename if filename != "": print >> self.stream, "set output \"%s\"" % filename # determine format if filename.endswith(".ps"): format = "ps" if filename.endswith(".pdf"): format = "pdf" if filename.endswith(".gif"): format = "gif" if filename.endswith(".png"): format = "png" if filename.endswith(".jpg"): format = "jpg" else: tmpfile = util.tempfile(".", "gnuplot", ".ps") print >> self.stream, "set output \"%s\"" % tmpfile return tmpfile # set terminal format if format == "ps": print >> self.stream, "set terminal postscript color" elif format == "pdf": print >> self.stream, "set terminal pdf" elif format == "gif": print >> self.stream, "set terminal gif" elif format == "jpg": print >> self.stream, "set terminal jpeg" else: print >> self.stream, "set terminal %s" % format