-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulti_pulse_process.py
145 lines (99 loc) · 3.94 KB
/
multi_pulse_process.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
"""
IMPORTANT NOTE:
As CU-ECG dataset is a private dataset, we're unable to provide the full details of the dataset. This script is the
data preprocessor in which we filter, downsample and get pulse segments. That being said, you can find the pulse
segments in "data" folder so that you can reproduce the outputs. Note that the original dataset takes 79 GB whereas
given data goes up to 45 MB at most.
More explanation regarding dataset is available in readme.txt.
"""
import numpy as np
import pandas as pd
from utils import *
import scipy.io.wavfile
import scipy.signal
import matplotlib.pyplot as plt
import pickle
import pywt
from sklearn.metrics.pairwise import manhattan_distances
import glob
from matplotlib import pyplot
df = pd.read_csv("D:\ECGDATA\ECGDB\\1\\1.csv")
df.columns = ['Timestamp', 'Peak(mV)']
del df["Timestamp"]
df['Peak(mV)'].plot()
f2, f1 = scipy.signal.butter(3, 0.01)
filtered = scipy.signal.filtfilt(f2, f1, df['Peak(mV)'])
plt.show()
### Raw versus Filtered ###
times = np.arange(0, len(df['Peak(mV)']), 1)
plt.figure(figsize=(10, 4))
plt.subplot(121)
plt.plot(times, df['Peak(mV)'])
plt.title("ECG Signal with Noise")
plt.margins(0, .05)
plt.subplot(122)
plt.plot(times, filtered)
plt.title("Filtered ECG Signal")
plt.margins(0, .05)
plt.tight_layout()
plt.show()
### Filtered and Downsampled ECG Signal ###
filtered = filtered[:500000]
R = 1000
filtered_downsampled = filtered.reshape(-1, R).mean(axis=1)
plt.title("Downsampled ECG data")
ts = np.arange(0, len(filtered_downsampled), 1)
plt.plot(ts, filtered_downsampled, color="blue")
plt.show()
test_sample = filtered_downsampled[50:80]
plt.title("Test sample")
ts_test_sample = np.arange(0, len(test_sample), 1)
plt.plot(ts_test_sample, test_sample, color="red")
plt.show()
R = 1000
test_list = [test_sample.tolist()]
glued_data = pd.DataFrame()
diff_score = []
org_ind = []
n = 3
for folder_name in glob.glob("D:\ECGDATA\ECGDB\*"):
print(folder_name)
for file_name in glob.glob(folder_name + '/*.csv'):
dfx = pd.read_csv(file_name)
dfx.columns = ['Timestamp', 'Peak(mV)']
del dfx["Timestamp"]
dx_filtered = scipy.signal.filtfilt(f2, f1, dfx['Peak(mV)'])
dx_times = np.arange(0, len(dfx['Peak(mV)']), 1)
if len(dx_filtered)>500000:
dx_filtered = dx_filtered[:500000]
dx_filtered_aranged = np.append(dx_filtered,np.zeros(500000-len(dx_filtered)))
dx_filtered_downsampled = dx_filtered_aranged.reshape(-1, R).mean(axis=1)
dx_scores = multi_similar_pulse_manhattan(dx_filtered_downsampled, test_list)
diff_score += [getSmallest(dx_scores, len(dx_scores), n)[0]]
org_ind += [getSmallest(dx_scores, len(dx_scores), n)[1][:n]]
# using org_ind array, lets create the pulses as follows:
pulse_extract = []
j=0
for folder_name in glob.glob("D:\ECGDATA\ECGDB\*"):
print(folder_name)
for file_name in glob.glob(folder_name +'/*.csv'):
dfx = pd.read_csv(file_name)
dfx.columns = ['Timestamp', 'Peak(mV)']
del dfx["Timestamp"]
dx_filtered = scipy.signal.filtfilt(f2, f1, dfx['Peak(mV)'])
dx_times = np.arange(0, len(dfx['Peak(mV)']), 1)
if len(dx_filtered) > 500000:
dx_filtered = dx_filtered[:500000]
dx_filtered_aranged = np.append(dx_filtered, np.zeros(500000 - len(dx_filtered)))
dx_filtered_downsampled = dx_filtered_aranged.reshape(-1, R).mean(axis=1)
for i in range(n):
pulse_extract += [list(dx_filtered_downsampled[org_ind[j][i] * 5:org_ind[j][i] * 5 + 30])]
j = j + 1
plt.title("Extracted Pulse")
ts_test_sample = np.arange(0, len(pulse_extract[200]), 1)
plt.plot(ts_test_sample, pulse_extract[200], color="red")
plt.show()
"""
with open("D:\ECGDATA\ECGPulses\\three_pulse_extract.txt", "wb") as fp: #Pickling
pickle.dump(pulse_extract, fp)
"""