-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid_search_pipeline.py
277 lines (228 loc) · 11.9 KB
/
grid_search_pipeline.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 16 13:37:49 2021
@author: fabioacl
"""
#%% Import Libraries
import numpy as np
import os
import gc
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import pandas as pd
import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
for i in range(len(physical_devices)):
tf.config.experimental.set_memory_growth(physical_devices[i], True)
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,Conv1D,SpatialDropout1D,Activation,BatchNormalization,Bidirectional,LSTM,Dropout,Dense
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.optimizers import Adam
from seizure_prediction_dataset_batch_generator import SeizurePredictionDatasetGenerator
from compute_eeg_features import ComputeEEGFeatures
import utils
#%% Functions
''' Get deep learning model architecture'''
def get_deep_model(nr_filters,filter_size,lstm_units=128):
swish_function = tf.keras.activations.swish
input_layer = Input(shape=(2560,19))
x = Conv1D(nr_filters,filter_size,1,'same')(input_layer)
x = Conv1D(nr_filters,filter_size,2,'same')(x)
x = SpatialDropout1D(0.5)(x)
x = Activation(swish_function)(x)
x = BatchNormalization()(x)
x = Conv1D(nr_filters*2,filter_size,1,'same')(x)
x = Conv1D(nr_filters*2,filter_size,2,'same')(x)
x = SpatialDropout1D(0.5)(x)
x = Activation(swish_function)(x)
x = BatchNormalization()(x)
x = Conv1D(nr_filters*4,filter_size,1,'same')(x)
x = Conv1D(nr_filters*4,filter_size,2,'same')(x)
x = SpatialDropout1D(0.5)(x)
x = Activation(swish_function)(x)
x = BatchNormalization()(x)
x = Bidirectional(LSTM(lstm_units,return_sequences=False))(x)
x = Dropout(0.5)(x)
x = Dense(2)(x)
output_layer = Activation('softmax')(x)
model = Model(input_layer,output_layer)
model.compile(optimizer=Adam(3e-4), loss='binary_crossentropy',
metrics='acc')
model.summary()
return model
def get_shallow_model(nr_features):
swish_function = tf.keras.activations.swish
features_input_layer = Input(shape=(1045,))
handcrafted_features = Dropout(0.5)(features_input_layer)
if nr_features!='No Reduction':
handcrafted_features = Dense(nr_features)(handcrafted_features)
handcrafted_features = Activation(swish_function)(handcrafted_features)
x = Dropout(0.5)(handcrafted_features)
x = Dense(2)(x)
output_layer = Activation('softmax')(x)
model = Model(features_input_layer,output_layer)
model.compile(optimizer=Adam(learning_rate=3e-4), loss='binary_crossentropy',metrics='acc')
model.summary()
return model
#%% Develop and Evaluate Seizure Prediction Model
# Random State
random_state = 42
# Root Path
root_path = "Datasets/"
# root_path = "Not Processed Datasets/"
architecture_type = 'shallow'
number_runs = 3
# Get all patients numbers
all_patient_numbers = utils.get_all_patients_numbers(root_path)
number_patients = len(all_patient_numbers)
for i in range(0,number_runs):
for patient_index in [0,1,3,6,12,13,15,17,26,40]:
patient_number = all_patient_numbers[patient_index]
print(f'Patient Number: {patient_number}')
#------------Get Patient Dataset------------
print("Get Patient Dataset...")
# Patient Folder
patient_folder = root_path + "pat_" + str(patient_number) + "/"
# Prepare Dataset
fs = 256
cutoff_freqs = [100,0.5,50]
filters_orders = [4,4]
dataset,datetimes,seizure_onset_datetimes = utils.prepare_dataset(patient_folder,fs,cutoff_freqs,filters_orders)
# SOP, SPH, and training time (SPH does not count)
sop = 30
sph = 10
training_time = 4
# Ratio of training and test seizures
training_ratio = 0.6
test_ratio = 1 - training_ratio
num_seizures = len(dataset)
print(f'Number of Seizures: {num_seizures}')
training_seizures = round(training_ratio * num_seizures)
# Remove the seizures that are going to be used for testing in future
dataset = dataset[0:training_seizures]
datetimes = datetimes[0:training_seizures]
seizure_onset_datetimes = seizure_onset_datetimes[0:training_seizures]
sop_gmeans = []
print(f'SOP: {sop} Minutes')
all_gmeans = []
# Dataset Labels
dataset_labels = utils.get_dataset_labels(datetimes,seizure_onset_datetimes,sop,sph)
# Remove Seizures with Small Preictal
fp_threshold = 0.5
window_seconds = 10
dataset,dataset_labels,datetimes,seizure_onset_datetimes = utils.remove_datasets_with_small_preictal(dataset,dataset_labels,datetimes,
seizure_onset_datetimes,sop,sph,window_seconds,
fp_threshold,training_seizures)
# Training dataset (last seizure will be used for evaluation)
sub_dataset = dataset[:-1]
sub_dataset_labels = dataset_labels[:-1]
sub_datetimes = datetimes[:-1]
sub_seizure_onset_datetimes = seizure_onset_datetimes[:-1]
# Get training dataset (only have 4h of data before each training seizure)
training_data,training_labels,training_datetimes = utils.get_training_dataset(sub_dataset, sub_dataset_labels,
sub_datetimes, sub_seizure_onset_datetimes,
training_time,sph,training_seizures)
# Merge all data
training_data,training_labels = utils.merge_seizure_datasets(training_data, training_labels)
# Convert labels into categorical labels (this is necessary to train deep neural networks with softmax)
training_labels_categorical = to_categorical(training_labels,2)
# If shallow neural network, compute features
if architecture_type=='shallow':
feature_groups = ['statistical','spectral band','spectral edge',
'hjorth parameters','wavelet','decorrelation time']
training_data = ComputeEEGFeatures(training_data).calculate_window_features(feature_groups)
num_windows = training_data.shape[0]
training_data = training_data.reshape((num_windows,-1))
# Divide the training data into training and validation sets
validation_ratio = 0.2
X_train,X_val,y_train,y_val = train_test_split(training_data,training_labels_categorical,
test_size=validation_ratio,random_state=random_state,
stratify=training_labels)
#------------Train Seizure Prediction Model------------
print("Train Seizure Prediction Model...")
# Get standardisation values
if architecture_type=='deep':
norm_values = [np.mean(X_train),np.std(X_train)]
else:
norm_values = [np.mean(X_train,axis=0),np.std(X_train,axis=0)]
# Compute training and validation generators (training generator balances the dataset)
batch_size = 8
training_batch_generator = SeizurePredictionDatasetGenerator(X_train,y_train,norm_values,batch_size,'training')
validation_batch_generator = SeizurePredictionDatasetGenerator(X_val,y_val,norm_values,batch_size,'validation')
# Construct artificial neural network architecture
if architecture_type=='deep':
nr_filters = 4 # Number filters of the first layer
filter_size = 3 # First dimension filter size
lstm_units = 32 # Number of LSTM units
model = get_deep_model(nr_filters,filter_size,lstm_units=lstm_units)
else:
nr_features = 'No Reduction'
model = get_shallow_model(nr_features)
train_epochs = 500
train_patience = 50
# Prepare models callbacks (model checkpoint allow the model to be trained until the end selecting the best weights)
early_stopping_cb = EarlyStopping(monitor='val_loss',patience=train_patience,restore_best_weights=True)
callbacks_parameters = [early_stopping_cb]
# Get number of training and validation samples
number_training_samples = len(X_train)
number_validation_samples = len(X_val)
# Train the model
train_history = model.fit(training_batch_generator,steps_per_epoch = len(training_batch_generator),
epochs = train_epochs,
verbose = 1,
validation_data = validation_batch_generator,
validation_steps = len(validation_batch_generator),
callbacks = callbacks_parameters)
last_epoch = np.argmin(train_history.history['val_loss'])
#------------Evaluate Model------------
print("Evaluate Seizure Prediction Model...")
# Predict labels
if architecture_type=='deep':
X_eval = (dataset[-1] - norm_values[0]) / norm_values[1]
else:
X_eval = dataset[-1]
X_eval = ComputeEEGFeatures(X_eval).calculate_window_features(feature_groups)
num_test_windows = X_eval.shape[0]
X_eval = X_eval.reshape((num_test_windows,-1))
X_eval = (X_eval-norm_values[0])/norm_values[1]
y_pred = model.predict(X_eval)
y_pred = np.argmax(y_pred,axis=1)
# Get true labels
y_eval = dataset_labels[-1]
# Get sensitivity and specificity
tn, fp, fn, tp = confusion_matrix(y_eval,y_pred).ravel()
ss = tp/(tp+fn)
sp = tn/(tn+fp)
# Save results in arrays
gmean = np.sqrt(ss*sp)
# Clear variables
del sub_dataset,sub_dataset_labels,sub_datetimes,sub_seizure_onset_datetimes,training_data,training_datetimes,X_train,X_val,training_batch_generator,validation_batch_generator
gc.collect()
print("Save Results...")
# Archive patient results
if architecture_type=='deep':
filename = f'results_architecture_search_sops_with_strides_{nr_filters}_filters_{filter_size}_{lstm_units}.csv'
else:
filename = f'results_architecture_search_sops_with_strides_{nr_features}.csv'
if os.path.isfile(filename):
all_results = pd.read_csv(filename,index_col=0)
new_results_dictionary = {'Patient':[patient_number],'Sensitivity':[ss],
'Specificity':[sp],'G-Mean':[gmean],
'Training Seizures':training_seizures,
'Last Epoch':last_epoch}
new_results = pd.DataFrame(new_results_dictionary)
all_results = all_results.append(new_results, ignore_index = True)
all_results.to_csv(filename)
else:
new_results_dictionary = {'Patient':[patient_number],'Sensitivity':[ss],
'Specificity':[sp],'G-Mean':[gmean],
'Training Seizures':training_seizures,
'Last Epoch':last_epoch}
new_results = pd.DataFrame(new_results_dictionary)
new_results.to_csv(filename)
# Clear variables
del dataset,datetimes,seizure_onset_datetimes
gc.collect()