-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroc.py
484 lines (434 loc) · 19.4 KB
/
roc.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
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from itertools import cycle
from sklearn.metrics import auc, roc_auc_score, roc_curve
# from matplotlib.patches import Polygon
from shapely.geometry import Polygon
from descartes import PolygonPatch
def roc(y_test, y_score, title, posLabel=None, dst='roc.png'):
lw = 2 #line width
plt.figure()
# Case for multiple scores or one
if isinstance(y_score[0], float):
if (not posLabel):
fpr, tpr, whoknowswhatthisis = roc_curve(y_test, y_score)
else:
fpr, tpr, whoknowswhatthisis = roc_curve(y_test, y_score, pos_label=posLabel)
tp_std, fp_std = np.std(tpr), np.std(fpr)
fp_UB, tp_UB, fp_LB, tp_LB = fpr-fp_std, tpr+tp_std, fpr+fp_std, tpr-tp_std
rocauc = auc(fpr, tpr)
roc_df = pd.DataFrame({
'FP1': fpr,
'TP1': tpr,
'FP1UB': fp_UB,
'TP1UB': tp_UB,
'FP1LB': fp_LB,
'TP1LB': tp_LB
})
roc_augmented(roc_df, title)
#then single plot
# plt.plot(
# fpr,
# tpr,
# color='darkorange',
# label=f'ROC curve (auc = {rocauc:.2})'
# )
else:
#then multiple plots
colors = cycle(['aqua', 'yellowgreen', 'cornflowerblue', 'purple', 'orange'])
for i, (scoreDescriptor, scores, y_test) in enumerate(y_score):
if (not posLabel):
fpr, tpr, whoknowswhatthisis = roc_curve(y_test, scores)
else:
fpr, tpr, whoknowswhatthisis = roc_curve(y_test, scores, pos_label=posLabel)
tp_std, fp_std = np.std(tpr), np.std(fpr)
fp_UB, tp_UB, fp_LB, tp_LB = fpr-fp_std, tpr+tp_std, fpr+fp_std, tpr-tp_std
rocauc = auc(fpr, tpr)
roc_df = pd.DataFrame({
'FP1': fpr,
'TP1': tpr,
'FP1UB': fp_UB,
'TP1UB': tp_UB,
'FP1LB': fp_LB,
'TP1LB': tp_LB
})
roc_augmented(roc_df, title)
rocauc = auc(fpr, tpr)
#then single plot
# plt.plot(
# fpr,
# tpr,
# color=next(colors),
# label=f'{scoreDescriptor} (auc = {rocauc:.2})'
# )
# plt.plot([0, 1], [0, 1], color="navy", lw=lw, linestyle="--")
# plt.xlim([0.0, 1.0])
# plt.ylim([0.0, 1.05])
# plt.xlabel("False Positive Rate")
# plt.ylabel("True Positive Rate")
# plt.title(title)
# plt.legend(loc='lower right')
plt.savefig(
dst
)
def roc_augmented(a1: pd.DataFrame, title: str):
## ROC curves of a random classifier, for reference
RANDOM_FP = np.arange(0, 1., 0.01)
RANDOM_TP = np.arange(0, 1., 0.01)
RANDOM_FP[0] = 1e-4
RANDOM_TP[0] = 1e-4
color='blue'
## Mean and confidence bands of FPR V.S. TPR curve
# a1 = pd.read_csv('./'+op+'A1_roc_std.csv')
# a1 = pd.read_csv('./A1_roc_std.csv')
conf_UB = a1[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a1[['FP1LB', 'TP1LB']].iloc[range(a1.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
conf.reset_index(inplace=True)
fig = plt.figure(figsize=(15, 4))
## FPR V.S. TPR curve
ax = fig.add_subplot(1, 3, 1)
plt.plot(a1['FP1'], a1['TP1'], color=color, label='RandomForest')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
print(poly.exterior)
print(poly.interiors)
# print(conf)
# print(poly)
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xlim(-0.05, 1.05)
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
## FPR (log-scale) V.S. TPR curve
ax = fig.add_subplot(1, 3, 2)
plt.plot(a1['FP1'], a1['TP1'], color=color, label='RandomForest')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
plt.title(title, fontsize=15, pad=10)
# Plot the three ROC graphs
# op=output prefix
# import os
# import numpy as np
# import pandas as pd
def three_roc_plot_mult(title='ROC Curves', op='', predictionsOutput=None, model="rf.model", predictionsFile="~/workspace/afib_detection/testset_binary_norm.csv"):
# ! ./random_forest load {model} option predict testds {predictionsFile}
if (predictionsOutput is None):
os.system(f"./random_forest load {model} option predict testds {predictionsFile}")
# ! sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv
os.system(f'sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv')
else:
os.system(f'sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv')
# ! ./random_forest option roc ds {op}predictions.csv
# ! python ../afib_detection/2class_process_roc_stds.py A1_roc
# ! python ../afib_detection/2class_process_roc_stds.py A2_roc
os.system(f"./random_forest option roc ds {op}predictions.csv")
os.system(f"python ../afib_detection/data/2class_process_roc_stds.py A1_roc")
os.system(f"python ../afib_detection/data/2class_process_roc_stds.py A2_roc")
## three roc curves in one plot
## ROC curves of a random classifier, for reference
RANDOM_FP = np.arange(0, 1., 0.01)
RANDOM_TP = np.arange(0, 1., 0.01)
RANDOM_FP[0] = 1e-4
RANDOM_TP[0] = 1e-4
color='blue'
## Mean and confidence bands of FPR V.S. TPR curve
# a1 = pd.read_csv('./'+op+'A1_roc_std.csv')
a1 = pd.read_csv('./A1_roc_std.csv')
conf_UB = a1[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a1[['FP1LB', 'TP1LB']].iloc[range(a1.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
fig = plt.figure(figsize=(15, 4))
## FPR V.S. TPR curve
ax = fig.add_subplot(1, 3, 1)
plt.plot(a1['FP1'], a1['TP1'], color=color, label='RandomForest')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xlim(-0.05, 1.05)
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
## FPR (log-scale) V.S. TPR curve
ax = fig.add_subplot(1, 3, 2)
plt.plot(a1['FP1'], a1['TP1'], color=color, label='RandomForest')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
plt.title(title, fontsize=15, pad=10)
## here A2 treats the 2nd class 'usa' as positive and the 1st class 'asia' as negative
a2 = pd.read_csv('./A2_roc_std.csv')
conf_UB = a2[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a2[['FP1LB', 'TP1LB']].iloc[range(a2.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
ax = fig.add_subplot(1, 3, 3)
plt.plot(a2['FP1'], a2['TP1'], color=color, label='RandomForest') #plt.plot(a2['FP1'], a2['TP1'], color='C0')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4) #poly = PolygonPatch(poly, linewidth=0, fc='C0', alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FNR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TNR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
# three_roc_plot(fig=p, op="lr", predictionsOutput='yup', title="LogisticRegression ROCs")
op = 'lr'; predictionsOutput='yup'; title='LogisticRegression ROCs'
if (predictionsOutput is None):
os.system(f"./random_forest load {model} option predict testds {predictionsFile}")
# ! sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv
os.system(f'sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv')
else:
os.system(f'sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv')
# ! ./random_forest option roc ds {op}predictions.csv
# ! python ../afib_detection/2class_process_roc_stds.py A1_roc
# ! python ../afib_detection/2class_process_roc_stds.py A2_roc
os.system(f"./random_forest option roc ds {op}predictions.csv")
os.system(f"python ../afib_detection/data/2class_process_roc_stds.py A1_roc")
os.system(f"python ../afib_detection/data/2class_process_roc_stds.py A2_roc")
## three roc curves in one plot
## ROC curves of a random classifier, for reference
RANDOM_FP = np.arange(0, 1., 0.01)
RANDOM_TP = np.arange(0, 1., 0.01)
RANDOM_FP[0] = 1e-4
RANDOM_TP[0] = 1e-4
## Mean and confidence bands of FPR V.S. TPR curve
# a1 = pd.read_csv('./'+op+'A1_roc_std.csv')
a1 = pd.read_csv('./A1_roc_std.csv')
conf_UB = a1[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a1[['FP1LB', 'TP1LB']].iloc[range(a1.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
color = 'orange'
## FPR V.S. TPR curve
ax = plt.subplot(1, 3, 1)
plt.plot(a1['FP1'], a1['TP1'], color=color, label='LogisticRegressor')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xlim(-0.05, 1.05)
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
## FPR (log-scale) V.S. TPR curve
ax = plt.subplot(1, 3, 2)
plt.plot(a1['FP1'], a1['TP1'], color=color, label='LogisticRegressor')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
plt.title('ROC Comparison', fontsize=15, pad=10)
## here A2 treats the 2nd class 'usa' as positive and the 1st class 'asia' as negative
a2 = pd.read_csv('./A2_roc_std.csv')
conf_UB = a2[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a2[['FP1LB', 'TP1LB']].iloc[range(a2.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
ax = plt.subplot(1, 3, 3)
plt.plot(a2['FP1'], a2['TP1'], color=color, label='LogisticRegressor') #plt.plot(a2['FP1'], a2['TP1'], color='C0')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc=color, alpha=0.4) #poly = PolygonPatch(poly, linewidth=0, fc='C0', alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FNR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TNR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
plt.subplots_adjust(wspace=0.4)
# plt.show()
plt.gcf().set_size_inches(12, 5)
from sklearn.metrics import confusion_matrix
df = pd.read_csv('~/Downloads/phillips_alerts_final.csv')
print(df[df['is_afib']==1])
print(confusion_matrix(df['final_label'], df['is_afib']))
cnf_matrix = confusion_matrix(df['final_label'], df['is_afib'])
print(cnf_matrix)
FP = cnf_matrix.sum(axis=0) - np.diag(cnf_matrix)
FN = cnf_matrix.sum(axis=1) - np.diag(cnf_matrix)
TP = np.diag(cnf_matrix)
TN = cnf_matrix.sum() - (FP + FN + TP)
FP = FP.astype(float)[1]
FN = FN.astype(float)[1]
TP = TP.astype(float)[1]
TN = TN.astype(float)[1]
print(FP, TP, FN, TN)
# Sensitivity, hit rate, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP)
# Precision or positive predictive value
PPV = TP/(TP+FP)
# Negative predictive value
NPV = TN/(TN+FN)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
print(TPR, FPR)
plt.subplot(1, 3, 1)
plt.plot([FPR], [TPR], color='GREEN', marker="1", label="Phillips")
plt.subplot(1, 3, 2)
plt.plot([FPR], [TPR], color='GREEN', marker="1", label="Phillips")
plt.subplot(1, 3, 3)
plt.plot([FNR], [TNR], color='GREEN', marker="1", label="Phillips")
plt.legend()
plt.savefig(
f'/home/romman/workspace/afib_detection/results/assets/roc.png'
)
def three_roc_plot(title='ROC Curves', op='', predictionsOutput=None, model="rf.model", predictionsFile="~/workspace/afib_detection/testset_binary_norm.csv"):
# p = three_roc_plot(title="Auton RandomForest ROCs")
# ! ./random_forest load {model} option predict testds {predictionsFile}
# three_roc_plot(fig=p, op="lr", predictionsOutput='yup', title="LogisticRegression ROCs")
if (predictionsOutput is None):
os.system(f"./random_forest load {model} option predict testds {predictionsFile}")
# ! sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv
os.system(f'sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv')
else:
os.system(f'sed -i "1s/.*/A0,A1,true_output/" {op}predictions.csv')
# ! ./random_forest option roc ds {op}predictions.csv
# ! python ../afib_detection/2class_process_roc_stds.py A1_roc
# ! python ../afib_detection/2class_process_roc_stds.py A2_roc
os.system(f"./random_forest option roc ds {op}predictions.csv")
os.system(f"python ../afib_detection/data/2class_process_roc_stds.py A1_roc")
os.system(f"python ../afib_detection/data/2class_process_roc_stds.py A2_roc")
## three roc curves in one plot
## ROC curves of a random classifier, for reference
RANDOM_FP = np.arange(0, 1., 0.01)
RANDOM_TP = np.arange(0, 1., 0.01)
RANDOM_FP[0] = 1e-4
RANDOM_TP[0] = 1e-4
## Mean and confidence bands of FPR V.S. TPR curve
# a1 = pd.read_csv('./'+op+'A1_roc_std.csv')
a1 = pd.read_csv('./A1_roc_std.csv')
conf_UB = a1[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a1[['FP1LB', 'TP1LB']].iloc[range(a1.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
if (not fig):
fig = plt.figure(figsize=(15, 4))
## FPR V.S. TPR curve
ax = fig.add_subplot(1, 3, 1)
plt.plot(a1['FP1'], a1['TP1'], color='C0')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc='C0', alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xlim(-0.05, 1.05)
plt.xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0], [0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
## FPR (log-scale) V.S. TPR curve
ax = fig.add_subplot(1, 3, 2)
plt.plot(a1['FP1'], a1['TP1'], color='C0')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc='C0', alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FPR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TPR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
plt.title(title, fontsize=15, pad=10)
## here A2 treats the 2nd class 'usa' as positive and the 1st class 'asia' as negative
a2 = pd.read_csv('./A2_roc_std.csv')
conf_UB = a2[['FP1UB', 'TP1UB']].copy()
conf_UB.columns = ['X', 'Y']
conf_LB = a2[['FP1LB', 'TP1LB']].iloc[range(a2.shape[0]-1, -1, -1)]
conf_LB.columns = ['X', 'Y']
conf = pd.concat((conf_UB, conf_LB))
ax = fig.add_subplot(1, 3, 3)
plt.plot(a2['FP1'], a2['TP1'], color='C0') #plt.plot(a2['FP1'], a2['TP1'], color='C0')
poly = Polygon(list(zip(conf['X'], conf['Y'])))
poly = PolygonPatch(poly, linewidth=0, fc='C0', alpha=0.4) #poly = PolygonPatch(poly, linewidth=0, fc='C0', alpha=0.4)
ax.add_patch(poly)
plt.plot(RANDOM_FP, RANDOM_TP, color='black', linestyle='--')
plt.xlabel('FNR', fontsize=14)
plt.xscale('log',base=10)
plt.xlim(0.6*1e-4, 1.6)
plt.xticks([1e-4, 1e-3, 1e-2, 1e-1, 1.0], ['$10^{-4}$', '$10^{-3}$', '$10^{-2}$', '$10^{-1}$', '1.0'])
plt.ylabel('TNR', fontsize=14)
plt.ylim(-0.05, 1.05)
plt.yticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
plt.tick_params(axis='both', which='major', labelsize=14)
plt.grid(linestyle='--')
plt.subplots_adjust(wspace=0.4)
# plt.show()
if (predictionsOutput):
plt.gcf().set_size_inches(12, 5)
plt.savefig(
f'/home/romman/workspace/afib_detection/results/assets/roc_{title}.png'
)
return fig