-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_curves.py
175 lines (149 loc) · 6.98 KB
/
plot_curves.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
# ruff: noqa: F403, F405
import os
import matplotlib.pyplot as plt
from matplotlib.ticker import FormatStrFormatter
import numpy as np
import seaborn as sns
import argparse
import importlib
import inspect
import sys
from src.plot_utils import *
sys.path.append("./configs/plots")
sns.set_context("paper")
# sns.set_style("whitegrid", {"legend.frameon": True})
sns.set_style("darkgrid", {"legend.frameon": True})
plt.rcParams["axes.axisbelow"] = False
# plt.rcParams["axes.grid"] = False
plt.rcParams["grid.linestyle"] = "--"
# plt.rcParams["font.family"] = "DejaVu Sans Mono"
plt.rcParams["font.family"] = "Bree Serif"
font_size = 12
plt.rcParams["font.size"] = font_size
# ------------------------------------------------------------------------------
# Note that this script tries to use the Bree Serif font. To install it, check
# where matplotlib fonts are saved by running
# from matplotlib.font_manager import findfont, FontProperties
# print(findfont(FontProperties(family=["sans-serif"])))
#
# Then, download Bree Serif and install it there.
#
# Finally, delete matplotlib cache. To find it, run
# matplotlib.get_cachedir()
# ------------------------------------------------------------------------------
def plot(folder):
n_seeds = 100
in_paper = [
"Gridworld-Easy-3x3-v0_mes50/iStatelessBinaryMonitor",
"Gridworld-Medium-3x3-v0_mes50/iStatelessBinaryMonitor",
"Gridworld-Medium-3x3-v0_mes50/iToySwitchMonitor",
"Gridworld-Medium-3x3-v0_mes50/iNMonitor_nm5",
"Gridworld-Medium-3x3-v0_mes50/iLimitedTimeMonitor",
"Gridworld-Medium-3x3-v0_mes50/iLimitedUseBonusMonitor_mb7",
"Gridworld-Easy-3x3-Stochastic-v0_mes50/iStatelessBinaryMonitor",
"Gridworld-Medium-3x3-Stochastic-v0_mes50/iStatelessBinaryMonitor",
"Gridworld-Medium-3x3-Stochastic-v0_mes50/iToySwitchMonitor",
"Gridworld-Medium-3x3-Stochastic-v0_mes50/iNMonitor_nm5",
"Gridworld-Medium-3x3-Stochastic-v0_mes50/iLimitedTimeMonitor",
"Gridworld-Medium-3x3-Stochastic-v0_mes50/iLimitedUseBonusMonitor_mb7",
]
max_algname_length = max([len(v) for v in alg_to_label.values()])
latex_table = {alg: "" for alg in alg_to_label.keys()}
for q0 in q_init_values:
for mon in mon_to_label.keys():
fig, axs = make_subplots(nrows=1, ncols=1, width_per_plot=2.6, height_per_plot=2)
axs = axs[0][0]
axs.set_prop_cycle(None)
nothing_to_plot = True
print(">>>", q0, mon)
for alg in alg_to_label.keys():
data_np = []
seeds_completed = 0
for seed in range(0, n_seeds):
try:
filename = os.path.join(folder, mon, q0, alg) + f"_test_{seed}.npy"
data_np.append(np.load(filename))
seeds_completed += 1
except Exception as e: # noqa: F841
print(f"{alg} missing seed {seed}")
pass
try:
data_np = np.stack(data_np)
except Exception as e:
print(e)
continue
error_shade_plot(
axs,
data_np,
testing_frequency,
smoothing_window=smoothing_window,
label=alg_to_label[alg],
lw=3,
)
# error_bar_plot(
# axs,
# data_np,
# testing_frequency,
# smoothing_window=smoothing_window,
# label=alg_to_label[alg],
# lw=1,
# elinewidth=1,
# capsize=1,
# )
nothing_to_plot = False
steps_converged = np.zeros(data_np.shape[0]) * np.nan
for j, d in enumerate(data_np):
for jj in range(len(d) - consecutive_steps_for_convergence):
try:
if np.all(d[jj:] >= mon_to_opt[mon]):
steps_converged[j] = jj * testing_frequency
break
except Exception as e: # noqa: F841
pass
n_converged = steps_converged.shape[0] - np.isnan(steps_converged).sum()
mean_steps = np.nanmean(steps_converged)
error_steps = 1.96 * np.nanstd(steps_converged) / np.sqrt(n_converged)
space_pad = max_algname_length - len(alg_to_label[alg])
stats_str = f"${mean_steps:,.0f} \pm {error_steps:,.0f}$ & {n_converged}/{seeds_completed}" # fmt: skip
stats_str = stats_str.replace("$nan \pm nan$", "---")
print(alg_to_label[alg], " " * space_pad, stats_str)
if mon in in_paper:
latex_table[alg] += " & " + stats_str
# axs.set_title(mon_to_label[mon], fontsize=font_size+4)
# axs.set_ylabel("Expected Return", fontsize=font_size)
axs.tick_params(axis="x", labelsize=font_size - 2, pad=-2)
axs.tick_params(axis="y", labelsize=font_size - 2, pad=y_tick_pad)
axs.yaxis.set_major_formatter(FormatStrFormatter("%.1f"))
if mon_to_yticks[mon] is not None:
axs.yaxis.set_ticks(mon_to_yticks[mon])
axs.set_ylim(mon_to_ylim[mon])
axs.ticklabel_format(style="sci", axis="x", scilimits=(3, 3))
if mon_to_xticks[mon] is not None:
axs.xaxis.set_ticks(mon_to_xticks[mon])
axs.set_xlim([0, mon_to_xlim[mon]])
xlabel = "Training Steps (1e3)" # depends on the format above
axs.set_xlabel(xlabel, fontsize=font_size, labelpad=-23, loc="right")
axs.xaxis.offsetText.set_visible(False) # hide the exp notation
if nothing_to_plot:
continue
plt.draw()
savepath = os.path.join(folder, savedir)
os.makedirs(savepath, exist_ok=True)
savename = os.path.join(mon, q0).replace("\\", "_").replace("/", "_")
savename = os.path.join(savepath, savename + ".png")
plt.savefig(savename, bbox_inches="tight", pad_inches=0, dpi=1500)
print()
print("-----\n")
for k, v in latex_table.items():
print(alg_to_label[k] + v + "\n" + "\\\\" + "\n" + "\\hline")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config")
parser.add_argument("-f", "--folder")
args = parser.parse_args()
# https://stackoverflow.com/a/77350187/754136
# inject config variables into the global namespace
cfgmod = importlib.import_module(inspect.getmodulename(args.config))
dicts = {k: v for k, v in inspect.getmembers(cfgmod) if not k.startswith("_")}
globals().update(**dicts)
plot(args.folder)