This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgraph_utils.py
313 lines (271 loc) · 8.22 KB
/
graph_utils.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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import numpy as np
import matplotlib.pyplot as plt
import os
import json
import re
import scipy.interpolate
def read_log_file(file_name, key_name, value_name, smooth=3):
keys, values = [], []
try:
with open(file_name, 'r') as f:
for line in f:
try:
e = json.loads(line.strip())
key, value = e[key_name], e[value_name]
keys.append(int(key))
values.append(float(value))
except:
pass
except:
print('bad file: %s' % file_name)
return None, None
keys, values = np.array(keys), np.array(values)
if smooth > 1 and values.shape[0] > 0:
K = np.ones(smooth)
ones = np.ones(values.shape[0])
values = np.convolve(values, K, 'same') / np.convolve(ones, K, 'same')
return keys, values
def parse_log_files(
file_name_template,
key_name,
value_name,
num_seeds,
smooth,
best_k=None,
max_key=True
):
all_values = []
all_keys = []
actual_keys = None
for seed in range(1, num_seeds + 1):
file_name = file_name_template % seed
keys, values = read_log_file(file_name, key_name, value_name, smooth)
if keys is None or keys.shape[0] == 0:
continue
all_keys.append(keys)
all_values.append(values)
if len(all_values) == 0:
return None, None, None
all_keys_tmp = sorted(all_keys, key=lambda x: x[-1])
keys = all_keys_tmp[-1] if max_key else all_keys_tmp[0]
threshold = keys.shape[0]
# interpolate
for idx, (key, value) in enumerate(zip(all_keys, all_values)):
f = scipy.interpolate.interp1d(key, value, fill_value='extrapolate')
all_keys[idx] = keys
all_values[idx] = f(keys)
means, half_stds = [], []
for i in range(threshold):
vals = []
for v in all_values:
if i < v.shape[0]:
vals.append(v[i])
if best_k is not None:
vals = sorted(vals)[-best_k:]
means.append(np.mean(vals))
half_stds.append(0.5 * np.std(vals))
means = np.array(means)
half_stds = np.array(half_stds)
keys = all_keys[-1][:threshold]
assert means.shape[0] == keys.shape[0]
print(file_name_template, means[-1])
return keys, means, half_stds
# return all_keys, all_values
def print_result(
root,
title,
label=None,
num_seeds=1,
smooth=3,
train=False,
key_name='step',
value_name='episode_reward',
max_time=None,
best_k=None,
timescale=1,
max_key=False
):
file_name = 'train.log' if train else 'eval.log'
file_name_template = os.path.join(root, 'seed_%d', file_name)
keys, means, half_stds = parse_log_files(
file_name_template,
key_name,
value_name,
num_seeds,
smooth=smooth,
best_k=best_k,
max_key=max_key
)
label = label or root.split('/')[-1]
if keys is None:
return
if max_time is not None:
idxs = np.where(keys <= max_time)
keys = keys[idxs]
means = means[idxs]
half_stds = half_stds[idxs]
keys *= timescale
plt.plot(keys, means, label=label)
plt.locator_params(nbins=10, axis='x')
plt.locator_params(nbins=10, axis='y')
plt.rcParams['figure.figsize'] = (10, 7)
plt.rcParams['figure.dpi'] = 100
plt.rcParams['font.size'] = 10
plt.subplots_adjust(left=0.165, right=0.99, bottom=0.16, top=0.95)
#plt.ylim(0, 1050)
plt.tight_layout()
plt.grid(alpha=0.8)
plt.title(title)
plt.fill_between(keys, means - half_stds, means + half_stds, alpha=0.2)
plt.legend(loc='lower right', prop={
'size': 6
}).get_frame().set_edgecolor('0.1')
plt.xlabel(key_name)
plt.ylabel(value_name)
def plot_seeds(
task,
exp_query,
root,
train=True,
smooth=3,
key_name='step',
value_name='episode_reward',
num_seeds=10
):
# root = os.path.join(root, task)
experiment = None
for exp in os.listdir(root):
if re.match(exp_query, exp):
experiment = os.path.join(root, exp)
break
if experiment is None:
return
file_name = 'train.log' if train else 'eval.log'
file_name_template = os.path.join(experiment, 'seed_%d', file_name)
plt.locator_params(nbins=10, axis='x')
plt.locator_params(nbins=10, axis='y')
plt.rcParams['figure.figsize'] = (10, 7)
plt.rcParams['figure.dpi'] = 100
plt.rcParams['font.size'] = 10
plt.subplots_adjust(left=0.165, right=0.99, bottom=0.16, top=0.95)
plt.grid(alpha=0.8)
plt.tight_layout()
plt.title(task)
plt.xlabel(key_name)
plt.ylabel(value_name)
for seed in range(1, num_seeds + 1):
file_name = file_name_template % seed
keys, values = read_log_file(file_name, key_name, value_name, smooth=smooth)
if keys is None or keys.shape[0] == 0:
continue
plt.plot(keys, values, label='seed_%d' % seed, linewidth=0.5)
plt.legend(loc='lower right', prop={
'size': 6
}).get_frame().set_edgecolor('0.1')
def print_baseline(task, baseline, data, color):
try:
value = data[task][baseline]
except:
return
plt.axhline(y=value, label=baseline, linestyle='--', color=color)
plt.legend(loc='lower right', prop={
'size': 6
}).get_frame().set_edgecolor('0.1')
def print_planet_baseline(
task, data, max_time=None, label='planet', color='black', offset=0
):
try:
keys, means, half_stds = data[task]
except:
return
if max_time is not None:
idx = np.searchsorted(keys, max_time)
keys = keys[:idx]
means = means[:idx]
half_stds = half_stds[:idx]
plt.plot(keys + offset, means, label=label, color=color)
plt.fill_between(
keys + offset,
means - half_stds,
means + half_stds,
alpha=0.2,
color=color
)
plt.legend(loc='lower right', prop={
'size': 6
}).get_frame().set_edgecolor('0.1')
def plot_experiment(
task,
exp_query,
neg_exp_query=None,
root='runs',
exp_ids=None,
smooth=3,
train=False,
key_name='step',
value_name='eval_episode_reward',
baselines_data=None,
num_seeds=10,
planet_data=None,
slac_data=None,
max_time=None,
best_k=None,
timescale=1,
max_key=False
):
root = os.path.join(root, task)
experiments = set()
for exp in os.listdir(root):
if re.match(exp_query, exp) and (neg_exp_query is None or re.match(neg_exp_query, exp) is None):
exp = os.path.join(root, exp)
experiments.add(exp)
exp_ids = list(range(len(experiments))) if exp_ids is None else exp_ids
for exp_id, exp in enumerate(sorted(experiments)):
if exp_id in exp_ids:
print_result(
exp,
task,
smooth=smooth,
num_seeds=num_seeds,
train=train,
key_name=key_name,
value_name=value_name,
max_time=max_time,
best_k=best_k,
timescale=timescale,
max_key=max_key
)
if baselines_data is not None:
print_baseline(task, 'd4pg_pixels', baselines_data, color='gray')
print_baseline(task, 'd4pg', baselines_data, color='black')
if planet_data is not None:
print_planet_baseline(
task,
planet_data,
max_time=max_time,
label='planet',
color='peru',
offset=5
)
if slac_data is not None:
action_repeat = {
'ball_in_cup_catch': 4,
'cartpole_swingup': 8,
'cheetah_run': 4,
'finger_spin': 2,
'walker_walk': 2,
'reacher_easy': 4
}
offset = 10 * action_repeat[task]
print_planet_baseline(
task,
slac_data,
max_time=max_time,
label='slac',
color='black',
offset=offset
)