-
Notifications
You must be signed in to change notification settings - Fork 24
/
run.py
executable file
·430 lines (356 loc) · 14.1 KB
/
run.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Run an experiment."""
import logging
import sys
import os
import yaml
import pprint
import importlib.util
import tensorflow as tf
import itertools
import copy
import datetime
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
level=logging.DEBUG,
stream=sys.stdout)
import numpy as np
import metrics
import seaborn as sns
sns.set()
import matplotlib.pyplot as plt
def main(yaml_filepath, mode):
"""Run experiments."""
cfgs = load_cfgs(yaml_filepath)
print("Running {} experiments.".format(len(cfgs)))
for cfg in cfgs:
seed = int(cfg['train']['seed'])
np.random.seed(seed)
# Print the configuration - just to make sure that you loaded what you
# wanted to load
module_dataset = load_module(cfg['dataset']['script_path'])
module_model = load_module(cfg['model']['script_path'])
module_optimizer = load_module(cfg['optimizer']['script_path'])
module_loss_function = load_module(cfg['loss_function']['script_path'])
module_train = load_module(cfg['train']['script_path'])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(cfg)
#print("loading dataset ...")
#nb_past_steps = cfg['dataset']['nb_past_steps']
#nb_past_steps_tmp = 36
#cfg['dataset']['nb_past_steps'] = nb_past_steps_tmp
x_train, y_train, x_valid, y_valid, x_test, y_test = module_dataset.load_dataset(cfg['dataset'])
#x_train = x_train[:,-nb_past_steps:,:]
#x_valid = x_valid[:,-nb_past_steps:,:]
#x_test = x_test[:,-nb_past_steps:,:]
print("x_train.shape: ", x_train.shape)
print("y_train.shape: ", y_train.shape)
print("x_valid.shape: ", x_valid.shape)
print("y_valid.shape: ", y_valid.shape)
print("x_test.shape: ", x_test.shape)
print("y_test.shape: ", y_test.shape)
#print("loading optimizer ...")
optimizer = module_optimizer.load(cfg['optimizer'])
#print("loading loss function ...")
loss_function = module_loss_function.load()
#print("loaded function {} ...".format(loss_function.__name__))
#print("loading model ...")
if 'tf_nll' in loss_function.__name__:
model = module_model.load(
x_train.shape[1:],
y_train.shape[1]*2,
cfg['model']
)
else:
model = module_model.load(
x_train.shape[1:],
y_train.shape[1],
cfg['model']
)
if 'initial_weights_path' in cfg['train']:
#print("Loading initial weights: ", cfg['train']['initial_weights_path'])
model.load_weights(cfg['train']['initial_weights_path'])
model.compile(
optimizer=optimizer,
loss=loss_function
)
#print(model.summary())
# training mode
if mode == 'train':
#print("training model ...")
train(model, module_train, x_train, y_train, x_valid, y_valid, cfg)
if mode == 'plot_nll':
plot_nll(model, x_test, y_test, cfg)
if mode == 'plot_noise_experiment':
plot_noise_experiment(model, x_test, y_test, cfg)
if mode == 'plot_seg':
plot_seg(model, x_test, y_test, cfg)
if mode == 'plot_dist':
plot_target_distribution(y_test, cfg)
# evaluation mode
if mode == 'evaluate':
evaluate(model, x_test, y_test, cfg)
def evaluate(model, x_test, y_test, cfg):
if 'xml_path' in cfg['dataset']:
basename = os.path.basename(cfg['dataset']['xml_path'])
patient_id = basename.split('-')[0]
else:
patient_id = ""
if 'scale' in cfg['dataset']:
scale = float(cfg['dataset']['scale'])
else:
scale = 1.0
# load the trained weights
weights_path = os.path.join(cfg['train']['artifacts_path'], "model.hdf5")
print("loading weights: {}".format(weights_path))
model.load_weights(weights_path)
y_pred = model.predict(x_test)[:,1].flatten()/scale
y_std = model.predict(x_test)[:,0].flatten()/scale
y_test = y_test.flatten()/scale
t0 = x_test[:,-1,0]/scale
rmse = metrics.root_mean_squared_error(y_test, y_pred)
print("patient id: ", patient_id)
with open(os.path.join(cfg['train']['artifacts_path'], "{}_rmse.txt".format(patient_id)), "w") as outfile:
outfile.write("{}\n".format(rmse))
seg = metrics.surveillance_error(y_test, y_pred)
with open(os.path.join(cfg['train']['artifacts_path'], "{}_seg.txt".format(patient_id)), "w") as outfile:
outfile.write("{}\n".format(seg))
t0_rmse = metrics.root_mean_squared_error(y_test, t0)
with open(os.path.join(cfg['train']['artifacts_path'], "{}_t0_rmse.txt".format(patient_id)), "w") as outfile:
outfile.write("{}\n".format(t0_rmse))
t0_seg = metrics.surveillance_error(y_test, t0)
with open(os.path.join(cfg['train']['artifacts_path'], "{}_t0_seg.txt".format(patient_id)), "w") as outfile:
outfile.write("{}\n".format(t0_seg))
with open(os.path.join(cfg['train']['artifacts_path'], "{}_mean_std.txt".format(patient_id)), "w") as outfile:
outfile.write("{}\n".format(np.mean(y_std)))
print("RMSE: ", rmse)
print("t0 RMSE: ", t0_rmse)
print("SEG: ", seg)
print("t0 SEG: ", t0_seg)
def train(model, module_train, x_train, y_train, x_valid, y_valid, cfg):
model = module_train.train(
model = model,
x_train = x_train,
y_train = y_train,
x_valid = x_valid,
y_valid = y_valid,
batch_size = int(cfg['train']['batch_size']),
epochs = int(cfg['train']['epochs']),
patience = int(cfg['train']['patience']),
shuffle = cfg['train']['shuffle'],
artifacts_path = cfg['train']['artifacts_path']
)
return model
def plot_target_distribution(y_test, cfg):
if 'xml_path' in cfg['dataset']:
basename = os.path.basename(cfg['dataset']['xml_path'])
patient_id = basename.split('-')[0]
else:
patient_id = ""
if 'scale' in cfg['dataset']:
scale = float(cfg['dataset']['scale'])
else:
scale = 1.0
plt.figure()
sns.distplot(y_test.flatten()/scale, kde=False, norm_hist=True)
save_path = os.path.join(cfg['train']['artifacts_path'], "{}_dist_plot.pdf".format(patient_id))
print("saving plot to: ", save_path)
plt.savefig(save_path, dpi=300)
def plot_nll(model, x_test, y_test, cfg):
if 'xml_path' in cfg['dataset']:
basename = os.path.basename(cfg['dataset']['xml_path'])
patient_id = basename.split('-')[0]
else:
patient_id = ""
if 'scale' in cfg['dataset']:
scale = float(cfg['dataset']['scale'])
else:
scale = 1.0
# load the trained weights
model.load_weights(os.path.join(cfg['train']['artifacts_path'], "model.hdf5"))
#day = (24*60//5)
start_index = 0
hours = 8
to_plot=hours*12
ticks_per_hour = 12
ticks = [i*ticks_per_hour for i in range(hours+1)]
ticks_labels = [str(i) for i in range(hours+1)]
y_pred = model.predict(x_test)
for i in range(5):
start_index = i*to_plot
y_pred_std = y_pred[:,0][start_index:start_index+to_plot]/scale
y_pred_mean = y_pred[:,1][start_index:start_index+to_plot]/scale
y_true = y_test[:,0][start_index:start_index+to_plot]/scale
xs = np.arange(len(y_true))
plt.clf()
plt.ylim([0, 400])
#plt.ylim([-2, 2])
plt.plot(xs, y_true, label='ground truth', linestyle='--')
plt.plot(xs, y_pred_mean, label='prediction')
plt.fill_between(xs, y_pred_mean-y_pred_std, y_pred_mean+y_pred_std,
alpha=0.5, edgecolor='#CC4F1B', facecolor='#FF9848')
plt.xlabel("Time [h]")
plt.ylabel("Glucose Concentration [mg/dl]")
plt.legend(loc='upper right')
#plt.xlabel("y")
#plt.ylabel("x")
plt.xticks(ticks, ticks_labels)
save_path = os.path.join(cfg['train']['artifacts_path'], "{}_nll_plot_{}.pdf".format(patient_id, i))
print("saving plot to: ", save_path)
plt.savefig(save_path, dpi=300)
def plot_noise_experiment(model, x_test, y_test, cfg):
# load the trained weights
model.load_weights(os.path.join(cfg['train']['artifacts_path'], "model.hdf5"))
#day = (24*60//5)
start_index = 0
hours = 8
to_plot=hours*12
ticks_per_hour = 12
ticks = [i*ticks_per_hour for i in range(hours+1)]
ticks_labels = [str(i) for i in range(hours+1)]
y_pred = model.predict(x_test)
start_index = 0
y_pred_std = y_pred[:,0][start_index:start_index+to_plot]
y_pred_mean = y_pred[:,1][start_index:start_index+to_plot]
y_true = y_test[:,0][start_index:start_index+to_plot]
xs = np.arange(len(y_true))
plt.clf()
#plt.ylim([0, 400])
plt.ylim([-3, 3])
plt.plot(xs, y_true, label='ground truth', linestyle='--')
plt.plot(xs, y_pred_mean, label='prediction')
plt.fill_between(xs, y_pred_mean-y_pred_std, y_pred_mean+y_pred_std,
alpha=0.5, edgecolor='#CC4F1B', facecolor='#FF9848')
#plt.xlabel("Time [h]")
#plt.ylabel("Glucose Concentration [mg/dl]")
plt.legend(loc='upper right')
plt.xlabel("x")
plt.ylabel("y")
plt.xticks(ticks, ticks_labels)
save_path = os.path.join(cfg['train']['artifacts_path'], "noise_experiment_plot.pdf")
print("saving plot to: ", save_path)
plt.savefig(save_path, dpi=300)
def plot_seg(model, x_test, y_test, cfg):
if 'xml_path' in cfg['dataset']:
basename = os.path.basename(cfg['dataset']['xml_path'])
patient_id = basename.split('-')[0]
else:
patient_id = ""
if 'scale' in cfg['dataset']:
scale = float(cfg['dataset']['scale'])
else:
scale = 1.0
# load the trained weights
model.load_weights(os.path.join(cfg['train']['artifacts_path'], "model.hdf5"))
y_pred = model.predict(x_test)
y_pred_std = y_pred[:,0][:]/scale
y_pred_mean = y_pred[:,1][:]/scale
y_true = y_test[:,0][:]/scale
data = np.loadtxt('seg.csv')
fig, ax = plt.subplots()
ax.set_title('Patient {} SEG'.format(patient_id))
ax.set_xlabel('Reference Concentration [mg/dl]')
ax.set_ylabel('Predicted Concentration [mg/dl]')
cax = ax.imshow(np.transpose(data), origin='lower', interpolation='nearest')
cbar = fig.colorbar(cax, ticks=[0.25, 1.0, 2.0, 3.0, 3.75], orientation='vertical')
cbar.ax.set_yticklabels(['None', 'Mild', 'Moderate', 'High', 'Extreme'],
rotation=90, va='center')
plt.scatter(y_true, y_pred_mean, s=25, facecolors='white', edgecolors='black')
save_path = os.path.join(cfg['train']['artifacts_path'], "{}_seg_plot.pdf".format(patient_id))
print("saving plot to: ", save_path)
plt.savefig(save_path, dpi=300)
def load_module(script_path):
spec = importlib.util.spec_from_file_location("module.name", script_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def load_cfg(yaml_filepath):
"""
Load a YAML configuration file.
Parameters
----------
yaml_filepath : str
Returns
-------
cfg : dict
"""
# Read YAML experiment definition file
with open(yaml_filepath, 'r') as stream:
cfg = yaml.load(stream)
cfg = make_paths_absolute(os.path.dirname(yaml_filepath), cfg)
return cfg
def load_cfgs(yaml_filepath):
"""
Load YAML configuration files.
Parameters
----------
yaml_filepath : str
Returns
-------
cfgs : [dict]
"""
# Read YAML experiment definition file
with open(yaml_filepath, 'r') as stream:
cfg = yaml.load(stream)
cfg = make_paths_absolute(os.path.dirname(yaml_filepath), cfg)
hyperparameters = []
hyperparameter_names = []
hyperparameter_values = []
# TODO: ugly, should handle arbitrary depth
for k1 in cfg.keys():
for k2 in cfg[k1].keys():
if k2.startswith("param_"):
hyperparameters.append((k1, k2))
hyperparameter_names.append((k1, k2[6:]))
hyperparameter_values.append(cfg[k1][k2])
hyperparameter_valuess = itertools.product(*hyperparameter_values)
artifacts_path = cfg['train']['artifacts_path']
cfgs = []
for hyperparameter_values in hyperparameter_valuess:
configuration_name = ""
for ((k1, k2), value) in zip(hyperparameter_names, hyperparameter_values):
#print(k1, k2, value)
cfg[k1][k2] = value
configuration_name += "{}_{}_".format(k2, str(value))
cfg['train']['artifacts_path'] = os.path.join(artifacts_path, configuration_name)
cfgs.append(copy.deepcopy(cfg))
return cfgs
def make_paths_absolute(dir_, cfg):
"""
Make all values for keys ending with `_path` absolute to dir_.
Parameters
----------
dir_ : str
cfg : dict
Returns
-------
cfg : dict
"""
for key in cfg.keys():
if key.endswith("_path"):
cfg[key] = os.path.join(dir_, cfg[key])
cfg[key] = os.path.abspath(cfg[key])
if not os.path.exists(cfg[key]):
logging.error("%s does not exist.", cfg[key])
if type(cfg[key]) is dict:
cfg[key] = make_paths_absolute(dir_, cfg[key])
return cfg
def get_parser():
"""Get parser object."""
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
parser = ArgumentParser(description=__doc__,
formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("-f", "--file",
dest="filename",
help="experiment definition file",
metavar="FILE",
required=True)
parser.add_argument("-m", "--mode",
dest="mode",
help="mode of run",
metavar="FILE",
required=True)
return parser
if __name__ == '__main__':
args = get_parser().parse_args()
main(args.filename, args.mode)