-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
534 lines (401 loc) · 15 KB
/
plot.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import argparse
import itertools
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import scipy
from sklearn import base, pipeline
import utils
sns.set_style("whitegrid")
plt.rcParams["xtick.major.pad"] = "1.5"
plt.rcParams["ytick.major.pad"] = "1.5"
SMALL_SIZE = 9
MEDIUM_SIZE = 9
BIGGER_SIZE = 10
plt.rc("font", size=SMALL_SIZE) # default text sizes
plt.rc("axes", titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc("axes", labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc("xtick", labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc("ytick", labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc("legend", fontsize=SMALL_SIZE) # legend fontsize
plt.rc("figure", titlesize=BIGGER_SIZE) # fontsize of the figure title
def set_labels(ax, config):
"""Adjust axis labels.
The *config* dict should contain:
xlabel -- the x-axis label (default None)
ylabel -- the y-axis label (default None)
"""
if config.get("xlabel") is not None:
ax.set_xlabel(config["xlabel"])
if config.get("ylabel") is not None:
ax.set_ylabel(config["ylabel"])
def set_ticks(ax, config):
"""Toggle tick labels.
The *config* dict should contain:
xticks -- enable x-axis ticks (default True)
yticks -- enable y-axis ticks (default True)
"""
if not config.get("xticks", True):
ax.set_xticklabels([])
if not config.get("yticks", True):
ax.set_yticklabels([])
def set_value_limit(ax, config):
"""Set the y-axis limits.
The same can be achieved with *set_ylim()*.
The *config* dict should contain:
vmin -- the minimal y-axis value (default automatic)
vmax -- the maximal y-axis value (default automatic)
"""
ax.dataLim.y0 = config.get("vmin", ax.dataLim.y0)
ax.dataLim.y1 = config.get("vmax", ax.dataLim.y1)
ax.autoscale_view()
def set_xlim(ax, config):
"""Set the x-axis limits.
The *config* dict should contain:
xlim -- a tuple of min and max x-axis values (default automatic)
"""
x0, x1 = config.get("xlim", (ax.dataLim.x0, ax.dataLim.x1))
ax.dataLim.x0, ax.dataLim.x1 = x0, x1
ax.autoscale_view()
def set_ylim(ax, config):
"""Set the y-axis limits.
The *config* dict should contain:
ylim -- a tuple of min and max y-axis values (default automatic)
"""
y0, y1 = config.get("ylim", (ax.dataLim.y0, ax.dataLim.y1))
ax.dataLim.y0, ax.dataLim.y1 = y0, y1
ax.autoscale_view()
def set_legend(ax, config):
"""Set the legend.
The *config* dict should contain:
legend -- If False do not show a legend, otherwise must be a dict
containing the key 'title' and optionally 'labels'
(default False)
"""
if config.get("legend", False) is False:
return
handles, labels = ax.get_legend_handles_labels()
config = config["legend"]
ax.legend(
handles=handles,
title=config.get("title"),
labels=config.get("labels", labels),
loc="center left",
bbox_to_anchor=(1.0, 0.5),
frameon=False,
)
def set_outline(ax, config):
"""Toggle a highlighted outline.
The *config* dict should contain:
outline -- toggle the outline (default False)
"""
if config.get("outline", False):
ax.patch.set(edgecolor="black", linestyle="--", linewidth=3)
def set_axis_formatter(ax):
def formatter(x):
return f"{x.get_text()}"
ax.set_yticklabels(label.get_text() for label in ax.get_yticklabels())
ax.set_xticklabels(list(map(formatter, ax.get_xticklabels())))
def dataset_2d(name, config):
"""Plot a 2D dataset.
The *config* dict should contain:
dataset -- dataset name
figsize -- matplotlib figsize (default automatic)
It can also contain parameters for the legend, axis limits, ticks
and axis labels.
"""
X, y = utils.load_dataset(config["dataset"])
neg, pos = X[y == 1], X[y == 0]
fig, ax = plt.subplots(figsize=config.get("figsize"))
ax.plot(pos[:, 0], pos[:, 1], "or", markersize=2, label="$\mathcal{X}^+$")
ax.plot(neg[:, 0], neg[:, 1], "^b", markersize=2, label="$\mathcal{X}^-$")
ax.set_aspect("equal")
set_legend(ax, config)
set_xlim(ax, config)
set_ylim(ax, config)
set_ticks(ax, config)
set_labels(ax, config)
return fig
def dataset_images(name, config):
"""Plot samples from a dataset containing images.
The *config* dict should contain:
dataset -- dataset name
rows -- number of rows
cols -- number of columns
classes -- list of class labels
img_width -- width of images
img_height -- height of images
img_channels -- number of color channels in the image (default 1)
figsize -- matplotlib figsize
"""
X, y = utils.load_dataset(config["dataset"])
figsize = config.get("figsize", (2.5, 1))
fig, axs = plt.subplots(
config["rows"],
config["cols"],
gridspec_kw={"wspace": 0.1, "hspace": 0.1},
squeeze=True,
figsize=figsize,
)
shape = (config.get("img_channels", 1), config["img_width"], config["img_height"])
for n, ax in zip(config["classes"], axs.flatten()):
idx = np.where(y == n)[0][0]
ax.imshow(X[idx].reshape(shape).transpose(1, 2, 0), cmap="gray", vmin=0, vmax=1)
ax.axis("off")
return fig
def _load_df(name):
"""Load a result and calculate additional metrics."""
df = utils.load_result(name)
if "score" in df:
df["interpolated"] = df["score"] == 1.0
if "dataset" in df:
n_samples = {}
for dataset_name in df["dataset"].unique():
X, y = utils.load_dataset(dataset_name)
n_samples[dataset_name] = X.shape[0]
df["n_samples"] = df.apply(lambda row: n_samples[row["dataset"]], axis=1)
print("Loaded features:")
for column in df.columns:
print(f" * {column}")
return df
def _make_pivot(df, config):
"""Return a pivot table of the results (using pandas).
The *config* dict should contain:
idx -- name of column to be used as index
col -- name of column to be used as column
val -- name of column to be used as values
agg -- aggregation function
round -- round float type index/column (default no rounding)
"""
index, columns, values = config["idx"], config["col"], config["val"]
round_ = config.get("round")
if round_ is not None:
if pd.api.types.is_float_dtype(df[index]):
df[index] = df[index].round(round_)
if pd.api.types.is_float_dtype(df[columns]):
df[columns] = df[columns].round(round_)
pivot = df.pivot_table(
index=index, columns=columns, values=values, aggfunc=config.get("agg")
)
return pivot
def add_contours(ax, pivot, config):
"""Plot contours onto an existing axis.
The *config* dict should contain a sub dict 'contours' with:
smoothing -- Gaussian smoothing (default 0.0)
vmin -- min value (default automatic)
vmax -- max value (default automatic)
levels -- number of levels (default automatic)
"""
if "contours" not in config:
return
config = config["contours"]
smoothing = config.get("smoothing", 0.0)
smoothed = scipy.ndimage.gaussian_filter(pivot, smoothing)
ax.contour(
smoothed,
vmin=config.get("vmin"),
vmax=config.get("vmax"),
levels=config.get("levels"),
cmap="viridis",
linewidths=0.75,
)
def heatmap(name, config):
"""Plot a heatmap.
The *config* dict should contain:
pivot -- a pivot config dict
vmin -- min value (default automatic)
vmax -- max value (default automatic)
contours -- a contour data dict (default no contours)
figsize -- matplotlib figsize
It can also contain parameters for ticks and axis labels.
"""
df = _load_df(name)
pivot = _make_pivot(df, config["pivot"])
fig, ax = plt.subplots(figsize=config.get("figsize"))
sns.heatmap(
pivot, vmin=config.get("vmin"), vmax=config.get("vmax"), square=True, ax=ax
)
ax.tick_params(axis="x", rotation=90)
ax.axes.invert_yaxis()
for _, spine in ax.spines.items():
spine.set_visible(True)
add_contours(ax, pivot, config)
set_axis_formatter(ax)
set_labels(ax, config)
set_ticks(ax, config)
# contour of the interpolation region
interpolation_pivot = {**config["pivot"], **{"val": "interpolated"}}
pivot = _make_pivot(df, interpolation_pivot)
smoothed = scipy.ndimage.gaussian_filter(pivot, 1.0)
ax.contour(
smoothed,
levels=[0.99],
cmap=mpl.colors.ListedColormap(["blueviolet"]),
linestyles="dashed",
linewidths=1.5,
)
return fig
def slices(name, config):
"""Plot heatmap slices.
The *config* dict should contain:
pivot -- a pivot config dict
slices -- list of slices to plot (from pivot columns)
marker_threshold -- interpolation probability threshold for markers
(default no markers)
figsize -- matplotlib figsize
It can also contain parameters for the legend, axis limits, ticks
and axis labels.
"""
fig, ax = plt.subplots(figsize=config.get("figsize", (6, 3)))
# Plot lines and error bars.
df = _load_df(name)
mask = df[config["pivot"]["col"]].isin(config["slices"])
df = df[mask]
sns.lineplot(
df,
x=config["pivot"]["idx"],
y=config["pivot"]["val"],
hue=config["pivot"]["col"],
style=config["pivot"]["col"],
markers=False,
dashes=False,
palette="flare",
seed=0,
ax=ax,
legend=False if config.get("legend", False) is False else "auto"
)
# Plot markers if interpolation probability is high enough.
pivot = _make_pivot(df, config["pivot"])
pivot_config = config["pivot"].copy()
pivot_config["val"] = "interpolated"
pivot_interpolation = _make_pivot(df, pivot_config)
markers = pivot_interpolation > config.get("marker_threshold", 2.0)
markers = markers.iloc[:: config.get("mark_every", 1)]
sns.scatterplot(
pivot[markers].iloc[:, ::-1],
palette="flare",
s=15,
legend=False,
zorder=100,
ax=ax,
)
set_value_limit(ax, config)
set_xlim(ax, config)
set_ylim(ax, config)
set_legend(ax, config)
set_ticks(ax, config)
set_labels(ax, config)
return fig
class Selector(base.TransformerMixin, base.BaseEstimator):
def __init__(self, transformer, index):
self.transformer = transformer
self.index = index
def transform(self, X):
outputs = self.transformer.transform(X)
return outputs[:, np.newaxis, self.index]
def _plot_decision_surface(
ax, estimator, steps, color, alpha=1.0, fill=False, linestyle=None
):
x, y = np.meshgrid(
np.linspace(*ax.get_xlim(), num=steps), np.linspace(*ax.get_ylim(), num=steps)
)
z = estimator.predict(np.c_[x.ravel(), y.ravel()]).reshape(x.shape)
col = mpl.colors.to_rgba(color, alpha=alpha)
cmap = mpl.colors.ListedColormap(["#00000000", col])
if fill:
ax.contourf(x, y, z, 1, cmap=cmap)
else:
ax.contour(x, y, z, 1, linestyles=linestyle, cmap=cmap)
def boundary_2d(name, config):
"""Plot the decision boundary on top of a 2d dataset.
The *data* dict should contain:
dataset -- dataset name
model -- a model config dict
legend -- toggle legend (default True)
figsize -- matplotlib figsize
It can also contain parameters for axis limits, ticks and axis
labels.
"""
markersize = config.get("markersize", 2)
dataset, model_args = config["dataset"], config["model"]
step = 150
X, y = utils.load_dataset(dataset)
neg, pos = X[y == 1], X[y == 0]
# Build and fit the model.
from algorithm import get_model
model = get_model(model_args, random_state=0)
model.fit(X, y)
# Plot the dataset to adjust the aspect ratio.
fig, ax = plt.subplots(figsize=config.get("figsize"))
ax.plot(pos[:, 0], pos[:, 1], "or", markersize=markersize, label="$\mathcal{X}^+$")
ax.plot(neg[:, 0], neg[:, 1], "^b", markersize=markersize, label="$\mathcal{X}^-$")
if config.get("legend", True):
ax.legend()
ax.set_aspect("equal")
set_xlim(ax, config)
set_ylim(ax, config)
# For each of the neurons of the second layer, we restrict the model
# to that neuron and draw a decision boundary.
estimator = model["multi"].estimators_[0]
colors = itertools.cycle(plt.rcParams["axes.prop_cycle"].by_key()["color"])
for index, color in zip(range(estimator["secondlayer"].dim_out_), colors):
restricted_model = pipeline.make_pipeline(
model["firstlayer"],
model["activation-1"],
Selector(estimator["secondlayer"], index),
estimator["activation-2"],
estimator["thirdlayer"],
)
_plot_decision_surface(ax, restricted_model, step, color, alpha=0.5, fill=True)
_plot_decision_surface(
ax, restricted_model, step, color, alpha=1.0, fill=False, linestyle="solid"
)
_plot_decision_surface(ax, model, step, "black", fill=False, linestyle="dashed")
# Draw the dataset on top of the decision boundaries.
ax.plot(pos[:, 0], pos[:, 1], "or", markersize=markersize, label="$\mathcal{X}^+$")
ax.plot(neg[:, 0], neg[:, 1], "^b", markersize=markersize, label="$\mathcal{X}^-$")
# Draw markers for the points associated to each of the neurons.
colors = itertools.cycle(plt.rcParams["axes.prop_cycle"].by_key()["color"])
for index, color in zip(range(estimator["secondlayer"].dim_out_), colors):
i = estimator["secondlayer"]._C[index]
ax.plot(
neg[i, 0],
neg[i, 1],
"*",
markersize=12,
markerfacecolor=color,
markeredgewidth=1.0,
5926
markeredgecolor="black"
)
if config.get("samples_on_top", False):
# Draw the dataset on top of the stars
ax.plot(pos[:, 0], pos[:, 1], "or", markersize=markersize, label="$\mathcal{X}^+$")
ax.plot(neg[:, 0], neg[:, 1], "^b", markersize=markersize, label="$\mathcal{X}^-$")
set_ticks(ax, config)
set_labels(ax, config)
set_outline(ax, config)
return fig
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--name", dest="name", type=str, required=True)
args = parser.parse_args()
config = utils.load_config(args.name)
plots = config["plots"]
print("Generating plots ...")
plot_fns = {
"dataset_2d": dataset_2d,
"dataset_images": dataset_images,
"heatmap": heatmap,
"slices": slices,
"boundary_2d": boundary_2d
}
for config in plots:
type_, name = config["type"], config["name"]
print(f" -> {name} ({type_})")
plot_fn = plot_fns[type_]
fig = plot_fn(args.name, config)
utils.save_plot(name, fig, subdir=args.name)
if __name__ == "__main__":
main()