|
a |
|
b/OMNI_OpenBCI_Pi_Inference/utils.py |
|
|
1 |
import numpy as np |
|
|
2 |
import scipy.signal |
|
|
3 |
import scipy.io |
|
|
4 |
import pickle |
|
|
5 |
from glob import glob |
|
|
6 |
import os |
|
|
7 |
from tqdm import tqdm |
|
|
8 |
import pandas as pd |
|
|
9 |
from sklearn.preprocessing import StandardScaler |
|
|
10 |
|
|
|
11 |
import torch |
|
|
12 |
from torch.utils.data import TensorDataset,DataLoader |
|
|
13 |
from torch.autograd import Variable |
|
|
14 |
|
|
|
15 |
def custom_resample(ECG,fs): |
|
|
16 |
modified_ECG = [] |
|
|
17 |
for i in range(int(len(ECG) * 500/fs)): |
|
|
18 |
modified_ECG.append(ECG[int(fs/500*i)].astype(float)) |
|
|
19 |
return modified_ECG |
|
|
20 |
|
|
|
21 |
def peak_correction(peak_locs,ecg_records): |
|
|
22 |
|
|
|
23 |
mod_peak_locs = [] |
|
|
24 |
ecg_records = ecg_records.cpu().numpy() |
|
|
25 |
for j in range(len(peak_locs)): |
|
|
26 |
mod_peak_locs.append(np.asarray([peak_locs[j][i] - 37 + np.argmax(ecg_records[j,0,peak_locs[j][i]-37:peak_locs[j][i]+37]) for i in range(len(peak_locs[j])) if(peak_locs[j][i]>37 and peak_locs[j][i]<5000-37)])) |
|
|
27 |
return mod_peak_locs |
|
|
28 |
|
|
|
29 |
def peak_finder(y_pred_array,x_test): |
|
|
30 |
|
|
|
31 |
fs_ = 500 |
|
|
32 |
peak_locs = [] |
|
|
33 |
for i in range(y_pred_array.shape[0]): |
|
|
34 |
peak_locs.append(scipy.signal.find_peaks(-y_pred_array[i,:],distance = 120,height = -0.4, prominence = 0.035)[0]) |
|
|
35 |
peak_locs[i] = peak_locs[i][(peak_locs[i] >= 0.5*fs_) & (peak_locs[i] <= 9.5*fs_)] |
|
|
36 |
modified_peak_locs = peak_correction(peak_locs,x_test) |
|
|
37 |
return modified_peak_locs |
|
|
38 |
|
|
|
39 |
def compute_heart_rate(r_peaks): |
|
|
40 |
fs_ = 500 |
|
|
41 |
r_peaks = r_peaks[(r_peaks >= 0.5*fs_) & (r_peaks <= 9.5*fs_)] |
|
|
42 |
return round( 60 * fs_ / np.mean(np.diff(r_peaks))) |
|
|
43 |
|