generated from nabenabe0928/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathviz.py
310 lines (270 loc) · 9.58 KB
/
viz.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
import os
from typing import Any, List, Tuple
from eaf import (
get_empirical_attainment_surface,
EmpiricalAttainmentFuncPlot,
)
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import mark_inset, zoomed_inset_axes
import numpy as np
from viz.constants import (
BENCH_NAMES,
COLOR_LABEL_DICT,
COSTS_SHAPE,
DATASET_NAMES,
EAF_ANCHOR_DICT,
EAF_ASPECT_DICT,
EAF_INSET_XLIM_DICT,
EAF_INSET_YLIM_DICT,
EAF_ZOOM_DICT,
LARGER_IS_BETTER_DICT,
LEVELS,
LINESTYLES_DICT,
LOGSCALE_DICT,
MARKER_DICT,
N_SAMPLES,
NAME_DICT,
OBJ_LABEL_DICT,
OBJ_NAMES_DICT,
TICK_PARAMS,
)
from viz.utils import get_costs, get_true_pareto_front_and_ref_point
plt.rcParams["font.family"] = "Times New Roman"
plt.rcParams["font.size"] = 18
plt.rcParams["mathtext.fontset"] = "stix" # The setting of math font
PLOT_CHECK_MODE = False
HV_MODE = False
def disable_axis_label(
ax: plt.Axes,
set_xlabel: bool = False,
set_ylabel: bool = False,
) -> None:
if not set_xlabel:
ax.set_xlabel("")
if not set_ylabel:
ax.set_ylabel("")
def add_inset_axis_for_eaf(
ax: plt.Axes,
dataset_name: str,
eaf_plot: EmpiricalAttainmentFuncPlot,
surfs_list: List[np.ndarray],
**plot_kwargs,
) -> None:
axins = zoomed_inset_axes(
ax,
zoom=EAF_ZOOM_DICT[dataset_name],
bbox_to_anchor=EAF_ANCHOR_DICT[dataset_name],
bbox_transform=ax.transAxes,
loc="upper right",
borderpad=0.1,
axes_kwargs=dict(aspect=EAF_ASPECT_DICT[dataset_name]),
)
eaf_plot.plot_multiple_surface_with_band(axins, surfs_list=surfs_list, **plot_kwargs)
label = "True Pareto front"
eaf_plot.plot_true_pareto_surface(axins, color="black", label=label, linestyle="--", marker="*", alpha=0.2)
axins.set_xlim(EAF_INSET_XLIM_DICT[dataset_name])
axins.set_ylim(EAF_INSET_YLIM_DICT[dataset_name])
axins.grid(which="minor", color="gray", linestyle=":")
axins.grid(which="major", color="black")
axins.tick_params(**TICK_PARAMS)
axins.tick_params(axis='y', which='major', labelsize=1)
axins.tick_params(axis='y', which='minor', labelsize=1)
disable_axis_label(axins)
mark_inset(ax, axins, loc1=2, loc2=4, fc="gray", ec="red", alpha=0.2)
def plot_eaf(
ax: plt.Axes,
eaf_plot: EmpiricalAttainmentFuncPlot,
obj_names: List[str],
dataset_name: str,
set_xlabel: bool,
set_ylabel: bool,
**kwargs
) -> Tuple[List[Any], List[str]]:
surfs_list, colors, labels, markers, linestyles = [], [], [], [], []
for opt_name, color_label in COLOR_LABEL_DICT.items():
color, label = color_label
colors.append(color)
labels.append(label)
linestyles.append(LINESTYLES_DICT[opt_name])
markers.append(MARKER_DICT[opt_name])
costs = get_costs(obj_names, dataset_name, opt_name)
surfs = get_empirical_attainment_surface(costs.copy(), levels=LEVELS, **kwargs)
surfs_list.append(surfs)
else:
plot_kwargs = dict(
colors=colors, labels=labels, linestyles=linestyles, markers=markers, markersize=3, alpha=0.5
)
lines = eaf_plot.plot_multiple_surface_with_band(ax, surfs_list=surfs_list, **plot_kwargs)
label = "True Pareto front"
lines.append(eaf_plot.plot_true_pareto_surface(
ax, color="black", label=label, linestyle="--", marker="*", alpha=0.2
))
labels.append(label)
if not dataset_name.endswith("_en"):
add_inset_axis_for_eaf(ax, dataset_name, eaf_plot, surfs_list, **plot_kwargs)
if set_xlabel:
ax.set_xlabel(OBJ_LABEL_DICT[obj_names[0]])
if set_ylabel:
ax.set_ylabel(OBJ_LABEL_DICT[obj_names[1]])
return lines, labels
def add_inset_axis_for_hv(
ax: plt.Axes,
lines: List[Any],
eaf_plot: EmpiricalAttainmentFuncPlot,
costs_array: np.ndarray,
log: bool,
**plot_kwargs,
) -> None:
x_min = 20
y_max = max(line._y[-1] for line in lines if hasattr(line, "_y"))
ylim = ax.get_ylim()
axins = zoomed_inset_axes(
ax,
zoom=2.5,
bbox_to_anchor=(1.76, 0.01),
bbox_transform=ax.transAxes,
loc="lower right",
borderpad=0.1,
axes_kwargs=dict(aspect=200/(ylim[1] - ylim[0])),
)
eaf_plot.plot_multiple_hypervolume2d_with_band(axins, costs_array, log=log, **plot_kwargs)
axins.set_xlim(x_min, 100)
axins.set_ylim(y_max - 0.04, y_max + 0.005)
axins.tick_params(**TICK_PARAMS)
disable_axis_label(axins)
mark_inset(ax, axins, loc1=3, loc2=1, fc="gray", ec="red", alpha=0.2)
def plot_hv(
ax: plt.Axes,
eaf_plot: EmpiricalAttainmentFuncPlot,
obj_names: List[str],
dataset_name: str,
log: bool,
set_xlabel: bool,
set_ylabel: bool,
**kwargs
) -> Tuple[List[Any], List[str]]:
n_opts = len(COLOR_LABEL_DICT)
costs_array, colors, labels, markers, linestyles = np.empty((n_opts, *COSTS_SHAPE)), [], [], [], []
for idx, (opt_name, color_label) in enumerate(COLOR_LABEL_DICT.items()):
color, label = color_label
colors.append(color)
labels.append(label)
markers.append(MARKER_DICT[opt_name])
linestyles.append(LINESTYLES_DICT[opt_name])
costs_array[idx] = get_costs(obj_names, dataset_name, opt_name)
else:
label = "True Pareto front"
plot_kwargs = dict(colors=colors, labels=labels, markers=markers, markevery=5, linestyles=linestyles)
lines = eaf_plot.plot_multiple_hypervolume2d_with_band(ax, costs_array, log=log, **plot_kwargs)
lines.append(
eaf_plot.plot_true_pareto_surface_hypervolume2d(
ax, n_observations=N_SAMPLES, color="black", label=label, linestyle="--"
)
)
labels.append(label)
ax.set_ylim(ymin=0.78, ymax=1.01)
if not dataset_name.endswith("en"):
add_inset_axis_for_hv(ax, lines, eaf_plot, costs_array, log, **plot_kwargs)
disable_axis_label(ax, set_xlabel=set_xlabel, set_ylabel=set_ylabel)
return lines, labels
def plot(
ax: plt.Axes,
bench_id: int,
data_id: int,
hv_mode: bool,
set_xlabel: bool,
set_ylabel: bool,
) -> List[Any]:
bench_name = BENCH_NAMES[bench_id]
dataset_name = DATASET_NAMES[bench_name][data_id]
obj_names = OBJ_NAMES_DICT[bench_name]
kwargs = dict(
larger_is_better_objectives=LARGER_IS_BETTER_DICT[bench_name],
log_scale=LOGSCALE_DICT[bench_name],
)
true_pf, ref_point = get_true_pareto_front_and_ref_point(obj_names, bench_name, dataset_name)
eaf_plot = EmpiricalAttainmentFuncPlot(true_pareto_sols=true_pf, ref_point=ref_point, **kwargs)
kwargs.update(set_xlabel=set_xlabel, set_ylabel=set_ylabel)
if hv_mode:
lines, labels = plot_hv(ax, eaf_plot, obj_names, dataset_name, log=False, **kwargs)
else:
lines, labels = plot_eaf(ax, eaf_plot, obj_names, dataset_name, **kwargs)
ax.set_title(NAME_DICT[dataset_name])
ax.grid(which="minor", color="gray", linestyle=":")
ax.grid(which="major", color="black")
return lines, labels
def plot_hv_for_hpolib(subplots_kwargs, legend_kwargs, hv_mode: bool) -> None:
_, axes = plt.subplots(**subplots_kwargs)
for data_id in range(4):
r, c = data_id // 2, data_id % 2
set_xlabel = r == 1
set_ylabel = c == 0
kwargs = dict(
set_xlabel=set_xlabel,
set_ylabel=set_ylabel,
bench_id=0,
data_id=data_id,
hv_mode=hv_mode,
)
lines, labels = plot(axes[r][c], **kwargs)
else:
axes[-1][0].legend(handles=lines, labels=labels, ncol=(len(labels) + 1) // 2, **legend_kwargs)
if hv_mode:
if PLOT_CHECK_MODE:
plt.show()
else:
plt.savefig("figs/hv2d-hpolib.pdf", bbox_inches='tight')
else:
if PLOT_CHECK_MODE:
plt.show()
else:
plt.savefig("figs/eaf-hpolib.pdf", bbox_inches='tight')
def plot_hv_for_nmt(subplots_kwargs, legend_kwargs, hv_mode: bool) -> None:
_, axes = plt.subplots(**subplots_kwargs)
for data_id in range(3):
set_ylabel = data_id == 0
kwargs = dict(
set_xlabel=True,
set_ylabel=set_ylabel,
bench_id=1,
data_id=data_id,
hv_mode=hv_mode,
)
lines, labels = plot(axes[data_id], **kwargs)
else:
axes[1].legend(handles=lines, labels=labels, ncol=(len(labels) + 1) // 2, **legend_kwargs)
if hv_mode:
if PLOT_CHECK_MODE:
plt.show()
else:
plt.savefig("figs/hv2d-nmt.pdf", bbox_inches='tight')
else:
if PLOT_CHECK_MODE:
plt.show()
else:
plt.savefig("figs/eaf-nmt.pdf", bbox_inches='tight')
if __name__ == "__main__":
os.makedirs("figs/", exist_ok=True)
subplots_kwargs = dict(
nrows=2,
ncols=2,
sharex=HV_MODE,
sharey=HV_MODE,
figsize=(20, 10),
gridspec_kw=dict(
wspace=0.03 if HV_MODE else 0.09,
hspace=0.125 if HV_MODE else 0.2,
)
)
legend_kwargs = dict(
loc='upper center',
fontsize=20,
bbox_to_anchor=(1.0, -0.16) if HV_MODE else (1.03, -0.16),
fancybox=False,
shadow=False,
)
plot_hv_for_hpolib(subplots_kwargs, legend_kwargs, hv_mode=HV_MODE)
subplots_kwargs.pop("nrows")
subplots_kwargs.update(ncols=3, figsize=(15, 3) if HV_MODE else (20, 3.5))
legend_kwargs.update(bbox_to_anchor=(0.5, -0.3) if HV_MODE else (0.5, -0.22), fontsize=18)
plot_hv_for_nmt(subplots_kwargs, legend_kwargs, hv_mode=HV_MODE)