plot_csv.py
Go to the documentation of this file.
00001 import csv
00002 import numpy as np
00003 import pylab as pb
00004 import matplotlib_util.util as mpu
00005 
00006 def load_data():
00007     mk = csv.reader(open('Mechanism Kinematics.csv', 'U'))
00008     data = []
00009     for r in mk:
00010         data.append(r)
00011 
00012     return data
00013 
00014 ##
00015 # Deal with repetition
00016 def expand(data, keys):
00017     repetition_idx = np.where(np.array(keys) == 'repetition')[0]
00018     data[repetition_idx] = [int(e) for e in data[repetition_idx]]
00019     repeated_data = []
00020 
00021     for i in range(len(data)):
00022         repeated_data.append([])
00023 
00024     for current_instance in range(len(data[0])):
00025         if data[repetition_idx][current_instance] > 1:
00026             for rep in range(1, data[repetition_idx][current_instance]):
00027                 for l, rl in zip(data, repeated_data):
00028                     rl.append(l[current_instance])
00029     for l, rl in zip(data, repeated_data):
00030         l += rl
00031 
00032     data.pop(repetition_idx)
00033     keys.pop(repetition_idx)
00034     return data
00035 
00036 def test_expand():
00037     data = [['a', 'b', 'c'], ['A', 'B', 'C'], [1, 4, 10]]
00038     keys = ['letters', 'LETTERS', 'repetition']
00039     new_data = expand(data, keys)
00040     print new_data
00041 
00042 def extract_keys(csv_data, keys):
00043     format = csv_data[1]
00044     indices = []
00045     llists = []
00046     for k in keys:
00047         indices.append(np.where(np.array(format) == k)[0])
00048         llists.append([])
00049 
00050     for i in range(2, len(csv_data)):
00051         if len(csv_data[i]) == 0:
00052             break
00053         if len(csv_data[i][indices[0]]) > 0:
00054             for lidx, data_idx in enumerate(indices):
00055                 llists[lidx].append(csv_data[i][data_idx])
00056     return llists
00057 
00058 def plot_radii(csv_data, color='#3366FF'):
00059     keys = ['radius', 'type', 'name', 'repetition']
00060     llists = expand(extract_keys(csv_data, keys), keys)
00061     rad_list = np.array([float(r) for r in llists[0]]) / 100.0
00062     types = llists[1]
00063     names = np.array(llists[2])
00064     all_types = set(types)
00065     print 'Radii types', all_types
00066 
00067     types_arr = np.array(types)
00068     # np.where(types_arr == 'C')
00069     cabinet_rad_list = rad_list[np.where(types_arr == 'C')[0]]
00070     others_rad_list = rad_list[np.where(types_arr != 'C')[0]]
00071     other_names = names[np.where(types_arr != 'C')[0]]
00072     print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
00073     print 'radii other names'
00074     for i, n in enumerate(other_names):
00075         print n, others_rad_list[i]
00076     print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>'
00077 
00078     rad_lists = [rad_list, cabinet_rad_list, others_rad_list]
00079     titles = ['Radii of Rotary Mechanisms', 'Radii of Cabinets', 'Radii of Other Mechanisms']
00080     bin_width = 0.05
00081     max_radius = np.max(rad_list)
00082     print 'MIN RADIUS', np.min(rad_list)
00083     print 'MAX RADIUS', max_radius
00084 
00085     mpu.set_figure_size(5.,5.)
00086     for idx, radii in enumerate(rad_lists):
00087         f = pb.figure()
00088         f.set_facecolor('w')
00089         bins = np.arange(0.-bin_width/2., max_radius+2*bin_width, bin_width)
00090         hist, bin_edges = np.histogram(radii, bins)
00091         h = mpu.plot_histogram(bin_edges[:-1]+bin_width/2., hist,
00092                            width=0.8*bin_width, xlabel='Radius (meters)',
00093                            ylabel='\# of mechanisms',
00094                            plot_title=titles[idx],
00095                            color=color, label='All')
00096         pb.xlim(.1, 1.)
00097         pb.ylim(0, 55)
00098         mpu.legend(display_mode = 'less_space', handlelength=1.)
00099 
00100     #-- different classes in different colors in the same histogram.
00101     f = pb.figure()
00102     f.set_facecolor('w')
00103     bins = np.arange(0.-bin_width/2., max_radius+2*bin_width, bin_width)
00104     hist, bin_edges = np.histogram(rad_lists[0], bins)
00105     h = mpu.plot_histogram(bin_edges[:-1]+bin_width/2., hist,
00106                        width=0.8*bin_width, xlabel='Radius (meters)',
00107                        ylabel='\# of mechanisms',
00108                        plot_title=titles[1],
00109                        color='g', label='Cabinets')
00110     hist, bin_edges = np.histogram(rad_lists[2], bins)
00111     h = mpu.plot_histogram(bin_edges[:-1]+bin_width/2., hist,
00112                        width=0.8*bin_width, xlabel='Radius (meters)',
00113                        ylabel='\# of mechanisms',
00114                        plot_title='Cabinets and Other Mechanisms',
00115                        color='y', label='Other')
00116     pb.xlim(.1, 1.)
00117     pb.ylim(0, 55)
00118     mpu.legend(display_mode = 'less_space', handlelength=1.)
00119 
00120     color_list = ['g', 'b', 'r']
00121     marker_list = ['s', '^', 'v']
00122     label_list = ['All', 'Cabinets', 'Other']
00123     scatter_size_list = [8, 5, 5]
00124     mpu.set_figure_size(5.,5.)
00125     mpu.figure()
00126     for idx, radii in enumerate(rad_lists):
00127         bins = np.arange(0.-bin_width/2., max_radius+2*bin_width, bin_width)
00128         hist, bin_edges = np.histogram(radii, bins)
00129         bin_midpoints = np.arange(0., max_radius+bin_width, bin_width)
00130         mpu.plot_yx(hist, bin_midpoints, color = color_list[idx],
00131                     alpha = 0.6, marker = marker_list[idx],
00132                     scatter_size = scatter_size_list[idx], xlabel='Radius (meters)',
00133                     ylabel='\# of mechanisms', label = label_list[idx])
00134     mpu.legend(display_mode = 'less_space')
00135 
00136 def plot_opening(csv_data):
00137     keys = ['distance', 'type', 'repetition']
00138     llists = expand(extract_keys(csv_data, keys), keys)
00139     dists = np.array([float(f) for f in llists[0]])/100.
00140     types = llists[1]
00141 
00142     types_arr = np.array(types)
00143     drawer_dists = dists[np.where(types_arr == 'R')[0]]
00144     other_dists = dists[np.where(types_arr != 'R')[0]]
00145 
00146     print 'Opening distances types', set(types)
00147     bin_width = 0.02
00148     bins = np.arange(-bin_width/2., np.max(dists)+2*bin_width, bin_width)
00149     dists_list = [dists]#, drawer_dists, other_dists]
00150     titles = ['Opening Distances of Drawers']#, 'drawer', 'other']
00151 
00152 #    print 'Total number of drawers:', len(dists)
00153     mpu.set_figure_size(5.,4.)
00154     for idx, d in enumerate(dists_list):
00155         f = pb.figure()
00156         f.set_facecolor('w')
00157         hist, bin_edges = np.histogram(d, bins)
00158         #import pdb; pdb.set_trace()
00159         mpu.plot_histogram(bin_edges[:-1]+bin_width/2., hist,
00160                            width=0.8*bin_width, xlabel='Opening Distance (meters)',
00161                            ylabel='\# of Mechanisms',
00162                            plot_title=titles[idx], color='#3366FF')
00163 
00164 #        pb.xticks(bins[np.where(bins > np.min(dists))[0][0]-2:-1])
00165 #        pb.yticks(range(0, 26, 5))
00166 #        pb.ylim(0, 25)
00167 
00168 def handle_height_histogram(mean_height_list, plot_title='', color='#3366FF', max_height=2.5, bin_width=.1, ymax=35):
00169     bins = np.arange(0.-bin_width/2., max_height+2*bin_width, bin_width)
00170     hist, bin_edges = np.histogram(np.array(mean_height_list), bins)
00171     f = pb.figure()
00172     f.set_facecolor('w')
00173     f.subplots_adjust(bottom=.16, top=.86)
00174     mpu.plot_histogram(bin_edges[:-1]+bin_width/2., hist,
00175                        width=bin_width*0.8, plot_title=plot_title,
00176                        xlabel='Height (meters)', ylabel='\# of mechanisms', color=color)
00177     pb.xlim(0, max_height)
00178     pb.ylim(0, ymax)
00179 
00180 def handle_height_histogram_advait(mean_height_list, plot_title='',
00181                                    color='#3366FF', max_height=2.2, bin_width=.1, ymax=35,
00182                                    new_figure = True, label = '__no_legend__'):
00183     if new_figure:
00184         mpu.set_figure_size(5.,4.)
00185 #        f = mpu.figure()
00186 #        f.subplots_adjust(bottom=.25, top=.99, right=0.99, left=0.12)
00187     bins = np.arange(0.-bin_width/2., max_height+2*bin_width, bin_width)
00188     hist, bin_edges = np.histogram(np.array(mean_height_list), bins)
00189     h = mpu.plot_histogram(bin_edges[:-1]+bin_width/2., hist,
00190                            width=bin_width*0.8, plot_title=plot_title,
00191                            xlabel='Height (meters)', ylabel='\# of mechanisms', color=color, label = label)
00192     pb.xlim(0, max_height)
00193     pb.ylim(0, ymax)
00194     return h
00195 
00196 def plot_rotary_heights(csv_data):
00197     keys = ['radius', 'bottom', 'top', 'type', 'name', 'repetition']
00198     llists = expand(extract_keys(csv_data, keys), keys)
00199     types = llists[3]
00200     names = np.array(llists[4])
00201     print 'Handle bottom edge types', set(types)
00202     bottom_pts = np.array([float(f) for f in llists[1]])/100.
00203 
00204     top_pts = []
00205     for i,f in enumerate(llists[2]):
00206         if f == '':
00207             f = llists[1][i]
00208         top_pts.append(float(f)/100.)
00209     top_pts = np.array(top_pts)
00210     print 'total number of doors:', len(top_pts)
00211     mid_pts = (bottom_pts + top_pts)/2.
00212 
00213     types_arr = np.array(types)
00214     cabinet_mid_pts = mid_pts[np.where(types_arr == 'C')[0]]
00215     other_mid_pts = mid_pts[np.where(types_arr != 'C')[0]]
00216     other_names = names[np.where(types_arr != 'C')[0]]
00217 
00218     print 'other names', other_names
00219     # Hai, Advait apologizes for changing code without making a copy.
00220     # He didn't realise. He'll make a copy from an earlier revision in
00221     # subversion soon.
00222     ymax = 85
00223     handle_height_histogram_advait(mid_pts, plot_title='Handle Heights', ymax=ymax, label = 'All')
00224     mpu.legend(display_mode = 'less_space', handlelength=1.)
00225     handle_height_histogram_advait(mid_pts, plot_title='Handle Heights', ymax=ymax, color = 'g', label = 'Cabinets')
00226     handle_height_histogram_advait(other_mid_pts, plot_title='Handle Heights', ymax=ymax, color = 'y', new_figure = False, label = 'Other')
00227     mpu.legend(display_mode = 'less_space', handlelength=1.)
00228 
00229 def plot_prismatic_heights(csv_data):
00230     keys = ['distance', 'bottom', 'top', 'type', 'name', 'home', 'repetition']
00231     llists = expand(extract_keys(csv_data, keys), keys)
00232     types = llists[3]
00233     names = np.array(llists[4])
00234     home = np.array(llists[5])
00235     print 'Handle bottom edge types', set(types)
00236     bottom_pts = np.array([float(f) for f in llists[1]])/100.
00237     top_pts = []
00238     for i,f in enumerate(llists[2]):
00239         if f == '':
00240             f = llists[1][i]
00241         top_pts.append(float(f)/100.)
00242     top_pts = np.array(top_pts)
00243     mid_pts = (bottom_pts + top_pts)/2.
00244 
00245     types_arr = np.array(types)
00246     max_height = np.max(bottom_pts)
00247 
00248     sort_order = np.argsort(bottom_pts)
00249     names = names[sort_order]
00250     bottom_pts = bottom_pts[sort_order]
00251     home = home[sort_order]
00252     for i, name in enumerate(names):
00253         print home[i], name, bottom_pts[i]
00254 
00255 
00256     handle_height_histogram(bottom_pts, plot_title='Heights of Handle Lower Edge (Drawers)', max_height = max_height)
00257 
00258     max_height = np.max(mid_pts)
00259     handle_height_histogram_advait(mid_pts, plot_title='Handle Heights', max_height = max_height+0.1, ymax=43)
00260 
00261 #test_expand()
00262 csv_data = load_data()
00263 plot_prismatic_heights(csv_data)
00264 #plot_radii(csv_data)
00265 plot_rotary_heights(csv_data)
00266 #plot_opening(csv_data)
00267 pb.show()
00268 
00269 


2010_biorob_everyday_mechanics
Author(s): Advait Jain, Hai Nguyen, Charles C. Kemp (Healthcare Robotics Lab, Georgia Tech)
autogenerated on Wed Nov 27 2013 11:58:43