-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheval.py
245 lines (182 loc) · 8.12 KB
/
eval.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
import pickle
import os
from tqdm import tqdm
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
from mpl_toolkits.axes_grid1 import make_axes_locatable
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
import seaborn as sns
sns.set()
import dataloader
def evaluate_BHS_Standard(filename):
"""
Evaluates PPG2ABP based on
BHS Standard Metric
"""
def BHS_metric(err):
"""
Computes the BHS Standard metric
Arguments:
err {array} -- array of absolute error
Returns:
tuple -- tuple of percentage of samples with <=5 mmHg, <=10 mmHg and <=15 mmHg error
"""
leq5 = 0
leq10 = 0
leq15 = 0
for i in range(len(err)):
if(abs(err[i]) <= 5):
leq5 += 1
leq10 += 1
leq15 += 1
elif(abs(err[i]) <= 10):
leq10 += 1
leq15 += 1
elif(abs(err[i]) <= 15):
leq15 += 1
return (leq5*100.0/len(err), leq10*100.0/len(err), leq15*100.0/len(err))
def calcError(Ytrue, Ypred, max_abp, min_abp, max_ppg, min_ppg):
"""
Calculates the absolute error of sbp,dbp,map etc.
Arguments:
Ytrue {array} -- ground truth
Ypred {array} -- predicted
max_abp {float} -- max value of abp signal
min_abp {float} -- min value of abp signal
max_ppg {float} -- max value of ppg signal
min_ppg {float} -- min value of ppg signal
Returns:
tuple -- tuple of abs. errors of sbp, dbp and map calculation
"""
sbps = []
dbps = []
maps = []
maes = []
gt = []
hist = []
for i in (range(len(Ytrue))):
y_t = Ytrue[i].ravel()
y_p = Ypred[i].ravel()
y_t = y_t * (max_abp - min_abp)
y_p = y_p * (max_abp - min_abp)
dbps.append(abs(min(y_t)-min(y_p)))
sbps.append(abs(max(y_t)-max(y_p)))
maps.append(abs(np.mean(y_t)-np.mean(y_p)))
return (sbps, dbps, maps)
dt = pickle.load(open(os.path.join('data', 'test.p'), 'rb')) # loading test data
X_test = dt['X_test']
Y_test = dt['Y_test']
dt = pickle.load(open(os.path.join('data', 'meta.p'), 'rb')) # loading meta data
max_abp = dt['max_abp']
min_abp = dt['min_abp']
Y_pred = pickle.load(open(filename, 'rb')) # loading prediction
(sbps, dbps, maps) = calcError(Y_test, Y_pred, max_abp, min_abp, max_ppg, min_ppg) # compute errors
sbp_percent = BHS_metric(sbps) # compute BHS metric for sbp
dbp_percent = BHS_metric(dbps) # compute BHS metric for dbp
map_percent = BHS_metric(maps) # compute BHS metric for map
print('----------------------------')
print('| BHS-Metric |')
print('----------------------------')
print('----------------------------------------')
print('| | <= 5mmHg | <=10mmHg | <=15mmHg |')
print('----------------------------------------')
print('| DBP | {} % | {} % | {} % |'.format(round(dbp_percent[0], 2), round(dbp_percent[1], 2), round(dbp_percent[2], 2)))
print('| MAP | {} % | {} % | {} % |'.format(round(map_percent[0], 2), round(map_percent[1], 2), round(map_percent[2], 2)))
print('| SBP | {} % | {} % | {} % |'.format(round(sbp_percent[0], 2), round(sbp_percent[1], 2), round(sbp_percent[2], 2)))
print('----------------------------------------')
def evaluate_AAMI_Standard(filename):
"""
Evaluate PPG2ABP using AAMI Standard metric
"""
def calcErrorAAMI(Ypred, Ytrue, max_abp, min_abp, max_ppg, min_ppg):
"""
Calculates error of sbp,dbp,map for AAMI standard computation
Arguments:
Ytrue {array} -- ground truth
Ypred {array} -- predicted
max_abp {float} -- max value of abp signal
min_abp {float} -- min value of abp signal
max_ppg {float} -- max value of ppg signal
min_ppg {float} -- min value of ppg signal
Returns:
tuple -- tuple of errors of sbp, dbp and map calculation
"""
sbps = []
dbps = []
maps = []
for i in (range(len(Ytrue))):
y_t = Ytrue[i].ravel()
y_p = Ypred[i].ravel()
y_t = y_t * (max_abp - min_abp)
y_p = y_p * (max_abp - min_abp)
dbps.append(min(y_p)-min(y_t))
sbps.append(max(y_p)-max(y_t))
maps.append(np.mean(y_p)-np.mean(y_t))
return (sbps, dbps, maps)
dt = pickle.load(open(os.path.join('data', 'test.p'), 'rb')) # loading test data
X_test = dt['X_test']
Y_test = dt['Y_test']
dt = pickle.load(open(os.path.join('data', 'meta.p'), 'rb')) # loading metadata
max_abp = dt['max_abp']
min_abp = dt['min_abp']
Y_pred = pickle.load(open(filename, 'rb')) # loading prediction
(sbps, dbps, maps) = calcErrorAAMI(Y_test, Y_pred, max_abp, min_abp, max_ppg, min_ppg) # compute error
print('---------------------')
print('| AAMI Standard |')
print('---------------------')
print('-----------------------')
print('| | ME | STD |')
print('-----------------------')
print('| DBP | {} | {} |'.format(round(np.mean(dbps), 3), round(np.std(dbps), 3)))
print('| MAP | {} | {} |'.format(round(np.mean(maps), 3), round(np.std(maps), 3)))
print('| SBP | {} | {} |'.format(round(np.mean(sbps), 3), round(np.std(sbps), 3)))
print('-----------------------')
def evaluate_metrics(filename, i):
def calcError(Ytrue, Ypred, max_abp, min_abp, max_ppg, min_ppg):
sbp_t = []
sbp_p = []
dbp_t = []
dbp_p = []
map_t = []
map_p = []
x = 0
y = 0
for i in (range(len(Ytrue))):
y_t = Ytrue[i].ravel()
y_p = Ypred[i].ravel()
y_t = y_t * (max_abp - min_abp)
y_p = y_p * (max_abp - min_abp)
sbp_p.append(abs(max(y_p)))
dbp_p.append(abs(min(y_p)))
map_p.append(abs(np.mean(y_p)))
sbp_t.append(abs(max(y_t)))
dbp_t.append(abs(min(y_t)))
map_t.append(abs(np.mean(y_t)))
print("SBP")
print("Mean Absolute Error : ", round(mean_absolute_error(sbp_t, sbp_p), 3))
print("Root Mean Squared Error : ", round(mean_squared_error(sbp_t, sbp_p, squared=False),3))
print("R2 : ", r2_score(sbp_t, sbp_p))
print("")
print("DBP")
print("Mean Absolute Error : ", round(mean_absolute_error(dbp_t, dbp_p),3))
print("Root Mean Squared Error : ", round(mean_squared_error(dbp_t, dbp_p, squared=False),3))
print("R2 : ", r2_score(dbp_t, dbp_p))
print("")
print("MAP")
print("Mean Absolute Error : ", mean_absolute_error(map_t, map_p))
print("Root Mean Squared Error : ", round(mean_squared_error(map_t, map_p, squared=False), 2))
print("R2 : ", r2_score(map_t, map_p))
print("------------------------------------------------------------------------")
dt = pickle.load(open(os.path.join('data', 'test.p'), 'rb')) # loading test data
X_test = dt['X_test']
Y_test = dt['Y_test']
dt = pickle.load(open(os.path.join('data', 'meta.p'), 'rb')) # loading meta data
max_abp = dt['max_abp']
min_abp = dt['min_abp']
Y_pred = pickle.load(open(filename, 'rb')) # loading prediction
calcError(Y_test, Y_pred, max_abp, min_abp, max_ppg, min_ppg)
evaluate_BHS_Standard('model/final.p')
evaluate_AAMI_Standard('model/final.p')
evaluate_metrics('model/final.p', 4)