-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_values.py
More file actions
154 lines (128 loc) · 4.64 KB
/
Copy pathplot_values.py
File metadata and controls
154 lines (128 loc) · 4.64 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
"""
Plot the trajectories of an objective function in the value space.
Usage:
plot_values <objective>
Arguments:
<objective> The key of the objective function.
Options:
-h --help Show this screen.
"""
import json
import matplotlib.pyplot as plt
import numpy as np
from docopt import docopt
from trajectories.constants import (
AGGREGATOR_ORDER,
AGGREGATORS,
LATEX_NAMES,
OBJECTIVES,
PLOT_VALUES_LIMS,
)
from trajectories.objectives import WithSPSMappingMixin
from trajectories.pareto_utils import compute_normalized_2d_pf_distances, sample_2d_pf
from trajectories.paths import RESULTS_DIR, get_value_plots_dir, get_values_dir
from trajectories.plotters import (
ContentLimAdjuster,
HeatmapPlotter,
LimAdjuster,
MultiTrajPlotter,
PFPlotter,
SquareBoxAspectSetter,
TitleSetter,
XAxisLabeller,
XTicksClearer,
YAxisLabeller,
YTicksClearer,
)
from trajectories.plotting_utils import (
compute_subplot_layout,
get_subplot_position,
get_unused_subplot_positions,
map_orders_to_indices,
)
def main():
print("Plotting in value space...")
arguments = docopt(__doc__)
objective_key = arguments["<objective>"]
# Read metadata.json
with open(RESULTS_DIR / objective_key / "metadata.json", "r") as f:
metadata = json.load(f)
values_dir = get_values_dir(objective_key)
value_plots_dir = get_value_plots_dir(objective_key)
value_plots_dir.mkdir(parents=True, exist_ok=True)
# This seems to be the only way to make the font be Type1, which is the only font type supported
# by ICML.
plt.rcParams.update({"text.usetex": True})
objective_key = metadata["objective_key"]
objective = OBJECTIVES[objective_key]
if objective.n_values != 2:
raise ValueError("Can only plot values trajectories for objectives with 2 values.")
common_plotter = SquareBoxAspectSetter()
aggregator_keys = metadata["aggregator_keys"]
aggregator_to_Y = {key: np.load(values_dir / f"{key}.npy") for key in aggregator_keys}
# The content to which the axes must be adjusted
first_agg_Y = list(aggregator_to_Y.values())[0]
initial_values = first_agg_Y[:, 0, :]
main_content = initial_values
if isinstance(objective, WithSPSMappingMixin):
pf_points_array = sample_2d_pf(objective).numpy()
common_plotter += PFPlotter(pf_points_array)
main_content = np.concatenate([main_content, pf_points_array])
if objective_key in PLOT_VALUES_LIMS:
lims = PLOT_VALUES_LIMS[objective_key]
xlim = lims["xlim"]
ylim = lims["ylim"]
common_plotter += LimAdjuster(xlim=xlim, ylim=ylim)
else:
adjust_plotter = ContentLimAdjuster(main_content)
common_plotter += adjust_plotter
xlim = adjust_plotter.xlim
ylim = adjust_plotter.ylim
if isinstance(objective, WithSPSMappingMixin):
distances = compute_normalized_2d_pf_distances(
objective,
y0_min=xlim[0],
y0_max=xlim[1],
y1_min=ylim[0],
y1_max=ylim[1],
n=200,
)
common_plotter += HeatmapPlotter(
values=distances.numpy(),
x_min=xlim[0],
x_max=xlim[1],
y_min=ylim[0],
y_max=ylim[1],
vmin=0,
vmax=1,
cmap="Reds",
)
n_aggregators = len(aggregator_keys)
n_rows, n_cols = compute_subplot_layout(n_aggregators)
key_to_index = map_orders_to_indices(aggregator_keys, AGGREGATOR_ORDER)
fig, axes = plt.subplots(n_rows, n_cols, figsize=(n_cols * 2, n_rows * 2.5))
# Ensure axes is always 2D
if n_rows == n_cols == 1:
axes = np.array([[axes]])
elif n_rows == 1:
axes = axes.reshape(1, -1)
# Hide unused subplots
unused_positions = get_unused_subplot_positions(n_aggregators, n_rows, n_cols)
for i, j in unused_positions:
axes[i][j].axis("off")
save_path = value_plots_dir / "all.pdf"
for aggregator_key, Y in aggregator_to_Y.items():
aggregator = AGGREGATORS[aggregator_key]
print(aggregator)
index = key_to_index[aggregator_key]
i, j = get_subplot_position(index, n_aggregators, n_rows, n_cols)
plotter = common_plotter + MultiTrajPlotter(Y) + TitleSetter(LATEX_NAMES[aggregator_key])
plotter += XAxisLabeller("Objective $1$") if i == n_rows - 1 else XTicksClearer()
plotter += YAxisLabeller("Objective $2$") if j == 0 else YTicksClearer()
plotter(axes[i][j])
fig.tight_layout(h_pad=-2.5)
print("Saving figure")
plt.savefig(save_path, bbox_inches="tight")
print()
if __name__ == "__main__":
main()