-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_charts.py
executable file
·367 lines (271 loc) · 11.5 KB
/
generate_charts.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
#!/usr/bin/python
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot
import argparse
from os.path import basename
import os
class Dataset:
def __init__(self, log):
self.name = basename(log.name)
self.timeline = self.parse_log(log)
self.plasmodia = self.group_plasmodia(self.timeline)
def parse_log(self, log):
timeline = []
previous_f = {}
for line in log.readlines():
f = dict(field.split('=') for field in line.rstrip().split(' '))
for (k, v) in f.iteritems():
if '.' in v:
v = float(v)
else:
try:
v = int(v)
except:
pass
f[k] = v
if f['plasmodium'] != '*':
temp_f = previous_f.copy()
temp_f.update(f)
f = temp_f
previous_f = f
timeline.append(f)
log.close()
return timeline
def group_plasmodia(self, timeline):
plasmodia = {}
for entry in timeline:
if entry['plasmodium'] not in plasmodia:
plasmodia[entry['plasmodium']] = []
plasmodia[entry['plasmodium']].append(entry)
return plasmodia
class PlotGenerator:
def __init__(self, datasets):
self.datasets = datasets
def multiple_instance_aggregated_key(self, key, aggregate, label=None,
states=('crawling',),
title=True, legend=True):
figure, plt = matplotlib.pyplot.subplots()
plt.set_axisbelow(True)
figure.set_size_inches((18, 6), forward=True)
plt.grid(b=True, which='major', color='lightgray', linestyle='-')
plt.grid(b=True, which='minor', color='whitesmoke', linestyle='-')
plt.minorticks_on()
max_epoch = 0
for dataset in self.datasets:
local_max_epoch = 0
data = {}
for entry in dataset.plasmodia['*']:
if entry['epoch'] > local_max_epoch:
local_max_epoch = entry['epoch']
if entry['epoch'] not in data:
data[entry['epoch']] = []
if local_max_epoch > max_epoch:
max_epoch = local_max_epoch
for name in dataset.plasmodia:
if isinstance(name, int):
for entry in dataset.plasmodia[name]:
if entry['state'] in states:
data[entry['epoch']].append(entry[key])
aggregated_data = []
for epoch, values in data.iteritems():
if len(values) > 0:
aggregated_data.append(aggregate(values))
plt.plot(aggregated_data, label=dataset.name)
if label is None:
label = key
plt.set_ylabel(label)
plt.set_xlabel('epoch')
plt.set_xlim(right=max_epoch)
x1, x2, y1, y2 = plt.axis()
if y1 < 10 and y2 > 1.5:
plt.axis((x1, x2, 0, y2*1.01))
if legend:
plt.legend(loc='best')
if title is True:
plt.set_title("%s aggregated %s - %s" %
(key, aggregate.__name__, dataset.name))
elif isinstance(title, basestring):
plt.set_title(title)
return [figure]
def multiple_instance_single_key(self, key, label=None, title=True,
legend=True):
figure, plt = matplotlib.pyplot.subplots()
plt.set_axisbelow(True)
figure.set_size_inches((18, 6), forward=True)
plt.grid(b=True, which='major', color='lightgray', linestyle='-')
plt.grid(b=True, which='minor', color='whitesmoke', linestyle='-')
plt.minorticks_on()
max_epoch = 0
for dataset in self.datasets:
data = []
for entry in dataset.plasmodia['*']:
if entry['epoch'] > max_epoch:
max_epoch = entry['epoch']
if key in entry:
data.append(entry[key])
plt.plot(data, label=dataset.name)
if label is None:
label = key
plt.set_ylabel(label)
plt.set_xlabel('epoch')
plt.set_xlim(right=max_epoch)
x1, x2, y1, y2 = plt.axis()
if y1 < 10 and y2 > 1.5:
plt.axis((x1, x2, 0, y2*1.01))
if legend:
plt.legend(loc='best')
if title is True:
plt.set_title("%s - %s" %
(key, dataset.name))
elif isinstance(title, basestring):
plt.set_title(title)
return [figure]
def single_instance_plasmodia_state(self):
plots = []
for dataset in self.datasets:
max_epoch = dataset.plasmodia['*'][-1]['epoch']
figure, plt = matplotlib.pyplot.subplots()
plt.set_axisbelow(True)
figure.set_size_inches((18, 6), forward=True)
plt.grid(b=True, which='major', color='lightgray', linestyle='-')
alive = []
dead = []
merge = []
delta_dead = 0
delta_alive = 0
delta_merge = 0
for entry in dataset.timeline:
if entry['plasmodium'] == '*':
if entry['epoch'] > 0:
alive.append(delta_alive)
dead.append(delta_dead)
merge.append(delta_merge)
else:
if entry['state'] == 'dead':
delta_dead += 1
delta_alive -= 1
elif entry['state'] == 'merge':
delta_alive -= 1
delta_merge += 1
elif entry['state'] == 'new':
delta_alive += 1
plt.plot(alive, label='alive')
plt.plot(dead, label='dead')
plt.plot(merge, label='merged')
plt.set_ylabel('number of plasmodia')
plt.set_xlabel('epoch')
plt.set_xlim(right=max_epoch)
plt.legend(loc='best')
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, y1, y2+1))
plt.set_title("state of colony - %s" % dataset.name)
plots.append(figure)
return plots
def single_instance_multiple_plasmodia(self, key, states=('crawling',),
label=None, title=True,
legend=False):
plots = []
for dataset in self.datasets:
max_epoch = dataset.plasmodia['*'][-1]['epoch']
figure, plt = matplotlib.pyplot.subplots()
plt.set_axisbelow(True)
figure.set_size_inches((18, 6), forward=True)
plt.grid(b=True, which='major', color='lightgray', linestyle='-')
plt.grid(b=True, which='minor', color='whitesmoke', linestyle='-')
plt.minorticks_on()
names = filter(lambda x: isinstance(x, int),
dataset.plasmodia.keys())
for name in names:
data = []
for entry in dataset.plasmodia[name]:
if entry['state'] in states:
data.append(entry[key])
plt.plot(data, label="#%i" % name)
if label is None:
label = key
plt.set_ylabel(label)
plt.set_xlabel('epoch')
plt.set_xlim(right=max_epoch)
x1, x2, y1, y2 = plt.axis()
if y1 < 10 and y2 > 1.5:
plt.axis((x1, x2, 0, y2*1.01))
if legend:
plt.legend(loc='best')
if title is True:
plt.set_title("%s for each plasmodium - %s" %
(key, dataset.name))
elif isinstance(title, basestring):
plt.set_title(title)
plots.append(figure)
return plots
if __name__ == '__main__':
matplotlib.pyplot.rcParams['backend'] = 'TkAgg'
matplotlib.pyplot.rcParams['agg.path.chunksize'] = 100000
matplotlib.pyplot.rc('text', usetex=True)
matplotlib.pyplot.rc('font', family='serif')
parser = argparse.ArgumentParser(description="Charts generator")
parser.add_argument('-l', '--log', type=argparse.FileType('r'),
required=True, nargs='+')
parser.add_argument('-o', '--out', type=str, required=True)
parser.add_argument('-f', '--format', type=str, default='png')
args = parser.parse_args()
if not os.path.exists(args.out):
os.makedirs(args.out)
datasets = map(Dataset, args.log)
generator = PlotGenerator(datasets)
f = []
f += generator.multiple_instance_single_key('total_food_eaten_count',
label='number of visited solutions')
f += generator.multiple_instance_single_key('memory',
label='total memory usage')
f += generator.multiple_instance_single_key('time',
label='execution time')
f += generator.multiple_instance_single_key('solution_cost',
label='cost of best solution')
f += generator.single_instance_plasmodia_state()
detailed = {'food': (('new', 'crawled'),
'plasmodium energy'),
'size': (('new', 'crawled', 'dead'),
'number of occupied solutions'),
'total_explored': (('new', 'crawled'),
'total number of explored solutions'),
'total_crawled': (('new', 'crawled'),
'total number of crawled solutions'),
'frontier': (('crawled', 'merged'),
'number of solutions in frontier'),
'frontier_best_cost': (('crawled',),
'cost of best solution in frontier'),
'frontier_worst_cost': (('crawled',),
'cost of worst solution in frontier')}
def avg(a):
return float(sum(a))/len(a)
aggregates = (min, max, sum, avg)
for key, conf in detailed.iteritems():
f += generator.single_instance_multiple_plasmodia(key, states=conf[0],
label=conf[1])
for agg in aggregates:
f += generator.multiple_instance_aggregated_key(key, agg,
states=conf[0],
label=conf[1])
for plot in f:
filename = plot.gca().title.get_text()
plot.gca().title.set_visible(False)
try:
plot.savefig("%s/%s.%s" % (args.out, filename, args.format),
bbox_inches='tight', dpi=150)
except:
pass
plot.gca().set_xlim(0, 0.2 * plot.gca().get_xlim()[1])
try:
plot.savefig("%s/%s.zoomed.%s" % (args.out, filename, args.format),
bbox_inches='tight', dpi=150)
except:
pass
plot.gca().set_xlim(0, 0.25 * plot.gca().get_xlim()[1])
try:
plot.savefig("%s/%s.zzzoomed.%s" % (args.out, filename, args.format),
bbox_inches='tight', dpi=150)
except:
pass
plot.clf()