-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvisual_regression_tests.py
364 lines (313 loc) · 13.1 KB
/
visual_regression_tests.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import argparse
import functools
import os
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Tuple
from typing import TYPE_CHECKING
from typing import TypeVar
import warnings
from jinja2 import Environment
from jinja2 import FileSystemLoader
import matplotlib.pylab as plt
import optuna
from optuna import Study
from optuna.exceptions import ExperimentalWarning
from optuna.importance import PedAnovaImportanceEvaluator
import optuna.visualization as plotly_visualization
import optuna.visualization.matplotlib as matplotlib_visualization
from studies import create_intermediate_value_studies
from studies import create_multi_objective_studies
from studies import create_multiple_single_objective_studies
from studies import create_pytorch_study
from studies import create_single_objective_studies
from studies import create_single_objective_studies_for_timeline
from studies import StudiesType
FigureType = TypeVar("FigureType")
if TYPE_CHECKING:
from matplotlib.axes import Axes
import plotly.graph_objs as go
warnings.filterwarnings("ignore", category=ExperimentalWarning)
optuna.logging.set_verbosity(optuna.logging.ERROR)
parser = argparse.ArgumentParser()
parser.add_argument("--output-dir", help="output directory (default: %(default)s)", default="tmp")
parser.add_argument("--width", help="plot width (default: %(default)s)", type=int, default=800)
parser.add_argument("--height", help="plot height (default: %(default)s)", type=int, default=600)
parser.add_argument("--heavy", help="create studies that takes long time", action="store_true")
args = parser.parse_args()
template_dirs = [os.path.join(os.path.dirname(os.path.abspath(__file__)), "templates")]
abs_output_dir = os.path.abspath(args.output_dir)
dpi = 100
plt.rcParams["figure.figsize"] = (args.width / dpi, args.height / dpi)
def wrap_plot_func(
plot_func: Callable[[Study], FigureType]
) -> Callable[[StudiesType], FigureType]:
@functools.wraps(plot_func)
def wrapper(studies: StudiesType) -> FigureType:
assert isinstance(studies, Study)
return plot_func(studies)
return wrapper
def stringify_plot_kwargs(plot_kwargs: Dict[str, Any]) -> str:
items = []
for key, value in plot_kwargs.items():
items.append(f"{key}={value}")
return ",".join(items)
def generate_plot_files(
studies: List[Tuple[str, StudiesType]],
base_dir: str,
plotly_plot: Callable[[StudiesType], "go.Figure"],
matplotlib_plot: Callable[[StudiesType], "Axes"],
filename_prefix: str,
) -> List[Tuple[str, str, str]]:
files = []
for title, study in studies:
plotly_filename = f"{filename_prefix}-{title}-plotly.png"
plotly_filepath = os.path.join(base_dir, plotly_filename)
matplotlib_filename = f"{filename_prefix}-{title}-matplotlib.png"
matplotlib_filepath = os.path.join(base_dir, matplotlib_filename)
try:
plotly_fig = plotly_plot(study)
plotly_fig.update_layout(
width=args.width,
height=args.height,
margin={"l": 10, "r": 10},
)
plotly_fig.write_image(plotly_filepath)
except: # NOQA
plotly_filename = ""
try:
matplotlib_plot(study)
plt.savefig(matplotlib_filepath, bbox_inches="tight", dpi=dpi)
except: # NOQA
matplotlib_filename = ""
files.append((title, plotly_filename, matplotlib_filename))
return files
def generate_optimization_history_plots(
studies: List[Tuple[str, StudiesType]],
base_dir: str,
plot_kwargs: Dict[str, Any],
) -> List[Tuple[str, str, str]]:
filename_prefix = "history"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
lambda s: plotly_visualization.plot_optimization_history(s, **plot_kwargs),
lambda s: matplotlib_visualization.plot_optimization_history(s, **plot_kwargs),
filename_prefix=filename_prefix,
)
def generate_contour_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "contour"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_contour(s, **plot_kwargs)),
wrap_plot_func(lambda s: matplotlib_visualization.plot_contour(s, **plot_kwargs)),
filename_prefix=filename_prefix,
)
def generate_rank_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "rank"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_rank(s, **plot_kwargs)),
wrap_plot_func(lambda s: matplotlib_visualization.plot_rank(s, **plot_kwargs)),
filename_prefix=filename_prefix,
)
def generate_edf_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "edf"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
lambda s: plotly_visualization.plot_edf(s, **plot_kwargs),
lambda s: matplotlib_visualization.plot_edf(s, **plot_kwargs),
filename_prefix=filename_prefix,
)
def generate_slice_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "slice"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_slice(s, **plot_kwargs)),
wrap_plot_func(lambda s: matplotlib_visualization.plot_slice(s, **plot_kwargs)),
filename_prefix=filename_prefix,
)
def generate_param_importances_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "importance"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(
lambda s: plotly_visualization.plot_param_importances(
s, evaluator=PedAnovaImportanceEvaluator(), **plot_kwargs
)
),
wrap_plot_func(
lambda s: matplotlib_visualization.plot_param_importances(
s, evaluator=PedAnovaImportanceEvaluator(), **plot_kwargs
)
),
filename_prefix=filename_prefix,
)
def generate_parallel_coordinate_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "parcoords"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_parallel_coordinate(s, **plot_kwargs)),
wrap_plot_func(
lambda s: matplotlib_visualization.plot_parallel_coordinate(s, **plot_kwargs)
),
filename_prefix=filename_prefix,
)
def generate_intermediate_value_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "intermediate"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_intermediate_values(s, **plot_kwargs)),
wrap_plot_func(
lambda s: matplotlib_visualization.plot_intermediate_values(s, **plot_kwargs)
),
filename_prefix=filename_prefix,
)
def generate_pareto_front_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "pareto-front"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_pareto_front(s, **plot_kwargs)),
wrap_plot_func(lambda s: matplotlib_visualization.plot_pareto_front(s, **plot_kwargs)),
filename_prefix=filename_prefix,
)
def generate_timeline_plots(
studies: List[Tuple[str, StudiesType]], base_dir: str, plot_kwargs: Dict[str, Any]
) -> List[Tuple[str, str, str]]:
filename_prefix = "timeline"
if len(plot_kwargs) > 0:
filename_prefix = f"{filename_prefix}-{stringify_plot_kwargs(plot_kwargs)}"
return generate_plot_files(
studies,
base_dir,
wrap_plot_func(lambda s: plotly_visualization.plot_timeline(s, **plot_kwargs)),
wrap_plot_func(lambda s: matplotlib_visualization.plot_timeline(s, **plot_kwargs)),
filename_prefix=filename_prefix,
)
def main() -> None:
if not os.path.exists(abs_output_dir):
os.mkdir(abs_output_dir)
env = Environment(loader=FileSystemLoader(template_dirs))
plot_results_template = env.get_template("plot_results.html")
list_pages_template = env.get_template("list_pages.html")
print("Creating single objective studies")
single_objective_studies = create_single_objective_studies()
multiple_single_objective_studies = create_multiple_single_objective_studies()
print("Creating multi objective studies")
multi_objective_studies = create_multi_objective_studies()
print("Creating studies that have intermediate values")
intermediate_value_studies = create_intermediate_value_studies()
single_objective_studies_for_timeline = create_single_objective_studies_for_timeline()
if args.heavy:
print("Creating pytorch study")
pytorch_study = create_pytorch_study()
assert pytorch_study is not None
single_objective_studies.append((pytorch_study.study_name, pytorch_study))
intermediate_value_studies.insert(0, (pytorch_study.study_name, pytorch_study))
single_objective_studies_with_multi_studies: List[Tuple[str, StudiesType]] = [
*single_objective_studies,
*multiple_single_objective_studies,
]
pages: List[Tuple[str, str]] = []
for funcname, studies, generate, plot_kwargs in [
(
"plot_optimization_history",
single_objective_studies_with_multi_studies,
generate_optimization_history_plots,
{},
),
(
"plot_optimization_history",
multiple_single_objective_studies,
generate_optimization_history_plots,
{"error_bar": True},
),
("plot_slice", single_objective_studies, generate_slice_plots, {}),
("plot_contour", single_objective_studies, generate_contour_plots, {}),
("plot_rank", single_objective_studies, generate_rank_plots, {}),
(
"plot_parallel_coordinate",
single_objective_studies,
generate_parallel_coordinate_plots,
{},
),
(
"plot_intermediate_values",
intermediate_value_studies,
generate_intermediate_value_plots,
{},
),
("plot_pareto_front", multi_objective_studies, generate_pareto_front_plots, {}),
("plot_param_importances", single_objective_studies, generate_param_importances_plots, {}),
(
"plot_edf",
single_objective_studies_with_multi_studies,
generate_edf_plots,
{},
),
("plot_timeline", single_objective_studies_for_timeline, generate_timeline_plots, {}),
]:
assert isinstance(plot_kwargs, Dict)
plot_files = generate(studies, abs_output_dir, plot_kwargs)
plot_kwargs_str = stringify_plot_kwargs(plot_kwargs)
filename = (
f"{funcname}.html"
if len(plot_kwargs_str) == 0
else f"{funcname}-{plot_kwargs_str}.html"
)
with open(os.path.join(abs_output_dir, filename), "w") as f:
f.write(
plot_results_template.render(
funcname=f"{funcname}({plot_kwargs_str})", plot_files=plot_files
)
)
pages.append((f"{funcname}({plot_kwargs_str})", filename))
with open(os.path.join(abs_output_dir, "index.html"), "w") as f:
f.write(list_pages_template.render(pages=pages))
print("Generated to:", os.path.join(abs_output_dir, "index.html"))
if __name__ == "__main__":
main()