/
visualize.py
463 lines (359 loc) · 15.9 KB
/
visualize.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import networkx as nx
import matplotlib.pyplot as plt
import analyze as a
import statistics as s
from math import sqrt
import matplotlib.animation as animation
def draw_noc(noc, axes = None, node_size = 10):
draw_graph(noc.graph, axes, node_size)
def draw_graph(graph, axes = None, node_size = 10):
if not axes:
axes = plt.subplot(111);
positions = {xy: xy for xy in nx.nodes(graph)}
nx.draw(graph, positions, ax=axes, node_size=node_size)
def save_noc_image(noc, path, title=None):
# Reset the global plot
plt.close()
if not title:
title = path
draw_noc_and_largest_graph(noc, title=title)
plt.savefig(path)
plt.close()
def draw_noc_and_largest_graph(noc, node_size = 10, title=None):
if title:
fig = plt.figure()
fig.suptitle(title)
axes = plt.subplot(121)
axes.set_title("All NoC Connected Graphs")
draw_noc(noc, axes, node_size)
axes = plt.subplot(122)
axes.set_title("Largest NoC Connected Graph")
sub_graphs = a.get_sub_graphs(noc.graph)
largest = a.get_largest_sub_graph(sub_graphs)
if largest:
draw_graph(largest, axes, node_size)
def plot_data(criteria):
c = criteria
get_figure = lambda: plt.figure()
set_super_title = lambda f: f.suptitle("")
get_x_axis = lambda: plt.subplot(111).get_xaxis()
set_x_axis_label = lambda axis: None
get_y_axis = lambda: plt.subplot(111).get_yaxis()
set_y_axis_label = lambda axis: None
get_x_values = lambda: []
get_y_values = lambda: []
plot_values = lambda xs, ys, format: plt.plot(xs, ys, format)
get_plot_format = lambda: "ok"
get_errorbar_data = lambda: []
plot_errorbars = lambda xs, ys, error: plt.errorbar(xs, ys, yerr=error, color="black", fmt=None)
present_plot = lambda: plt.show()
figure = try_to_call(c.get_figure).then(get_figure).go()
try_to_call(c.set_super_title, figure).then(set_super_title, figure).go()
x_axis = try_to_call(c.get_x_axis).then(get_x_axis).go()
try_to_call(c.set_x_axis_label, x_axis).then(set_x_axis_label, x_axis).go()
y_axis = try_to_call(c.get_y_axis).then(get_y_axis).go()
try_to_call(c.set_y_axis_label, y_axis).then(set_y_axis_label, y_axis).go()
xs = try_to_call(c.get_x_values).then(get_x_values).go()
ys = try_to_call(c.get_y_values).then(get_y_values).go()
format = try_to_call(c.get_plot_format).then(get_plot_format).go()
try_to_call(c.plot_values, xs, ys, format).then(plot_values, xs, ys, format).go()
error = try_to_call(c.get_errorbar_data).then(get_errorbar_data).go()
try_to_call(c.plot_errorbars, xs, ys, error).then(plot_errorbars, xs, ys, error).go()
try_to_call(c.present_plot).then(present_plot).go()
def test(trials):
c = PlotCriteria()
c.set_super_title = lambda fig: fig.suptitle('Disconnected Ratio vs Wire Count')
c.set_x_axis_label = lambda axis: axis.label.set_text('Wire Count')
c.set_y_axis_label = lambda axis: axis.label.set_text('Disconnected Nodes')
c.get_x_values = lambda: [r.wire_count for r in trials[0]]
data_points = len(trials[0])
transpose = [ [t[n].disconnected_node_count for t in trials] for n in range(data_points)]
ys = [s.mean(x) for x in transpose]
print([r.wire_count for r in trials[0]])
c.get_y_values = lambda: ys
trial_count = len(trials)
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(x) for x in transpose]
stderr = [x / data_points_root for x in stdev]
c.get_errorbar_data = lambda: stderr
plot_data(c)
class PlotCriteria:
def __init__(self):
self.get_figure = None
self.set_super_title = None
self.get_x_axis = None
self.set_x_axis_label = None
self.get_y_axis = None
self.set_y_axis_label = None
self.get_x_values = None
self.get_y_values = None
self.get_plot_format = None
self.plot_values = None
self.get_errorbar_data = None
self.plot_errorbars = None
self.present_plot = None
def try_to_call(callee, *args):
class ChainExecution:
def __init__(self, callee, *args):
self.value = None
self.success = False
self._do_work(callee, *args)
def _do_work(self, callee, *args):
if callable(callee):
self.value = callee(*args)
self.called = True
def then(self, callee, *args):
# If the last callee was not callable
# try the next one, otherwise pass forward
# self holding the value returned form the callee
if not self.success:
self._do_work(callee, *args)
return self
def go(self):
return self.value
return ChainExecution(callee, *args)
def plot_disconnected_nodes_vs_wire_count(trials):
fig = plt.figure()
fig.suptitle('Disconnected Ratio vs Wire Count')
plt.xlabel('Wire Count')
plt.ylabel('Disconnected Nodes')
xs = [r.wire_count for r in trials[0]]
trial_count = len(trials)
data_points = len(trials[0])
#if not all()
transpose = [ [t[n].disconnected_node_count for t in trials] for n in range(data_points)]
means = [s.mean(x) for x in transpose]
# There's a bug in errorbar() when called without fmt=None that
# causes connecting lines to be drawn between data points. This creates
# a significant amount of visual noise. The page below says it was
# supposed to be fixed in 1.4 and higher, but this doesn't seem to be
# the cases or I haven't configured the module import correctly.
# An alternate solution is to draw the data points and the errorbars
# separately as is done below. The error bars seems to be slightly off
# center, but it's better than the alternative.
# See: http://stackoverflow.com/questions/18498742/how-do-you-make-an-errorbar-plot-in-matplotlib-using-linestyle-none-in-rcparams
plt.plot(xs, means, "ok")
# Calculate the standard error for each x's seqence of
# y values. There must be at least two data points or exceptions
# are thrown.
# See: https://en.wikipedia.org/wiki/Standard_error
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(x) for x in transpose]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, means, yerr=stderr, color="black", fmt=None)
#plt.plot(xs, ys, 'o')
#plt.savefig('disconnected.png')
plt.show()
def plot_connectivity_ratio(trials):
fig = plt.figure()
fig.suptitle('Connectivity Ratio')
plt.xlabel('Wire Count')
plt.ylabel('Connectivity Ratio (largest graph node count / disconnected node count) Nodes')
xs = [r.wire_count for r in trials[0]]
trial_count = len(trials)
data_points = len(trials[0])
#if not all()
transpose = [ [t[n].largest_graph_node_count / max(1, t[n].disconnected_node_count) for t in trials] for n in range(data_points)]
means = [s.mean(x) for x in transpose]
# There's a bug in errorbar() when called without fmt=None that
# causes connecting lines to be drawn between data points. This creates
# a significant amount of visual noise. The page below says it was
# supposed to be fixed in 1.4 and higher, but this doesn't seem to be
# the cases or I haven't configured the module import correctly.
# An alternate solution is to draw the data points and the errorbars
# separately as is done below. The error bars seems to be slightly off
# center, but it's better than the alternative.
# See: http://stackoverflow.com/questions/18498742/how-do-you-make-an-errorbar-plot-in-matplotlib-using-linestyle-none-in-rcparams
plt.plot(xs, means, "ok")
# Calculate the standard error for each x's seqence of
# y values. There must be at least two data points or exceptions
# are thrown.
# See: https://en.wikipedia.org/wiki/Standard_error
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(x) for x in transpose]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, means, yerr=stderr, color="black", fmt=None)
#plt.plot(xs, ys, 'o')
#plt.savefig('disconnected.png')
plt.show()
def coplot_largest_graph_and_disconnected_nodes(trials):
fig = plt.figure()
plot = plt.subplot()
fig.suptitle('Connected & Disconnected Nodes vs. Wire Count')
plt.xlabel('Wire Count')
plt.ylabel('Largest graph Node Count')
xs = [r.wire_count for r in trials[0]]
trial_count = len(trials)
data_points = len(trials[0])
#if not all()
large_set = [ [t[n].largest_graph_node_count for t in trials] for n in range(data_points)]
largest = [s.mean(x) for x in large_set]
discon_set = [ [t[n].disconnected_node_count for t in trials] for n in range(data_points)]
disconnected = [s.mean(y) for y in discon_set]
# There's a bug in errorbar() when called without fmt=None that
# causes connecting lines to be drawn between data points. This creates
# a significant amount of visual noise. The page below says it was
# supposed to be fixed in 1.4 and higher, but this doesn't seem to be
# the cases or I haven't configured the module import correctly.
# An alternate solution is to draw the data points and the errorbars
# separately as is done below. The error bars seems to be slightly off
# center, but it's better than the alternative.
# See: http://stackoverflow.com/questions/18498742/how-do-you-make-an-errorbar-plot-in-matplotlib-using-linestyle-none-in-rcparams
h1, = plot.plot(xs, largest, "ok", label="Largest")
# Calculate the standard error for each x's seqence of
# y values. There must be at least two data points or exceptions
# are thrown.
# See: https://en.wikipedia.org/wiki/Standard_error
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(value) for value in large_set]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, largest, yerr=stderr, color="black", fmt=None)
twin = plot.twinx()
twin.yaxis.label.set_text('Disconnected Node Count')
h2, = twin.plot(xs, disconnected, "*r", label="Disconnected")
plt.legend(handles=[h1,h2])
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(value) for value in discon_set]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, disconnected, yerr=stderr, color="black", fmt=None)
#plt.plot(xs, ys, 'o')
plt.savefig('connected_vs_disconnected.png')
#plt.show()
def coplot_shortest_average_path_and_total_wire_length(trials):
fig = plt.figure()
plot = plt.subplot()
fig.suptitle('Shortest Average Path & Total Wire Length vs. Wire Count')
plt.xlabel('Wire Count')
plt.ylabel('Shortest Average Path (units)')
xs = [r.wire_count for r in trials[0]]
trial_count = len(trials)
data_points = len(trials[0])
#if not all()
large_set = [ [t[n].average_shortest_path_length for t in trials] for n in range(data_points)]
largest = [s.mean(x) for x in large_set]
discon_set = [ [t[n].total_wire_length for t in trials] for n in range(data_points)]
disconnected = [s.mean(y) for y in discon_set]
# There's a bug in errorbar() when called without fmt=None that
# causes connecting lines to be drawn between data points. This creates
# a significant amount of visual noise. The page below says it was
# supposed to be fixed in 1.4 and higher, but this doesn't seem to be
# the cases or I haven't configured the module import correctly.
# An alternate solution is to draw the data points and the errorbars
# separately as is done below. The error bars seems to be slightly off
# center, but it's better than the alternative.
# See: http://stackoverflow.com/questions/18498742/how-do-you-make-an-errorbar-plot-in-matplotlib-using-linestyle-none-in-rcparams
h1, = plot.plot(xs, largest, "ok", label="Shortest")
# Calculate the standard error for each x's seqence of
# y values. There must be at least two data points or exceptions
# are thrown.
# See: https://en.wikipedia.org/wiki/Standard_error
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(value) for value in large_set]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, largest, yerr=stderr, color="black", fmt=None)
twin = plot.twinx()
twin.yaxis.label.set_text('Total Wire Length (units)')
h2, = twin.plot(xs, disconnected, "*r", label="Total")
plt.legend(handles=[h1,h2])
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(value) for value in discon_set]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, disconnected, yerr=stderr, color="black", fmt=None)
#plt.plot(xs, ys, 'o')
plt.savefig('shortest_vs_total.png')
plt.show()
def coplot_node_density_and_total_wire_length(trials):
fig = plt.figure()
plot = plt.subplot()
fig.suptitle('Node Density & Total Wire Length vs. Wire Count')
plt.xlabel('Wire Count')
plt.ylabel('Node Density (node/unit^2)')
xs = [r.wire_count for r in trials[0]]
trial_count = len(trials)
data_points = len(trials[0])
#if not all()
large_set = [ [t[n].average_shortest_path_length / 60 for t in trials] for n in range(data_points)]
largest = [s.mean(x) for x in large_set]
discon_set = [ [t[n].total_wire_length for t in trials] for n in range(data_points)]
disconnected = [s.mean(y) for y in discon_set]
# There's a bug in errorbar() when called without fmt=None that
# causes connecting lines to be drawn between data points. This creates
# a significant amount of visual noise. The page below says it was
# supposed to be fixed in 1.4 and higher, but this doesn't seem to be
# the cases or I haven't configured the module import correctly.
# An alternate solution is to draw the data points and the errorbars
# separately as is done below. The error bars seems to be slightly off
# center, but it's better than the alternative.
# See: http://stackoverflow.com/questions/18498742/how-do-you-make-an-errorbar-plot-in-matplotlib-using-linestyle-none-in-rcparams
h1, = plot.plot(xs, largest, "ok", label="Density")
# Calculate the standard error for each x's seqence of
# y values. There must be at least two data points or exceptions
# are thrown.
# See: https://en.wikipedia.org/wiki/Standard_error
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(value) for value in large_set]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, largest, yerr=stderr, color="black", fmt=None)
twin = plot.twinx()
twin.yaxis.label.set_text('Total Wire Length (units)')
h2, = twin.plot(xs, disconnected, "*r", label="Total")
plt.legend(handles=[h1,h2])
if trial_count >= 2:
data_points_root = sqrt(trial_count)
stdev = [s.stdev(value) for value in discon_set]
stderr = [x / data_points_root for x in stdev]
plt.errorbar(xs, disconnected, yerr=stderr, color="black", fmt=None)
#plt.plot(xs, ys, 'o')
plt.savefig('density_vs_total.png')
plt.show()
def plot_largest_graph_vs_wire_count(trials):
fig = plt.figure()
fig.suptitle('Largest Graph Nodes vs Wire Count')
plt.xlabel('Wire Count')
plt.ylabel('Largest Graph Nodes')
for results in trials:
xs = [r.wire_count for r in results]
ys = [float(r.largest_graph_node_count) / max(1, r.wire_count) for r in results]
plt.plot(xs, ys, 'o')
plt.savefig('largest.png')
plt.show()
def plot_average_shortest_path_vs_wire_count(trials):
fig = plt.figure()
fig.suptitle('Average Shortest Path vs Wire Count')
plt.xlabel('Wire Count')
plt.ylabel('Average Shortest Path')
for results in trials:
xs = [r.wire_count for r in results]
ys = [float(r.average_shortest_path_length) for r in results]
plt.plot(xs, ys, 'o')
plt.savefig('shortest.png')
plt.show()
def visualize(trials):
#coplot_largest_graph_and_disconnected_nodes(trials)
#coplot_shortest_average_path_and_total_wire_length(trials)
coplot_node_density_and_total_wire_length(trials)
#plot_connectivity_ratio(trials)
#plot_disconnected_nodes_vs_wire_count(trials)
#plot_largest_graph_vs_wire_count(trials)
#plot_average_shortest_path_vs_wire_count(trials)
class GraphCreationAnimator:
def __init__(self):
self.graphs = []
def add_graph(self, noc, *args):
self.graphs.append(noc.create_graph())
def create_animation(self, save_path):
fig = plt.figure(figsize=(30,30))
func = lambda num: nx.draw(self.graphs[num], {xy: xy for xy in nx.nodes(self.graphs[num])} )
line_ani = animation.FuncAnimation(fig, func, len(self.graphs))
plt.rcParams['animation.ffmpeg_path'] = '/usr/local/bin/ffmpeg'
Writer = animation.writers['ffmpeg']
writer = Writer(metadata=dict(artist='Me'), bitrate=1800)
line_ani.save(save_path, fps=1, writer=writer)