-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
204 lines (194 loc) · 8.45 KB
/
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
import pandas as pd
import numpy as np
from sklearn import metrics
from sklearn.metrics import confusion_matrix, classification_report, average_precision_score, precision_recall_curve, accuracy_score, confusion_matrix, average_precision_score
def episode_metrics(model, x, parameters, rhythm, out_message=False):
""" arg
"""
# Rhythmn predictions
predictions_qa, predictions_r = model.predict(x)
# If the estimated prob of AF(column 1) is higher than the estimated prob of non-AF(column 0)
# than consider the window as an AF window.
y_predictions = np.argmax(predictions_r, axis=1)
y_truth = np.argmax(rhythm, axis=1)
# Confusion_matrix
cf = confusion_matrix(y_truth, y_predictions, labels=[0, 1])
TN, FP, FN, TP = cf.ravel()
support = TN+FP+FN+TP
# Sensitivity, 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)
# F1 score
f1 = metrics.f1_score(y_truth, y_predictions, average=None)
# selecting f1score for positive case if present
f1_pos = f1[1] if len(f1) > 1 else f1[0]
# Area under precision recall curve
#auprc = metrics.average_precision_score(y_truth, rhythm[:,1])
if out_message:
print(pd.DataFrame(cf).rename(columns={0: "Predicted Non-AF", 1:" Predicted AF"}, index={0: "True Non-AF", 1:"True AF"}))
print("Sensitivity/Recall: %0.4f" % TPR)
print("Specificity: %0.4f" % TNR)
print("Precision/PPV: %0.4f" % PPV)
print("Negative predictive value/NPV: %0.2f" % NPV)
print("False positive rate: %0.4f" % FPR)
print("False negative rate: %0.4f" % FNR)
print("F1 score: %0.4f" % f1_pos)
print('support: ', support)
print('\n\n\n')
episode_metrics = [TPR, TNR, PPV, NPV, FPR, FNR, f1_pos, support]
return episode_metrics
def episode_metrics_singletask(model, x, parameters, rhythm, out_message=False):
""" arg
"""
# Rhythmn predictions
predictions_r = model.predict(x)
# If the estimated prob of AF(column 1) is higher than the estimated prob of non-AF(column 0)
# than consider the window as an AF window.
y_predictions = np.argmax(predictions_r, axis=1)
y_truth = np.argmax(rhythm, axis=1)
# Confusion_matrix
cf = confusion_matrix(y_truth, y_predictions, labels=[0, 1])
TN, FP, FN, TP = cf.ravel()
support = TN+FP+FN+TP
# Sensitivity, 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)
# F1 score
f1 = metrics.f1_score(y_truth, y_predictions, average=None)
# selecting f1score for positive case if present
f1_pos = f1[1] if len(f1) > 1 else f1[0]
# Area under precision recall curve
#auprc = metrics.average_precision_score(y_truth, rhythm[:,1])
if out_message:
print(pd.DataFrame(cf).rename(columns={0: "Predicted Non-AF", 1:" Predicted AF"}, index={0: "True Non-AF", 1:"True AF"}))
print("Sensitivity/Recall: %0.4f" % TPR)
print("Specificity: %0.4f" % TNR)
print("Precision/PPV: %0.4f" % PPV)
print("Negative predictive value/NPV: %0.2f" % NPV)
print("False positive rate: %0.4f" % FPR)
print("False negative rate: %0.4f" % FNR)
print("F1 score: %0.4f" % f1_pos)
print('support: ', support)
print('\n\n\n')
episode_metrics = [TPR, TNR, PPV, NPV, FPR, FNR, f1_pos, support]
return episode_metrics
def collecting_individual_metrics(model, x, parameters, rhythm, out_message=False):
"""
"""
individual_metrics = {}
for i in np.unique(parameters['ID']):
# Sub-selecting individuals
p_indx = np.where(parameters['ID'] == i)[0]
x_pID = x[p_indx]
parameters_pID = parameters.iloc[p_indx]
rhythm_pID = rhythm[p_indx]
# Rhythmn predictions
predictions_qa, predictions_r = model.predict(x_pID)
# If the estimated prob of AF(column 1) is higher than the estimated prob of non-AF(column 0)
# than consider the window as an AF window.
y_predictions = np.argmax(predictions_r, axis=1)
y_truth = np.argmax(rhythm_pID, axis=1)
# Confusion_matrix
cf = confusion_matrix(y_truth, y_predictions, labels=[0, 1])
TN, FP, FN, TP = cf.ravel()
support = TN+FP+FN+TP
# Sensitivity, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# F1 score
f1 = metrics.f1_score(y_truth, y_predictions, average=None)
# selecting f1score for positive case if present
f1_pos = f1[1] if len(f1) > 1 else f1[0]
# Area under precision recall curve
#auprc = metrics.average_precision_score(y_test_truth, rhythm_pID[:,1])
if (TP + FN) > 0:
individual_metrics[i] = [TPR, np.NaN, np.NaN, FNR, f1_pos, support]
if (FP + TN) > 0:
individual_metrics[i] = [np.NaN, TNR, FPR, np.NaN, np.NaN, support]
if out_message:
print("PATIENT :", i , '\n')
print(pd.DataFrame(cf).rename(columns={0: "Predicted Non-AF", 1:" Predicted AF"}, index={0: "True Non-AF", 1:"True AF"}))
if (TP + FN) > 0:
print("Sensitivity/Recall: %0.4f" % TPR)
print("False negative rate: %0.4f" % FNR)
print("F1 score: %0.4f" % f1_pos)
print('support: ' , support)
if (FP + TN) > 0:
print("Specificity: %0.4f" % TNR)
print("False positive rate: %0.4f" % FPR)
print('support: ' , support)
return individual_metrics
def collecting_individual_metrics_singletask(model, x, parameters, rhythm, out_message=False):
"""
"""
individual_metrics = {}
for i in np.unique(parameters['ID']):
# Sub-selecting individuals
p_indx = np.where(parameters['ID'] == i)[0]
x_pID = x[p_indx]
parameters_pID = parameters.iloc[p_indx]
rhythm_pID = rhythm[p_indx]
# Rhythmn predictions
predictions_r = model.predict(x_pID)
# If the estimated prob of AF(column 1) is higher than the estimated prob of non-AF(column 0)
# than consider the window as an AF window.
y_predictions = np.argmax(predictions_r, axis=1)
y_truth = np.argmax(rhythm_pID, axis=1)
# Confusion_matrix
cf = confusion_matrix(y_truth, y_predictions, labels=[0, 1])
TN, FP, FN, TP = cf.ravel()
support = TN+FP+FN+TP
# Sensitivity, recall, or true positive rate
TPR = TP/(TP+FN)
# Specificity or true negative rate
TNR = TN/(TN+FP)
# Fall out or false positive rate
FPR = FP/(FP+TN)
# False negative rate
FNR = FN/(TP+FN)
# F1 score
f1 = metrics.f1_score(y_truth, y_predictions, average=None)
# selecting f1score for positive case if present
f1_pos = f1[1] if len(f1) > 1 else f1[0]
# Area under precision recall curve
#auprc = metrics.average_precision_score(y_test_truth, rhythm_pID[:,1])
if (TP + FN) > 0:
individual_metrics[i] = [TPR, np.NaN, np.NaN, FNR, f1_pos, support]
if (FP + TN) > 0:
individual_metrics[i] = [np.NaN, TNR, FPR, np.NaN, np.NaN, support]
if out_message:
print("PATIENT :", i , '\n')
print(pd.DataFrame(cf).rename(columns={0: "Predicted Non-AF", 1:" Predicted AF"}, index={0: "True Non-AF", 1:"True AF"}))
if (TP + FN) > 0:
print("Sensitivity/Recall: %0.4f" % TPR)
print("False negative rate: %0.4f" % FNR)
print("F1 score: %0.4f" % f1_pos)
print('support: ' , support)
if (FP + TN) > 0:
print("Specificity: %0.4f" % TNR)
print("False positive rate: %0.4f" % FPR)
print('support: ' , support)
return individual_metrics