-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathpreprocess_signals.py
179 lines (121 loc) · 4.43 KB
/
preprocess_signals.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
import pandas as pd
import numpy as np
import json
import scipy.signal as signal
from biosppy.signals import ecg
from biosppy.signals.tools import filter_signal
import matplotlib.pyplot as plt
from tqdm import tqdm
from Decompose.SBD import SBD
class PreProcessing:
def run(self, X):
# pre-processing
X = np.reshape(X, (-1, 12))
if X.shape[0] < 36000:
X_new = np.zeros((36000, 12))
X_new[: X.shape[0], :] = X[:, :]
for i in range(12):
X_new[X.shape[0] :, i] = X_new[X.shape[0] - 1, i]
X = X_new
elif X.shape[0] > 36000:
X = X[:36000, :]
# downsample
X = self.resample(X)
# remove baseline wander
# X = self.baseline_compensation(X)
# feature engineering
R_peaks = self.apply_r_peaks_segmentation(X)
X = self.normalize_channels(X, R_peaks)
# X = np.concatenate((X, R_peaks.reshape(-1, 1)), axis=1)
X = self.apply_sbd(X).astype(np.float32)
return X
######################## Signal pre-processing #####################
def resample(self, X):
Fs = 500
F1 = 400
q = Fs / F1
q = int(X.shape[0] / q)
X_dec = np.zeros((q, X.shape[1]))
for i in range(X.shape[1]):
X_dec[:, i] = signal.resample(X[:, i], q) # (x=X[:, i], q=q, ftype='iir')
return X_dec
def baseline_compensation(self, X):
win_length = 501
# raw = X[:, 0].copy()
for i in range(X.shape[1]):
channel = X[:, i].copy()
channel = np.concatenate((np.zeros((win_length - 1)), channel))
channel = np.concatenate((channel, np.zeros((int(win_length / 2)))))
channel = pd.Series(channel).rolling(win_length).median().values
X[:, i] -= channel[win_length - 1 + int(win_length / 2) :]
# plt.plot( X[:1000,0])
# plt.plot(raw[:1000])
# plt.show()
return X
def normalize_channels(self, X, peaks):
peaks = np.where(peaks > 0)[0]
scaling_val = np.median(X[peaks, 0])
for i in range(X.shape[1]):
X[:, i] = X[:, i] / scaling_val
return X
def apply_sbd(self, X):
X = np.reshape(X, (1, X.shape[0], -1))
for i in range(1):
SBD_arr = SBD(X[:, :, i])
X = np.concatenate((X, SBD_arr), axis=2)
X = np.reshape(X, (X.shape[1], X.shape[2]))
return X
def apply_r_peaks_segmentation(self, X):
peaks = self.find_peaks(X)
R_peaks = np.zeros((X.shape[0]))
R_peaks[peaks] = 1
return R_peaks
######################## Features #####################
######################## Functions #####################
def find_peaks(self, X, channel=0):
X_processed = X[:, channel].copy()
X_processed = filter_signal(
signal=X_processed, ftype='FIR', band='bandpass', order=150, frequency=[3, 45], sampling_rate=400
)[0]
# check original polarity
ecg_object = ecg.ecg(signal=X_processed, sampling_rate=400, show=False)
peaks_plus = ecg_object['rpeaks'] # rpeak indices
# check reversed polarity
ecg_object = ecg.ecg(signal=-1 * X_processed, sampling_rate=400, show=False)
peaks_minus = ecg_object['rpeaks'] #
# select polarity
if np.abs(np.median(X_processed[peaks_minus])) > np.abs(np.median(X_processed[peaks_plus])):
peaks = peaks_minus.copy()
else:
peaks = peaks_plus.copy()
return peaks
def load_data(filename):
# load waveforms
X = np.load('./data/formatted/' + filename + '.npy')
return X
def save_data(filename, X):
# load waveforms
np.save('./data/processed/' + filename + '.npy', X)
return True
def load_split_table():
records = []
def intersection(lst1, lst2):
lst3 = [value for value in lst1 if value in lst2]
return lst3
train_prev = []
for i in range(5):
data = json.load(open('./data/fold_split/' + f'training_lookup_cv{i+1}.json'))
for i in data['val']:
records.append(i)
return records
def main():
# load a list of files
records = load_split_table()
# object for processing
processing = PreProcessing()
for i in tqdm(records):
signal = load_data(i)
signal = processing.run(signal)
save_data(i, signal)
if __name__ == "__main__":
main()