00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 import roslib; roslib.load_manifest('hai_sandbox')
00029 import hrl_lib.util as ut
00030 import pylab as pb
00031 import numpy as np
00032 import pdb
00033
00034 def conf_to_percent(rec):
00035 conf = rec['mat']
00036 conf[0,:] = conf[0,:] / rec['neg']
00037 conf[1,:] = conf[1,:] / rec['pos']
00038 return conf[0,0], conf[1,1]
00039
00040 def plot_classifier_performance(fname, pname, plot_all):
00041 results = ut.load_pickle(fname)
00042
00043
00044
00045
00046
00047
00048 scores = {}
00049 for rlist in results['perf_on_other_scans']:
00050 for d in rlist:
00051 if scores.has_key(d['name']):
00052 scores[d['name']].append(conf_to_percent(d['conf']))
00053 else:
00054 scores[d['name']] = [conf_to_percent(d['conf'])]
00055 for k in scores.keys():
00056 scores[k] = zip(*scores[k])
00057
00058 if results.has_key('train_set_statistics'):
00059 train_neg, train_pos = zip(*[conf_to_percent(d['conf']) for d in results['train_set_statistics']])
00060 else:
00061 train_neg = train_pos = None
00062
00063 if results.has_key('current_scan_statistics'):
00064 pdb.set_trace()
00065 test_neg, test_pos = zip(*[conf_to_percent(d['conf']) for d in results['current_scan_statistics']])
00066 else:
00067 test_neg = test_pos = None
00068
00069 n_iterations = np.array(range(len(results['train_set_statistics'])))
00070
00071
00072 pb.figure(1)
00073 if results.has_key('train_set_statistics'):
00074 pb.plot(n_iterations, train_neg, label='train ' + pname)
00075 if test_neg != None:
00076 pb.plot(n_iterations, test_neg, label='test ' + pname)
00077 if plot_all:
00078 for i, k in enumerate(scores.keys()):
00079 pb.plot(n_iterations, scores[k][0], '--', label=str(i))
00080
00081 if results.has_key('converged_at_iter'):
00082 pb.plot([results['converged_at_iter'], results['converged_at_iter']], [0., 1.], 'r')
00083
00084 pb.title('True negatives')
00085 pb.legend()
00086
00087
00088 pb.figure(2)
00089 if train_pos != None:
00090 pb.plot(n_iterations, train_pos, label='train ' + pname)
00091 if test_pos != None:
00092 pb.plot(n_iterations, test_pos, label='test ' + pname)
00093
00094
00095 print 'mapping from dataset to id'
00096 if plot_all:
00097 for i, k in enumerate(scores.keys()):
00098 pb.plot(n_iterations, scores[k][1], '--', label=str(i))
00099 print 'ID', i, 'dataset', k
00100
00101 if results.has_key('converged_at_iter'):
00102 pb.plot([results['converged_at_iter'], results['converged_at_iter']], [0., 1.], 'r')
00103
00104 pb.title('True positives')
00105 pb.legend()
00106
00107 def plot_features_perf(fnames, pnames):
00108
00109 all_scores = {}
00110 dset_names = None
00111 for fname, pname in zip(fnames, pnames):
00112 results = ut.load_pickle(fname)
00113 train_neg, train_pos = zip(*[conf_to_percent(d['conf']) for d in results['train_set_statistics']])
00114 scores = {}
00115 for rlist in results['perf_on_other_scans']:
00116 for d in rlist:
00117 if scores.has_key(d['name']):
00118 scores[d['name']].append(conf_to_percent(d['conf']))
00119 else:
00120 scores[d['name']] = [conf_to_percent(d['conf'])]
00121 for k in scores.keys():
00122 scores[k] = zip(*scores[k])
00123 scores['train'] = [(train_neg), (train_pos)]
00124 all_scores[pname] = scores
00125 if dset_names == None:
00126 dset_names = scores.keys()
00127
00128
00129 neg_by_dset = {}
00130 for n in dset_names:
00131 posn = []
00132 for pname in pnames:
00133 posn.append(all_scores[pname][n][0][0])
00134 neg_by_dset[n] = posn
00135
00136 pos_by_dset = {}
00137 for n in dset_names:
00138 posn = []
00139 for pname in pnames:
00140 posn.append(all_scores[pname][n][1][0])
00141 pos_by_dset[n] = posn
00142
00143 ind = np.arange(len(pnames))
00144 width = 0.05
00145
00146
00147 fig = pb.figure(1)
00148 ax = fig.add_subplot(111)
00149 rects=[]
00150 for i, name in enumerate(dset_names):
00151 rect = ax.bar(ind+(width*i), pos_by_dset[name], width, color=tuple(np.random.rand(3).tolist()))
00152 rects.append(rect)
00153 ax.set_ylabel('accuracy')
00154 ax.set_title('True positives by dataset and features used')
00155 ax.set_xticks(ind+width)
00156 ax.set_xticklabels(tuple(pnames))
00157
00158 fig = pb.figure(2)
00159 ax = fig.add_subplot(111)
00160 rects=[]
00161 for i, name in enumerate(dset_names):
00162 rect = ax.bar(ind+(width*i), neg_by_dset[name], width, color=tuple(np.random.rand(3).tolist()))
00163 rects.append(rect)
00164 ax.set_ylabel('accuracy')
00165 ax.set_title('True negatives by dataset and features used')
00166 ax.set_xticks(ind+width)
00167 ax.set_xticklabels(tuple(pnames))
00168
00169 if __name__ == '__main__':
00170 import sys
00171 import optparse
00172 p = optparse.OptionParser()
00173 p.add_option("-m", "--mode", action="store", type="string")
00174 p.add_option("-f", "--file", action="append", type="string")
00175 p.add_option('-n', '--name', action="append", type="string")
00176 opt, args = p.parse_args()
00177
00178 if opt.mode == 'active':
00179 if len(opt.file) <= 1:
00180 plot_all = True
00181 else:
00182 plot_all = False
00183
00184 for i in range(len(opt.file)):
00185 plot_classifier_performance(opt.file[i], opt.name[i], plot_all)
00186 pb.show()
00187
00188 if opt.mode == 'features':
00189 plot_features_perf(opt.file, opt.name)
00190 pb.show()
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201