-
Notifications
You must be signed in to change notification settings - Fork 2
/
exp_train_st_all.py
453 lines (379 loc) · 13.9 KB
/
exp_train_st_all.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/python
# %%
# Import relevant libraries
from helper_functions import *
import warnings
from icecream import ic
ic("Importing packages...")
with warnings.catch_warnings():
warnings.filterwarnings("ignore")
import os
from scipy.stats import mstats
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sys
import getopt
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras
from zipfile import ZipFile
import os
import random
import bcolors
import mne
import mne_nirs
from mne_nirs.experimental_design import make_first_level_design_matrix
from mne_nirs.statistics import run_glm
from mne_nirs.channels import (get_long_channels,
get_short_channels,
picks_pair_to_idx)
from mne.preprocessing.nirs import optical_density, beer_lambert_law
from nilearn.plotting import plot_design_matrix
from itertools import compress
from icecream import ic
# Performance logging
import wandb
from wandb.keras import WandbCallback
import time
wb = train = True
v = p = p_loss = False
config = {
"learning_rate": 0.00002,
"epochs": 200,
"batch_size": 24,
"loss_function": "mae",
"optimizer": "nadam",
"dropout": 0.5,
"units": 100,
"past": 39,
"future": 16,
"preprocess": "medium",
"bidirectional": True,
"activation_function": "tanh",
"normalize": True,
"l_pass": 0.7,
"h_pass": 0.01,
"train_split": 0.7,
"raw_path": "data/snirf/pretrain_3.snirf",
"architecture": "LSTM-3", # Dense, LSTM, LSTM-3
"pretrain_dense_units": 128,
"test_channel": 0,
"patience": 25
}
raw_path = config.get("raw_path")
paths = ["data/snirf/pretrain_1.snirf",
"data/snirf/pretrain_2.snirf",
"data/snirf/pretrain_3.snirf",
"data/snirf/pretrain_4.snirf",
"data/snirf/pretrain_5.snirf", ]
if config.get("preprocess") == "none":
filter_haemo = [preprocess(p,
config.get("l_pass"),
config.get("h_pass"),
bandpass=False,
short_ch_reg=False,
tddr=False,
negative_correlation=False,
verbose=v) for p in paths]
if config.get("preprocess") == "simple":
if v:
ic("Simple preprocessing:", paths)
filter_haemo = [preprocess(p,
config.get("l_pass"),
config.get("h_pass"),
bandpass=True,
short_ch_reg=False, tddr=False, negative_correlation=False, verbose=v) for p in paths]
elif config.get("preprocess") == "medium":
if v:
ic("Medium preprocessing:", paths)
filter_haemo = [preprocess(p,
config.get("l_pass"),
config.get("h_pass"),
bandpass=True,
short_ch_reg=False, tddr=True, negative_correlation=False, verbose=v) for p in paths]
elif config.get("preprocess") == "advanced":
if v:
ic("Advanced preprocessing:", paths)
filter_haemo = [preprocess(p,
config.get("l_pass"),
config.get("h_pass"),
bandpass=True,
short_ch_reg=True, tddr=True, negative_correlation=True, verbose=v) for p in paths]
# %%
# Make the hemoglobin values into a dataframe
full_df = [haemo.to_data_frame() for haemo in filter_haemo]
df_nrows = [df.shape[0] for df in full_df]
df_ncols = [df.shape[1] for df in full_df]
nrows_sum = np.sum(df_nrows)
nrows_test = int(nrows_sum * (1 - config.get("train_split")))
idx = np.abs(np.subtract(df_nrows, nrows_test)).argmin()
ic("Calculated test data rows")
ic(nrows_test)
ic(idx)
ic("Rows for the test data:")
ic(df_nrows[idx])
ic("All rows:")
ic(df_nrows)
if v:
ic("Normalise the data with a basis in the train split so there's no data leakage")
val_data = normalize_and_remove_time(
full_df[idx])
train_data = [normalize_and_remove_time(df) for df in full_df]
train_data = pd.concat(train_data)
if p:
if v:
ic("Plot this stuff.")
test_features.plot(use_index=True, alpha=0.2, title="Test data").get_figure().savefig(
"output/test_data.png")
train_data.plot(use_index=True, alpha=0.2, title="Train data").get_figure(
).savefig("output/train_data.png")
# Show heatmap of all column correlations (channel correlations)
if p:
show_heatmap(train_data)
# Setting the step size (downsampling basically)
step = 1
# How much data can it use
past = config.get("past")
# How long into the future should it predict
future = config.get("future")
# What is the batch size
batch_size = config.get("batch_size")
# How many epochs
epochs = config.get("epochs")
# Settings patience
patience = config.get("patience")
# What is the time column?
date_time_key = "time"
if v:
ic("Select the start and end indices for the training data")
start = past + future
end = start + len(train_data)
if v:
ic("Select all train data from start to end as one big array of arrays")
# y = one value per array in x
x_train = train_data.iloc[:-future].values
y_train = train_data.iloc[start:].values[:, config.get("test_channel")]
if v:
ic("Define sequence length from past values divided by the step (in this case 1)")
sequence_length = int(past / step)
print(x_train.shape)
print(y_train.shape)
if v:
ic("Make a training dataset from arrays with definitions of sequence length")
if config.get("architecture") == "Dense":
dataset_train = keras.preprocessing.timeseries_dataset_from_array(
x_train[:, config.get("test_channel")].flatten(),
y_train,
sequence_length=sequence_length,
sampling_rate=step,
batch_size=batch_size,
)
elif config.get("architecture") in ["LSTM", "LSTM-3"]:
dataset_train = keras.preprocessing.timeseries_dataset_from_array(
x_train,
y_train,
sequence_length=sequence_length,
sampling_rate=step,
batch_size=batch_size,
)
if v:
ic("Make a validation dataset from like the train dataset")
x_val = val_data.iloc[:-future].values
y_val = val_data.iloc[start:].values[:, config.get("test_channel")]
# y_val = np.append(y_val, [1000000000 for i in range(past)])
if v:
ic("Calculate the chance levels for the validation data")
y_list = y_val.ravel()
x_list = x_val[past:, config.get("test_channel")]
ic(y_list.shape)
ic(x_list.shape)
chance_df = pd.DataFrame(
data={
"y": y_list,
"x": x_list
})
chance_df["guess"] = [np.mean(y_list) for i in range(len(chance_df))]
chance_df["gauss"] = [np.random.normal(0) for i in range(len(chance_df))]
chance_df["zero"] = [0 for i in range(len(chance_df))]
chance_df["diff_mean"] = [
np.abs(
chance_df["guess"].iloc[i] -
chance_df["y"].iloc[i])
for i in range(len(chance_df))]
chance_df["diff_gauss"] = [
np.abs(
chance_df["gauss"].iloc[i] -
chance_df["y"].iloc[i])
for i in range(len(chance_df))]
chance_df["diff_last"] = [
np.abs(
chance_df["x"].iloc[i] -
chance_df["y"].iloc[i])
for i in range(len(chance_df))]
chance_df["diff_zero"] = [
np.abs(
chance_df["zero"].iloc[i] -
chance_df["y"].iloc[i])
for i in range(len(chance_df))]
chance_gauss = chance_df["diff_gauss"].mean()
chance_mean = chance_df["diff_mean"].mean()
chance_last = chance_df["diff_last"].mean()
chance_zero = chance_df["diff_zero"].mean()
print(f"{bcolors.HEADER}Mean value performance:",
chance_mean, f"{bcolors.ENDC}")
print(f"{bcolors.HEADER}Last value chance performance:",
chance_last, f"{bcolors.ENDC}")
print(f"{bcolors.HEADER}Gaussian chance performance:",
chance_gauss, f"{bcolors.ENDC}")
print(f"{bcolors.HEADER}Zero chance performance:",
chance_zero, f"{bcolors.ENDC}")
if v:
ic("Make a validation dataset with the same definitions as the training dataset")
if config.get("architecture") == "Dense":
dataset_val = keras.preprocessing.timeseries_dataset_from_array(
x_val[:, config.get("test_channel")].flatten(),
y_val,
sequence_length=sequence_length,
sampling_rate=step,
batch_size=batch_size,
)
elif config.get("architecture") in ["LSTM", "LSTM-3"]:
dataset_val = keras.preprocessing.timeseries_dataset_from_array(
x_val,
y_val,
sequence_length=sequence_length,
sampling_rate=step,
batch_size=batch_size,
)
if v:
print(
f"Take batches out of the training dataset (currently {batch_size} samples)")
for batch in dataset_train.take(1):
inputs, targets = batch
print("Input shape:", inputs.numpy().shape)
print("Target shape:", targets.numpy().shape)
path_checkpoint = "model_weights.h5"
if wb:
wandb.init(project="thought_classification", entity="esbenkran",
name=f"{config.get('architecture')}_{int(random.random() * 1000)}", config=config, tags=["ALL", "used_model"])
config = wandb.config
if train:
if v:
ic("Define the model architecture")
if config.get("architecture") == "Dense":
inputs = keras.layers.Input(shape=(inputs.shape[1]))
elif config.get("architecture") == "LSTM-3":
inputs = keras.layers.Input(shape=(inputs.shape[1], inputs.shape[2]))
lstm_1 = keras.layers.Bidirectional(
keras.layers.LSTM(
config.get("units"),
activation=config.get("activation_function"),
dropout=config.get("dropout"),
return_sequences=True,
))(inputs)
lstm_2 = keras.layers.Bidirectional(
keras.layers.LSTM(
config.get("units"),
activation=config.get("activation_function"),
dropout=config.get("dropout"),
return_sequences=True,
))(lstm_1)
lstm_out = keras.layers.Bidirectional(
keras.layers.LSTM(
config.get("units"),
activation=config.get("activation_function"),
dropout=config.get("dropout"),
))(lstm_2)
elif config.get("architecture") in ["LSTM"]:
inputs = keras.layers.Input(shape=(inputs.shape[1], inputs.shape[2]))
lstm_out = keras.layers.LSTM(
config.get("units"),
activation=config.get("activation_function"),
dropout=config.get("dropout"))(inputs)
dense_out = keras.layers.Dense(config.get("pretrain_dense_units"), activation="relu")(
lstm_out if config.get("architecture") in ["LSTM", "LSTM-3"] else inputs)
outputs = keras.layers.Dense(1)(dense_out)
# if v:
# ic("Generate a learning rate schedule with exponential decay")
# lr_schedule = keras.optimizers.schedules.ExponentialDecay(
# initial_learning_rate=config.get("learning_rate"),
# decay_steps=10000,
# decay_rate=0.96,
# staircase=True)
if v:
ic("Define the optimizer")
optimizer = keras.optimizers.Adam()
if v:
ic("Compile the model")
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(
optimizer=optimizer,
loss=config.get("loss_function"),
metrics=['mean_absolute_error', 'mean_squared_error'])
if v:
ic("Plot model summary")
model.summary()
if v:
ic("Save checkpoints (W&B also does this)")
path_checkpoint = "model_checkpoint.h5"
if v:
print(f"Set early stopping with {patience} patience")
es_callback = keras.callbacks.EarlyStopping(
monitor="val_mean_absolute_error", min_delta=0, patience=patience, verbose=1, mode="min")
if v:
ic("Set a callback to save checkpoints")
modelckpt_callback = keras.callbacks.ModelCheckpoint(
monitor="mean_absolute_error",
filepath=path_checkpoint,
verbose=1,
save_weights_only=True,
save_best_only=True,
)
if v:
ic("Fit the model and save results to history. W&B has a callback to save everything")
if wb:
history = model.fit(
dataset_train,
epochs=epochs,
validation_data=dataset_val,
callbacks=[es_callback, modelckpt_callback,
WandbCallback(data_type="time series")],
)
else:
history = model.fit(
dataset_train,
epochs=epochs,
validation_data=dataset_val,
callbacks=[es_callback, modelckpt_callback],
)
else:
if v:
ic("Load the model")
model = keras.models.load_model(path_checkpoint)
if v:
ic("Calculate the model accuracy")
predictions = []
diff = []
for batch in dataset_val.take(1):
inputs, targets = batch
if config.get("architecture") == "Dense":
predictions = model.predict(inputs).flatten()
else:
predictions = [model.predict(i[None, ...]) for i in inputs]
diff = [np.abs(predictions[i][0] - y_val[i])
for i in range(len(predictions))]
ic("Model predictions: ", np.mean(diff))
if train:
# Visualize the loss
if p_loss:
visualize_loss(history, "Training and Validation Loss")
if p_loss:
ic("Visualize 5 predictions")
for x, y in dataset_val.take(5):
show_plot(
[x[0][:, 1].numpy(), y[0].numpy(), model.predict(x)[0]],
future,
"Single Step Prediction",
)