-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_output.py
252 lines (222 loc) · 6.87 KB
/
render_output.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
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
from statistics import median
import numpy as np
def index_axis(axes, idx):
shape = axes.shape
if len(shape) == 1:
return axes[idx]
else:
height, width = shape
return axes[idx // width, idx % width]
class OutputRenderer:
def __init__(self, baseline=0.0, metric="(Unspecified metric)", linemarker="o"):
self.x_values = [0.35, 2.70, 6.10, 16.10]
#self.box_color = "Pink"
self.baseline = baseline
self.metric = metric
self.linemarker = linemarker
def set_lim(self, ax=None, y_max=None):
if y_max is None:
y_max = 1
# mostly hardcoded based on our problem's specifications
x_lim = (-1, 17)
y_lim = (0, y_max)
if ax is None:
plt.xlim(*x_lim)
plt.ylim(*y_lim)
else:
ax.set_xlim(*x_lim)
ax.set_ylim(*y_lim)
def draw_box(self, ax, ys, box_color):
if type(ys[0]) != list:
return
solid_color = mcolors.to_rgb(box_color)
# a black version of the given color
black_ratio = 0.2
black = (0.0, 0.0, 0.0)
edge_color = tuple(
(1 - black_ratio) * np.array(solid_color)
+ black_ratio * np.array(black)
)
bplot = ax.boxplot(
ys,
positions=self.x_values,
widths=1,
manage_ticks=False,
patch_artist=True,
zorder=5,
medianprops=dict(
color=edge_color,
linewidth=2
),
whiskerprops=dict(
color=edge_color,
linewidth=2
),
capprops=dict(
color=edge_color,
linewidth=2
),
flierprops=dict(
markersize=5,
markeredgecolor=solid_color,
markerfacecolor=solid_color,
marker=".",
# the "x" marker is cursed, idky
# markeredgecolor=box_color,
# marker="x",
# linewidth=15,
),
boxprops=dict(
color=edge_color,
linewidth=2
),
)
for patch in bplot["boxes"]:
patch.set_facecolor(box_color)
def draw_line(self, ax, ys, label=None, color="b"):
medians = [
median(vals) if type(vals) == list
else vals
for vals in ys
]
PROP_REF = {
".": dict(
),
"o": dict(
markerfacecolor="none",
markeredgewidth=2,
markersize=8,
)
}
props = PROP_REF.get(self.linemarker, PROP_REF["o"])
line = ax.plot(
self.x_values,
medians,
marker=self.linemarker,
color=color,
linestyle="-",
**props,
# alpha=0.5,
zorder=8,
label=label,
)
return line
def draw_random_annotation(self, y_max=None):
if self.baseline is None:
return
plt.axhline(
y=self.baseline,
color="orange",
# alpha=0.5,
zorder=2,
)
if y_max is None:
y_max = 1
offset = (-0.1, 0.02 * y_max)
plt.text(
plt.xlim()[0] + offset[0],
self.baseline + offset[1],
"baseline\nmetric",
color="orange",
horizontalalignment="right",
)
def meta_info(self, title=None):
plt.xlabel("Parameters (in billions)")
plt.ylabel(self.metric)
plt.title(title or "Model Performance")
def draw_bands(self, ax, ys, color="b"):
if type(ys[0]) != list:
return
q1 = [np.percentile(val, 25) for val in ys]
q3 = [np.percentile(val, 75) for val in ys]
iqr = [b - a for a, b in zip(q1, q3)]
# print(q1, q3)
ax.fill_between(
self.x_values,
list(map(lambda x, y: x - 1.5 * y, q1, iqr)),
list(map(lambda x, y: x + 1.5 * y, q3, iqr)),
alpha=0.1,
zorder=5,
color=color,
)
ax.fill_between(
self.x_values,
q1,
q3,
alpha=0.2,
zorder=5,
color=color,
)
def render_lines(self, ax, y_lines):
colors = ["b", "g", "r", "c", "m", "y"]
lines = []
for idx, (key, ys) in enumerate(y_lines.items()):
color = colors[idx % len(colors)]
self.draw_bands(ax, ys, color=color)
line = self.draw_line(ax, ys, label=key, color=color)
lines += line
box_color = mcolors.to_rgb(color)
box_color += (0.3, )
self.draw_box(ax, ys, box_color)
return lines
def render(self, ys, y_max=None, save=None, title=None):
y_lines = ys
if not isinstance(y_lines, dict):
y_lines = { "unnamed": y_lines }
#for key, ys in y_lines.items():
# ys = [
# y if isinstance(y, list)
# else [y]
# for y in ys
# ]
# y_lines[key] = ys
fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
plt.grid(True)
self.set_lim(y_max=y_max)
self.meta_info(title=title)
self.draw_random_annotation(y_max=y_max)
self.render_lines(ax, y_lines)
plt.legend()
if save is not None:
# save must come before show
plt.savefig(save, bbox_inches="tight")
print("Saved figure to", save)
plt.show()
def render_multi(
self,
yss,
metrics,
subtitles,
dims,
title,
figsize=(8, 4),
save=None,
):
fig, axes = plt.subplots(*dims, figsize=figsize)
for idx, (ys, metric, subtitle) in enumerate(zip(yss, metrics, subtitles)):
ax = index_axis(axes, idx)
ax.set_xlabel("Parameters (billions)")
ax.set_ylabel(metric)
ax.set_title(subtitle)
ax.grid(True)
self.set_lim(ax)
lines = self.render_lines(ax, ys)
legend_keys = list(ys.keys())
fig.legend(
lines,
legend_keys,
loc="upper center",
bbox_to_anchor=(0.5,0.91),
ncol=len(legend_keys),
)
plt.suptitle(title, fontsize=20, fontweight="bold")
plt.tight_layout()
fig.subplots_adjust(top=0.76)
if save is not None:
# save must come before show
plt.savefig(save, bbox_inches="tight")
print("Saved figure to", save)
plt.show()