-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
145 lines (118 loc) · 3.85 KB
/
main.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
from pandas import DataFrame
import pandas as pd
import numpy as np
from numpy import diff
from scipy.stats import linregress
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt
df = pd.read_csv('/Users/twitter/Desktop/Python Projects/Sample Data/Sample_ECG5.txt', delimiter = "\t", index_col=0)
frame = DataFrame(df)
frame.index.names = ['time']
df.columns=['ecg', 'bp']
#Low pass filter for ECG signal
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y
Filtered_ECG = butter_lowpass_filter(frame['ecg'], 3.667, 1000.0, order=5)
# Filter requirements.
order = 6
fs = 30.0 # sample rate, Hz
cutoff = 3.667 # desired cutoff frequency of the filter, Hz
# Get the filter coefficients so we can check its frequency response.
b, a = butter_lowpass(cutoff, fs, order)
frame['filtered_ecg']= Filtered_ECG
#QRS threshold
threshold = (np.max(frame.filtered_ecg) - (np.max(frame.filtered_ecg)-np.min(frame.filtered_ecg))*.2)
#QRS detection
ind = 0
ind2 = 0
hbindex = [frame.index[0]]
heartbeat = []
RRindex = []
hbs = [0]
timestamp = []
for x in Filtered_ECG:
ind += 1
if x > threshold and Filtered_ECG[ind-1] > Filtered_ECG[(ind-1)-1] and Filtered_ECG[ind-1] > Filtered_ECG[(ind-1)+1]: #Threshold on frame ecg works
ind2 += 1
hbs.append(ind2)
heartbeat.append(ind2)
hbindex.append(frame.index[ind]) #timestamp for all detectected QRS in ECG
else:
heartbeat.append(ind2)
frame['heart_beat'] = heartbeat
#Heart Rate
RR = np.diff(hbindex)
HR = 60/RR
RR[0] = RR[1]
HR[0] = HR[1]
#beat to beat BP values for each QRS
sbp = []
mbp = []
dbp = []
for x in hbs:
sbp.append(np.max(frame[frame.heart_beat == x].bp))
mbp.append(np.mean(frame[frame.heart_beat == x].bp))
dbp.append(np.min(frame[frame.heart_beat == x].bp))
print frame
#Output dataframe
frame2 = DataFrame(hbs[1:], columns = ['hb'])
frame2['time'] = hbindex[1:]
frame2['RR'] = RR
frame2['HR'] = HR
frame2['sbp'] = sbp[1:]
frame2['mbp'] = mbp[1:]
frame2['dbp'] = dbp[1:]
#Binning by SBP
BPbin= []
for y in frame2.sbp:
BPbin.append(int((y - min(frame2.sbp))/3))
frame2['bin'] = BPbin
frame2 = frame2[:(len(frame2))-2] #removes trailing incomplete cardiac cycle
print frame2
groupedRR = frame2['RR'].groupby(frame2['bin'])
RRarray = groupedRR.mean()
groupedSBP = frame2['sbp'].groupby(frame2['bin'])
SBParray = np.asarray(groupedSBP.mean())
print SBParray
bin_weight = groupedSBP.size()/frame2['hb'].max()
frame3 = frame2.mean()
#linear regression
#RR vs SBP
slope, intercept, r_value, p_value, std_err = linregress(SBParray, RRarray)
frame3['BRS slope'] = slope
frame3['R^2'] = r_value**2
print frame3
bestfit = [(i*0.012020)+intercept for i in SBParray]
#plots plots plots plots plots plots plots plots plots plots plots
fig = plt.figure()
#ECG plot
ax1 = fig.add_subplot(2, 1, 1)
plt.plot(frame.index, frame.filtered_ecg)
plt.plot(frame2.time, frame.filtered_ecg[frame2.time], linestyle=' ', marker='o')
#blood pressure plot
ax2 = fig.add_subplot(2, 1, 2)
plt.xlabel('Time, s')
plt.plot(frame.index, frame.bp)
plt.plot(frame2.time, frame2.sbp)
plt.plot(frame2.time, frame2.mbp)
plt.plot(frame2.time, frame2.dbp)
plt.ylabel('Blood Pressure')
fig2 = plt.figure()
#correlation plot
#RR vs SBP
fig3 = plt.figure()
ax4 = fig3.add_subplot(1, 1, 1)
plt.plot(SBParray, RRarray, linestyle=' ', marker='.', color='k')
plt.ylabel('RR interval')
plt.xlabel('Systolic Blood Pressure')
plt.plot(SBParray, bestfit, marker=' ', linestyle='--', color='k')
plt.xlim(117, 144)
plt.text(119, 1.08, r'$Slope = %s,\ R^2=%s$' % (np.round(slope, decimals = 2), np.round(frame3['R^2'], decimals = 2)))
plt.show()