forked from ALEX95GOGO/CAGNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
495 lines (417 loc) · 15.6 KB
/
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
"""
Some code are adapted from https://github.com/liyaguang/DCRNN
and https://github.com/xlwang233/pytorch-DCRNN, which are
licensed under the MIT License.
"""
from contextlib import contextmanager
from sklearn.metrics import precision_recall_curve, accuracy_score, roc_auc_score
from sklearn.metrics import f1_score, recall_score, precision_score
from collections import OrderedDict, defaultdict
from itertools import repeat
from datetime import datetime
from pathlib import Path
from collections import defaultdict
from scipy.sparse import linalg
import sklearn
import matplotlib.cm as cm
import pandas as pd
import torch.nn.functional as F
import torch.nn as nn
import math
import tqdm
import shutil
import queue
import random
import time
import json
import torch
import h5py
import logging
import numpy as np
import os
import sys
import pickle
import scipy.sparse as sp
MASK = 0.
LARGE_NUM = 1e9
@contextmanager
def timer(name="Main", logger=None):
t0 = time.time()
yield
msg = f"[{name}] done in {time.time() - t0} s"
if logger is not None:
logger.info(msg)
else:
print(msg)
def seed_torch(seed=123):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
def get_save_dir(base_dir, training, id_max=500):
"""Get a unique save directory by appending the smallest positive integer
`id < id_max` that is not already taken (i.e., no dir exists with that id).
Args:
base_dir (str): Base directory in which to make save directories.
training (bool): Save dir. is for training (determines subdirectory).
id_max (int): Maximum ID number before raising an exception.
Returns:
save_dir (str): Path to a new directory with a unique name.
"""
for uid in range(1, id_max):
subdir = 'train' if training else 'test'
save_dir = os.path.join(
base_dir, subdir, '{}-{:02d}'.format(subdir, uid))
if not os.path.exists(save_dir):
os.makedirs(save_dir)
return save_dir
raise RuntimeError('Too many save directories created with the same name. \
Delete old save directories or use another name.')
class CheckpointSaver:
"""Class to save and load model checkpoints.
Save the best checkpoints as measured by a metric value passed into the
`save` method. Overwrite checkpoints with better checkpoints once
`max_checkpoints` have been saved.
Args:
save_dir (str): Directory to save checkpoints.
metric_name (str): Name of metric used to determine best model.
maximize_metric (bool): If true, best checkpoint is that which maximizes
the metric value passed in via `save`. Otherwise, best checkpoint
minimizes the metric.
log (logging.Logger): Optional logger for printing information.
"""
def __init__(self, save_dir, metric_name, maximize_metric=False, log=None):
super(CheckpointSaver, self).__init__()
self.save_dir = save_dir
self.metric_name = metric_name
self.maximize_metric = maximize_metric
self.best_val = None
self.ckpt_paths = queue.PriorityQueue()
self.log = log
#self._print('Saver will {}imize {}...'
# .format('max' if maximize_metric else 'min', metric_name))
def is_best(self, metric_val):
"""Check whether `metric_val` is the best seen so far.
Args:
metric_val (float): Metric value to compare to prior checkpoints.
"""
if metric_val is None:
# No metric reported
return False
if self.best_val is None:
# No checkpoint saved yet
return True
return ((self.maximize_metric and self.best_val <= metric_val)
or (not self.maximize_metric and self.best_val >= metric_val))
def _print(self, message):
"""Print a message if logging is enabled."""
if self.log is not None:
self.log.info(message)
def save(self, epoch, model, optimizer, metric_val):
"""Save model parameters to disk.
Args:
epoch (int): Current epoch.
model (torch.nn.DataParallel): Model to save.
optimizer: optimizer
metric_val (float): Determines whether checkpoint is best so far.
"""
ckpt_dict = {
'epoch': epoch,
'model_state': model.state_dict(),
'optimizer_state': optimizer.state_dict()
}
checkpoint_path = os.path.join(self.save_dir, 'last.pth.tar')
torch.save(ckpt_dict, checkpoint_path)
best_path = ''
if self.is_best(metric_val):
# Save the best model
self.best_val = metric_val
best_path = os.path.join(self.save_dir, 'best.pth.tar')
shutil.copy(checkpoint_path, best_path)
#self._print('New best checkpoint at epoch {}...'.format(epoch))
def load_model_checkpoint(checkpoint_file, model, optimizer=None):
checkpoint = torch.load(checkpoint_file)
model.load_state_dict(checkpoint['model_state'])
if optimizer is not None:
optimizer.load_state_dict(checkpoint['optimizer_state'])
return model, optimizer
return model
def build_finetune_model(model_new, model_pretrained, num_rnn_layers,
num_layers_frozen=0):
"""
Load pretrained weights to DCRNN model
"""
# Load in pre-trained parameters
for l in range(num_rnn_layers):
model_new.encoder.encoding_cells[l].dconv_gate = model_pretrained.encoder.encoding_cells[l].dconv_gate
model_new.encoder.encoding_cells[l].dconv_candidate = model_pretrained.encoder.encoding_cells[l].dconv_candidate
return model_new
class AverageMeter:
"""Keep track of average values over time.
Adapted from:
> https://github.com/pytorch/examples/blob/master/imagenet/main.py
"""
def __init__(self):
self.avg = 0
self.sum = 0
self.count = 0
def reset(self):
"""Reset meter."""
self.__init__()
def update(self, val, num_samples=1):
"""Update meter with new value `val`, the average of `num` samples.
Args:
val (float): Average value to update the meter with.
num_samples (int): Number of samples that were averaged to
produce `val`.
"""
self.count += num_samples
self.sum += val * num_samples
self.avg = self.sum / self.count
def calculate_normalized_laplacian(adj):
"""
# L = D^-1/2 (D-A) D^-1/2 = I - D^-1/2 A D^-1/2
# D = diag(A 1)
"""
adj = sp.coo_matrix(adj)
d = np.array(adj.sum(1))
d_inv_sqrt = np.power(d, -0.5).flatten()
d_inv_sqrt[np.isinf(d_inv_sqrt)] = 0.
d_mat_inv_sqrt = sp.diags(d_inv_sqrt)
normalized_laplacian = sp.eye(
adj.shape[0]) - adj.dot(d_mat_inv_sqrt).transpose().dot(d_mat_inv_sqrt).tocoo()
return normalized_laplacian
def calculate_random_walk_matrix(adj_mx):
"""
State transition matrix D_o^-1W in paper.
"""
adj_mx = sp.coo_matrix(adj_mx)
d = np.array(adj_mx.sum(1))
d_inv = np.power(d, -1).flatten()
d_inv[np.isinf(d_inv)] = 0.
d_mat_inv = sp.diags(d_inv)
random_walk_mx = d_mat_inv.dot(adj_mx).tocoo()
return random_walk_mx
def calculate_reverse_random_walk_matrix(adj_mx):
"""
Reverse state transition matrix D_i^-1W^T in paper.
"""
return calculate_random_walk_matrix(np.transpose(adj_mx))
def calculate_scaled_laplacian(adj_mx, lambda_max=2, undirected=True):
"""
Scaled Laplacian for ChebNet graph convolution
"""
if undirected:
adj_mx = np.maximum.reduce([adj_mx, adj_mx.T])
L = calculate_normalized_laplacian(adj_mx) # L is coo matrix
if lambda_max is None:
lambda_max, _ = linalg.eigsh(L, 1, which='LM')
lambda_max = lambda_max[0]
# L = sp.csr_matrix(L)
M, _ = L.shape
I = sp.identity(M, format='coo', dtype=L.dtype)
L = (2 / lambda_max * L) - I
# return L.astype(np.float32)
return L.tocoo()
def get_logger(log_dir, name, log_filename='info.log', level=logging.INFO):
logger = logging.getLogger(name)
logger.setLevel(level)
# Add file handler and stdout handler
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler = logging.FileHandler(os.path.join(log_dir, log_filename))
file_handler.setFormatter(formatter)
# Add console handler.
console_formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s')
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(console_formatter)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# Add google cloud log handler
logger.info('Log directory: %s', log_dir)
return logger
def count_parameters(model):
"""
Counter total number of parameters, for Pytorch
"""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def eval_dict(y_pred, y, y_prob=None, file_names=None, average='macro'):
"""
Args:
y_pred: Predicted labels of all samples
y : True labels of all samples
file_names: File names of all samples
average: 'weighted', 'micro', 'macro' etc. to compute F1 score etc.
Returns:
scores_dict: Dictionary containing scores such as F1, acc etc.
pred_dict: Dictionary containing predictions
true_dict: Dictionary containing labels
"""
scores_dict = {}
pred_dict = defaultdict(list)
true_dict = defaultdict(list)
# write into output dictionary
#if file_names is not None:
# for idx, f_name in enumerate(file_names):
# pred_dict[f_name] = y_pred[idx]
# true_dict[f_name] = y[idx]
if y is not None:
scores_dict['acc'] = accuracy_score(y_true=y, y_pred=y_pred)
scores_dict['F1'] = f1_score(y_true=y, y_pred=y_pred, average=average)
scores_dict['precision'] = precision_score(
y_true=y, y_pred=y_pred, average=average)
scores_dict['recall'] = recall_score(
y_true=y, y_pred=y_pred, average=average)
#if y_prob is not None:
# if len(set(y)) <= 2: # binary case
# scores_dict['auroc'] = roc_auc_score(y_true=y, y_score=y_prob)
return scores_dict, pred_dict, true_dict
def thresh_max_f1(y_true, y_prob):
"""
Find best threshold based on precision-recall curve to maximize F1-score.
Binary calssification only
"""
if len(set(y_true)) > 2:
raise NotImplementedError
precision, recall, thresholds = precision_recall_curve(y_true, y_prob)
thresh_filt = []
fscore = []
n_thresh = len(thresholds)
for idx in range(n_thresh):
curr_f1 = (2 * precision[idx] * recall[idx]) / \
(precision[idx] + recall[idx])
if not (np.isnan(curr_f1)):
fscore.append(curr_f1)
thresh_filt.append(thresholds[idx])
# locate the index of the largest f score
ix = np.argmax(np.array(fscore))
best_thresh = thresh_filt[ix]
return best_thresh
def last_relevant_pytorch(output, lengths, batch_first=True):
lengths = lengths.cpu()
# masks of the true seq lengths
masks = (lengths - 1).view(-1, 1).expand(len(lengths), output.size(2))
time_dimension = 1 if batch_first else 0
masks = masks.unsqueeze(time_dimension)
masks = masks.to(output.device)
last_output = output.gather(time_dimension, masks).squeeze(time_dimension)
last_output.to(output.device)
return last_output
class Timer:
def __init__(self):
self.cache = datetime.now()
def check(self):
now = datetime.now()
duration = now - self.cache
self.cache = now
return duration.total_seconds()
def reset(self):
self.cache = datetime.now()
def build_sparse_matrix(L):
"""
Build pytorch sparse tensor from scipy sparse matrix
reference: https://stackoverflow.com/questions/50665141
"""
shape = L.shape
i = torch.LongTensor(np.vstack((L.row, L.col)).astype(int))
v = torch.FloatTensor(L.data)
return torch.sparse.FloatTensor(i, v, torch.Size(shape))
def compute_sampling_threshold(cl_decay_steps, global_step):
"""
Compute scheduled sampling threshold
"""
return cl_decay_steps / \
(cl_decay_steps + np.exp(global_step / cl_decay_steps))
class StandardScaler:
"""
Standardize the input
"""
def __init__(self, mean, std):
self.mean = mean # (1,num_nodes,1)
self.std = std # (1,num_nodes,1)
def transform(self, data):
return (data - self.mean) / self.std
def inverse_transform(self, data, is_tensor=False, device=None, mask=None):
"""
Masked inverse transform
Args:
data: data for inverse scaling
is_tensor: whether data is a tensor
device: device
mask: shape (batch_size,) nodes where some signals are masked
"""
mean = self.mean.copy()
std = self.std.copy()
if len(mean.shape) == 0:
mean = [mean]
std = [std]
if is_tensor:
mean = torch.FloatTensor(mean)
std = torch.FloatTensor(std)
if device is not None:
mean = mean.to(device)
std = std.to(device)
#mean = torch.FloatTensor([mean])
#std = torch.FloatTensor([std])
return (data * std + mean)
def masked_mae_loss(y_pred, y_true, mask_val=0.):
"""
Only compute loss on unmasked part
"""
masks = (y_true != mask_val).float()
masks /= masks.mean()
loss = torch.abs(y_pred - y_true)
loss = loss * masks
# trick for nans:
# https://discuss.pytorch.org/t/how-to-set-nan-in-tensor-to-0/3918/3
loss[loss != loss] = 0
return loss.mean()
def masked_mse_loss(y_pred, y_true, mask_val=0.):
"""
Only compute MSE loss on unmasked part
"""
masks = (y_true != mask_val).float()
masks /= masks.mean()
loss = (y_pred - y_true).pow(2)
loss = loss * masks
# trick for nans:
# https://discuss.pytorch.org/t/how-to-set-nan-in-tensor-to-0/3918/3
loss[loss != loss] = 0
loss = torch.sqrt(torch.mean(loss))
return loss
def compute_regression_loss(
y_true,
y_predicted,
standard_scaler=None,
device=None,
loss_fn='mae',
mask_val=0.,
is_tensor=True):
"""
Compute masked MAE loss with inverse scaled y_true and y_predict
Args:
y_true: ground truth signals, shape (batch_size, mask_len, num_nodes, feature_dim)
y_predicted: predicted signals, shape (batch_size, mask_len, num_nodes, feature_dim)
standard_scaler: class StandardScaler object
device: device
mask: int, masked node ID
loss_fn: 'mae' or 'mse'
is_tensor: whether y_true and y_predicted are PyTorch tensor
"""
if device is not None:
y_true = y_true.to(device)
y_predicted = y_predicted.to(device)
if standard_scaler is not None:
y_true = standard_scaler.inverse_transform(y_true,
is_tensor=is_tensor,
device=device)
y_predicted = standard_scaler.inverse_transform(y_predicted,
is_tensor=is_tensor,
device=device)
if loss_fn == 'mae':
return masked_mae_loss(y_predicted, y_true, mask_val=mask_val)
else:
return masked_mse_loss(y_predicted, y_true, mask_val=mask_val)