def process(self): """Do the real work""" ctr = 0 olds = None pcontainer = list() self.plot = NaivePlot(xmin=-0.1, ymin=-0.1) for line in self.handle: ctr += 1 if ctr == 1 and self.args.ignore_first: continue values = [float(val.strip()) for val in \ line.strip().split(self.args.sep) if val] x = float(ctr) if self.args.xy: x = float(values[0]) points = [Point(x, val) for val in values if x and val] pcontainer.append(points) if olds: for i in xrange(len(points)): if not self.args.cols or i not in self.args.cols: continue if not olds[i] or not points[i]: continue l = Line(olds[i], points[i]) (_, lchar, lcol) = self.get_format(i) self.plot.add_curve(Curve(l, 0.0, 1.0, self.args.gap), lchar, lcol) olds = points (xmin, xmax, ymin, ymax) = (0, 0, 0, 0) for points in pcontainer: for i in xrange(len(points)): if not self.args.cols or i not in self.args.cols: continue (pchar, _, pcol) = self.get_format(i) self.plot.add_curve(points[i], pchar, pcol) xmin = min(xmin, points[i].x) xmax = max(xmax, points[i].x) ymin = min(ymin, points[i].y) ymax = max(ymax, points[i].y) self.plot.zoom(xmin, xmax, ymin, ymax) return
def overlap(self): """Try to figure out if there is an overlap """ self.overlappers = list() for fbox in self.fboxes: for gbox in self.gboxes: # TODO: remove illustration tmp = NaivePlot(60, 20, -1, 7, -1, 11) cf = Curve(self.f, 0.0, 1.0, 0.001) cg = Curve(self.g, 0.0, 1.0, 0.001) cfb = Curve(fbox, 0.0, 1.0, 0.001) cgb = Curve(gbox, 0.0, 1.0, 0.001) tmp.add_curve(cf, "/", "white") tmp.add_curve(cg, "\\", "white") tmp.add_curve(cfb, "f", "red") tmp.add_curve(cgb, "g", "blue") tmp.fit_curve(cf) tmp.fit_curve(cg) print tmp if fbox.overlap(gbox): self.overlappers.append((fbox, gbox)) return len(self.overlappers) > 0
self.overlappers = new_overlappers return if __name__ == "__main__": f = Line(Point(0, 0), Point(6, 10)) g = Line(Point(1, 6), Point(3, 2)) p = ParaSolver(f, g) print "Overlap? %s" % p.overlap() olap = p.seed(0.0, 1.0, 0.2, 0.0, 1.0, 0.2) print "Overlap? %s" % olap plot = NaivePlot(60, 20, -1, 7, -1, 11) plot.add_curve(Curve(f, 0.0, 1.0, 0.001), "/", "white") plot.add_curve(Curve(g, 0.0, 1.0, 0.001), "\\", "white") for _ in xrange(1): for (rbox, bbox) in p.overlappers: tmp_plot = NaivePlot(60, 20, -1, 7, -1, 11) tmp_plot.add_curve(Curve(f, 0.0, 1.0, 0.001), "/", "white") tmp_plot.add_curve(Curve(g, 0.0, 1.0, 0.001), "\\", "white") tmp_plot.add_curve(Curve(rbox, 0.0, 1.0, 0.001), "f", "red") tmp_plot.add_curve(Curve(bbox, 0.0, 1.0, 0.001), "g", "blue") print tmp_plot p.iterate()
class NaiveParserPlotter: """Class for reading and plotting""" def __init__(self): """Setup place holders""" self.args = None self.points = None self.lines = None self.colors = None self.plot = None return def setup(self): """Do all setup after parsing args""" self.get_handle() self.setup_formats() return def get_handle(self): """Get a handle to read from""" if self.args.std_in: self.handle = stdin elif self.args.in_file: self.handle = open(self.args.in_file, 'r') else: pass # TODO: exception? return def setup_formats(self): """Return format vectors""" self.points = list(ascii_uppercase) self.lines = ['.', '-', ':', '~', "'"] self.colors = ['blue', 'red', 'green', 'yellow', 'magenta', 'cyan', 'grey'] #'white' return def get_format(self, idx): """get approproate combo""" attrs = list() for container in [self.points, self.lines, self.colors]: attrs.append(container[idx%len(container)]) return tuple(attrs) def parse_args(self, args=None): """Parse the arguments""" parser = ArgumentParser(description="Plot the numbers given in a file " "or in stdin") rgroup = parser.add_argument_group("Read from...") rgroup.add_argument('--std-in', action="store_true", default=False, help="Perform doc tests and exit instead.") rgroup.add_argument('--in-file', '-f', type=str, default=None, help="Specify input file path.") dgroup = parser.add_argument_group("Input data...") dgroup.add_argument('--xy', '-x', action="store_true", default=False, help="Treat first column as x values, and the " "following as y-values (default False).") dgroup.add_argument('--col', '-c', action="append", dest='cols', type=int, default=list(), help="Specify which columns to investigate. " "Repeat if needed. Default: All") dgroup.add_argument('--ignore-first', '-i', action="store_true", default=False, help="ignore first line") dgroup.add_argument('--sep', '-s', default=' ', help="Specify separator, default: space") fgroup = parser.add_argument_group("Formatting...") fgroup.add_argument('--gap', '-g', type=float, default=0.01, help="inverted number of subpoints in lines") fgroup.add_argument('--not-implemented') if args: self.args = parser.parse_args(args) else: self.args = parser.parse_args() return def process(self): """Do the real work""" ctr = 0 olds = None pcontainer = list() self.plot = NaivePlot(xmin=-0.1, ymin=-0.1) for line in self.handle: ctr += 1 if ctr == 1 and self.args.ignore_first: continue values = [float(val.strip()) for val in \ line.strip().split(self.args.sep) if val] x = float(ctr) if self.args.xy: x = float(values[0]) points = [Point(x, val) for val in values if x and val] pcontainer.append(points) if olds: for i in xrange(len(points)): if not self.args.cols or i not in self.args.cols: continue if not olds[i] or not points[i]: continue l = Line(olds[i], points[i]) (_, lchar, lcol) = self.get_format(i) self.plot.add_curve(Curve(l, 0.0, 1.0, self.args.gap), lchar, lcol) olds = points (xmin, xmax, ymin, ymax) = (0, 0, 0, 0) for points in pcontainer: for i in xrange(len(points)): if not self.args.cols or i not in self.args.cols: continue (pchar, _, pcol) = self.get_format(i) self.plot.add_curve(points[i], pchar, pcol) xmin = min(xmin, points[i].x) xmax = max(xmax, points[i].x) ymin = min(ymin, points[i].y) ymax = max(ymax, points[i].y) self.plot.zoom(xmin, xmax, ymin, ymax) return def __str__(self): """just print""" return str(self.plot)
return 13*cos(t) - 5*cos(2*t) - 2*cos(3*t) - cos(4*t) def curvex(s): """x part of a curve""" return s def curvey(s): """y part of a curve""" return 0.7*s - 7*cos(0.3*s) ** 2 if __name__ == '__main__': print "Demo Plot 1" plot = NaivePlot(xmin=-3.5, xmax=3.3, ymin=-3.3, ymax=2.7) c1 = Curve(Func(lambda x: 1-x/2), -pi, pi, 0.02) c2 = Curve(ParaFunc(lambda t: sin(2*t)-2, lambda t: 2*cos(t)), 0, 2*pi, 0.02) c3 = Curve(ParaFunc(lambda t: 2+sin(t), lambda t: cos(t)-2), 0, 2*pi, 0.02) for (curve, cross) in [(c1, '~'), (c2, '8'), (c3, 'o')]: plot.add_curve(curve, cross) plot.fit_curve(curve) for p in [1, -1, 2, -2]: plot.add_curve(Point(p, -p), 'X') plot.add_curve(Point(p, p), 'X')