-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
654 lines (537 loc) · 21.5 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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
"""
Some code are adapted from https://github.com/liyaguang/DCRNN
and https://github.com/xlwang233/pytorch-DCRNN, https://github.com/tsy935/eeg-gnn-ssl 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
import wandb
from sentence_transformers import SentenceTransformer
from constants import CORTEX_REGIONS_DESCRIPTIONS, ELECTRODES_BROADMANN_MAPPING, BROADMANN_AREA_DESCRIPTIONS
from collections import defaultdict
MASK = 0.
LARGE_NUM = 1e9
class WandbLogger():
def __init__(self, project, is_used, name=None):
self.is_used = is_used
if is_used and not name:
wandb.init(project=project)
elif is_used and name:
wandb.init(project=project, name=name)
def watch_model(self,model):
if self.is_used:
wandb.watch(model)
def log_hyperparams(self, params):
if self.is_used:
wandb.config.update(params)
def log_metrics(self, metrics):
if self.is_used:
wandb.log(metrics)
def log(self, key, value, round_idx):
if self.is_used:
wandb.log({key: value, "Round": round_idx})
def log_str(self, key, value):
if self.is_used:
wandb.log({key: value})
def save_file(self, path):
if path is not None and os.path.exists(path) and self.is_used:
wandb.save(path)
@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, model_name='dcrnn'):
"""
Load pretrained weights to DCRNN or NeuroGNN model
"""
# Load in pre-trained parameters
if model_name == 'dcrnn':
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
elif model_name == 'neurognn':
model_new.encoder = model_pretrained.encoder
else:
raise NotImplementedError
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)
def get_electrode_descriptions(electrode_brodmann_map, brodmann_area_descrips):
"""map electrode names to brodmann areas descriptions and return a dictionary for electrode descriptions
Args:
electrode_brodmann_map (dict): electrode to brodmann area mapping
brodmann_area_descrips (dict): brodmann area descriptions
Returns:
dict: electrode descriptions
"""
electrode_descriptions = dict()
for electrode, brodmann_area in electrode_brodmann_map.items():
electrode_descriptions[electrode] = brodmann_area_descrips[brodmann_area]
return electrode_descriptions
def get_semantic_embeds():
os.environ["TOKENIZERS_PARALLELISM"] = "false"
llm = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
descriptions = []
node_descriptions = get_electrode_descriptions(ELECTRODES_BROADMANN_MAPPING, BROADMANN_AREA_DESCRIPTIONS)
for node, descp in node_descriptions.items():
# descp = f'This node represents electrode {node.split()[1]} recordings. {descp}'
descriptions.append(descp)
for node, descp in CORTEX_REGIONS_DESCRIPTIONS.items():
# descp = f'This is a meta-node that represents the recordings for {node} region of the cortext. {descp}'
descriptions.append(descp)
# global node description
embeddings = llm.encode(descriptions)
return embeddings
def get_meta_node_indices(electrodes_regions):
meta_node_indices = defaultdict(list)
for i, (node, region) in enumerate(electrodes_regions.items()):
meta_node_indices[region].append(i)
return dict(meta_node_indices)
def get_adjacency_matrix(distance_df, sensor_ids, dist_k=0.9):
"""
Args:
distance_df: data frame with three columns: [from, to, distance].
sensor_ids: list of sensor ids.
dist_k: threshold for graph sparsity
Returns:
adj_mx: adj
"""
num_sensors = len(sensor_ids)
dist_mx = np.zeros((num_sensors, num_sensors), dtype=np.float32)
dist_mx[:] = np.inf
# Builds sensor id to index map.
sensor_id_to_ind = {}
for i, sensor_id in enumerate(sensor_ids):
sensor_id_to_ind[sensor_id] = i
# Fills cells in the matrix with distances.
for row in distance_df.values:
if row[0] not in sensor_id_to_ind or row[1] not in sensor_id_to_ind:
continue
dist_mx[sensor_id_to_ind[row[0]], sensor_id_to_ind[row[1]]] = row[2]
# Calculates the standard deviation as theta.
distances = dist_mx[~np.isinf(dist_mx)].flatten()
std = distances.std()
# Sets entries that lower than a threshold, i.e., k, to zero for sparsity.
adj_mx = np.exp(-np.square(dist_mx / std))
adj_mx[dist_mx > dist_k] = 0
return adj_mx, sensor_id_to_ind, dist_mx
def get_extended_adjacency_matrix(distance_df, sensor_ids, electrodes_regions, dist_k=0.9):
adj_mat, sensor_id_to_ind, dist_mx = get_adjacency_matrix(distance_df, sensor_ids, dist_k)
# map the sensor_id_to_ind to regions
region_to_indices = get_meta_node_indices(electrodes_regions)
# Get the number of regions and initialize a matrix for the meta nodes
num_regions = len(region_to_indices)
num_sensors = len(sensor_ids)
meta_dist_mx = np.zeros((num_sensors + num_regions, num_sensors + num_regions))
# Copy the original distance matrix to the extended matrix
meta_dist_mx[:num_sensors, :num_sensors] = dist_mx
# Calculate the mean distance for each region and add it to the matrix
for region, indices in region_to_indices.items():
region_index = num_sensors + list(region_to_indices.keys()).index(region)
for i in range(num_sensors):
meta_dist_mx[i, region_index] = dist_mx[i, indices].mean()
meta_dist_mx[region_index, i] = dist_mx[indices, i].mean()
for other_region, region_indices_j in region_to_indices.items():
if other_region != region:
other_region_index = num_sensors + list(region_to_indices.keys()).index(other_region)
meta_dist_mx[region_index, other_region_index] = dist_mx[indices][:, region_indices_j].mean()
meta_dist_mx[other_region_index, region_index] = dist_mx[region_indices_j][:, indices].mean()
# Calculate the adjacency matrix using the Gaussian kernel and the threshold
distances = meta_dist_mx[~np.isinf(meta_dist_mx)].flatten()
std = distances.std()
meta_adj_mat = np.exp(-np.square(meta_dist_mx / std))
meta_adj_mat[meta_dist_mx > dist_k] = 0
return meta_adj_mat, sensor_id_to_ind, meta_dist_mx