save figures from matplotlib
authorTomas Musil <tomik.musil@gmail.com>
Sat, 28 Jun 2014 19:06:40 +0000 (21:06 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Sat, 28 Jun 2014 19:06:40 +0000 (21:06 +0200)
imago_pack/intrsc.py
imago_pack/linef.py
test/html_results.py [new file with mode: 0644]
test/test.py

index 9fabcd5..1d4dd67 100644 (file)
@@ -55,6 +55,8 @@ def board(image, lines, show_all, do_something):
 
     if show_all:
         import matplotlib.pyplot as pyplot
+        import Image
+        fig = pyplot.figure(figsize=(8, 6))
         pyplot.scatter(luma, saturation, 
                        color=[(s[2][0]/255.,
                                s[2][1]/255.,
@@ -62,13 +64,18 @@ def board(image, lines, show_all, do_something):
                                    for s in board_raw])
         pyplot.xlim(0,1)
         pyplot.ylim(0,1)
-        pyplot.show()
+        fig.canvas.draw()
+        size = fig.canvas.get_width_height()
+        buff = fig.canvas.tostring_rgb()
+        image_p = Image.fromstring('RGB', size, buff, 'raw')
+        do_something(image_p, "color distribution")
 
     clusters = k_means.cluster(3, 2,zip(zip(luma, saturation), range(len(luma))),
                                [[0., 0.5], [0.5, 0.5], [1., 0.5]])
    #clusters.sort(key=mean_luma)
 
     if show_all:
+        fig = pyplot.figure(figsize=(8, 6))
         pyplot.scatter([d[0][0] for d in clusters[0]], [d[0][1] for d in clusters[0]],
                                                  color=(1,0,0,1))
         pyplot.scatter([d[0][0] for d in clusters[1]], [d[0][1] for d in clusters[1]],
@@ -77,7 +84,11 @@ def board(image, lines, show_all, do_something):
                                                  color=(0,0,1,1))
         pyplot.xlim(0,1)
         pyplot.ylim(0,1)
-        pyplot.show()
+        fig.canvas.draw()
+        size = fig.canvas.get_width_height()
+        buff = fig.canvas.tostring_rgb()
+        image_p = Image.fromstring('RGB', size, buff, 'raw')
+        do_something(image_p, "color clustering")
 
     clusters[0] = [(p[1], 'B') for p in clusters[0]]
     clusters[1] = [(p[1], '.') for p in clusters[1]]
index 2e40fca..ab7c95d 100644 (file)
@@ -66,11 +66,8 @@ def run_ransac(image):
     line2 = [line_to_points(line2, 0), line_to_points(line2, width - 1)]
     return [sorted(points), sorted(points2)], line, line2
 
-
-
 def find_lines(image, show_image, logger):
     """Find lines in the *image*."""
-    # TODO refactor into smaller functions
     
     logger("preprocessing")
     show_image(image, "original image")
@@ -100,20 +97,8 @@ def find_lines(image, show_image, logger):
         draw.line(line_from_angl_dist(line, image.size), fill=(120, 255, 120))
     show_image(image_g, "lines")
 
-    return lines, l1, l2, bounds, hough # TODO
-
-def combine(image1, image2):
-    """Return a list of points that are present in both images."""
-    im_l1 = image1.load()
-    im_l2 = image2.load()
-
-    on_both = []
 
-    for x in xrange(image1.size[0]):
-        for y in xrange(image1.size[1]):
-            if im_l1[x, y] and im_l2[x, y]:
-                on_both.append((x, y))
-    return on_both
+    return lines, l1, l2, bounds, hough # TODO
 
 def line_from_angl_dist((angle, distance), size):
     """Take *angle* and *distance* (from the center of the image) of a line and
diff --git a/test/html_results.py b/test/html_results.py
new file mode 100644 (file)
index 0000000..a89c8af
--- /dev/null
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+
+import sys
+import pickle
+
+print "<html>"
+print "<head>"
+print "<style>"
+print "table {border-collapse: collapse; table-layout: fixed;}"
+print "td {border-style: solid; border-width: 1px; border-color: grey;}"
+print "</style>"
+print "</head>"
+
+print "<body>"
+
+print "<h1>Imago test</h1>"
+
+print "<pre>"
+
+res = pickle.load(open(sys.argv[1], 'r'))
+
+total = len(res)
+
+failed = [(name,error) for (name, time, output, error, r_code) in res
+          if r_code > 0]
+print "failed tests:", str(len(failed)) + '/' + str(total)
+
+for (name, error) in failed:
+    print "    ", name
+
+print ""
+
+n = 0
+t = 0
+correct = 0
+
+for (name, time, output, error, r_code) in res:
+    if r_code > 0:
+        continue
+    n += 1
+    t += time
+    ref = open(name.split('.')[0] + '.txt').read()
+    if output == ref:
+        correct += 1
+    else:
+        #TODO find and record the error
+        pass
+
+print "average runtime:", (t/n)
+print "correct:", str(correct) + '/' + str(n)
+
+print "</pre>"
+
+for (name, time, output, error, r_code) in res:
+    print "<div>"
+    print "<h2>" + name + "</h2>"
+    print "<image src=\"" + name + "\" width=\"600\" style=\"float: right;\" />"
+    print "<table>"
+    output = output.split('\n')
+    correct = open(name[:-4]+".txt").readlines()
+    output = map(lambda s: s.split(), output)
+    correct = map(lambda s: s.split(), correct)
+    for i in range(len(output)):
+        print "<tr>"
+        for j in range(len(output[i])):
+            if output[i][j] == correct[i][j]:
+                print "<td><div style=\"width: 15px\" >{}</td>".format(output[i][j])
+            else:
+                print "<td><div style=\"background-color: red; \
+                      width: 15px\">{}</td>".format(output[i][j])
+        print "</tr>"
+    print "</table>"
+    print "<div style=\"clear: both;\"></div>"
+    print "</div>"
+
+print "</body>"
+print "</html>"
index da0a1f4..e6b5b6d 100755 (executable)
@@ -22,11 +22,18 @@ res_name = "res" + datetime.datetime.now().strftime("%Y%m%d%H%M") + "-" + git_ha
 test_results = []
 n = 1
 
+args = ["-w", "640"]
+if len(sys.argv) > 1 and sys.argv[1] == "debug":
+    args = ["-vds"] + args
+
+args = ["../imago"] + args
+
 for fl in test_files:
     print "testing file", fl, str(n) + "/" + str(len(test_files))
+    print " ".join(args + [fl]) 
     n += 1
     t0 = time.time()
-    proc = subprocess.Popen(["../imago", "-w", "640", fl],
+    proc = subprocess.Popen(args + [fl],
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
     [output, err] = proc.communicate()