-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_utils.py
More file actions
253 lines (198 loc) · 8.61 KB
/
plot_utils.py
File metadata and controls
253 lines (198 loc) · 8.61 KB
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
import seaborn as sns
import matplotlib.pyplot as plt
from run_icml import split_max_episodes
plt.rcParams['figure.dpi'] = 100
plt.rcParams['savefig.dpi'] = 100
import pandas as pd
sns.set_theme(rc={"figure.dpi":100, 'savefig.dpi':6000})
plt.style.use('seaborn-whitegrid')
CartPoleEpisodes = 6000
AcrobotEpisodes = 2000
MountainCarEpisodes = 5000
MountainCarContinuousEpisodes = 1000
LunarLanderEpisodes = 6000
PendulumEpisodes = 6000
PENDULUM_K = 25
MOUNTAINCARCONTINUOUS_K = 2
# envs
max_episodes_dict = {
"Acrobot-v1": AcrobotEpisodes,
"CartPole-v1": CartPoleEpisodes,
"MountainCar-v0": MountainCarEpisodes,
"MountainCarContinuous-v0": MountainCarContinuousEpisodes,
"Pendulum-v1": PendulumEpisodes,
"LunarLander-v2": LunarLanderEpisodes
}
old_splits_dict = {
"Acrobot-v1": False,
"CartPole-v1": False,
"MountainCar-v0": False,
"MountainCarContinuous-v0": False,
"Pendulum-v1": False,
"LunarLander-v2": False
}
def data_dict(methods, envs, seeds, folder="results"):
data = {}
for method in methods:
data[method] = {}
for env in envs:
data[method][env] = {}
for seed in seeds:
data[method][env][seed] = load_data(method, env, seed, folder=folder)
return data
def data_dict_icml(algos: list[str], envs: list[str], seeds: list[str]):
data = {}
for algo in algos:
data[algo] = {}
for env in envs:
data[algo][env] = {}
for seed in seeds:
data[algo][env][seed] = load_data_icml(env, algo, seed, max_episodes_dict[env], old_splits_dict[env])
return data
def load_data(method, env, seed, folder='results'):
path = f"{folder}/{method}/{env}/{method}_{seed}.csv"
data = pd.read_csv(path)
# add column for accumulated reward
data['accumulated reward'] = data['reward'].cumsum()
# add column for success rate
data['success rate'] = data['success'].cumsum() / (data.index + 1)
return data
def load_data_icml(env: str, algo: str, seed: int, max_episodes: int, old_split=False) -> pd.DataFrame:
policy_episodes_percent = 0.8 if old_split else 0.6
policy_episodes, experiment_episodes = split_max_episodes(max_episodes, policy_episode_percent=policy_episodes_percent)
if env == "MountainCarContinuous-v0":
path = f"results/icml/{env}/icml_{policy_episodes}_{MOUNTAINCARCONTINUOUS_K}_{algo}_{experiment_episodes}_{seed}.csv"
elif env == "Pendulum-v1":
path = f"results/icml/{env}/icml_{policy_episodes}_{PENDULUM_K}_{algo}_{experiment_episodes}_{seed}.csv"
else:
path = f"results/icml/{env}/icml_{policy_episodes}_{algo}_{experiment_episodes}_{seed}.csv"
data = pd.read_csv(path)
# add column for accumulated reward
data['accumulated reward'] = data['rewards'].cumsum()
# add column for success rate
data['success rate'] = data['success'].cumsum() / (data.index + 1)
return data
def create_plot_data(data, method, env, seeds, ax):
plot_data = pd.DataFrame()
for seed in seeds:
plot_data = pd.concat([plot_data, data[method][env][seed][[ax]]], axis=1)
# mean of accumulated reward
mean = plot_data.mean(axis=1)
std = plot_data.std(axis=1)
plot_data["mean"] = mean
plot_data["std"] = std
plot_data["episode"] = plot_data.index
return plot_data
def create_plot_data_icml(data, method, env, seeds, ax):
plot_data = pd.DataFrame()
for seed in seeds:
plot_data = pd.concat([plot_data, data[method][env][seed][[ax]]], axis=1)
# mean of accumulated reward
mean = plot_data.mean(axis=1)
std = plot_data.std(axis=1)
plot_data["mean"] = mean
plot_data["std"] = std
plot_data["episode"] = plot_data.index
return plot_data
def plot(data, methods, env, seeds, ax, p=plt, xPos=0):
for method in methods:
plot_data = create_plot_data(data, method, env, seeds, ax)
if method == "binQ":
method = "Discretization"
p.plot(plot_data["episode"], plot_data["mean"], label=method)
p.fill_between(plot_data["episode"], plot_data["mean"] - plot_data["std"], plot_data["mean"] + plot_data["std"], alpha=0.2)
# add legend
# p.legend()
# p.ticklabel_format(axis='both', style='scientific', scilimits=(-10,10))
# if plt is the default plt
if p == plt:
p.xlabel("Episode")
p.ylabel(ax[0].upper() + ax[1:])
p.title(f"{env}")
# if success rate, set y limit to [0,1]
if ax == "success rate":
p.ylim(0, 1)
else:
p.set_xlabel("Episode")
if xPos == 0:
p.set_ylabel(ax[0].upper() + ax[1:])
p.set_title(f"{env}")
# if success rate, set y limit to [0,1]
if ax == "success rate":
p.set_ylim(-0.1, 1)
def plot_icml(data, algos: list[str], env: str, seeds:int, ax: str, p=plt, xPos:int =0, colors: list[str]=["red"]):
for algo in algos:
plot_data = create_plot_data_icml(data, algo, env, seeds, ax)
p.plot(plot_data["episode"], plot_data["mean"], label="Demonstrator", color=colors[0])
p.fill_between(plot_data["episode"], plot_data["mean"] - plot_data["std"], plot_data["mean"] + plot_data["std"], alpha=0.2, color=colors[0])
# add legend
# p.ticklabel_format(axis='both', style='scientific', scilimits=(-10,10))
# if plt is the default plt
if p == plt:
p.xlabel("Episode")
p.ylabel(ax[0].upper() + ax[1:])
p.title(f"{env}")
# if success rate, set y limit to [0,1]
if ax == "success rate":
p.ylim(0, 1)
else:
p.set_xlabel("Episode")
if xPos == 0:
p.set_ylabel(ax[0].upper() + ax[1:])
p.set_title(f"{env}")
# if success rate, set y limit to [0,1]
if ax == "success rate":
p.set_ylim(-0.1, 1)
def create_plot_grid(data, methods, envs, seeds, ax, save_name=None):
fig, axs = plt.subplots(2, 3, figsize=(10, 5))
# add margin bewteen subplots
fig.subplots_adjust(hspace = 0.5, wspace=.25)
# add title to the whole plot
# fig.suptitle("Comparing abstraction methods on " + ax)
for i, env in enumerate(envs):
plot(data, methods, env, seeds, ax, axs[i // 3][i % 3], xPos=i%3)
handles, labels = axs[i // 3][i % 3].get_legend_handles_labels()
axs[1][1].legend(handles = handles , labels=labels,loc='upper center',
bbox_to_anchor=(0.5, -0.2),fancybox=False, shadow=False, ncol=3)
#save the plot as svg
if save_name is not None:
plt.savefig(f"images/{save_name}-{ax}.pdf", bbox_inches='tight', format='pdf')
else:
plt.savefig(f"images/{ax}.pdf", bbox_inches='tight', format='pdf')
plt.show()
def create_plot_grid_icml(data, algos: list[str], envs: list[str], seeds: list[int], ax: str, save_name="ppo"):
fig, axs = plt.subplots(2, 3, figsize=(10, 5))
# add margin bewteen subplots
fig.subplots_adjust(hspace = 0.5, wspace=.4)
# add title to the whole plot
# fig.suptitle("Comparing abstraction methods on " + ax)
for i, env in enumerate(envs):
plot_icml(data, algos, env, seeds, ax, axs[i // 3][i % 3], xPos=i%3)
handles, labels = axs[i // 3][i % 3].get_legend_handles_labels()
axs[1][1].legend(handles = handles , labels=labels,loc='upper center',
bbox_to_anchor=(0.5, -0.2),fancybox=False, shadow=False, ncol=3)
#save the plot as svg
if save_name is not None:
plt.savefig(f"images/{save_name}-{ax}.pdf", bbox_inches='tight', format='pdf')
else:
plt.savefig(f"images/{ax}.pdf", bbox_inches='tight', format='pdf')
plt.show()
def eval_grid_plot(data, data_icml, methods, methods_icml, envs, seeds, seeds_icml, ax, save_name=None):
fig, axs = plt.subplots(2, 3, figsize=(10, 5))
# add margin bewteen subplots
fig.subplots_adjust(hspace = 0.5, wspace=.25)
# add title to the whole plot
# fig.suptitle("Comparing abstraction methods on " + ax)
for i, env in enumerate(envs):
plot(data, methods, env, seeds, ax, axs[i // 3][i % 3], xPos=i%3)
for i, env in enumerate(envs):
plot_icml(data_icml, methods_icml, env, seeds_icml, ax, axs[i // 3][i % 3], xPos=i%3)
handles, labels = axs[i // 3][i % 3].get_legend_handles_labels()
axs[1][1].legend(handles = handles , labels=labels,loc='upper center',
bbox_to_anchor=(0.5, -0.2),fancybox=False, shadow=False, ncol=3)
#save the plot as svg
if save_name is not None:
plt.savefig(f"images/{save_name}-{ax}.pdf", bbox_inches='tight', format='pdf')
else:
plt.savefig(f"images/{ax}.pdf", bbox_inches='tight', format='pdf')
plt.show()