-
Notifications
You must be signed in to change notification settings - Fork 2
/
plotting.py
283 lines (223 loc) · 12.1 KB
/
plotting.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
import numpy as np
import matplotlib.pyplot as plt
import csv
import os
# Define a custom color palette for the lines
color_palette = ['#FF5733', '#FFC300', '#33FF57', '#3399FF', '#A63B33']
# Define the font size for titles
title_fontsize = 20
legend_fontsize = 18
from scipy import stats
from scipy.ndimage.filters import uniform_filter1d
main_dir = '/results'
def read_results(exp, alg):
success = []
dist = []
n_runs = 0
dir = main_dir + exp + '/' + alg + '/'
for file in os.listdir(dir):
if file.startswith(exp + "_" + alg) and file.endswith(".csv"):
frames = []
s = []
d = []
n_runs += 1
file = dir + file
with open(file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
frames.append(int(row[0]))
s.append(float(row[1]))
d.append(float(row[2]))
line_count += 1
print(f'Processed {line_count} lines.')
success.append(s)
dist.append(d)
success = np.array(success)
dist = np.array(dist)
return frames, np.mean(success, axis=0), np.std(success, axis=0)
def plot(exp, n_runs, ax, window=30, mode='nearest', label='exp'):
if exp == 'AntMaze':
alg = ['star', 'star', 'hrac', 'hiro', 'lesson']
else:
alg = ['star', 'hrac', 'hiro', 'lesson']
r = dict()
v = dict()
for a in alg:
frames, r[a], v[a] = read_results(exp, a)
# Plot star
ax.plot(frames, uniform_filter1d(r['star'], size=window, mode=mode),'tab:orange', label='STAR')
ax.fill_between(frames, uniform_filter1d(r['star'] + v['star'] / 2, size=window, mode=mode),
uniform_filter1d(r['star'] - v['star'] / 2, size=window, mode=mode), facecolor='tab:orange', alpha=0.2)
# Plot starold
if exp == 'AntMaze':
ax.plot(frames, uniform_filter1d(r['star'], size=window, mode=mode), 'tab:red', label='star')
ax.fill_between(frames, uniform_filter1d(r['star'] + v['star'] / 2, size=window, mode=mode),
uniform_filter1d(r['star'] - v['star'] / 2, size=window, mode=mode), facecolor='tab:red',
alpha=0.2)
# Plot hrac
ax.plot(frames, uniform_filter1d(r['hrac'], size=window, mode=mode), 'tab:green', label='HRAC')
ax.fill_between(frames, uniform_filter1d(r['hrac'] + v['hrac'] / 2, size=window, mode=mode),
uniform_filter1d(r['hrac'] - v['hrac'] / 2, size=window, mode=mode), facecolor='tab:green', alpha=0.2)
# Plot hiro
ax.plot(frames, uniform_filter1d(r['hiro'], size=window, mode=mode), 'tab:blue', label='HIRO')
ax.fill_between(frames, uniform_filter1d(r['hiro'] + v['hiro'] / 2, size=window, mode=mode),
uniform_filter1d(r['hiro'] - v['hiro'] / 2, size=window, mode=mode), facecolor='tab:blue', alpha=0.2)
# Plot lesson
ax.plot(frames, uniform_filter1d(r['lesson'], size=window, mode=mode), 'tab:brown', label='LESSON')
ax.fill_between(frames, uniform_filter1d(r['lesson'] + v['lesson'] / 2, size=window, mode=mode),
uniform_filter1d(r['lesson'] - v['lesson'] / 2, size=window, mode=mode), facecolor='tab:brown', alpha=0.2)
return ax
# Create a new figure and specify the number of rows and columns
fig, axes = plt.subplots(1, 3, figsize=(18, 4))
plt.subplots_adjust(wspace=0.4) # Adjust the horizontal space between subplots
for ax in axes:
# Add a faint grid behind the graphs
ax.grid(color='gray', linestyle='--', linewidth=0.5, alpha=0.5)
# Initialize legend labels
legend_labels = []
alg_legend_labels = [] # List to store legend labels for algorithms
experiments = ['AntMaze', 'AntFall', 'AntMazeCam']
# Call plot function for each experiment and customize the colors
for i, exp_name in enumerate(experiments):
ax = axes[i]
label = exp_name
ax = plot(exp_name, 3 , ax, window=30, mode='nearest', label=label)
legend_labels.append(label)
# Add axis labels to each subplot
ax.set_xlabel("Timesteps")
ax.set_ylabel("Average success rate")
# Set the experiment name as the title for each graph
ax.set_title(exp_name, fontsize=title_fontsize)
# plt.legend(loc="lower right", bbox_to_anchor=(1., 1.02), borderaxespad=0., ncol=5)
# Collect legend labels for algorithms from each subplot
legend = ax.get_legend()
if legend:
alg_legend_labels.extend(legend.get_texts())
# fig.legend(loc='lower center', bbox_to_anchor=(1., 1.02), borderaxespad=0., ncol=5)
# alg_legend_labels = ['gara', 'garaold', 'hrac', 'hiro', 'lesson']
# Create a custom legend for algorithms outside of subplots
alg_legend = fig.legend(alg_legend_labels, loc="upper left", bbox_to_anchor=(0.13, 0.87), fontsize=legend_fontsize,
title='Algorithms')
# Add the algorithm legend back to the figure
fig.add_artist(alg_legend)
plt.show()
def comparison_plot():
window = 20
mode = 'nearest'
experiments = ['AntMaze', 'AntFall', 'AntMazeCam']
# Create a new figure and specify the number of rows and columns
plt.subplots(1, 3, figsize=(18, 4))
plt.subplots_adjust(hspace=0.5) # Adjust the vertical space between subplots
exp = 'AntMaze'
a = ['star', 'gara', 'hrac', 'hiro', 'lesson']
r = dict()
v = dict()
frames, r[a[0]], v[a[0]] = read_results(exp, a[0])
frames, r[a[1]], v[a[1]] = read_results(exp, a[1])
frames, r[a[2]], v[a[2]] = read_results(exp, a[2])
frames, r[a[3]], v[a[3]] = read_results(exp, a[3])
frames, r[a[4]], v[a[4]] = read_results(exp, a[4])
plt.subplot(1, 3, 1)
# Plot star
plt.plot(frames, uniform_filter1d(r['star'], size=window, mode=mode), 'tab:orange', label='STAR')
plt.fill_between(frames, uniform_filter1d(r['star'] + v['star'] / 2, size=window, mode=mode),
uniform_filter1d(r['star'] - v['star'] / 2, size=window, mode=mode), facecolor='tab:orange',
alpha=0.2)
plt.plot(frames, uniform_filter1d(r['gara'], size=window, mode=mode), 'tab:red', label='GARA')
plt.fill_between(frames, uniform_filter1d(r['gara'] + v['gara'] / 2, size=window, mode=mode),
uniform_filter1d(r['gara'] - v['gara'] / 2, size=window, mode=mode), facecolor='tab:red',
alpha=0.2)
# Plot hrac
plt.plot(frames, uniform_filter1d(r['hrac'], size=window, mode=mode), 'tab:green', label='HRAC')
plt.fill_between(frames, uniform_filter1d(r['hrac'] + v['hrac'] / 2, size=window, mode=mode),
uniform_filter1d(r['hrac'] - v['hrac'] / 2, size=window, mode=mode), facecolor='tab:green',
alpha=0.2)
# Plot hiro
plt.plot(frames, uniform_filter1d(r['hiro'], size=window, mode=mode), 'tab:blue', label='HIRO')
plt.fill_between(frames, uniform_filter1d(r['hiro'] + v['hiro'] / 2, size=window, mode=mode),
uniform_filter1d(r['hiro'] - v['hiro'] / 2, size=window, mode=mode), facecolor='tab:blue',
alpha=0.2)
# Plot lesson
plt.plot(frames, uniform_filter1d(r['lesson'], size=window, mode=mode), 'tab:brown', label='LESSON')
plt.fill_between(frames, uniform_filter1d(r['lesson'] + v['lesson'] / 2, size=window, mode=mode),
uniform_filter1d(r['lesson'] - v['lesson'] / 2, size=window, mode=mode), facecolor='tab:brown',
alpha=0.2)
plt.title(exp, fontsize=title_fontsize)
plt.grid(linestyle='--', alpha=0.5) # Add a faint grid
plt.xlabel("Timesteps", fontsize=15)
plt.ylabel("Average success rate", fontsize=15)
# Manually create a combined legend for the entire figure
handles, labels = [], []
for ax in plt.gcf().get_axes():
h, l = ax.get_legend_handles_labels()
handles.extend(h)
labels.extend(l)
exp = 'AntFall'
alg = ['star', 'hrac', 'hiro', 'lesson']
r = dict()
v = dict()
for a in alg:
frames, r[a], v[a] = read_results(exp, a)
plt.subplot(1, 3, 2)
# Plot star
plt.plot(frames, uniform_filter1d(r['star'], size=window, mode=mode), 'tab:orange', label='STAR')
plt.fill_between(frames, uniform_filter1d(r['star'] + v['star']/2, size=window, mode=mode),
uniform_filter1d(r['star'] - v['star']/2, size=window, mode=mode), facecolor='tab:orange', alpha=0.2)
# Plot hrac
plt.plot(frames, uniform_filter1d(r['hrac'], size=window, mode=mode), 'tab:green', label='HRAC')
plt.fill_between(frames, uniform_filter1d(r['hrac'] + v['hrac']/2, size=window, mode=mode),
uniform_filter1d(r['hrac'] - v['hrac']/2, size=window, mode=mode), facecolor='tab:green', alpha=0.2)
# Plot hiro
plt.plot(frames, uniform_filter1d(r['hiro'], size=window, mode=mode), 'tab:blue', label='HIRO')
plt.fill_between(frames, uniform_filter1d(r['hiro'] + v['hiro']/2, size=window, mode=mode),
uniform_filter1d(r['hiro'] - v['hiro']/2, size=window, mode=mode), facecolor='tab:blue', alpha=0.2)
# Plot lesson
plt.plot(frames, uniform_filter1d(r['lesson'], size=window, mode=mode), 'tab:brown', label='LESSON')
plt.fill_between(frames, uniform_filter1d(r['lesson'] + v['lesson']/2, size=window, mode=mode),
uniform_filter1d(r['lesson'] - v['lesson']/2, size=window, mode=mode), facecolor='tab:brown', alpha=0.2)
plt.title(exp, fontsize=title_fontsize)
plt.grid(linestyle='--', alpha=0.5) # Add a faint grid
plt.xlabel("Timesteps", fontsize = 15)
plt.ylabel("Average success rate", fontsize = 15)
exp = 'AntMazeCam'
alg = ['star', 'hrac', 'hiro', 'lesson']
r = dict()
v = dict()
for a in alg:
frames, r[a], v[a] = read_results(exp, a)
frames, r[alg[0]], v[alg[0]] = read_results(exp, alg[0])
frames, r[alg[2]], v[alg[2]] = read_results(exp, alg[2])
plt.subplot(1, 3, 3)
# Plot star
plt.plot(frames, uniform_filter1d(r['star'], size=window, mode=mode), 'tab:orange', label='STAR')
plt.fill_between(frames, uniform_filter1d(r['star'] + v['star']/2, size=window, mode=mode),
uniform_filter1d(r['star'] - v['star']/2, size=window, mode=mode), facecolor='tab:orange', alpha=0.2)
# Plot hrac
plt.plot(frames, uniform_filter1d(r['hrac'], size=window, mode=mode), 'tab:green', label='HRAC')
plt.fill_between(frames, uniform_filter1d(r['hrac'] + v['hrac']/2, size=window, mode=mode),
uniform_filter1d(r['hrac'] - v['hrac']/2, size=window, mode=mode), facecolor='tab:green', alpha=0.2)
# Plot hiro
plt.plot(frames, uniform_filter1d(r['hiro'], size=window, mode=mode), 'tab:blue', label='HIRO')
plt.fill_between(frames, uniform_filter1d(r['hiro'] + v['hiro']/2, size=window, mode=mode),
uniform_filter1d(r['hiro'] - v['hiro']/2, size=window, mode=mode), facecolor='tab:blue', alpha=0.2)
# Plot lesson
plt.plot(frames, uniform_filter1d(r['lesson'], size=window, mode=mode), 'tab:brown', label='LESSON')
plt.fill_between(frames, uniform_filter1d(r['lesson'] + v['lesson']/2, size=window, mode=mode),
uniform_filter1d(r['lesson'] - v['lesson']/2, size=window, mode=mode), facecolor='tab:brown', alpha=0.2)
plt.title(exp, fontsize=title_fontsize)
plt.grid(linestyle='--', alpha=0.5) # Add a faint grid
plt.xlabel("Timesteps", fontsize = 15)
plt.ylabel("Average success rate", fontsize = 15)
# Position the combined legend at the "lower center"
plt.figlegend(handles, labels, loc='lower center', bbox_to_anchor=(0.5, -0.15), ncol=5, fontsize=legend_fontsize) # Increase the legend's font size
# Display the figure
plt.tight_layout() # Adjust subplot layout for better spacing
# plt.show()
fname = "./comparison10runs.png"
plt.savefig(fname, dpi=300, bbox_inches='tight')
comparison_plot()