-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_new.py
152 lines (127 loc) · 4.99 KB
/
plot_new.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
import os, glob, numpy as np, pandas as pd
from collections import defaultdict
import matplotlib.pyplot as plt
from tensorboard.backend.event_processing import event_accumulator
# from pylab import rcParams, axes
SIZE_GUIDANCE = {
'compressedHistograms': 500,
'images': 4,
'audio': 4,
'scalars': 10000,
'histograms': 1,
}
STORE_EVERYTHING_SIZE_GUIDANCE = {
'compressedHistograms': 0,
'images': 0,
'audio': 0,
'scalars': 0,
'histograms': 0,
}
# params = {
# 'axes.labelsize': 28,
# 'axes.titlesize': 32,
# 'legend.fontsize': 14,
# 'xtick.labelsize': 'x-large',
# 'ytick.labelsize': 'x-large',
# 'text.usetex': True,
# 'figure.figsize': [14, 12]
# }
SMOOTH = 1000
CUT = 30000
def get_values(filename, scalar="Episodic_Reward"):
ea = event_accumulator.EventAccumulator(filename, size_guidance=STORE_EVERYTHING_SIZE_GUIDANCE)
ea.Reload()
ea_scalar = ea.Scalars(tag=scalar)
ea_scalar = pd.DataFrame(ea_scalar)
return ea_scalar
def smooth(y, box_pts):
box = np.ones(box_pts)/box_pts
y_smooth = np.convolve(y, box, mode='same')
# return np.array(pd.Series(y).rolling(box_pts).mean())
return y_smooth
def save_numpy(log_dir):
logs = glob.glob(os.path.join(log_dir, "*/logs/*"), recursive=True)
l = []
for event in logs:
l.append(event.split('/')[2].split('--'))
merged = defaultdict(lambda: [])
for i in l: merged['--'.join(i[:-1])].append('--'.join(i))
count = -1
for exp in merged:
print("================================\n"+exp+"\n================================\n")
vals = []
count+=1
for i in merged[exp]:
logs = glob.glob(os.path.join('./save', i, "logs/event*"), recursive=True)[0]
# save numpy arrays
logs_dir = glob.glob(os.path.join('./save', i, "logs"), recursive=True)[0]
with open(logs_dir+'/arr.npy', 'wb') as f:
np.save(f, get_values(logs)['value'].to_numpy())
def preprocess(log_dir):
params = {
'axes.labelsize': 28,
'axes.titlesize': 32,
'legend.fontsize': 14,
'xtick.labelsize': 'x-large',
'ytick.labelsize': 'x-large',
'text.usetex': True,
'figure.figsize': [10, 8]
}
from pylab import plot, rcParams, legend, axes, grid
rcParams.update(params)
logs = glob.glob(os.path.join(log_dir, "*/logs/*"), recursive=True)
# pprint.pprint(logs)
l = []
for event in logs:
l.append(event.split('/')[2].split('--'))
# pprint.pprint(l)
merged = defaultdict(lambda: [])
for i in l: merged['--'.join(i[:-1])].append('--'.join(i))
# pprint.pprint(merged)
# pprint.pprint(list(merged.keys()))
count = -1
colors = ['#006BB2', '#B22400', '#006BB2', '#B22400', '#006BB2', '#B22400']
labels = ['HAMMER', 'IL','HAMMER', 'IL','HAMMER', 'IL','HAMMER', 'IL']
for exp in merged:
if ('n_3' in exp) and not ('dru_1' in exp and 'meslen_0' in exp):
# if ('n_3' in exp) and ('dru_0' in exp):
print("================================\n"+exp+"\n================================\n")
vals = []
count+=1
for i in merged[exp]:
# if ('randomseed_5327' in i):
# if any(n in i for n in ['5327', '8651', '14712', '186',
# '9538', '106', '90300', '4973', '310', '530', '606']):
# if any(n in i for n in ['5327', '8651', '14712', '186',
# '9538', '106']):
print(i)
logs = glob.glob(os.path.join('./save', i, "logs/*.npy"), recursive=True)[0]
arr = np.load(logs)
vals.append(arr[:CUT])
# vals.append(smooth(arr[:CUT], box_pts=SMOOTH)[SMOOTH:CUT-SMOOTH])
# break
print(np.array(vals).shape)
val_means = np.array(vals).mean(axis=0)
val_stds = np.array(vals).std(axis=0)
plt.plot(val_means, label=exp)
# plt.plot(val_means, label=labels[count], color=colors[count])
plt.fill_between(np.arange(1, val_means.shape[0]+1),
val_means - val_stds,
val_means + val_stds,
alpha=0.1)
# break
rcParams.update(params)
legend = plt.legend(loc="lower right")
# legend = legend(loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=3, fancybox=True, shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.9')
frame.set_edgecolor('0.9')
plt.title('HAMMER')
plt.xlabel("Number of Episodes")
plt.ylabel("Average Returns per Agent")
plt.xlim((0, CUT-SMOOTH))
plt.grid()
plt.show()
if __name__ == '__main__':
save_numpy('./save')
# preprocess('./save')