-
Notifications
You must be signed in to change notification settings - Fork 5
/
record_inference_time.py
377 lines (256 loc) · 11.1 KB
/
record_inference_time.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
import helpermethods
import numpy as np
import sys
import edgeml_pytorch.utils as utils
from edgeml_pytorch.graph.bonsai import Bonsai
import torch
import time
import pandas as pd
from ecgdetectors import Detectors
from scipy.signal import butter, lfilter,filtfilt, iirnotch
from scipy.signal import freqs
import matplotlib.pyplot as plt
from scipy.signal import medfilt
fs = 250
n = 0.5*fs
f_high = 0.5
cut_off = f_high/n
order = 4
def loadModel(currDir):
'''
Load the Saved model and load it to the model using constructor
Returns two dict one for params and other for hyperParams
'''
paramDir = currDir + '/'
paramDict = {}
paramDict['W'] = np.load(paramDir + "W.npy")
paramDict['V'] = np.load(paramDir + "V.npy")
paramDict['T'] = np.load(paramDir + "T.npy")
paramDict['Z'] = np.load(paramDir + "Z.npy")
hyperParamDict = np.load(paramDir + "hyperParam.npy", allow_pickle=True).item()
return paramDict, hyperParamDict
def pipelinedRpeakExtraction(x, fs):
x = detectors.swt_detector(x)
# x = detectors.hamilton_detector(x)
# x = detectors.pan_tompkins_detector(x)
return x
# def get_mean_nni(nn_intervals, fs):
# diff_nni = np.diff(nn_intervals)
# length_int = len(nn_intervals)
# return np.mean(nn_intervals)
def _2017_top_4_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
return nni_50, pnni_50, nni_20, cvsd
def _2017_top_6_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
max_hr = max(heart_rate_list)
return nni_50, pnni_50, nni_20, cvsd, cvnni, max_hr
def _2017_top_8_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
max_hr = max(heart_rate_list)
mean_hr = np.mean(heart_rate_list)
sdnn = np.std(nn_intervals, ddof = 1)
return nni_50, pnni_50, nni_20, cvsd, cvnni, max_hr, mean_hr, sdsd
def _2017_top_10_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
max_hr = max(heart_rate_list)
mean_hr = np.mean(heart_rate_list)
sdnn = np.std(nn_intervals, ddof = 1)
std_hr = np.std(heart_rate_list)
pnni_20 = 100 * nni_20 / length_int
return nni_50, pnni_50, nni_20, cvsd, cvnni, max_hr, mean_hr, sdsd, std_hr, pnni_20
def _2017_top_12_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
max_hr = max(heart_rate_list)
mean_hr = np.mean(heart_rate_list)
sdnn = np.std(nn_intervals, ddof = 1)
std_hr = np.std(heart_rate_list)
pnni_20 = 100 * nni_20 / length_int
rmssd = np.sqrt(np.mean(diff_nni ** 2))
sdnn = np.std(nn_intervals, ddof = 1)
return nni_50, pnni_50, nni_20, cvsd, cvnni, max_hr, mean_hr, sdsd, std_hr, pnni_20, rmssd, sdnn
def afdb_top_4_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
pnni_20 = 100 * nni_20 / length_int
return np.array([nni_20, nni_50, pnni_20, pnni_50, 1])
def afdb_top_6_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
pnni_20 = 100 * nni_20 / length_int
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
return nni_20, nni_50, pnni_20, pnni_50, cvnni, cvsd
def afdb_top_8_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
pnni_20 = 100 * nni_20 / length_int
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
std_hr = np.std(heart_rate_list)
return nni_20, nni_50, pnni_20, pnni_50, cvnni, cvsd, sdnn, std_hr
def afdb_top_10_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
pnni_20 = 100 * nni_20 / length_int
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
std_hr = np.std(heart_rate_list)
max_hr = max(heart_rate_list)
sdsd = np.std(diff_nni)
return nni_20, nni_50, pnni_20, pnni_50, cvnni, cvsd, sdnn, std_hr, max_hr, sdsd
def afdb_top_12_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
pnni_20 = 100 * nni_20 / length_int
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
std_hr = np.std(heart_rate_list)
max_hr = max(heart_rate_list)
sdsd = np.std(diff_nni)
mean_hr = np.mean(heart_rate_list)
rmssd = np.sqrt(np.mean(diff_nni ** 2))
return nni_20, nni_50, pnni_20, pnni_50, cvnni, cvsd, sdnn, std_hr, max_hr, sdsd, mean_hr, rmssd
def afdb_top_14_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
abs_diff = np.abs(diff_nni)
length_int = len(nn_intervals)
mean_nni = np.mean(nn_intervals)
rmssd = np.sqrt(np.mean(diff_nni ** 2))
nni_50 = sum(abs_diff > 12.5)
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(abs_diff > 5)
pnni_20 = 100 * nni_20 / length_int
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / mean_nni
cvsd = rmssd / mean_nni
heart_rate_list = np.divide(60, nn_intervals)
std_hr = np.std(heart_rate_list)
max_hr = max(heart_rate_list)
sdsd = np.std(diff_nni)
mean_hr = np.mean(heart_rate_list)
min_hr = min(heart_rate_list)
return np.array([nni_20, nni_50, pnni_20, pnni_50, cvnni, cvsd, sdnn, std_hr, max_hr, sdsd, mean_hr, rmssd, min_hr, mean_nni, 1])
def _2017_top_14_features(nn_intervals, fs):
diff_nni = np.diff(nn_intervals)
length_int = len(nn_intervals)
nni_50 = sum(np.abs(diff_nni) > (50*fs/1000))
pnni_50 = 100 * nni_50 / length_int
nni_20 = sum(np.abs(diff_nni) > (20*fs/1000))
cvsd = np.sqrt(np.mean(diff_nni ** 2)) / np.mean(nn_intervals)
sdnn = np.std(nn_intervals, ddof = 1)
cvnni = sdnn / np.mean(nn_intervals)
heart_rate_list = np.divide(60000, nn_intervals)
max_hr = max(heart_rate_list)
mean_hr = np.mean(heart_rate_list)
sdnn = np.std(nn_intervals, ddof = 1)
std_hr = np.std(heart_rate_list)
pnni_20 = 100 * nni_20 / length_int
rmssd = np.sqrt(np.mean(diff_nni ** 2))
sdnn = np.std(nn_intervals, ddof = 1)
min_hr = min(heart_rate_list)
mean_nni = np.mean(nn_intervals)
return nni_50, pnni_50, nni_20, cvsd, cvnni, max_hr, mean_hr, sdsd, std_hr, pnni_20, rmssd, sdnn, min_hr, mean_nni
device = torch.device("cpu")
MODEL_DIR = "/hdd/physio/edgeml/examples/pytorch/Bonsai/AFDB_top14/PyTorchBonsaiResults/16_50_10_09_08_21"
paramDict, hyperParamDict = loadModel(MODEL_DIR)
bonsai = Bonsai(hyperParamDict['numClasses'], hyperParamDict['dataDim'], hyperParamDict['projDim'],
hyperParamDict['depth'], hyperParamDict['sigma'], W=paramDict['W'], T=paramDict['T'], V=paramDict['V'],
Z=paramDict['Z']).to(device)
sigmaI = 1e9
def normalize(data):
return (data - np.min(data)) / (np.max(data) - np.min(data))
window = np.load("1window.npy")
fs = 250
detectors = Detectors(fs)
b, a = butter(order, cut_off,btype='high')
times = []
for i in range(int(sys.argv[1])):
start = time.time()
window = filtfilt(b, a, window)
window = normalize(window)
x = pipelinedRpeakExtraction(window, fs)
x = np.diff(x)
features = afdb_top_14_features(x, fs)
_, _ = bonsai(torch.from_numpy(features.astype(np.float32)), sigmaI)
end = time.time()
times.append(end - start)
print("features + model + baseline wander removal: ", np.mean(times)*1000, "ms")
times = []
for i in range(int(sys.argv[1])):
start = time.time()
x = pipelinedRpeakExtraction(window, fs)
x = np.diff(x)
features = afdb_top_14_features(x, fs)
_, _ = bonsai(torch.from_numpy(features.astype(np.float32)), sigmaI)
end = time.time()
times.append(end - start)
print("features + model : ", np.mean(times)*1000, "ms")
print("features + model : max", np.max(times)*1000, "ms")
print("features + model : min", np.min(times)*1000, "ms")
times = []
for i in range(int(sys.argv[1])):
start = time.time()
_, _ = bonsai(torch.from_numpy(features.astype(np.float32)), sigmaI)
end = time.time()
times.append(end - start)
print("model : ", np.mean(times)*1000, "ms")