-
Notifications
You must be signed in to change notification settings - Fork 2
/
uncertainty_utils.py
353 lines (294 loc) · 11.8 KB
/
uncertainty_utils.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
import numpy as np
from scipy import stats
from shapely.geometry import Polygon, LineString
from shapely.ops import polygonize, unary_union
marker_size = 0.8
diag_color = "tab:green"
def plot_color(ax, xx, yy, good):
from matplotlib.colors import from_levels_and_colors
from matplotlib.collections import LineCollection
cmap, norm = from_levels_and_colors([0.0, 0.5, 1.5], ['blue', 'black'])
points = np.array([xx, yy]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lines = LineCollection(segments, cmap=cmap, norm=norm)
lines.set_array(good.astype(int))
ax.add_collection(lines)
def plot_calibration(y_pred, y_std, y_true, ax, num_bins=200):
"""
Return lists of expected and observed proportions of points falling into
intervals corresponding to a range of quantiles.
"""
# Compute proportions
exp_proportions = np.linspace(0, 1, num_bins)
norm = stats.norm(loc=0, scale=1)
gaussian_lower_bound = norm.ppf(0.5 - exp_proportions / 2.0)
gaussian_upper_bound = norm.ppf(0.5 + exp_proportions / 2.0)
residuals = y_pred - y_true
normalized_residuals = (residuals.flatten() / y_std.flatten()).reshape(-1, 1)
above_lower = normalized_residuals >= gaussian_lower_bound
below_upper = normalized_residuals <= gaussian_upper_bound
within_quantile = above_lower * below_upper
obs_proportions = np.sum(within_quantile, axis=0).flatten() / len(residuals)
ax.plot([0, 1], [0, 1], "--", label="Ideal", c=diag_color)
plot_color(ax, exp_proportions, obs_proportions,(exp_proportions-obs_proportions)>0)
#ax.scatter(exp_proportions, obs_proportions, c = (exp_proportions-obs_proportions)>0, edgecolor=None)
ax.fill_between(exp_proportions, exp_proportions, obs_proportions,
where= (exp_proportions-obs_proportions)>0, interpolate = True,
color="black", alpha=0.2, label="Overconfident")
ax.fill_between(exp_proportions, exp_proportions, obs_proportions,
where= (exp_proportions-obs_proportions)<0, interpolate=True,
color="blue", alpha=0.2, label="Underconfident")
ax.set_aspect('equal', adjustable='box')
buff = 0
ax.set_xlim([0 - buff, 1 + buff])
ax.set_ylim([0 - buff, 1 + buff])
# 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",
)
ax.set_xlabel("Theoretical proportion in Gaussian interval")
ax.set_ylabel("Observed proportion in Gaussian interval")
ax.legend()
def plot_interval(y_pred, y_std, y_true, ax, settings,ind, num_stds_confidence_bound=2):
# randomly select 100 samples for better visualization
#selection = np.random.choice(np.arange(len(y_pred)),100)
#y_pred, y_std, y_true = y_pred[selection], y_std[selection], y_true[selection]
intervals = num_stds_confidence_bound * y_std
ax.errorbar(
y_true,
y_pred,
yerr = intervals,
fmt="o",
ls="none",
ms=marker_size,
linewidth=marker_size,
c="#1f77b4",
alpha=0.5,
zorder=1,
rasterized=True
)
ax.scatter(y_true, y_pred, s=marker_size, c="tab:blue",zorder=2, rasterized=True)
# Determine lims
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]))),
]
# plot 45-degree parity line
ax.plot(lims_ext, lims_ext, "--", linewidth=marker_size, c=diag_color,zorder=3)
# Format
name = settings["target_names"][ind]
ax.set_xlim(lims_ext)
ax.set_ylim(lims_ext)
ax.set_xlabel(f"Target ({settings.get('units','dimensionless')})")
ax.set_ylabel(f"Prediction ({settings.get('units','dimensionless')})")
ax.set_aspect("equal", "box")
def plot_interval_ordered(y_pred, y_std, y_true, ax, settings,ind, num_stds_confidence_bound=2):
intervals = num_stds_confidence_bound * y_std
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))
ax.errorbar(
xs,
y_pred,
yerr=intervals,
fmt="o",
ls="none",
linewidth=marker_size,
c="#1f77b4",
alpha=0.5,
ms = marker_size,
zorder=1,
rasterized=True
)
ax.scatter(xs, y_pred, s=marker_size, c="tab:blue", label='Predictions', zorder=2, rasterized=True)
ax.plot(xs, y_true, "--", linewidth=marker_size, c=diag_color, label='Target', zorder=3)
ax.set_xlim([0,len(y_pred)])
ax.legend()
# Determine lims
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]))),
]
# Format
name = settings["target_names"][ind]
ax.set_ylim(lims_ext)
ax.set_xlabel(f"Index ordered by target value")
ax.set_ylabel(f"Prediction ({settings.get('units','dimensionless')})")
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
ax.set_aspect((x1-x0)/(y1-y0))
def plot_std(y_pred, y_std, y_true, dknn, ax, settings,ind, num_stds_confidence_bound=2):
error = np.absolute(y_pred-y_true)
#intervals = num_stds_confidence_bound * y_std
ax.scatter(
y_std,
error,
c="#1f77b4",
alpha=0.5,
s=marker_size,
rasterized=True
)
ax2 = ax.twinx()
ax2.scatter(
y_std,
dknn,
c="red",
alpha=0.5,
s=marker_size,
rasterized=True
)
ax2.spines['right'].set_color('red')
ax2.tick_params(axis='y', colors='red', labelsize=8)
# Format
name = settings["target_names"][ind]
ax.set_xlabel(f"Predicted $\\sigma$ ({settings.get('units', 'dimensionless')})")
ax.set_ylabel(f"Absolute error ({settings.get('units','dimensionless')})")
ax2.set_ylabel("$d_{KNN}$", color="red")
# _,x1 = ax.get_xlim()
# _,y1 = ax.get_ylim()
# ax.set_xlim([0,x1])
# ax.set_ylim([0,y1])
# ax.set_aspect((x1 - 0) / (y1 - 0))
# _,x1 = ax2.get_xlim()
# _,y1 = ax2.get_ylim()
# ax2.set_xlim([0,x1])
# ax2.set_ylim([0,y1])
ax.set_xlim(0)
ax.set_ylim(0)
ax2.set_ylim(0)
# ax2.set_aspect((x1-0)/(y1-0))
def plot_std_by_index(y_pred, y_std, y_true, ax, settings,ind, num_stds_confidence_bound=2):
error = np.absolute(y_pred-y_true)
order = np.argsort(error.flatten())
y_pred, y_std, y_true, error = y_pred[order], y_std[order], y_true[order], error[order]
intervals = num_stds_confidence_bound * y_std
xs = np.arange(len(order))
ax.errorbar(
xs,
error,
yerr=intervals,
fmt="o",
ls="none",
ms=marker_size,
linewidth=marker_size,
c="#1f77b4",
alpha=0.5,
rasterized=True
)
ax.plot(xs, error, "o", ms=marker_size, c="#1f77b4")
#ax.plot(xs, y_true, "--", linewidth=2.0, c=diag_color, label='Target')
# Determine lims
intervals_lower_upper = [error - intervals, error + intervals]
lims_ext = [
int(np.floor(np.min(intervals_lower_upper[0]))),
int(np.ceil(np.max(intervals_lower_upper[1]))),
]
# Format
name = settings["target_names"][ind]
#ax.set_xlim(lims_ext)
ax.set_ylim(lims_ext)
ax.set_xlabel(f"Index ordered by error")
ax.set_ylabel(f"Error ({settings.get('units','dimensionless')})")
ax.set_xlim([0,len(y_pred)])
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
ax.set_aspect((x1-x0)/(y1-y0))
def plot_ordered_mae(y_pred, y_std, y_true, dknn, ax, settings,ind, legend=False, yticks=True):
print("Generating confidence vs error curves...")
error = np.absolute(y_pred-y_true)
order = np.argsort(y_std.flatten())[::-1]
std_error = error[order]
perfect_error = np.sort(error)[::-1]
np.random.seed(0)
random_error = np.zeros(shape=(1000,len(error)))
for i in range(len(random_error)):
random_error[i,:] = np.random.permutation(error)
random_mean = np.zeros(len(error))
random_std = np.zeros(len(error))
order = np.argsort(dknn.flatten())[::-1]
if not np.isnan(dknn).any():
dknn_error = error[order]
for i in range(len(error)):
perfect_error[i] = perfect_error[i:].mean()
std_error[i] = std_error[i:].mean()
if not np.isnan(dknn).any():
dknn_error[i] = dknn_error[i:].mean()
random_mean[i] = random_error[:,i:].mean()
random_std[i] = random_error[:, i:].mean(axis=1).std()
mae = np.mean(random_mean, axis=-1)
def moving_average(x, w=15):
return np.convolve(x, np.ones(w), "valid") / w
percintile = np.linspace(0,100, len(moving_average(error)))
lines = []
lines.append(ax.plot(percintile, moving_average(perfect_error / mae), c='tab:green', label='Error ranked'))
lines.append(ax.plot(percintile, moving_average(random_mean / mae), c='tab:red', label='Randomly ranked'))
ax.fill_between(percintile, moving_average(random_mean-random_std) / mae, moving_average(random_mean+random_std) / mae, color='tab:red',alpha=0.2)
lines.append(ax.plot(percintile, moving_average(std_error / mae), c='tab:blue', label='$\\sigma$ ranked', ls='--'))
if not np.isnan(dknn).any():
lines.append(ax.plot(percintile, moving_average(dknn_error / mae), c='tab:orange', label='$d_\\mathrm{KNN}$ ranked', ls='-.'))
x0, x1 = 0, 100
y0, y1 = 0, 1.2
ax.set_xlim((x0, x1))
ax.set_ylim((y0, y1))
ax.set_aspect((x1-x0)/(y1-y0))
ax.set_xticks([0, 25, 50, 75, 100])
if yticks:
ax.set_yticks([0, 0.25, 0.50, 0.75, 1.0])
else:
ax.set_yticks([])
name = settings["target_names"][ind]
if legend:
ax.legend(loc='lower left')
ax.set_xlabel("Confidence percentile")
ax.set_ylabel(f"Error relative to MAE")
else:
ax.text(5, 1.1, name)
return lines
if __name__ == "__main__":
# for testing purposes
import matplotlib
import matplotlib.pyplot as plt
DARK2_COLOURS = plt.cm.get_cmap("Dark2").colors
matplotlib.use("pdf")
HEIGHT = 2.5
matplotlib.rcParams["font.size"] = 8
matplotlib.rcParams["xtick.labelsize"] = 8
matplotlib.rcParams["ytick.labelsize"] = 8
print('plotting')
fig, axs = plt.subplots(2,3,figsize=(3 * 1.25 * HEIGHT + 0.5, 2 * HEIGHT * 1.25 + 0.5))
axs = axs.flatten()
y = np.arange(300)
preds = y + (np.random.rand(len(y))-0.5)*30
unc = np.absolute(preds-y)/3 + np.random.rand(len(y))*3
unc[unc.argsort()[-100:]] = unc[unc.argsort()[-100:]]*10
dknn = np.absolute(preds-y)/2 + np.random.rand(len(y))*3
settings = {'target_names':['eform'], 'units':'eV'}
plot_calibration(preds, unc, y, axs[0])
plot_interval(preds, unc, y, axs[1], settings, 0)
plot_interval_ordered(preds, unc, y, axs[2], settings, 0)
plot_std(preds, unc, y, axs[3], settings, 0)
plot_std_by_index(preds, unc, y, axs[4], settings, 0)
plot_ordered_mae(preds, unc, y, dknn, axs[5], settings, 0)
fig.tight_layout()
fig.suptitle('Dummy test plot')
fig.savefig('test_fig.pdf')