-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_signal.py
198 lines (147 loc) · 7.11 KB
/
plot_signal.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
import wfdb
import os
import argparse
import shutil
import math
import matplotlib.pyplot as plt
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-f','--files', dest='files', required=True, nargs='*', help="ecg recordings file name")
parser.add_argument('-p','--position', dest='position', nargs='*', type=int, help="position (multimple input works only for one registration at a time)")
parser.add_argument('-s','--size', dest='size', default=1000, type=int, help="frame half size")
parser.add_argument('-c','--colprop', dest='colprop', default=1, help="columns proportion")
parser.add_argument('-r','--rowprop', dest='rowprop', default=4, help="row proportion")
parser.add_argument('-fl','--filtered', dest='filtered', action='store_true', help="plot also the filtered signal")
args = parser.parse_args()
files = args.files
labels = ['N', 'L', 'R', 'e', 'j', 'A', 'a', 'J', 'S', 'V', 'E', 'F', '/', 'f', 'Q']
sig = []
lab = []
pos = []
lab_filtered = []
pos_filtered = []
sig_filtered = []
th = []
for i, file in enumerate(files):
f_th = open('./output/threshold/default/'+file+'_th.txt', 'r')
th.append(int(f_th.read()))
lab_filtered.append([])
pos_filtered.append([])
r = wfdb.rdrecord('./dataset/raw/'+file)
ann = wfdb.rdann('./dataset/raw/'+file, 'atr', return_label_elements=['label_store', 'symbol'])
sig.append(np.array(r.p_signal[:,0]))
# intsig = np.array(r.p_signal[:,0])
# sig_len = len(sig)
lab.append(ann.symbol)
pos.append(ann.sample)
for l, p in zip(lab[i], pos[i]):
if l in labels:
lab_filtered[i].append(l)
pos_filtered[i].append(p)
sig_filtered.append([])
f_sig_filtered = open('./output/dataset/raw_text/'+file+'_filtered.txt', 'r')
Lines = f_sig_filtered.readlines()
for line in Lines:
sig_filtered[i].append(int(line.strip()))
f_sig_filtered.close()
# os.remove('./output/dataset/raw_text/'+d+'_filtered.txt')
f_filter_delay = open('./output/dataset/raw_text/filter_delay.txt', 'r')
filter_delay = int(f_filter_delay.read())
subplot_col_prop = float(args.colprop)
subplot_row_prop = float(args.rowprop)
position = []
if args.position != None:
position.extend(args.position)
if args.size != None:
size = args.size
num_files = len(files)
num_plot = len(position) if len(files) == 1 else len(files)
num_plot = num_plot if num_plot else 1
subplot_col = round(math.sqrt(num_plot/(subplot_col_prop*subplot_row_prop)) * subplot_col_prop)
if subplot_col < 1:
subplot_col = 1
subplot_row = math.ceil(num_plot/subplot_col)
if subplot_col:
if subplot_col > num_plot:
subplot_col = num_plot
else:
subplot_col = 1
while (subplot_col*subplot_row - subplot_row) >= num_plot:
subplot_col -= 1
fig = plt.figure()
# fig.suptitle(f"ECG raw & filtered signal")
if num_files != 1:
for i, (s, s_filtered, file) in enumerate(zip(sig, sig_filtered, files)):
ax1 = fig.add_subplot(subplot_row,subplot_col,i+1)
color = 'tab:blue'
if position:
ax1.plot(range(position[0]-size,position[0]+size),np.array(s[position[0]-size:position[0]+size]), color=color)
else:
ax1.plot(np.array(s), color=color)
ax1.set(title=f'File: {file}')
ax1.set_ylabel("Raw signal", color=color)
ax1.set_xlabel("Sample")
ax1.grid()
for p, l in enumerate(lab_filtered[i]):
if len(position):
if pos_filtered[i][p] >= position[0]-size and pos_filtered[i][p] <= position[0]+size:
plt.text(pos_filtered[i][p],s[pos_filtered[i][p]],f'{lab_filtered[i][p]}', va='bottom', ha='center', weight="bold")
else:
plt.text(pos_filtered[i][p],s[pos_filtered[i][p]],f'{lab_filtered[i][p]}', va='bottom', ha='center', weight="bold")
if args.filtered:
color = 'tab:red'
ax2 = ax1.twinx()
ax2.set_ylabel("Filtered signal", color=color, alpha=0.5)
if len(position):
ax2.plot(range(position[0]-size,position[0]+size),s_filtered[position[0]-size+filter_delay:position[0]+size+filter_delay], color=color, alpha=0.45)
ax2.plot(range(position[0]-size,position[0]+size),[th[i]]*(size*2), linestyle='dashed', color=color, alpha=0.35)
plt.text(position[0]+size,th[i], 'Threshold', color=color, alpha=0.65, weight="bold", va='bottom', ha='right')
else:
ax2.plot(s_filtered[filter_delay:], color=color, alpha=0.45)
ax2.plot([th[i]]*(len(s_filtered[filter_delay:])), linestyle='dashed', color=color, alpha=0.35)
plt.text(len(s_filtered[filter_delay:]),th[i], 'Threshold', color=color, alpha=0.65, weight="bold", va='bottom', ha='right')
else:
if len(position):
for i, P in enumerate(position):
ax1 = fig.add_subplot(subplot_row,subplot_col,i+1)
color = 'tab:blue'
ax1.plot(range(P-size,P+size),np.array(sig[0][P-size:P+size]), color=color)
ax1.set(title=f'File: {file}')
ax1.set_ylabel("Raw signal", color=color)
ax1.grid()
for x,y in zip(range(2*size), np.array(sig[0][P-size:P+size])):
print(f'{x} {y}')
for p, l in enumerate(lab_filtered[0]):
if pos_filtered[0][p] >= P-size and pos_filtered[0][p] <= P+size:
plt.text(pos_filtered[0][p], sig[0][pos_filtered[0][p]],f'{lab_filtered[0][p]}', va='bottom', ha='center', weight="bold")
ax1.set_xticks([])
ax1.set_yticks([])
if args.filtered:
color = 'tab:red'
ax2 = ax1.twinx()
ax2.set_ylabel("Filtered signal", color=color, alpha=0.5)
ax2.plot(range(P-size,P+size), sig_filtered[0][P-size+filter_delay:P+size+filter_delay], color=color, alpha=0.45)
ax2.plot(range(P-size,P+size), [th[0]]*(size*2), linestyle='dashed', color=color, alpha=0.35)
plt.text(P+size,th[i], 'Threshold', color=color, alpha=0.65, weight="bold", va='bottom', ha='right')
else:
ax1 = fig.add_subplot(subplot_row,subplot_col,i+1)
color = 'tab:blue'
if position:
ax1.plot(range(P-size,P+size),np.array(sig[0][P-size:P+size]), color=color)
else:
ax1.plot(np.array(sig[0]), color=color)
ax1.set(title=f'File: {file}')
ax1.set_ylabel("Raw signal", color=color)
ax1.grid()
for p, l in enumerate(lab_filtered[0]):
plt.text(pos_filtered[0][p],sig[0][pos_filtered[0][p]],f'{lab_filtered[0][p]}', va='bottom', ha='center', weight="bold")
if args.filtered:
color = 'tab:red'
ax2 = ax1.twinx()
ax2.set_ylabel("Filtered signal", color=color, alpha=0.5)
ax2.plot(sig_filtered[0][filter_delay:], color=color, alpha=0.45)
ax2.plot([th[0]]*(len(sig_filtered[0][filter_delay:])), linestyle='dashed', color=color, alpha=0.35)
plt.text(len(sig_filtered[0][filter_delay:]),th[i], 'Threshold', color=color, alpha=0.65, weight="bold", va='bottom', ha='right')
fig.tight_layout()
plt.show()