-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.py
663 lines (608 loc) · 16.3 KB
/
serve.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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import Spacer, column, row
from bokeh.models import CheckboxGroup, LinearColorMapper, Slider
from bokeh.palettes import Colorblind8, Purples
from bokeh.plotting import figure
from bokeh.transform import transform
from distributions import NormalDistData
from metrics import Metrics
def threshold_slider_range_handler(attr, old, new):
"""Update threshold slider range based on total spread of data."""
new_min = metrics.roc_thresholds.data["thresholds"].min()
new_max = metrics.roc_thresholds.data["thresholds"].max()
threshold_slider.start = new_min
threshold_slider.end = new_max
def checkbox_callback(attr, old, new):
"""Update plot visibility based on checkbox status."""
plot_roc.visible = 0 in checks1.active
roc_auc_bar.visible = 1 in checks1.active
plot_pr.visible = 2 in checks1.active
pr_auc_bar.visible = 3 in checks1.active
plot_cm.visible = 0 in checks2.active
plot_mcc_f1.visible = 1 in checks2.active
acc_bar.visible = 2 in checks2.active
recall_bar.visible = 3 in checks2.active
spec_bar.visible = 0 in checks3.active
precision_bar.visible = 1 in checks3.active
npv_bar.visible = 2 in checks3.active
f1_bar.visible = 3 in checks3.active
mcc_bar.visible = 4 in checks3.active
# Initial distribution settings
DEFAULT_N = 100
DEFAULT_MEAN_0 = 20.0
DEFAULT_MEAN_1 = 22.0
DEFAULT_SD = 3.0
DEFAULT_SKEW = 0.0
dist0 = NormalDistData(DEFAULT_N, DEFAULT_MEAN_0, DEFAULT_SD, DEFAULT_SKEW)
dist1 = NormalDistData(DEFAULT_N, DEFAULT_MEAN_1, DEFAULT_SD, DEFAULT_SKEW)
# Calculate all classification metrics
metrics = Metrics(dist0, dist1)
# Set up colorblind friendly colormap & shift order
cmap = list(Colorblind8)
cmap.insert(0, cmap.pop())
# Interactive GUI Sliders
slider_n0 = Slider(
title="N",
start=50,
end=500,
step=10,
value=DEFAULT_N,
max_width=125,
bar_color=cmap[0],
)
slider_mean0 = Slider(
title="Mean",
start=0,
end=50,
step=0.5,
value=DEFAULT_MEAN_0,
max_width=125,
bar_color=cmap[0],
)
slider_sd0 = Slider(
title="SD",
start=0.1,
end=20,
step=0.1,
value=DEFAULT_SD,
max_width=125,
bar_color=cmap[0],
)
slider_skew0 = Slider(
title="Skew",
start=-50,
end=50,
step=1,
value=DEFAULT_SKEW,
max_width=75,
bar_color=cmap[0],
)
slider_n1 = Slider(
title="N",
start=50,
end=500,
step=10,
value=DEFAULT_N,
max_width=125,
bar_color=cmap[2],
)
slider_mean1 = Slider(
title="Mean",
start=0,
end=50,
step=0.5,
value=DEFAULT_MEAN_1,
max_width=125,
bar_color=cmap[2],
)
slider_sd1 = Slider(
title="SD",
start=0.1,
end=20,
step=0.1,
value=DEFAULT_SD,
max_width=125,
bar_color=cmap[2],
)
slider_skew1 = Slider(
title="Skew",
start=-50,
end=50,
step=1,
value=DEFAULT_SKEW,
max_width=75,
bar_color=cmap[2],
)
threshold_slider = Slider(
start=metrics.roc_thresholds.data["thresholds"].min(),
end=metrics.roc_thresholds.data["thresholds"].max(),
value=metrics.roc_thresholds.data["thresholds"].min(),
step=0.001,
title="classification threshold",
max_width=125,
bar_color=cmap[4],
margin=(5, 5, 5, 50),
)
# Interactivity callback handling between plots & underlying data
slider_n0.on_change(
"value",
dist0.n_handler,
metrics.threshold_line_y_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
)
slider_mean0.on_change(
"value",
dist0.mean_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
threshold_slider_range_handler,
)
slider_sd0.on_change(
"value",
dist0.sd_handler,
metrics.threshold_line_y_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
threshold_slider_range_handler,
)
slider_skew0.on_change(
"value",
dist0.skew_handler,
metrics.threshold_line_y_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
threshold_slider_range_handler,
)
slider_n1.on_change(
"value",
dist1.n_handler,
metrics.threshold_line_y_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
)
slider_mean1.on_change(
"value",
dist1.mean_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
threshold_slider_range_handler,
)
slider_sd1.on_change(
"value",
dist1.sd_handler,
metrics.threshold_line_y_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
threshold_slider_range_handler,
)
slider_skew1.on_change(
"value",
dist1.skew_handler,
metrics.threshold_line_y_handler,
metrics.roc_curve_handler,
metrics.pr_curve_handler,
metrics.cm_handler,
metrics.mcc_f1_curve_handler,
metrics.metrics_handler,
threshold_slider_range_handler,
)
threshold_slider.on_change(
"value",
metrics.threshold_line_x_handler,
metrics.roc_threshold_dot_handler,
metrics.pr_threshold_dot_handler,
metrics.cm_handler,
metrics.mcc_f1_threshold_dot_handler,
metrics.metrics_handler,
)
# Checkboxes for toggling individual plots
PLOT_CHECKS1 = ["ROC Curve", "ROC AUC", "PR Curve", "PR AUC"]
PLOT_CHECKS2 = ["Confusion Matrix", "MCC-F1 Curve", "Accuracy", "Recall"]
PLOT_CHECKS3 = ["Specificty", "Precision", "NPV", "F1", "MCC*"]
checks1 = CheckboxGroup(
labels=PLOT_CHECKS1, active=[0, 1], margin=(-45, 5, 5, 70)
) # top right bottom left
checks2 = CheckboxGroup(labels=PLOT_CHECKS2, active=[0], margin=(-45, 5, 5, -190))
checks3 = CheckboxGroup(labels=PLOT_CHECKS3, active=[], margin=(-45, 5, 5, -160))
checks1.on_change("active", checkbox_callback)
checks2.on_change("active", checkbox_callback)
checks3.on_change("active", checkbox_callback)
# ====================================================================
# PLOTS
# Distributions
plot_distributions = figure(
title="Class Distributions",
x_axis_label="Model Prediction (arbitrary)",
y_axis_label="Count",
plot_height=300,
plot_width=325,
toolbar_location=None
# output_backend='webgl'
)
plot_distributions.line(
"x", "y", source=dist0.kde_curve, line_color=cmap[0], line_width=2
)
plot_distributions.line(
"x", "y", source=dist1.kde_curve, line_color=cmap[2], line_width=2
)
plot_distributions.line(
"x", "y", source=metrics.threshold_line, line_color=cmap[4], line_width=4
)
# ROC Curve
plot_roc = figure(
title="ROC Curve",
x_axis_label="False Positive Rate",
y_axis_label="True Positive Rate (Recall)",
plot_height=300,
plot_width=325,
toolbar_location=None
# output_backend='webgl'
)
plot_roc.line(
"x", "y_upper", source=metrics.roc_curve, line_width=2, line_color=cmap[1]
)
plot_roc.line([0, 1], [0, 1], line_width=1, line_color="grey", line_dash="dashed")
plot_roc.scatter(
"x",
"y",
source=metrics.roc_threshold_dot,
size=13,
fill_color=cmap[4],
line_color=cmap[4],
)
# Add shading under ROC curve
plot_roc.varea(
source=metrics.roc_curve,
x="x",
y1="y_lower",
y2="y_upper",
fill_color=cmap[1],
alpha=0.1,
)
# ROC AUC Bar
roc_auc_bar = figure(
title="ROC AUC",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
roc_auc_bar.vbar(
x=0.5, top="roc_auc", source=metrics.metrics, width=0.5, fill_color=cmap[1]
)
roc_auc_bar.y_range.start = 0.0
roc_auc_bar.y_range.end = 1.0
roc_auc_bar.xgrid.grid_line_color = None
roc_auc_bar.xaxis.major_label_text_font_size = "0pt"
roc_auc_bar.xaxis.major_tick_line_color = None
roc_auc_bar.xaxis.minor_tick_line_color = None
roc_auc_bar.line(
[0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed"
)
# PR Curve
plot_pr = figure(
title="PR Curve",
x_axis_label="True Positive Rate (Recall)",
y_axis_label="Precision",
y_range=[0.0, 1.04],
plot_height=300,
plot_width=325,
toolbar_location=None,
# output_backend="webgl",
)
plot_pr.line("x", "y_upper", source=metrics.pr_curve, line_width=2, line_color=cmap[1])
plot_pr.line(
"x",
"y_lower",
source=metrics.pr_curve,
line_width=1,
line_color="grey",
line_dash="dashed",
)
plot_pr.scatter(
"x",
"y",
source=metrics.pr_threshold_dot,
size=13,
fill_color=cmap[4],
line_color=cmap[4],
)
# Add shading for area under PR curve
plot_pr.varea(
source=metrics.pr_curve,
x="x",
y1="y_lower",
y2="y_upper",
fill_color=cmap[1],
alpha=0.1,
)
# PR AUC Bar
pr_auc_bar = figure(
title="PR AUC",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
pr_auc_bar.vbar(
x=0.5, top="avg_prec", source=metrics.metrics, width=0.5, fill_color=cmap[1]
)
pr_auc_bar.y_range.start = 0.0
pr_auc_bar.y_range.end = 1.0
pr_auc_bar.xgrid.grid_line_color = None
pr_auc_bar.xaxis.major_label_text_font_size = "0pt"
pr_auc_bar.xaxis.major_tick_line_color = None
pr_auc_bar.xaxis.minor_tick_line_color = None
pr_auc_bar.line(
"x",
"y_lower",
source=metrics.pr_curve,
line_width=1,
line_color="grey",
line_dash="dashed",
)
# Confusion Matrix
plot_cm = figure(
title="Confusion Matrix",
x_axis_label="Predicted",
y_axis_label="True",
x_range=[-0.5, 1.5],
y_range=[1.5, -0.5],
plot_height=300,
plot_width=325,
toolbar_location=None
# output_backend='webgl'
)
cm_cmap = list(reversed(Purples[256]))[256 // 5 : -256 // 4]
mapper = LinearColorMapper(palette=cm_cmap)
plot_cm.rect(
x="x",
y="y",
width=1,
height=1,
source=metrics.cm,
line_color=None,
fill_color=transform("cm_values", mapper),
)
plot_cm.axis.minor_tick_line_color = None
plot_cm.xaxis.ticker = [0, 1]
plot_cm.yaxis.ticker = [0, 1]
plot_cm.text(
x="value_coord_x",
y="value_coord_y",
source=metrics.cm,
text="cm_values",
color="black",
text_align="center",
text_font_size={"value": "12px"},
)
# MCC-F1 Curve
# (MCC is unit normalized 0-1 instead of -1 to +1)
# Cao et al. 2020
plot_mcc_f1 = figure(
title="MCC-F1",
x_axis_label="F1-Score",
y_axis_label="Unit Normalized MCC",
y_range=[0, 1],
plot_height=300,
plot_width=325,
toolbar_location=None
# output_backend='webgl'
)
plot_mcc_f1.line("x", "y", source=metrics.mcc_f1_pts, line_width=2, line_color=cmap[1])
plot_mcc_f1.line(
[0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed"
)
plot_mcc_f1.scatter(
"x", "y", source=metrics.mcc_f1_dot, size=13, fill_color=cmap[4], line_color=cmap[4]
)
# Accuracy Bar
acc_bar = figure(
title="Accuracy",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
acc_bar.vbar(
x=0.5, top="accuracy", source=metrics.metrics, width=0.5, fill_color=cmap[1]
)
acc_bar.y_range.start = 0.0
acc_bar.y_range.end = 1.0
acc_bar.xgrid.grid_line_color = None
acc_bar.xaxis.major_label_text_font_size = "0pt"
acc_bar.xaxis.major_tick_line_color = None
acc_bar.xaxis.minor_tick_line_color = None
acc_bar.line([0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed")
# Recall Bar
recall_bar = figure(
title="Recall",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
recall_bar.vbar(
x=0.5, top="recall", source=metrics.metrics, width=0.5, fill_color=cmap[1]
)
recall_bar.y_range.start = 0.0
recall_bar.y_range.end = 1.0
recall_bar.xgrid.grid_line_color = None
recall_bar.xaxis.major_label_text_font_size = "0pt"
recall_bar.xaxis.major_tick_line_color = None
recall_bar.xaxis.minor_tick_line_color = None
recall_bar.line([0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed")
# Specificity (TNR) Bar
spec_bar = figure(
title="Spec.",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
spec_bar.vbar(
x=0.5, top="specificity", source=metrics.metrics, width=0.5, fill_color=cmap[1]
)
spec_bar.y_range.start = 0.0
spec_bar.y_range.end = 1.0
spec_bar.xgrid.grid_line_color = None
spec_bar.xaxis.major_label_text_font_size = "0pt"
spec_bar.xaxis.major_tick_line_color = None
spec_bar.xaxis.minor_tick_line_color = None
spec_bar.line([0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed")
# Precision Bar
precision_bar = figure(
title="Precision",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
precision_bar.vbar(
x=0.5, top="precision", source=metrics.metrics, width=0.5, fill_color=cmap[1]
)
precision_bar.y_range.start = 0.0
precision_bar.y_range.end = 1.0
precision_bar.xgrid.grid_line_color = None
precision_bar.xaxis.major_label_text_font_size = "0pt"
precision_bar.xaxis.major_tick_line_color = None
precision_bar.xaxis.minor_tick_line_color = None
precision_bar.line(
[0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed"
)
# NPV Bar
npv_bar = figure(
title="NPV",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
npv_bar.vbar(x=0.5, top="npv", source=metrics.metrics, width=0.5, fill_color=cmap[1])
npv_bar.y_range.start = 0.0
npv_bar.y_range.end = 1.0
npv_bar.xgrid.grid_line_color = None
npv_bar.xaxis.major_label_text_font_size = "0pt"
npv_bar.xaxis.major_tick_line_color = None
npv_bar.xaxis.minor_tick_line_color = None
npv_bar.line([0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed")
# F1-Score Bar
f1_bar = figure(
title="F1-Score",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
f1_bar.vbar(
x=0.5,
top="f1",
source=metrics.metrics,
width=0.5,
fill_color=cmap[1],
line_color=cmap[1],
)
f1_bar.y_range.start = 0.0
f1_bar.y_range.end = 1.0
f1_bar.xgrid.grid_line_color = None
f1_bar.xaxis.major_label_text_font_size = "0pt"
f1_bar.xaxis.major_tick_line_color = None
f1_bar.xaxis.minor_tick_line_color = None
# Matthew's Correlation Coefficient Bar
mcc_bar = figure(
title="MCC*",
x_range=[0, 1],
plot_height=300,
plot_width=92,
toolbar_location=None,
# output_backend="webgl",
)
mcc_bar.vbar(
x=0.5,
top="mcc_norm",
source=metrics.metrics,
width=0.5,
fill_color=cmap[1],
line_color=cmap[1],
)
mcc_bar.y_range.start = 0.0
mcc_bar.y_range.end = 1.0
mcc_bar.xgrid.grid_line_color = None
mcc_bar.xaxis.major_label_text_font_size = "0pt"
mcc_bar.xaxis.major_tick_line_color = None
mcc_bar.xaxis.minor_tick_line_color = None
mcc_bar.line([0, 1], [0.5, 0.5], line_width=1, line_color="grey", line_dash="dashed")
# Initialize plot visibility
plot_roc.visible = 0 in checks1.active
roc_auc_bar.visible = 1 in checks1.active
plot_pr.visible = 2 in checks1.active
pr_auc_bar.visible = 3 in checks1.active
plot_cm.visible = 0 in checks2.active
plot_mcc_f1.visible = 1 in checks2.active
acc_bar.visible = 2 in checks2.active
recall_bar.visible = 3 in checks2.active
spec_bar.visible = 0 in checks3.active
precision_bar.visible = 1 in checks3.active
npv_bar.visible = 2 in checks3.active
f1_bar.visible = 3 in checks3.active
mcc_bar.visible = 4 in checks3.active
# ====================================================================
# Arrange plots and widgets in a layout
spacer = Spacer(width=200, height=1)
slider_row1 = row(slider_n0, slider_mean0, slider_sd0, slider_skew0, spacer)
slider_row2 = row(
slider_n1,
slider_mean1,
slider_sd1,
slider_skew1,
threshold_slider,
checks1,
checks2,
checks3,
)
graph_row1 = row(plot_distributions, plot_roc, roc_auc_bar, plot_pr, pr_auc_bar)
graph_row2 = row(
plot_cm,
plot_mcc_f1,
acc_bar,
recall_bar,
spec_bar,
precision_bar,
npv_bar,
f1_bar,
mcc_bar,
)
layout = column(slider_row1, slider_row2, graph_row1, graph_row2)
# Display
curdoc().add_root(layout)