-
Notifications
You must be signed in to change notification settings - Fork 2
/
signal_delineat_byx.py
244 lines (231 loc) · 9.17 KB
/
signal_delineat_byx.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
import numpy as np
import os
import pandas as pd
import glob
import matplotlib.pyplot as plt
import tqdm
import pywt
import scipy.signal
import scipy.ndimage
import tqdm
from scipy import stats
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from scipy import signal
def indenpendent_delineate(signals, rpeaks, sampling_rate=500, plot=True):
locations = {}
if plot:
fig = go.Figure()
for i in range(len(signals)):
try:
signal = np.array(signals[i])
rpeak = rpeaks[i]
if signal[rpeak] < 0:
gra1 = np.gradient(signal)
if gra1[rpeak] < 0:
mingra1 = np.argmin(gra1)
for idx in range(mingra1, mingra1 // 2, -1):
if gra1[idx] < 0 and gra1[idx - 1] > 0:
rpeak = idx
break
Q = _delineate_Q(signal, rpeak)
Ppeak = _delineate_Ppeak(signal, rpeak)
Poff = _delineate_Poff(signal, rpeak)
Pon = _delineate_Pon(signal, rpeak)
QRSon = _delineate_QRSon(signal, rpeak, Poff)
if QRSon is np.nan:
QRSon = Q
QRSoff = _delineate_QRSoff(signal, rpeak, QRSon, Pon)
Tpeak = _delineate_Tpeak(signal, QRSoff)
Toff = _delineate_Toff(signal, rpeak, QRSoff, Tpeak)
location = {"Poff": Poff, "Pon": Pon, "QRSon": QRSon, "QRSoff": QRSoff, "Toff": Toff, "Rpeak": rpeak}
locations[i] = location
if plot == True:
x = np.linspace(0, len(signal), len(signal))
show_node = [Poff, Pon, QRSon, Toff, Toff, QRSoff]
fig.add_trace(go.Scatter(x=x, y=signal,
mode='lines',
name=i))
fig.add_vline(x=show_node[0], line_dash="dash", annotation_text="Poff")
fig.add_vline(x=show_node[1], line_dash="dash", annotation_text="Pon")
fig.add_vline(x=show_node[2], line_dash="dash", annotation_text="QRSon")
fig.add_vline(x=show_node[-1], line_dash="dash", annotation_text="QRSoff")
fig.add_vline(x=show_node[3], line_dash="dash", annotation_text="Toff")
except Exception as e:
print(i,e)
if plot:
fig.show()
return locations
def denpendent_delineate(signals, rpeak, sampling_rate=500, plot=True, subplot=True):
"""
:param signals: signal including I,II,V1,V2,V5
:param rpeak: rpeak of average signal
:param sampling_rate: Hz
:return: Pon,Poff,QRSon,QRSoff,Toff
"""
r1 = signals['I']
r2 = signals['II']
v1 = signals['V1']
v2 = signals['V2']
v3 = signals['V3']
v4 = signals['V4']
v5 = signals['V5']
if r2[rpeak] < 0:
gra1 = np.gradient(r2)
if gra1[rpeak] < 0:
mingra1 = np.argmin(gra1)
for idx in range(mingra1,mingra1//2,-1):
if gra1[idx] < 0 and gra1[idx-1] > 0:
rpeak = idx
break
Q = _delineate_Q(r1, rpeak)
Ppeak = _delineate_Ppeak(r2, rpeak)
Poff = _delineate_Poff(r1, rpeak)
Pon = _delineate_Pon(r2, rpeak)
QRSon = _delineate_QRSon(v3, rpeak, Poff)
if QRSon is np.nan:
QRSon = Q
QRSoff = _delineate_QRSoff(v5, rpeak, QRSon, Pon)
Tpeak_v3 = _delineate_Tpeak(v3, QRSoff)
Toff_v3 = _delineate_Toff(v3, rpeak, QRSoff, Tpeak_v3)
Tpeak_v2 = _delineate_Tpeak(v2, QRSoff)
Toff_v2 = _delineate_Toff(v2, rpeak, QRSoff, Tpeak_v2)
Toff = int(np.mean([Toff_v2,Toff_v3]))
location = {"Poff":Poff,"Pon":Pon,"QRSon":QRSon,"QRSoff":QRSoff,"Toff":Toff,"Rpeak":rpeak}
if plot==True:
Y = [r1, r2, v1, v2, v3, v5, v4]
x = np.linspace(0, len(r1), len(r1))
name = ['I', 'II', 'V1', 'V2', 'V3', 'V5','V4']
node_name = ['Poff', 'Pon', 'QRSon', 'Toff', 'Toff', 'QRSoff']
show_node = [Poff, Pon, QRSon, Toff, Toff, QRSoff]
if subplot==True:
_plot_delineate(x, Y, name,show_node,node_name,subplot=True)
else:
_plot_delineate(x, signals, name, show_node, node_name, subplot=False)
return location
def _plot_delineate(x,Y,name,show_node,node_name,subplot=True):
if subplot==True:
fig = make_subplots(rows=len(name), cols=1, shared_xaxes=True, vertical_spacing=0)
for i in range(len(name)):
fig.append_trace(go.Scatter(x=x, y=Y[i],
mode='lines',
name=name[i]), row=i + 1, col=1)
if i <= 5:
fig.append_trace(go.Scatter(x=[show_node[i]], y=[Y[i][show_node[i]]],
mode='markers',
name=node_name[i]), row=i + 1, col=1)
if i == 0:
fig.add_vline(x=show_node[0], line_dash="dash", annotation_text="Poff")
fig.add_vline(x=show_node[1], line_dash="dash", annotation_text="Pon")
fig.add_vline(x=show_node[2], line_dash="dash", annotation_text="QRSon")
fig.add_vline(x=show_node[-1], line_dash="dash", annotation_text="QRSoff")
fig.add_vline(x=show_node[3], line_dash="dash", annotation_text="Toff")
else:
fig.add_vline(x=show_node[0], line_dash="dash")
fig.add_vline(x=show_node[1], line_dash="dash")
fig.add_vline(x=show_node[2], line_dash="dash")
fig.add_vline(x=show_node[-1], line_dash="dash")
fig.add_vline(x=show_node[3], line_dash="dash")
fig.update_layout(height=1200, width=600, title_text="Delineate")
fig.show()
else:
fig = go.Figure()
for i in Y.keys():
fig.add_trace(go.Scatter(x=x, y=Y[i] * 4.88 / 1000,
mode='lines',
name=i))
fig.add_vline(x=show_node[0], line_dash="dash", annotation_text="Poff")
fig.add_vline(x=show_node[1], line_dash="dash", annotation_text="Pon")
fig.add_vline(x=show_node[2], line_dash="dash", annotation_text="QRSon")
fig.add_vline(x=show_node[-1], line_dash="dash", annotation_text="QRSoff")
fig.add_vline(x=show_node[3], line_dash="dash", annotation_text="Toff")
fig['layout']['xaxis']['showgrid'] = False
fig['layout']['yaxis']['showgrid'] = False
fig['layout']["yaxis_title"] = "voltage(mV)"
fig['layout']["xaxis_title"] = "sample points"
fig['layout']['paper_bgcolor'] = 'rgba(0,0,0,0)'
fig['layout']['plot_bgcolor'] = 'rgba(0,0,0,0)'
fig.update_layout(font=dict(size=24))
fig.show()
def _delineate_Poff(seg,rpeak):
ppeak = _delineate_Ppeak(seg,rpeak)
Q = _delineate_Q(seg,rpeak)
end = Q-(Q-ppeak)//3
gra1 = np.gradient(seg)
gra2 = np.gradient(gra1)
gra2peak = np.argmax(gra2[ppeak:end])+ppeak
idx = gra2peak
return idx
def _delineate_Tpeak(seg,QRSoff):
seg1 = signal.detrend(seg[QRSoff:])
gra1 = np.gradient(seg1)
gra2 = np.gradient(gra1)
for start in range(0,len(gra1)): #sampling_rate
if gra2[start] < 0.1 and gra2[start] > -0.1:
break
#print('start',start)
seg = signal.detrend(seg[QRSoff+start:])
if np.argmax(gra1[start:]) > np.argmin(gra1[start:]):
#print('min')
idx = np.argmin(seg) + QRSoff +start
else:
#print('max')
idx = np.argmax(seg) + QRSoff + start
return idx
def _delineate_Ppeak(seg,rpeak):
Q = _delineate_Q(seg,rpeak)
seg = signal.detrend(seg[:Q])
#print('Q',Q)
idx = np.argmax(seg[Q//3:Q]) + Q//3
return idx
def _delineate_Pon(seg,rpeak):
ppeak = _delineate_Ppeak(seg,rpeak)
#print('ppeak',ppeak)
gra1 = np.gradient(seg)
gra2 = np.gradient(gra1)
delta = gra2-gra1
minidx = np.argmin(delta[ppeak//2:ppeak]) + ppeak//2
idx = minidx - 15 #delay
return idx
def _delineate_Toff(seg,rpeak,QRSoff,Tpeak):
gra1 = np.gradient(seg)
gra2 = np.gradient(gra1)
yuzhi= 0.2
count = 0
for idx in range(Tpeak,len(seg)):
if gra1[idx] < yuzhi and gra1[idx] > -yuzhi and idx > Tpeak+20: #sampling_rate=500
count += 1
if count == 2:
break
return idx
def _delineate_QRSoff(seg,rpeak,QRSon,Pon):
t = (rpeak - QRSon) * 2
gra1 = np.gradient(seg)
gra2 = np.gradient(gra1)
gra3 = np.gradient(gra2)
gra2bottom = np.argmin(gra2)
gra2peak = np.argmax(gra2[gra2bottom:])+gra2bottom
idx = np.argmin(gra2[gra2peak+1:gra2peak+t])+gra2peak
return idx+3 #delay
def _delineate_QRSon(seg,rpeak,Poff):
gra1 = np.gradient(seg)
gra2 = np.gradient(gra1)
#gra3 = np.gradient(gra2)
end = np.argmin(gra1)
gra2peak = np.argmax(gra2[Poff:end]) + Poff
idx = gra2peak
for idx in range(gra2peak,Poff,-1):
if gra2[idx] < 0.2:
break
if idx == gra2peak:
return np.nan
return idx
def _delineate_Q(seg,rpeak):
gra1 = np.gradient(seg)
gra2 = np.gradient(gra1)
idx = rpeak
for idx in range(rpeak-2,rpeak//2,-1):
if gra1[idx] > 0 and gra1[idx-1] < 0:
break
return idx