plot_bench.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import matplotlib.pyplot as plt
00004 import numpy as np
00005 import csv
00006 import sys
00007 import os
00008 from itertools import chain
00009 from math import pi
00010 import argparse
00011 
00012 parser = argparse.ArgumentParser(description="Plot benchmark result csv")
00013 parser.add_argument("csv_file", help="csv file")
00014 parser.add_argument("--image-suffix", default="eps", help="suffix to save image")
00015 parser.add_argument("--only-save-image", action="store_true", help="die right after saving image")
00016 parser.add_argument("--min", default=0, type=float, help="minimum value")
00017 parser.add_argument("--max", default=0.2, type=float, help="maximum value")
00018 args = parser.parse_args()
00019 csv_file = args.csv_file
00020 methods = ['none']
00021 dx = 3
00022 dy = 3
00023 
00024 def plot(theta, one_data, ax):
00025     global im
00026     xs = [data[0] for data in one_data]
00027     ys = [data[1] for data in one_data]
00028     zs = {}
00029     for x, y, i in zip(xs, ys, range(len(xs))):
00030         zs[(x, y)] = one_data[i][2]
00031     xs.sort()
00032     ys.sort()
00033 
00034     minz = args.min
00035     maxz = args.max
00036 
00037     img = np.arange(0, len(xs) * len(ys), 1.0).reshape((len(xs), len(ys)))
00038     for x, i in zip(xs, range(len(xs))):
00039         for y, j in zip(ys, range(len(ys))):
00040             try:
00041                 z = zs[(x, y)]
00042             except:
00043                 print >> sys.stderr, "Failed to find (%f, %f)" % (x, y)
00044             img[j][i] = z
00045     #im = ax.pcolor(img, vmin=minz, vmax=maxz, cmap="gnuplot")
00046     im = ax.imshow(img, vmin=minz, vmax=maxz, cmap="gnuplot")
00047     ax.set_xticklabels(np.arange(-dx - 1, dx+1))
00048     ax.set_yticklabels(np.arange(-dy - 1, dy+1))
00049     ax.set_xlabel("$x$ [m]")
00050     ax.set_ylabel("$y$ [m]")
00051     ax.set_title('$\\theta = %.0f$ deg' % (theta / pi * 180.0))
00052 
00053 
00054 reader = csv.reader(open(csv_file))
00055 fields = reader.next()
00056 
00057 initialized = False
00058 
00059 data = {}
00060 # theta -> x, y, time
00061 
00062 for row in reader:
00063     # Initialization
00064     if not initialized:
00065         figure_num = int(row[fields.index("n_theta")])
00066         if figure_num % 3 == 0:
00067             xnum = figure_num / 3
00068         else:
00069             xnum = figure_num / 3 + 1
00070         fig, axes = plt.subplots(xnum, 3)
00071         initialized = True
00072     theta_str = row[fields.index("theta")]
00073     theta = float(theta_str)
00074     if not data.has_key(theta_str):
00075         data[theta_str] = []
00076     data[theta_str].append((float(row[fields.index("x")]), float(row[fields.index("y")]), float(row[fields.index("one_time")])))
00077 
00078 counter = 0
00079 #for theta, one_data in data.items():
00080 for theta in sorted(data.keys()):
00081     one_data = data[theta]
00082     print "Plotting theta=", theta
00083     plot(float(theta), one_data, axes.flat[counter])
00084     counter = counter + 1
00085     
00086 for a in axes.flat[counter:]:
00087     fig.delaxes(a)
00088 plt.tight_layout()
00089 fig.subplots_adjust(right = 0.8)
00090 cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
00091 cb = fig.colorbar(im, cax=cbar_ax)
00092 cb.set_label("Time [sec]")
00093 plt.interactive(True)
00094 plt.show()
00095 # save figure
00096 eps_file = os.path.basename(csv_file) + "." + args.image_suffix
00097 print "Saving to %s file: " % (args.image_suffix), eps_file
00098 plt.savefig(eps_file)
00099 if args.only_save_image:
00100     sys.exit(0)
00101 while True:
00102     plt.pause(1)


jsk_footstep_planner
Author(s): Ryohei Ueda
autogenerated on Fri Apr 19 2019 03:45:28