-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeepNetAF.py
More file actions
337 lines (283 loc) · 12.8 KB
/
Copy pathDeepNetAF.py
File metadata and controls
337 lines (283 loc) · 12.8 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
'''
Deep Learning FrameWork for atrial fibrillation (AF) classification. Here we
deal with the following questions:
1. How can we harness the power of CNNs for 1d temporal signals such as ECG?
2. How do we handle skewness and label imbalance in data?
3. How do we train large tensors using data generators?
4. How can we parallelize data transfer using generators?
CopyRight , Feb, 2019
Hooman Sedghamiz
'''
import os
import sys
import re
import random
from scipy.io import loadmat
from scipy.signal import stft
import matplotlib.pyplot as plt
import numpy as np
import pandas as pn
import keras
from shutil import copyfile
from DataGenerator import DataGenerator
class ProgressBar(object):
'''
Progress bar Class
'''
DEFAULT = 'Progress: %(bar)s %(percent)3d%%'
FULL = 'Loading: %(bar)s %(current)d/%(total)d (%(percent)3d%%) %(remaining)d to go'
def __init__(self, total, width=40, fmt=DEFAULT, symbol='=',
output=sys.stderr):
assert len(symbol) == 1
self.total = total
self.width = width
self.symbol = symbol
self.output = output
self.fmt = re.sub(r'(?P<name>%\(.+?\))d',
r'\g<name>%dd' % len(str(total)), fmt)
self.current = 0
def __call__(self):
self.current += 1
percent = self.current / float(self.total)
size = int(self.width * percent)
remaining = self.total - self.current
bar = '[' + self.symbol * size + ' ' * (self.width - size) + ']'
args = {
'total': self.total,
'bar': bar,
'current': self.current,
'percent': percent * 100,
'remaining': remaining
}
print('\r' + self.fmt % args, file=self.output, end='')
def done(self):
self.current = self.total
print('', file=self.output)
class DeepNetAF(object):
ReadPath = ""
FilesToLoad = []
Signals = np.array([])
partition = {'train': [], 'validation': []}
labels = {}
# Parameters
params = {'dim': None,
'batch_size': 40,
'n_classes': 2,
'n_channels': 3,
'shuffle': True}
def __init__(self, PathR = os.path.realpath(__file__), AnnotN=None):
'''
Constructor for ApneaDeepLearning:
PathR : The Path to Annotation Files default is current filename path.
Example : A = ApneaDeepLearning('D:/heartbeat/polysomnography/annotations-events-nsrr/baseline')
'''
self.ReadPath = PathR
self.AnnotF = AnnotN
self.listFiles('.mat')
def listFiles(self,Ext):
'''
Loads a set of files with extention input EXT : 'mat','xml'
Ext : Extension of the file e.g. ".mat"
Example : listFiles(".mat")
'''
for file in os.listdir(self.ReadPath):
if file.endswith(Ext):
self.FilesToLoad.append(file)
def ImportAllSigs(self,y_index, Fs=300):
'''
Imports all of the recordings in PathLoad
Returns:
A panda dataframe containing all of the patients
Returns: A Tensor N*T*F (Number of trials * time-samples * Features)
'''
LD = ProgressBar(len(self.FilesToLoad), fmt=ProgressBar.FULL)
ShortSegs = []
true_index = []
counter = 0
for j,i in enumerate(self.FilesToLoad):
LD()
if y_index[j]:
temp = self.ReadMatFile(i)
d = temp.shape
if d[0] < d[1]:
temp = np.transpose(temp)
if j==0:
self.Signals = np.zeros((np.sum(y_index),temp.shape[0],1)) # Nr trials, timesteps, data_dim
# --- Truncate the signal ------ #
if temp.shape[0] > self.Signals.shape[1]:
temp = temp[:self.Signals.shape[1]]
if temp.shape[0] == self.Signals.shape[1]:
self.Signals[counter,:,:] = temp
counter +=1
true_index.append(True)
#------------------ compute STFT ---------------------------#
if counter == 1:
f, t, PSD = stft(self.Signals[counter,:,0], Fs, nperseg=Fs/2)
PSD_img = self.CreateImage(f,t,PSD)
self.params['dim'] = tuple(PSD_img.shape[:2])
else:
_, _, PSD = stft(self.Signals[counter,:,0], Fs, nperseg=Fs/2)
PSD_img = self.CreateImage(f,t,PSD)
#------------------ Save PSD to disk -------------------------- #
np.save(os.path.join('AFdata', 'id-'+ str(counter)), PSD_img)
else:
true_index.append(False)
ShortSegs.append(j)
self.Signals = self.Signals[:counter]
LD.done()
print(len(true_index))
return true_index,ShortSegs
def ComputePSD(self,Fs=300):
'''
Computes and saves PSD of the signals in self.Signals
'''
if self.Signals.size == 0:
raise ValueError('No sequence found. First load the signals.')
# --- Compute the ST-ft in non-overlapping windows of 0.5 sec --- #
LD = ProgressBar(self.Signals.shape[0], fmt=ProgressBar.FULL)
for i in range(self.Signals.shape[0]):
LD()
if i == 0:
f, t, PSD = stft(self.Signals[i,:,0], Fs, nperseg=Fs/2)
PSD = self.CreateImage(f,t,PSD)
# ------------- Instantiate Image matrix RGB -------------- #
x_train = np.zeros((self.Signals.shape[0], PSD.shape[0], PSD.shape[1], 3))
# ------------- Create plots from PSD --------------------- #
x_train[i,:,:,:] = PSD
else:
# ------------- Instantiate Image matrix RGB -------------- #
_, _, PSD = stft(self.Signals[i,:,0], Fs, nperseg=Fs/2)
# ------------- Create plots from PSD --------------------- #
x_train[i,:,:,:] = self.CreateImage(f,t,PSD)
LD.done()
return x_train
def CreateImage(self,f,t,PSD):
'''
Accepts frequency (f), time (t) and PSD and
creates an image, then returns the image as a matrix.
'''
fig = plt.figure(frameon=False)
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')
plt.pcolormesh(t, f, np.abs(PSD), vmin=0)
fig.canvas.draw()
width, height = fig.canvas.get_width_height()
test = fig.canvas.tostring_rgb()
mplimage = np.fromstring(test, dtype=np.uint8).reshape(height, width, 3)
plt.close('all')
return mplimage
def ScalerF(self,data):
'''
Use SKlearn to standardize
'''
# train the standardization
scaler = StandardScaler()
scaler = scaler.fit(data)
normalized = scaler.transform(data)
return normalized, scaler
def ReadMatFile(self,Fname):
'''
Read a .mat format file (Fname) and import to a np array
'''
# Read XML iteratively : Only searches in ScoredEvents
RF = os.path.join(self.ReadPath,Fname)
# Import the signal
Sig = loadmat(RF)['val']
return Sig
def SplitData(self,InputSize, trainSize = 0.9):
'''
Splits the input into test and train and returns the indices of them as
test_label: Indices of test data
train_label: Indices of training data
Mask_B: Binary mask where the true bits represent the training samples and false bits the test
'''
Mask_B = np.zeros(InputSize, dtype = bool)
train_label = random.sample(np.arange(0, InputSize).tolist(), round(trainSize*float(InputSize)))
Mask_B[train_label] = True
test_label = np.where(Mask_B==False)[0].tolist()
return test_label, train_label, Mask_B
def ReadAnnot(self,AnnotF):
'''
Import the annotation csv file
'''
if AnnotF != None:
dataframe = pn.read_csv(os.path.join(self.ReadPath,AnnotF),header = None,engine='python',usecols=[1],squeeze = True)
y_index = np.logical_or(dataframe.str.contains("N"),dataframe.str.contains("A"))
dataframe = dataframe[y_index]
y_train = np.zeros((dataframe.shape[0],))
y_train[dataframe.str.contains("A")] = 1
else:
raise ValueError('No Annotation File Provided!')
return y_train,y_index
def UpSample(self,ind, factor, total_size_data, TrainFlag = 'train'):
'''
Given the index of files, this function upsamples them by factor and saves the results in the same folder
and new ID which is incremented based on the total size of data
TrainFlag : Set to 'validation' if the data upsampled is for testing otherwise leave as default
'''
for i in ind:
for j in range(0,factor):
copyfile(os.path.join('AFdata','id-'+ str(i+1) + '.npy'),os.path.join('AFdata','id-'+ str(total_size_data+1) + '.npy'))
self.partition[TrainFlag].append('id-'+str(total_size_data+1))
self.labels['id-'+ str(total_size_data+1)] = self.labels['id-'+ str(i+1)]
total_size_data += 1
def Create_Partition(self, ind, y_train, TrainFlag = 'train'):
'''
Packs the labels (y_train) and data in a dictionary for keras generator
Note: y_train should contain all of the labels for data
'''
for i in ind:
self.partition[TrainFlag].append('id-'+str(i+1))
self.labels['id-'+str(i+1)] = y_train[i]
if __name__ == "__main__":
'''
Helper to test out the classes.
'''
# -------------- Initialize the class ---------- #
data = DeepNetAF('C:/Users/hooma/Documents/Visual Studio 2015/Projects/DL-AtrialD/training2017')
# --------------Import labels ------------------ #
y_train,y_index = data.ReadAnnot('REFERENCE-original.csv')
# --------------Import ECG segments ------------ #
y_index = data.ImportAllSigs(y_index)
# --------Prepare training and validation------- #
y_train = y_train[y_index[0][:]]
normal_test_ind, normal_train_ind, _ = data.SplitData(sum(y_train==0))
print('Nr of Test= %d , Nr Training= %d' % (len(normal_test_ind), len(normal_train_ind)))
Afib_test_ind, Afib_train_ind, _ = data.SplitData(sum(y_train==1))
print('Nr of Test= %d , Nr Training= %d' % (len(Afib_test_ind), len(Afib_train_ind)))
#--------------- Partition Data ----------------#
data.Create_Partition(normal_test_ind,y_train,TrainFlag = 'validation')
data.Create_Partition(normal_train_ind,y_train)
data.Create_Partition(Afib_test_ind,y_train,TrainFlag = 'validation')
data.Create_Partition(Afib_train_ind,y_train)
#------------- Upsample ---------------- #
data.UpSample(Afib_train_ind, 7, len(y_train))
data.UpSample(Afib_test_ind, 7, len(y_train),TrainFlag = 'validation')
# ---------------- Generators ----------------------- #
training_generator = DataGenerator(data.partition['train'], data.labels, **data.params)
validation_generator = DataGenerator(data.partition['validation'], data.labels, **data.params)
#------------- Create a simple VGG network----------- #
model = keras.models.Sequential()
# input: 100x100 images with 3 channels -> (640, 480, 3) tensors.
# this applies 32 convolution filters of size 3x3 each.
INP = list(data.params['dim'])
INP.append(data.params['n_channels'])
model.add(keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=tuple(INP)))
model.add(keras.layers.Conv2D(32, (3, 3), activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(keras.layers.Conv2D(64, (3, 3), activation='relu'))
model.add(keras.layers.MaxPooling2D(pool_size=(2, 2)))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(256, activation='relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(2, activation='softmax'))
model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
hist = model.fit_generator(generator=training_generator,
validation_data=validation_generator,
use_multiprocessing=True,
workers=6)
# -------------- Print Validation Accuracy --------------- #
print(hist.history)