-
Notifications
You must be signed in to change notification settings - Fork 3
/
signal2image.py
207 lines (177 loc) · 7.36 KB
/
signal2image.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
import json
import matplotlib.pyplot as plt
import tqdm
import random
import cv2
from os.path import isfile, join
from os import listdir
import os
import wfdb
import numpy as np
import time
import glob
_range_to_ignore = 20
#_directory = '/users/mac/Downloads/MIT_ECG/data/'
_directory = ('/users/mac/Downloads/PTB_XL/records100/00000/')
#_directory = '/home/linh/Downloads/MIT_ECG/train/'
#_directory = '/home/linh/Downloads/MIT_ECG/val/'
#_dataset_dir = '/users/mac/Downloads/MIT_ECG/d/train/'
#_dataset_dir = '/users/mac/Downloads/MIT_ECG/dat/val/'
_dataset_dir = '/users/mac/Downloads/PTB_XL/records100/'
_split_percentage = .70
_split_validation_percentage = 0.70
_split_test_percentage = 0.50
_width = 2503
_height = 3361
size=64
labels_json = '{ ".": "NOR", "N": "NOR", "V": "PVC", "/": "PAB", "L": "LBB", "R": "RBB", "A": "APC", "!": "VFW", "E": "VEB" }'
labels_to_float = '{ "NOR": "0", "PVC" : "1", "PAB": "2", "LBB": "3", "RBB": "4", "APC": "5", "VFW": "6", "VEB": "7" }'
float_to_labels = '{ "0": "NOR", "1" : "PVC", "2": "PAB", "3": "LBB", "4": "RBB", "5": "APC", "6": "VFW", "7": "VEB" }'
labels = json.loads(labels_to_float)
revert_labels = json.loads(float_to_labels)
original_labels = json.loads(labels_json)
def cropping(image, filename, size=size):
"""
:param image: the image to crop
:param filename:
:param size: prefered size
:return:
""" """
# Left Top Crop
crop = image[:96, :96]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '1' + '.png', crop)
# Center Top Crop
crop = image[:96, 16:112]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '2' + '.png', crop)
# Right Top Crop
crop = image[:96, 32:]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '3' + '.png', crop)
# Left Center Crop
crop = image[16:112, :96]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '4' + '.png', crop)
# Center Center Crop
crop = image[16:112, 16:112]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '5' + '.png', crop)
# Right Center Crop
crop = image[16:112, 32:]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '6' + '.png', crop)
# Left Bottom Crop
crop = image[32:, :96]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '7' + '.png', crop)
# Center Bottom Crop
crop = image[32:, 16:112]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '8' + '.png', crop)
# Right Bottom Crop
crop = image[32:, 32:]
crop = cv2.resize(crop, size)
cv2.imwrite(filename[:-5] + '9' + '.png', crop)"""
image = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
#image = np.invert(image)
image = cv2.resize(image, (size, size))
cv2.imwrite(filename[:5] + '10' + '.png', image)
def create_img_from_sign(size=(size, size), augmentation=True):
if not os.path.exists(_directory):
os.makedirs(_directory)
files = [f[:-4] for f in listdir(_directory) if isfile(join(_directory, f)) if (f.find('.dat') != -1)]
random.shuffle(files)
train = files[: int(len(files) * _split_percentage)]
test = files[int(len(files) * _split_percentage):]
for file in files:
time.sleep(10)
sig, _ = wfdb.rdsamp(_directory + file)
ann = wfdb.rdann(_directory + file, extension='atr')
for i in tqdm.tqdm(range(1, len(ann.sample) - 1)):
if ann.symbol[i] not in original_labels:
continue
label = original_labels[ann.symbol[i]]
if file in train:
dir = '{}train/{}'.format(_dataset_dir, label)
else:
dir = '{}validation/{}'.format(_dataset_dir, label)
if not os.path.exists(dir):
os.makedirs(dir)
''' Get the Q-peak intervall '''
start = ann.sample[i - 1] + _range_to_ignore
end = ann.sample[i + 1] - _range_to_ignore
''' Get the signals '''
plot_x = [sig[i][0] for i in range(start, end)]
plot_y = [i * 1 for i in range(start, end)]
''' Plot and save the beat'''
fig = plt.figure(frameon=False)
plt.plot(plot_y, plot_x)
plt.xticks([]), plt.yticks([])
for spine in plt.gca().spines.values():
spine.set_visible(False)
''' Convert in gray scale and resize img '''
if file in train:
filename = '{}train/{}/{}_{}{}{}0.png'.format(_dataset_dir, label, label, file[-3:], start, end)
else:
filename = '{}validation/{}/{}_{}{}{}0.png'.format(_dataset_dir, label, label, file[-3:], start, end)
fig.savefig(filename)
im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
im_gray = cv2.resize(im_gray, size, interpolation=cv2.INTER_LANCZOS4)
cv2.imwrite(filename, im_gray)
if augmentation:
cropping(im_gray, filename, size)
plt.cla()
plt.clf()
plt.close('all')
def create_img_from_sign_filtered(size=(size, size), size_paa=100, augmentation=True):
if not os.path.exists(_directory):
os.makedirs(_directory)
if not os.path.exists(_dataset_dir):
os.makedirs(_dataset_dir)
files = [f[:-4] for f in listdir(_directory) if isfile(join(_directory, f)) if (f.find('.dat') != -1)]
files = files
for file in files:
sig, _ = wfdb.rdsamp(_directory + file)
ann = wfdb.rdann(_directory + file, extension='atr')
for i in tqdm.tqdm(range(1, len(ann.sample) - 1), ncols=10):
if ann.symbol[i] not in original_labels:
continue
label = original_labels[ann.symbol[i]]
''' Get the Q-peak intervall '''
start = ann.sample[i - 1] + _range_to_ignore
end = ann.sample[i + 1] - _range_to_ignore
signal = [sig[i][0] for i in range(start, end)]
paa = piecewise_aggregate_approximation(signal, size_paa)
plot_x = paa
plot_y = [i for i in range(len(paa))]
''' Plot and save the beat'''
fig = plt.figure(frameon=False)
plt.plot(plot_y, plot_x)
plt.xticks([]), plt.yticks([])
for spine in plt.gca().spines.values():
spine.set_visible(False)
filename = '{}{}_{}{}{}0.png'.format(_dataset_dir, label, file[-3:], start, end)
fig.savefig(filename, bbox_inches='tight')
im_gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE)
im_gray = cv2.resize(im_gray, size, interpolation=cv2.INTER_LANCZOS4)
im_gray = np.invert(im_gray)
cv2.imwrite(filename, im_gray)
plt.cla()
plt.clf()
plt.close('all')
def piecewise_aggregate_approximation(vector, paa_dim: int):
Y = np.array(vector)
if Y.shape[0] % paa_dim == 0:
sectionedArr = np.array_split(Y, paa_dim)
res = np.array([item.mean() for item in sectionedArr])
else:
value_space = np.arange(0, Y.shape[0] * paa_dim)
output_index = value_space // Y.shape[0]
input_index = value_space // paa_dim
uniques, nUniques = np.unique(output_index, return_counts=True)
res = [Y[indices].sum() / Y.shape[0] for indices in
np.split(input_index, nUniques.cumsum())[:-1]]
return res
#create_img_from_sign()
create_img_from_sign_filtered()