This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplot.py
196 lines (173 loc) · 6.18 KB
/
plot.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
import argparse
import json
import numpy as np
import matplotlib
from matplotlib.font_manager import FontProperties
import os
import settings
from itertools import cycle
class Plot(object):
"""
Plot cumulative max similarity for all experiments
"""
def __init__(self):
# Python 2 and 3 compatibility hack
try:
input = raw_input
except NameError:
pass
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
'--small-font',
nargs='?',
dest='small_font',
help='Use a small font in the legend',
const=True,
required=False,
default=False
)
arg_parser.add_argument(
'--max',
nargs='?',
dest='max',
help='Show max series',
const=True,
required=False,
default=False
)
arg_parser.add_argument(
'--avg',
nargs='?',
dest='avg',
help='Show avg series',
const=True,
required=False,
default=False
)
arg_parser.add_argument(
'--label',
dest='label',
help='Which arg to use as label',
required=False,
default=None
)
arg_parser.add_argument(
'--output',
dest='output',
help='Output image file (PNG). If specified, interactive window will not appear.',
required=False,
default=None
)
arg_parser.add_argument(
'--agg',
nargs='?',
dest='use_agg',
help='Use this argument if you encounter errors like "_tkinter.TclError: no display'
' name and no $DISPLAY environment variable" in a Linux environment',
const=True,
required=False,
default=False
)
args = arg_parser.parse_args()
if args.use_agg:
matplotlib.use('Agg')
import matplotlib.pyplot as plt
experiments = {}
for root, dirs, files in os.walk(settings.STATS_DATA_DIRECTORY):
if dirs:
for experiment_dir in dirs:
if 'test' in experiment_dir:
continue
stats_file_path = os.path.join(
settings.STATS_DATA_DIRECTORY,
experiment_dir,
'stats.json'
)
if os.path.exists(stats_file_path):
experiment_id = experiment_dir.split('__')[1]
if experiment_id not in experiments:
experiments[experiment_id] = []
experiments[experiment_id].append(stats_file_path)
all_series = []
all_series_labels = []
for experiment_id in experiments:
stats_data_objects = []
for stats_file_path in experiments[experiment_id]:
with open(stats_file_path) as stats_file:
stats = json.load(stats_file)
stats_data_objects.append(stats)
experiment_series = [] # series for this experiment
for stats_data_object in stats_data_objects:
# compute cumulative maximum similarity
max_similarity = stats_data_object['generations'][0]['similarity_max']
similarity_series = []
for generation in stats_data_object['generations']:
if generation['similarity_max'] > max_similarity:
max_similarity = generation['similarity_max']
similarity_series.append(max_similarity)
experiment_series.append(similarity_series)
# take the average of experiment series
avg_similarity_values = []
for k in range(len(experiment_series[0])):
max_similarity = max(series[k] for series in experiment_series)
avg_similarity = sum(series[k] for series in experiment_series) / len(experiment_series)
avg_similarity_values.append({
'max': max_similarity,
'avg': avg_similarity
})
all_series.append(avg_similarity_values)
if args.label is None:
print(stats_data_objects[0]['args'])
label = input('label name? ')
all_series_labels.append(label)
else:
all_series_labels.append(stats_data_objects[0]['args'][args.label])
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_title('Best individual')
ax.set_ylabel('similarity measure')
colors = [
'#1f77b4',
'#ff7f0e',
'#2ca02c',
'#d62728',
'#9467bd',
'#8c564b',
'#e377c2',
'#7f7f7f',
'#bcbd22',
'#17becf'
]
color_cycle = cycle(colors).next
handles = []
for i, series in enumerate(all_series):
color = color_cycle()
x = np.array(range(len(series)))
if args.max:
max_series_plot, = plt.plot(
x,
np.array([y['max'] for y in series]),
label=str(all_series_labels[i]) + ' (max)',
color=color,
linestyle='-.'
)
handles.append(max_series_plot)
if args.avg:
avg_series_plot, = plt.plot(
x,
np.array([y['avg'] for y in series]),
label=str(all_series_labels[i]) + ' (avg)',
color=color
)
handles.append(avg_series_plot)
font_p = FontProperties()
if args.small_font:
font_p.set_size('small')
plt.legend(handles=handles, prop=font_p, loc='best')
ax.set_xlabel('# generations')
if args.output is None:
plt.show()
else:
plt.savefig(args.output, dpi=300)
if __name__ == "__main__":
Plot()