forked from uncertainty-toolbox/uncertainty-toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviz.py
628 lines (527 loc) · 19.8 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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
"""
Visualizations for predictive uncertainties and metrics.
"""
from typing import Union, Tuple, List, Any, NoReturn
import pathlib
import numpy as np
from scipy import stats
import matplotlib
import matplotlib.pyplot as plt
from sklearn.metrics import (
mean_absolute_error,
mean_squared_error,
r2_score,
median_absolute_error,
)
from shapely.geometry import Polygon, LineString
from shapely.ops import polygonize, unary_union
from uncertainty_toolbox.metrics_calibration import (
get_proportion_lists,
get_proportion_lists_vectorized,
adversarial_group_calibration,
)
def plot_xy(
y_pred: np.ndarray,
y_std: np.ndarray,
y_true: np.ndarray,
x: np.ndarray,
n_subset: Union[int, None] = None,
ylims: Union[Tuple[float, float], None] = None,
xlims: Union[Tuple[float, float], None] = None,
num_stds_confidence_bound: int = 2,
leg_loc: Union[int, str] = 3,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot one-dimensional inputs with associated predicted values, predictive
uncertainties, and true values.
Args:
y_pred: 1D array of the predicted means for the held out dataset.
y_std: 1D array of the predicted standard deviations for the held out dataset.
y_true: 1D array of the true labels in the held out dataset.
x: 1D array of input values for the held out dataset.
n_subset: Number of points to plot after filtering.
ylims: a tuple of y axis plotting bounds, given as (lower, upper).
xlims: a tuple of x axis plotting bounds, given as (lower, upper).
num_stds_confidence_bound: width of confidence band, in terms of number of
standard deviations.
leg_loc: location of legend as a str or legend code int.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
# Order points in order of increasing x
order = np.argsort(x)
y_pred, y_std, y_true, x = (
y_pred[order],
y_std[order],
y_true[order],
x[order],
)
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true, x] = filter_subset([y_pred, y_std, y_true, x], n_subset)
intervals = num_stds_confidence_bound * y_std
h1 = ax.plot(x, y_true, ".", mec="#ff7f0e", mfc="None")
h2 = ax.plot(x, y_pred, "-", c="#1f77b4", linewidth=2)
h3 = ax.fill_between(
x,
y_pred - intervals,
y_pred + intervals,
color="lightsteelblue",
alpha=0.4,
)
ax.legend(
[h1[0], h2[0], h3],
["Observations", "Predictions", "$95\%$ Interval"],
loc=leg_loc,
)
# Format plot
if ylims is not None:
ax.set_ylim(ylims)
if xlims is not None:
ax.set_xlim(xlims)
ax.set_xlabel("$x$")
ax.set_ylabel("$y$")
ax.set_title("Confidence Band")
ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable="box")
return ax
def plot_intervals(
y_pred: np.ndarray,
y_std: np.ndarray,
y_true: np.ndarray,
n_subset: Union[int, None] = None,
ylims: Union[Tuple[float, float], None] = None,
num_stds_confidence_bound: int = 2,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot predictions and predictive intervals versus true values.
Args:
y_pred: 1D array of the predicted means for the held out dataset.
y_std: 1D array of the predicted standard deviations for the held out dataset.
y_true: 1D array of the true labels in the held out dataset.
n_subset: Number of points to plot after filtering.
ylims: a tuple of y axis plotting bounds, given as (lower, upper).
num_stds_confidence_bound: width of intervals, in terms of number of standard
deviations.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], n_subset)
# Compute intervals
intervals = num_stds_confidence_bound * y_std
# Plot
ax.errorbar(
y_true,
y_pred,
intervals,
fmt="o",
ls="none",
linewidth=1.5,
c="#1f77b4",
alpha=0.5,
)
h1 = ax.plot(y_true, y_pred, "o", c="#1f77b4")
# Determine lims
if ylims is None:
intervals_lower_upper = [y_pred - intervals, y_pred + intervals]
lims_ext = [
int(np.floor(np.min(intervals_lower_upper[0]))),
int(np.ceil(np.max(intervals_lower_upper[1]))),
]
else:
lims_ext = ylims
# plot 45-degree line
h2 = ax.plot(lims_ext, lims_ext, "--", linewidth=1.5, c="#ff7f0e")
# Legend
ax.legend([h1[0], h2[0]], ["Predictions", "$f(x) = x$"], loc=4)
# Format plot
ax.set_xlim(lims_ext)
ax.set_ylim(lims_ext)
ax.set_xlabel("Observed Values")
ax.set_ylabel("Predicted Values and Intervals")
ax.set_title("Prediction Intervals")
ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable="box")
return ax
def plot_intervals_ordered(
y_pred: np.ndarray,
y_std: np.ndarray,
y_true: np.ndarray,
n_subset: Union[int, None] = None,
ylims: Union[Tuple[float, float], None] = None,
num_stds_confidence_bound: int = 2,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot predictions and predictive intervals versus true values, with points ordered
by true value along x-axis.
Args:
y_pred: 1D array of the predicted means for the held out dataset.
y_std: 1D array of the predicted standard deviations for the held out dataset.
y_true: 1D array of the true labels in the held out dataset.
n_subset: Number of points to plot after filtering.
ylims: a tuple of y axis plotting bounds, given as (lower, upper).
num_stds_confidence_bound: width of intervals, in terms of number of standard
deviations.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], n_subset)
order = np.argsort(y_true.flatten())
y_pred, y_std, y_true = y_pred[order], y_std[order], y_true[order]
xs = np.arange(len(order))
intervals = num_stds_confidence_bound * y_std
# Plot
ax.errorbar(
xs,
y_pred,
intervals,
fmt="o",
ls="none",
linewidth=1.5,
c="#1f77b4",
alpha=0.5,
)
h1 = ax.plot(xs, y_pred, "o", c="#1f77b4")
h2 = ax.plot(xs, y_true, "--", linewidth=2.0, c="#ff7f0e")
# Legend
ax.legend([h1[0], h2[0]], ["Predicted Values", "Observed Values"], loc=4)
# Determine lims
if ylims is None:
intervals_lower_upper = [y_pred - intervals, y_pred + intervals]
lims_ext = [
int(np.floor(np.min(intervals_lower_upper[0]))),
int(np.ceil(np.max(intervals_lower_upper[1]))),
]
else:
lims_ext = ylims
# Format plot
ax.set_ylim(lims_ext)
ax.set_xlabel("Index (Ordered by Observed Value)")
ax.set_ylabel("Predicted Values and Intervals")
ax.set_title("Ordered Prediction Intervals")
ax.set_aspect(1.0 / ax.get_data_ratio(), adjustable="box")
return ax
def plot_calibration(
y_pred: np.ndarray,
y_std: np.ndarray,
y_true: np.ndarray,
n_subset: Union[int, None] = None,
curve_label: Union[str, None] = None,
show: bool = False,
vectorized: bool = True,
exp_props: Union[np.ndarray, None] = None,
obs_props: Union[np.ndarray, None] = None,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot the observed proportion vs prediction proportion of outputs falling into a
range of intervals, and display miscalibration area.
Args:
y_pred: 1D array of the predicted means for the held out dataset.
y_std: 1D array of the predicted standard deviations for the held out dataset.
y_true: 1D array of the true labels in the held out dataset.
n_subset: Number of points to plot after filtering.
curve_label: legend label str for calibration curve.
vectorized: plot using get_proportion_lists_vectorized.
exp_props: plot using the given expected proportions.
obs_props: plot using the given observed proportions.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], n_subset)
if (exp_props is None) or (obs_props is None):
# Compute exp_proportions and obs_proportions
if vectorized:
(
exp_proportions,
obs_proportions,
) = get_proportion_lists_vectorized(y_pred, y_std, y_true)
else:
(exp_proportions, obs_proportions) = get_proportion_lists(
y_pred, y_std, y_true
)
else:
# If expected and observed proportions are given
exp_proportions = np.array(exp_props).flatten()
obs_proportions = np.array(obs_props).flatten()
if exp_proportions.shape != obs_proportions.shape:
raise RuntimeError("exp_props and obs_props shape mismatch")
# Set label
if curve_label is None:
curve_label = "Predictor"
# Plot
ax.plot([0, 1], [0, 1], "--", label="Ideal", c="#ff7f0e")
ax.plot(exp_proportions, obs_proportions, label=curve_label, c="#1f77b4")
ax.fill_between(exp_proportions, exp_proportions, obs_proportions, alpha=0.2)
# Format plot
ax.set_xlabel("Predicted Proportion in Interval")
ax.set_ylabel("Observed Proportion in Interval")
ax.axis("square")
buff = 0.01
ax.set_xlim([0 - buff, 1 + buff])
ax.set_ylim([0 - buff, 1 + buff])
ax.set_title("Average Calibration")
# Compute miscalibration area
polygon_points = []
for point in zip(exp_proportions, obs_proportions):
polygon_points.append(point)
for point in zip(reversed(exp_proportions), reversed(exp_proportions)):
polygon_points.append(point)
polygon_points.append((exp_proportions[0], obs_proportions[0]))
polygon = Polygon(polygon_points)
x, y = polygon.exterior.xy # original data
ls = LineString(np.c_[x, y]) # closed, non-simple
lr = LineString(ls.coords[:] + ls.coords[0:1])
mls = unary_union(lr)
polygon_area_list = [poly.area for poly in polygonize(mls)]
miscalibration_area = np.asarray(polygon_area_list).sum()
# Annotate plot with the miscalibration area
ax.text(
x=0.95,
y=0.05,
s="Miscalibration area = %.2f" % miscalibration_area,
verticalalignment="bottom",
horizontalalignment="right",
fontsize="small",
)
return ax
def plot_adversarial_group_calibration(
y_pred: np.ndarray,
y_std: np.ndarray,
y_true: np.ndarray,
n_subset: Union[int, None] = None,
cali_type: str = "mean_abs",
curve_label: Union[str, None] = None,
group_size: Union[np.ndarray, None] = None,
score_mean: Union[np.ndarray, None] = None,
score_stderr: Union[np.ndarray, None] = None,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot adversarial group calibration plots by varying group size from 0% to 100% of
dataset size and recording the worst calibration occurred for each group size.
Args:
y_pred: 1D array of the predicted means for the held out dataset.
y_std: 1D array of the predicted standard deviations for the held out dataset.
y_true: 1D array of the true labels in the held out dataset.
n_subset: Number of points to plot after filtering.
cali_type: Calibration type str.
curve_label: legend label str for calibration curve.
group_size: 1D array of group size ratios in [0, 1].
score_mean: 1D array of metric means for group size ratios in group_size.
score_stderr: 1D array of metric standard devations for group size ratios in group_size.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(7, 5))
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], n_subset)
# Compute group_size, score_mean, score_stderr
if (group_size is None) or (score_mean is None):
# Compute adversarial group calibration
adv_group_cali_namespace = adversarial_group_calibration(
y_pred, y_std, y_true, cali_type=cali_type
)
group_size = adv_group_cali_namespace.group_size
score_mean = adv_group_cali_namespace.score_mean
score_stderr = adv_group_cali_namespace.score_stderr
else:
# If expected and observed proportions are give
group_size = np.array(group_size).flatten()
score_mean = np.array(score_mean).flatten()
score_stderr = np.array(score_stderr).flatten()
if (group_size.shape != score_mean.shape) or (
group_size.shape != score_stderr.shape
):
raise RuntimeError(
"Input arrays for adversarial group calibration shape mismatch"
)
# Set label
if curve_label is None:
curve_label = "Predictor"
# Plot
ax.plot(group_size, score_mean, "-o", label=curve_label, c="#1f77b4")
ax.fill_between(
group_size,
score_mean - score_stderr,
score_mean + score_stderr,
alpha=0.2,
)
# Format plot
buff = 0.02
ax.set_xlim([0 - buff, 1 + buff])
ax.set_ylim([0 - buff, 0.5 + buff])
ax.set_xlabel("Group size")
ax.set_ylabel("Calibration Error of Worst Group")
ax.set_title("Adversarial Group Calibration")
return ax
def plot_sharpness(
y_std: np.ndarray,
n_subset: Union[int, None] = None,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot sharpness of the predictive uncertainties.
Args:
y_std: 1D array of the predicted standard deviations for the held out dataset.
n_subset: Number of points to plot after filtering.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], n_subset)
# Plot sharpness curve
ax.hist(y_std, edgecolor="#1f77b4", color="#a5c8e1", density=True)
# Format plot
xlim = (y_std.min(), y_std.max())
ax.set_xlim(xlim)
ax.set_xlabel("Predicted Standard Deviation")
ax.set_ylabel("Normalized Frequency")
ax.set_title("Sharpness")
ax.set_yticklabels([])
ax.set_yticks([])
# Calculate and report sharpness
sharpness = np.sqrt(np.mean(y_std ** 2))
ax.axvline(x=sharpness, label="sharpness", color="k", linewidth=2, ls="--")
if sharpness < (xlim[0] + xlim[1]) / 2:
text = "\n Sharpness = %.2f" % sharpness
h_align = "left"
else:
text = "\nSharpness = %.2f " % sharpness
h_align = "right"
ax.text(
x=sharpness,
y=ax.get_ylim()[1],
s=text,
verticalalignment="top",
horizontalalignment=h_align,
fontsize="small",
)
return ax
def plot_residuals_vs_stds(
y_pred: np.ndarray,
y_std: np.ndarray,
y_true: np.ndarray,
n_subset: Union[int, None] = None,
ax: Union[matplotlib.axes.Axes, None] = None,
) -> matplotlib.axes.Axes:
"""Plot absolute value of the prediction residuals versus standard deviations of the
predictive uncertainties.
Args:
y_pred: 1D array of the predicted means for the held out dataset.
y_std: 1D array of the predicted standard deviations for the held out dataset.
y_true: 1D array of the true labels in the held out dataset.
n_subset: Number of points to plot after filtering.
ax: matplotlib.axes.Axes object.
Returns:
matplotlib.axes.Axes object with plot added.
"""
# Create ax if it doesn't exist
if ax is None:
fig, ax = plt.subplots(figsize=(5, 5))
# Optionally select a subset
if n_subset is not None:
[y_pred, y_std, y_true] = filter_subset([y_pred, y_std, y_true], n_subset)
# Compute residuals
residuals = y_true - y_pred
# Put stds on same scale as residuals
residuals_sum = np.sum(np.abs(residuals))
y_std_scaled = (y_std / np.sum(y_std)) * residuals_sum
# Plot residuals vs standard devs
h1 = ax.plot(y_std_scaled, np.abs(residuals), "o", c="#1f77b4")
# Plot 45-degree line
xlims = ax.get_xlim()
ylims = ax.get_ylim()
lims = [np.min([xlims[0], ylims[0]]), np.max([xlims[1], ylims[1]])]
h2 = ax.plot(lims, lims, "--", c="#ff7f0e")
# Legend
ax.legend([h1[0], h2[0]], ["Predictions", "$f(x) = x$"], loc=4)
# Format plot
ax.set_xlabel("Standard Deviations (Scaled)")
ax.set_ylabel("Residuals (Absolute Value)")
ax.set_title("Residuals vs. Predictive Standard Deviations")
ax.set_xlim(lims)
ax.set_ylim(lims)
ax.axis("square")
return ax
def filter_subset(input_list: List[List[Any]], n_subset: int) -> List[List[Any]]:
"""Keep only n_subset random indices from all lists given in input_list.
Args:
input_list: list of lists.
n_subset: Number of points to plot after filtering.
Returns:
List of all input lists with sizes reduced to n_subset.
"""
assert type(n_subset) is int
n_total = len(input_list[0])
idx = np.random.choice(range(n_total), n_subset, replace=False)
idx = np.sort(idx)
output_list = []
for inp in input_list:
outp = inp[idx]
output_list.append(outp)
return output_list
def set_style(style_str: str = "default") -> NoReturn:
"""Set the matplotlib plotting style.
Args:
style_str: string for style file.
"""
if style_str == "default":
plt.style.use((pathlib.Path(__file__).parent / "matplotlibrc").resolve())
def save_figure(
file_name: str = "figure",
ext_list: Union[list, str, None] = None,
white_background: bool = True,
) -> NoReturn:
"""Save matplotlib figure for all extensions in ext_list.
Args:
file_name: name of saved image file.
ext_list: list of strings (or single string) denoting file type.
white_background: set background of image to white if True.
"""
# Default ext_list
if ext_list is None:
ext_list = ["pdf", "png"]
# If ext_list is a single str
if isinstance(ext_list, str):
ext_list = [ext_list]
# Set facecolor and edgecolor
(fc, ec) = ("w", "w") if white_background else ("none", "none")
# Save each type in ext_list
for ext in ext_list:
save_str = file_name + "." + ext
plt.savefig(save_str, bbox_inches="tight", facecolor=fc, edgecolor=ec)
print(f"Saved figure {save_str}")
def update_rc(key_str: str, value: Any) -> NoReturn:
"""Update matplotlibrc parameters.
Args:
key_str: string for a matplotlibrc parameter.
value: associated value to set the matplotlibrc parameter.
"""
plt.rcParams.update({key_str: value})