forked from raminmh/CfC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duv_utils.py
627 lines (455 loc) · 20.3 KB
/
duv_utils.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
###########################
# Latent ODEs for Irregularly-Sampled Time Series
# Author: Yulia Rubanova
###########################
import os
import logging
import pickle
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import math
import glob
import re
from shutil import copyfile
import sklearn as sk
import subprocess
import datetime
def makedirs(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname)
def save_checkpoint(state, save, epoch):
if not os.path.exists(save):
os.makedirs(save)
filename = os.path.join(save, 'checkpt-%04d.pth' % epoch)
torch.save(state, filename)
def get_logger(logpath, filepath, package_files=[],
displaying=True, saving=True, debug=False):
logger = logging.getLogger()
if debug:
level = logging.DEBUG
else:
level = logging.INFO
logger.setLevel(level)
if saving:
info_file_handler = logging.FileHandler(logpath, mode='w')
info_file_handler.setLevel(level)
logger.addHandler(info_file_handler)
if displaying:
console_handler = logging.StreamHandler()
console_handler.setLevel(level)
logger.addHandler(console_handler)
logger.info(filepath)
for f in package_files:
logger.info(f)
with open(f, 'r') as package_f:
logger.info(package_f.read())
return logger
def inf_generator(iterable):
"""Allows training with DataLoaders in a single infinite loop:
for i, (x, y) in enumerate(inf_generator(train_loader)):
"""
iterator = iterable.__iter__()
while True:
try:
yield iterator.__next__()
except StopIteration:
iterator = iterable.__iter__()
def dump_pickle(data, filename):
with open(filename, 'wb') as pkl_file:
pickle.dump(data, pkl_file)
def load_pickle(filename):
with open(filename, 'rb') as pkl_file:
filecontent = pickle.load(pkl_file)
return filecontent
def make_dataset(dataset_type = "spiral",**kwargs):
if dataset_type == "spiral":
data_path = "data/spirals.pickle"
dataset = load_pickle(data_path)["dataset"]
chiralities = load_pickle(data_path)["chiralities"]
elif dataset_type == "chiralspiral":
data_path = "data/chiral-spirals.pickle"
dataset = load_pickle(data_path)["dataset"]
chiralities = load_pickle(data_path)["chiralities"]
else:
raise Exception("Unknown dataset type " + dataset_type)
return dataset, chiralities
def split_last_dim(data):
last_dim = data.size()[-1]
last_dim = last_dim//2
if len(data.size()) == 3:
res = data[:,:,:last_dim], data[:,:,last_dim:]
if len(data.size()) == 2:
res = data[:,:last_dim], data[:,last_dim:]
return res
def init_network_weights(net, std = 0.1):
for m in net.modules():
if isinstance(m, nn.Linear):
nn.init.normal_(m.weight, mean=0, std=std)
nn.init.constant_(m.bias, val=0)
def flatten(x, dim):
return x.reshape(x.size()[:dim] + (-1, ))
def subsample_timepoints(data, time_steps, mask, n_tp_to_sample = None):
# n_tp_to_sample: number of time points to subsample. If not None, sample exactly n_tp_to_sample points
if n_tp_to_sample is None:
return data, time_steps, mask
n_tp_in_batch = len(time_steps)
if n_tp_to_sample > 1:
# Subsample exact number of points
assert(n_tp_to_sample <= n_tp_in_batch)
n_tp_to_sample = int(n_tp_to_sample)
for i in range(data.size(0)):
missing_idx = sorted(np.random.choice(np.arange(n_tp_in_batch), n_tp_in_batch - n_tp_to_sample, replace = False))
data[i, missing_idx] = 0.
if mask is not None:
mask[i, missing_idx] = 0.
elif (n_tp_to_sample <= 1) and (n_tp_to_sample > 0):
# Subsample percentage of points from each time series
percentage_tp_to_sample = n_tp_to_sample
for i in range(data.size(0)):
# take mask for current training sample and sum over all features -- figure out which time points don't have any measurements at all in this batch
current_mask = mask[i].sum(-1).cpu()
non_missing_tp = np.where(current_mask > 0)[0]
n_tp_current = len(non_missing_tp)
n_to_sample = int(n_tp_current * percentage_tp_to_sample)
subsampled_idx = sorted(np.random.choice(non_missing_tp, n_to_sample, replace = False))
tp_to_set_to_zero = np.setdiff1d(non_missing_tp, subsampled_idx)
data[i, tp_to_set_to_zero] = 0.
if mask is not None:
mask[i, tp_to_set_to_zero] = 0.
return data, time_steps, mask
def cut_out_timepoints(data, time_steps, mask, n_points_to_cut = None):
# n_points_to_cut: number of consecutive time points to cut out
if n_points_to_cut is None:
return data, time_steps, mask
n_tp_in_batch = len(time_steps)
if n_points_to_cut < 1:
raise Exception("Number of time points to cut out must be > 1")
assert(n_points_to_cut <= n_tp_in_batch)
n_points_to_cut = int(n_points_to_cut)
for i in range(data.size(0)):
start = np.random.choice(np.arange(5, n_tp_in_batch - n_points_to_cut-5), replace = False)
data[i, start : (start + n_points_to_cut)] = 0.
if mask is not None:
mask[i, start : (start + n_points_to_cut)] = 0.
return data, time_steps, mask
def get_device(tensor):
device = torch.device("cpu")
if tensor.is_cuda:
device = tensor.get_device()
return device
def sample_standard_gaussian(mu, sigma):
device = get_device(mu)
d = torch.distributions.normal.Normal(torch.Tensor([0.]).to(device), torch.Tensor([1.]).to(device))
r = d.sample(mu.size()).squeeze(-1)
return r * sigma.float() + mu.float()
def split_train_test(data, train_fraq = 0.8):
n_samples = data.size(0)
data_train = data[:int(n_samples * train_fraq)]
data_test = data[int(n_samples * train_fraq):]
return data_train, data_test
def split_train_test_data_and_time(data, time_steps, train_fraq = 0.8):
n_samples = data.size(0)
data_train = data[:int(n_samples * train_fraq)]
data_test = data[int(n_samples * train_fraq):]
assert(len(time_steps.size()) == 2)
train_time_steps = time_steps[:, :int(n_samples * train_fraq)]
test_time_steps = time_steps[:, int(n_samples * train_fraq):]
return data_train, data_test, train_time_steps, test_time_steps
def get_next_batch(dataloader):
# Make the union of all time points and perform normalization across the whole dataset
data_dict = dataloader.__next__()
batch_dict = get_dict_template()
# remove the time points where there are no observations in this batch
non_missing_tp = torch.sum(data_dict["observed_data"],(0,2)) != 0.
batch_dict["observed_data"] = data_dict["observed_data"][:, non_missing_tp]
batch_dict["observed_tp"] = data_dict["observed_tp"][non_missing_tp]
# print("observed data")
# print(batch_dict["observed_data"].size())
if ("observed_mask" in data_dict) and (data_dict["observed_mask"] is not None):
batch_dict["observed_mask"] = data_dict["observed_mask"][:, non_missing_tp]
batch_dict[ "data_to_predict"] = data_dict["data_to_predict"]
batch_dict["tp_to_predict"] = data_dict["tp_to_predict"]
non_missing_tp = torch.sum(data_dict["data_to_predict"],(0,2)) != 0.
batch_dict["data_to_predict"] = data_dict["data_to_predict"][:, non_missing_tp]
batch_dict["tp_to_predict"] = data_dict["tp_to_predict"][non_missing_tp]
# print("data_to_predict")
# print(batch_dict["data_to_predict"].size())
if ("mask_predicted_data" in data_dict) and (data_dict["mask_predicted_data"] is not None):
batch_dict["mask_predicted_data"] = data_dict["mask_predicted_data"][:, non_missing_tp]
if ("labels" in data_dict) and (data_dict["labels"] is not None):
batch_dict["labels"] = data_dict["labels"]
batch_dict["mode"] = data_dict["mode"]
return batch_dict
def get_ckpt_model(ckpt_path, model, device):
if not os.path.exists(ckpt_path):
raise Exception("Checkpoint " + ckpt_path + " does not exist.")
# Load checkpoint.
checkpt = torch.load(ckpt_path)
ckpt_args = checkpt['args']
state_dict = checkpt['state_dict']
model_dict = model.state_dict()
# 1. filter out unnecessary keys
state_dict = {k: v for k, v in state_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(state_dict)
# 3. load the new state dict
model.load_state_dict(state_dict)
model.to(device)
def update_learning_rate(optimizer, decay_rate = 0.999, lowest = 1e-3):
for param_group in optimizer.param_groups:
lr = param_group['lr']
lr = max(lr * decay_rate, lowest)
param_group['lr'] = lr
def linspace_vector(start, end, n_points):
# start is either one value or a vector
size = np.prod(start.size())
assert(start.size() == end.size())
if size == 1:
# start and end are 1d-tensors
res = torch.linspace(start, end, n_points)
else:
# start and end are vectors
res = torch.Tensor()
for i in range(0, start.size(0)):
res = torch.cat((res,
torch.linspace(start[i], end[i], n_points)),0)
res = torch.t(res.reshape(start.size(0), n_points))
return res
def reverse(tensor):
idx = [i for i in range(tensor.size(0)-1, -1, -1)]
return tensor[idx]
def create_net(n_inputs, n_outputs, n_layers = 1,
n_units = 100, nonlinear = nn.Tanh):
layers = [nn.Linear(n_inputs, n_units)]
for i in range(n_layers):
layers.append(nonlinear())
layers.append(nn.Linear(n_units, n_units))
layers.append(nonlinear())
layers.append(nn.Linear(n_units, n_outputs))
return nn.Sequential(*layers)
def get_item_from_pickle(pickle_file, item_name):
from_pickle = load_pickle(pickle_file)
if item_name in from_pickle:
return from_pickle[item_name]
return None
def get_dict_template():
return {"observed_data": None,
"observed_tp": None,
"data_to_predict": None,
"tp_to_predict": None,
"observed_mask": None,
"mask_predicted_data": None,
"labels": None
}
def normalize_data(data):
reshaped = data.reshape(-1, data.size(-1))
att_min = torch.min(reshaped, 0)[0]
att_max = torch.max(reshaped, 0)[0]
# we don't want to divide by zero
att_max[ att_max == 0.] = 1.
if (att_max != 0.).all():
data_norm = (data - att_min) / att_max
else:
raise Exception("Zero!")
if torch.isnan(data_norm).any():
raise Exception("nans!")
return data_norm, att_min, att_max
def normalize_masked_data(data, mask, att_min, att_max):
# we don't want to divide by zero
att_max[ att_max == 0.] = 1.
if (att_max != 0.).all():
data_norm = (data - att_min) / att_max
else:
raise Exception("Zero!")
if torch.isnan(data_norm).any():
raise Exception("nans!")
# set masked out elements back to zero
data_norm[mask == 0] = 0
return data_norm, att_min, att_max
def shift_outputs(outputs, first_datapoint = None):
outputs = outputs[:,:,:-1,:]
if first_datapoint is not None:
n_traj, n_dims = first_datapoint.size()
first_datapoint = first_datapoint.reshape(1, n_traj, 1, n_dims)
outputs = torch.cat((first_datapoint, outputs), 2)
return outputs
def split_data_extrap(data_dict, dataset = ""):
device = get_device(data_dict["data"])
n_observed_tp = data_dict["data"].size(1) // 2
if dataset == "hopper":
n_observed_tp = data_dict["data"].size(1) // 3
split_dict = {"observed_data": data_dict["data"][:,:n_observed_tp,:].clone(),
"observed_tp": data_dict["time_steps"][:n_observed_tp].clone(),
"data_to_predict": data_dict["data"][:,n_observed_tp:,:].clone(),
"tp_to_predict": data_dict["time_steps"][n_observed_tp:].clone()}
split_dict["observed_mask"] = None
split_dict["mask_predicted_data"] = None
split_dict["labels"] = None
if ("mask" in data_dict) and (data_dict["mask"] is not None):
split_dict["observed_mask"] = data_dict["mask"][:, :n_observed_tp].clone()
split_dict["mask_predicted_data"] = data_dict["mask"][:, n_observed_tp:].clone()
if ("labels" in data_dict) and (data_dict["labels"] is not None):
split_dict["labels"] = data_dict["labels"].clone()
split_dict["mode"] = "extrap"
return split_dict
def split_data_interp(data_dict):
device = get_device(data_dict["data"])
split_dict = {"observed_data": data_dict["data"].clone(),
"observed_tp": data_dict["time_steps"].clone(),
"data_to_predict": data_dict["data"].clone(),
"tp_to_predict": data_dict["time_steps"].clone()}
split_dict["observed_mask"] = None
split_dict["mask_predicted_data"] = None
split_dict["labels"] = None
if "mask" in data_dict and data_dict["mask"] is not None:
split_dict["observed_mask"] = data_dict["mask"].clone()
split_dict["mask_predicted_data"] = data_dict["mask"].clone()
if ("labels" in data_dict) and (data_dict["labels"] is not None):
split_dict["labels"] = data_dict["labels"].clone()
split_dict["mode"] = "interp"
return split_dict
def add_mask(data_dict):
data = data_dict["observed_data"]
mask = data_dict["observed_mask"]
if mask is None:
mask = torch.ones_like(data).to(get_device(data))
data_dict["observed_mask"] = mask
return data_dict
def subsample_observed_data(data_dict, n_tp_to_sample = None, n_points_to_cut = None):
# n_tp_to_sample -- if not None, randomly subsample the time points. The resulting timeline has n_tp_to_sample points
# n_points_to_cut -- if not None, cut out consecutive points on the timeline. The resulting timeline has (N - n_points_to_cut) points
if n_tp_to_sample is not None:
# Randomly subsample time points
data, time_steps, mask = subsample_timepoints(
data_dict["observed_data"].clone(),
time_steps = data_dict["observed_tp"].clone(),
mask = (data_dict["observed_mask"].clone() if data_dict["observed_mask"] is not None else None),
n_tp_to_sample = n_tp_to_sample)
if n_points_to_cut is not None:
# Remove consecutive time points
data, time_steps, mask = cut_out_timepoints(
data_dict["observed_data"].clone(),
time_steps = data_dict["observed_tp"].clone(),
mask = (data_dict["observed_mask"].clone() if data_dict["observed_mask"] is not None else None),
n_points_to_cut = n_points_to_cut)
new_data_dict = {}
for key in data_dict.keys():
new_data_dict[key] = data_dict[key]
new_data_dict["observed_data"] = data.clone()
new_data_dict["observed_tp"] = time_steps.clone()
new_data_dict["observed_mask"] = mask.clone()
if n_points_to_cut is not None:
# Cut the section in the data to predict as well
# Used only for the demo on the periodic function
new_data_dict["data_to_predict"] = data.clone()
new_data_dict["tp_to_predict"] = time_steps.clone()
new_data_dict["mask_predicted_data"] = mask.clone()
return new_data_dict
def split_and_subsample_batch(data_dict, args, data_type = "train"):
if data_type == "train":
# Training set
if args.extrap:
processed_dict = split_data_extrap(data_dict, dataset = args.dataset)
else:
processed_dict = split_data_interp(data_dict)
else:
# Test set
if args.extrap:
processed_dict = split_data_extrap(data_dict, dataset = args.dataset)
else:
processed_dict = split_data_interp(data_dict)
# add mask
processed_dict = add_mask(processed_dict)
# Subsample points or cut out the whole section of the timeline
if (args.sample_tp is not None) or (args.cut_tp is not None):
processed_dict = subsample_observed_data(processed_dict,
n_tp_to_sample = args.sample_tp,
n_points_to_cut = args.cut_tp)
# if (args.sample_tp is not None):
# processed_dict = subsample_observed_data(processed_dict,
# n_tp_to_sample = args.sample_tp)
return processed_dict
def compute_loss_all_batches(model,
test_dataloader, args,
n_batches, experimentID, device,
n_traj_samples = 1, kl_coef = 1.,
max_samples_for_eval = None):
total = {}
total["loss"] = 0
total["likelihood"] = 0
total["mse"] = 0
total["kl_first_p"] = 0
total["std_first_p"] = 0
total["pois_likelihood"] = 0
total["ce_loss"] = 0
n_test_batches = 0
classif_predictions = torch.Tensor([]).to(device)
all_test_labels = torch.Tensor([]).to(device)
for i in range(n_batches):
print("Computing loss... " + str(i))
batch_dict = get_next_batch(test_dataloader)
results = model.compute_all_losses(batch_dict,
n_traj_samples = n_traj_samples, kl_coef = kl_coef)
if args.classif:
n_labels = model.n_labels #batch_dict["labels"].size(-1)
n_traj_samples = results["label_predictions"].size(0)
classif_predictions = torch.cat((classif_predictions,
results["label_predictions"].reshape(n_traj_samples, -1, n_labels)),1)
all_test_labels = torch.cat((all_test_labels,
batch_dict["labels"].reshape(-1, n_labels)),0)
for key in total.keys():
if key in results:
var = results[key]
if isinstance(var, torch.Tensor):
var = var.detach()
total[key] += var
n_test_batches += 1
# for speed
if max_samples_for_eval is not None:
if n_batches * batch_size >= max_samples_for_eval:
break
if n_test_batches > 0:
for key, value in total.items():
total[key] = total[key] / n_test_batches
if args.classif:
if args.dataset == "physionet":
#all_test_labels = all_test_labels.reshape(-1)
# For each trajectory, we get n_traj_samples samples from z0 -- compute loss on all of them
all_test_labels = all_test_labels.repeat(n_traj_samples,1,1)
idx_not_nan = ~torch.isnan(all_test_labels)
classif_predictions = classif_predictions[idx_not_nan]
all_test_labels = all_test_labels[idx_not_nan]
dirname = "plots/" + str(experimentID) + "/"
os.makedirs(dirname, exist_ok=True)
total["auc"] = 0.
if torch.sum(all_test_labels) != 0.:
print("Number of labeled examples: {}".format(len(all_test_labels.reshape(-1))))
print("Number of examples with mortality 1: {}".format(torch.sum(all_test_labels == 1.)))
# Cannot compute AUC with only 1 class
total["auc"] = sk.metrics.roc_auc_score(all_test_labels.cpu().numpy().reshape(-1),
classif_predictions.cpu().numpy().reshape(-1))
else:
print("Warning: Couldn't compute AUC -- all examples are from the same class")
if args.dataset == "activity":
all_test_labels = all_test_labels.repeat(n_traj_samples,1,1)
labeled_tp = torch.sum(all_test_labels, -1) > 0.
all_test_labels = all_test_labels[labeled_tp]
classif_predictions = classif_predictions[labeled_tp]
# classif_predictions and all_test_labels are in on-hot-encoding -- convert to class ids
_, pred_class_id = torch.max(classif_predictions, -1)
_, class_labels = torch.max(all_test_labels, -1)
pred_class_id = pred_class_id.reshape(-1)
total["accuracy"] = sk.metrics.accuracy_score(
class_labels.cpu().numpy(),
pred_class_id.cpu().numpy())
return total
def check_mask(data, mask):
#check that "mask" argument indeed contains a mask for data
n_zeros = torch.sum(mask == 0.).cpu().numpy()
n_ones = torch.sum(mask == 1.).cpu().numpy()
# mask should contain only zeros and ones
assert((n_zeros + n_ones) == np.prod(list(mask.size())))
# all masked out elements should be zeros
assert(torch.sum(data[mask == 0.] != 0.) == 0)