-
Notifications
You must be signed in to change notification settings - Fork 66
/
graph_results.py
333 lines (287 loc) · 14 KB
/
graph_results.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import os, argparse
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.patches as mpatches
import matplotlib as mp
from matplotlib.colors import LinearSegmentedColormap
from src.graph_globals import global_params
from src.graphs import graph, boxplot, multi_line, multi_line_with_CI, get_cmap, scatter, save_graph
from src.picklefuncs import load_data
from src.helper_funcs import check_and_make_dir
def main():
global_params()
#you must have the same number of colours as labels
colours = ['b', 'c', 'orange', 'y', 'm', 'gray']
labels = {'ddpg':'DDPG', 'dqn':'DQN', 'sotl':'SOTL', 'maxpressure':'Max-pressure', 'websters':'Webster\'s', 'uniform':'Uniform'}
if len(colours) != len(labels):
assert 0, 'Error: the number of colours '+str(len(colours))+' does not equal the number of labels'+str(len(labels))
#make dict of labels to colours
colours = { l:c for c, l in zip(colours, labels)}
args = parse_cl_args()
check_and_make_dir(args.save_dir)
if args.type == 'moe':
fp = 'metrics/'
graph_travel_time(labels, colours, fp, args.save_dir)
metrics = ['queue', 'delay']
graph_individual_intersections(labels, colours, fp, metrics, args.save_dir)
elif args.type == 'hp':
fp = 'hp/'
graph_hyper_params(labels, colours, fp, args.save_dir)
else:
assert 0, print('Error, supplied graph type argument '+str(args.type)+' does not exist')
def parse_cl_args():
parser = argparse.ArgumentParser()
##sumo params
parser.add_argument("-type", type=str, default='moe', dest='type', help='Data to be graphed, default: moe, options: moe, hp')
parser.add_argument("-save_dir", type=str, default='figures/', dest='save_dir', help='Directory to save figures, default: figures/')
args = parser.parse_args()
return args
def graph_hyper_params(labels, colours, fp, save_dir):
tsc = os.listdir(fp)
tsc_hp = {}
#get data
for t in tsc:
tsc_fp = fp+t+'/'
data = [ load_data(tsc_fp+f) for f in os.listdir(tsc_fp)]
tsc_hp[t] = np.stack([ [np.mean(d), np.std(d)] for d in data]).T
#create appropriate graph
n = len(tsc)
if n == 1:
f, axes = plt.subplots()
axes = [axes]
else:
nrows = 2
ncols = int(n/nrows) if n%nrows == 0 else int((n+1)/nrows)
f, axes = plt.subplots(nrows=nrows,ncols=ncols)
axes = axes.flat
if n%nrows != 0:
f.delaxes(axes[-1])
XTITLE = 'Mean\nTravel Time '+r"$(s)$"
YTITLE = ('Standard\nDeviation\nTravel Time '+r"$(s)$", 80)
#graph each tsc hyperparemeter
for ax, t, i in zip(axes, tsc, range(len(tsc))):
#order hp performance from low to high
#w.r.t mean+std
mean_data = tsc_hp[t][0]
std_data = tsc_hp[t][1]
data = sorted([ (m+s, m, s) for m,s in zip(mean_data, std_data) ], key = lambda x:x[0] )
data = np.stack([ [d[1], d[2]] for d in data]).T
mean_data = data[0]
std_data = data[1]
#rainbow_colours = mp.cm.rainbow(np.linspace(0, 1, len(mean_data)))
rg_colours = mp.cm.brg(np.linspace(1.0, 0.5, len(mean_data)))
if i%ncols == 0 and i >= len(tsc)/2:
xtitle = XTITLE
ytitle = YTITLE
elif i%ncols == 0:
xtitle = ''
ytitle = YTITLE
elif i >= len(tsc)/2:
xtitle = XTITLE
ytitle = ''
else:
xtitle = ''
ytitle = ''
#graph each tsc hp performance
graph( ax, mean_data, scatter( ax, mean_data, std_data, rg_colours, ['']*len(mean_data)),
xtitle=xtitle,
ytitle_pad = ytitle,
title=str(labels[t]),
xlim = [0.0, max(mean_data)*1.05],
ylim= [0.0, max(std_data)*1.05],
grid=True)
#axis colourbar
cax = f.add_axes([0.915, 0.1, 0.05, 0.85])
cmap = mp.cm.brg
cm = LinearSegmentedColormap.from_list('rg', rg_colours, N=rg_colours.shape[0])
norm = mp.colors.Normalize(vmin=0.5, vmax=1.0)
cb = mp.colorbar.ColorbarBase(cax, cmap=cm,
norm=norm,
orientation='vertical')
#color bar axis text
#print([ l._text for l in cb.ax.get_yticklabels()])
#cb_labels = ['']*rg_colours.shape[0]
cb_labels = [ l._text for l in cb.ax.get_yticklabels()]
cb_labels[0] = 'Best'
cb_labels[-1] = 'Worst'
cb.ax.set_yticklabels(cb_labels)
f.suptitle('Hyperparameter Performance')
save_graph(f, save_dir+'tsc_hp.pdf', 600, 14, 24.9)
plt.show()
#now compare all tsc hp sets together in one graph
#prepare data
data_order = sorted(tsc_hp.keys())
#tsc_color = colours[:len(data_order)]
mean_data, std_data, colors, tsc_labels = [], [], [], []
for d in data_order:
n = len(tsc_hp[d][0])
mean_data.extend(tsc_hp[d][0])
std_data.extend(tsc_hp[d][1])
tsc_labels.extend(labels[d])
#colors.extend([c]*len(tsc_hp[d][0]))
colors.extend( [colours[d]]*n )
#graph all hp data all together
f, ax = plt.subplots(1,1)
graph( ax, mean_data, scatter( ax, mean_data, std_data, colors, ['']*len(mean_data)),
xtitle=XTITLE,
ytitle_pad = YTITLE,
title='Traffic Signal Control\nHyperparameter Comparison',
xlim = [0.0, 200.0],
ylim= [0.0, 200.0],
#xlim = [0.0, max(mean_data)*1.05],
#ylim= [0.0, max(std_data)*1.05],
#legend=(0.82, 0.72),
#colours=colours,
grid=True)
#colorbar
#add legend manually because we only
#want one for each tsc
patches = []
for d in data_order:
c = colours[d]
patches.append( mpatches.Patch(color=c, label=labels[d]) )
plt.legend(handles=patches, framealpha=1.0)
save_graph(f, save_dir+'hp.pdf', 600, 14, 24.9)
plt.show()
def graph_travel_time(labels, colours, fp, save_dir):
#read metric data for all tsc types
data = get_data(fp, 'traveltime', get_folder_data)
#prepare data for graph
data_order = sorted(data.keys())
data = [ data[d] for d in data_order]
labels = [ labels[d] for d in data_order]
c = [ colours[d] for d in data_order]
#graph data
f, ax = plt.subplots(1,1)
t = 'Travel Time '+r"$(s)$"+'\n('+r"$\mu,\sigma,$"+'median'+r"$)$"
graph( ax, data, boxplot( ax, data, c, labels),
xtitle='Traffic Signal Controller',
#ytitle_pad = ('Travel Time (s)\n('+r"/mu,/sigma,"+"median)", 60),
ytitle_pad = (t, 60),
title='Traffic Signal Controller\nTravel Time) ',
legend=(0.82, 0.72),
grid=True)
for i, d in enumerate(data_order):
text = '('+str(int(np.mean(data[i])))+', '+str(int( np.std(data[i]) ) )+', '+str(int( np.median(data[i]) ) )+r"$)$"
ax.text(i+1.1, 300, text, color= c[i])
#f.suptitle('Travel Time')
#display graph
save_graph(f, save_dir+'travel_time.pdf', 600, 14, 24.9)
plt.show()
def graph_conf_interval(labels, colours, fp, metric):
#read metric data for all tsc types
data = get_data(fp, metric, get_metric_data)
#prepare data for graph
data_order = sorted(data.keys())
data = [ data[d] for d in data_order]
labels = [ labels[d] for d in data_order]
#graph data
f, ax = plt.subplots(1,1)
metric_title = metric.capitalize()
graph( ax, data, multi_line_with_CI( ax, data, colours, labels),
xtitle='Time (s)',
ytitle_pad = (metric_title, 60),
title=metric_title+' by\nTraffic Signal Controller',
legend=(0.72, 0.72),
grid=True)
#f.suptitle(metric_title)
#display graph
plt.show()
def get_data(fp, metric, read_data_func):
tsc = os.listdir(fp)
tsc_data = { t:read_data_func(fp+t+'/'+metric) for t in tsc}
return tsc_data
def get_metric_data(fp):
#for use with queue and delay data
#sort all metric data from same tsc_id
if not os.path.exists(fp):
assert 0, 'Supplied path '+str(fp)+' does not exist.'
tsc_data = {tsc_id:sorted(os.listdir(fp+'/'+tsc_id))
for tsc_id in os.listdir(fp)}
sim_runs_data = []
#until all data has been popped
k = list(tsc_data.keys())[0]
while len(tsc_data[k]) > 0:
#get file path for each intersection from same sim run
same_run_data = [ fp+'/'+tsc_id+'/'+tsc_data[tsc_id].pop(0)
for tsc_id in tsc_data ]
same_run_data = [ load_data(f) for f in same_run_data ]
#sum across time axis, each element of array
#represents the sum of all tsc_id metric
sim_runs_data.append( np.sum(same_run_data, axis=0) )
return np.stack(sim_runs_data)
def get_folder_data(fp):
#all the travel times can be
#grouped together by extending list
if not os.path.exists(fp):
assert 0, 'Supplied path '+str(fp)+' does not exist.'
data = []
for f in os.listdir(fp):
data.extend(load_data(fp+'/'+f))
return np.array(data)
def stack_folder_files(fp):
data = [ load_data(fp+f) for f in os.listdir(fp)]
return np.stack(data)
def graph_individual_intersections(labels, colours, fp, metrics, save_dir):
#rows are metrics
#columns are intersections
tsc = os.listdir(fp)
tsc.remove('sotl')
intersections = os.listdir(fp+tsc[0]+'/'+metrics[0]+'/')
ncols = len(intersections)
nrows = len(metrics)
f, ax = plt.subplots(nrows=nrows,ncols=ncols)
if ax.ndim == 1:
ax = ax[...,np.newaxis]
for m, r in zip(metrics, range(nrows)):
#get metric data for each intersection
data = {}
for t in tsc:
data[t] = {}
for i in intersections:
alias_p = 60
data[t][i] = alias( stack_folder_files(fp+'/'+t+'/'+m+'/'+i+'/'), alias_p)
xtitle = 'Time '+r" $(min)$" if r == nrows-1 else ''
#graph same metric for each intersection
for I, c in zip(intersections, range(ncols)):
title = I if r == 0 else ''
if m == 'queue':
ytitle = m.capitalize()+r" $(veh)$" if c == 0 else ''
else:
ytitle = m.capitalize()+r" $(s)$" if c == 0 else ''
legend = (0.59, 0.6)
data_order = sorted(data.keys())
alias_p = 60
order_data = [ data[d][I] for d in data_order]
order_labels = [ labels[d] for d in data_order]
order_colors = [ colours[d] for d in data_order]
label_c = { labels[d]:colours[d] for d in data_order}
graph( ax[r,c], order_data, multi_line_with_CI( ax[r,c], order_data, order_colors, order_labels),
xtitle=xtitle,
ytitle_pad = (ytitle , 60),
title=title,
legend=legend,
colours = label_c,
grid=True)
ax[r,c].set_xlim(left=0)
ax[r,c].set_ylim(bottom=0)
f.suptitle('Intersection Measures of Effectiveness')
save_graph(f, save_dir+'intersection_moe.pdf', 600, 14, 24.9)
plt.show()
def alias(data, a):
n = data.shape[-1]
if n % a != 0:
#error?
a = 1
stop = n-a
m = int(n/a)
alias_data = []
for d in data:
#alias data timeseries
alias_data.append( np.array([np.sum(d[i*a:(i+1)*a]) for i in range(m) ]) )
return np.stack(alias_data)
#return np.stack([np.sum(data[i*a:(i+1)*a]) for i in range(stop) ])
#return np.array([ np.sum(data[i*a:(i+1)*a]) for i in range(stop)])
if __name__ == '__main__':
main()