-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
474 lines (395 loc) · 15.3 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
from cmath import exp
import os
import math
import random
import pickle
import numpy as np
import torch
from torch.nn import LayerNorm, BatchNorm1d
import torch
import random
import numpy as np
import datetime
import torch.nn as nn
from sklearn.metrics import precision_recall_curve, auc, roc_auc_score, mean_absolute_error, r2_score
from scipy.stats import pearsonr
import torch.nn.functional as F
def normalize(x):
return (x - x.min()) / (x.max() - x.min())
def create_dir(dir_list):
assert isinstance(dir_list, list) == True
for d in dir_list:
if not os.path.exists(d):
os.makedirs(d)
def save_model_dict(model, model_dir, msg):
model_path = os.path.join(model_dir, msg + '.pt')
torch.save(model.state_dict(), model_path)
print("model has been saved to %s." % (model_path))
def load_model_dict(model, ckpt):
model.load_state_dict(torch.load(ckpt))
def uniform(size, tensor):
bound = 1.0 / math.sqrt(size)
if tensor is not None:
tensor.data.uniform_(-bound, bound)
def reset(nn):
def _reset(item):
if hasattr(item, 'reset_parameters'):
item.reset_parameters()
if nn is not None:
if hasattr(nn, 'children') and len(list(nn.children())) > 0:
for item in nn.children():
_reset(item)
else:
_reset(nn)
def seed_torch(seed=1029):
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
class NodeLevelBatchNorm(BatchNorm1d):
r"""
Applies Batch Normalization over a batch of graph data.
Shape:
- Input: [batch_nodes_dim, node_feature_dim]
- Output: [batch_nodes_dim, node_feature_dim]
batch_nodes_dim: all nodes of a batach graph
"""
def _check_input_dim(self, input):
if input.dim() != 2:
raise ValueError('expected 2D input (got {}D input)'
.format(input.dim()))
def extra_repr(self):
return '{num_features}, affine={affine}'.format(**self.__dict__)
class NodeLevelLayerNorm(LayerNorm):
r"""
Applies node level layer normalization over a batch of graph data.
LayerNorm in/out: [N, **] number of examples, etc.
Shape:
- Input: [batch_nodes_dim, node_feature_dim]
- Output: [batch_nodes_dim, node_feature_dim]
"""
def _check_input_dim(self, input):
if input.dim() != 2:
raise ValueError('expected 2D input (got {}D input)'
.format(input.dim()))
def extra_repr(self):
return '{normalized_shape},' \
'elementwise_affine={elementwise_affine}'.format(**self.__dict__)
def del_file(path):
for i in os.listdir(path):
path_file = os.path.join(path,i)
if os.path.isfile(path_file):
os.remove(path_file)
else:
del_file(path_file)
def write_pickle(filename, obj):
with open(filename, 'wb') as f:
pickle.dump(obj, f)
def read_pickle(filename):
with open(filename, 'rb') as f:
obj = pickle.load(f)
return obj
class BestMeter(object):
"""Computes and stores the best value"""
def __init__(self, best_type):
self.best_type = best_type
self.count = 0
self.reset()
def reset(self):
if self.best_type == 'min':
self.best = float('inf')
else:
self.best = -float('inf')
def update(self, best):
self.best = best
self.count = 0
def get_best(self):
return self.best
def counter(self):
self.count += 1
return self.count
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
def get_average(self):
self.avg = self.sum / (self.count + 1e-12)
return self.avg
def angle(vector1, vector2):
cos_angle = vector1.dot(vector2) / (np.linalg.norm(vector1)*np.linalg.norm(vector2)+1e-8)
angle = np.arccos(cos_angle)
# angle2=angle*360/2/np.pi
return angle
def area_triangle(vector1, vector2):
trianglearea = 0.5 * np.linalg.norm( \
np.cross(vector1, vector2))
return trianglearea
def area_triangle_vertex(vertex1, vertex2, vertex3):
trianglearea = 0.5 * np.linalg.norm( \
np.cross(vertex2 - vertex1, vertex3 - vertex1))
return trianglearea
def cal_angle_area(vector1, vector2):
return angle(vector1, vector2), area_triangle(vector1, vector2)
def cal_dist(vertex1, vertex2, ord=2):
return np.linalg.norm(vertex1 - vertex2, ord=ord)
def set_random_seed(seed, deterministic=True):
"""Set random seed."""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
if deterministic:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
class EarlyStopping(object):
def __init__(self, mode='higher', patience=15, filename=None, tolerance=0.0):
if filename is None:
dt = datetime.datetime.now()
filename = './save/early_stop_{}_{:02d}-{:02d}-{:02d}.pth'.format(dt.date(), dt.hour, dt.minute, dt.second)
assert mode in ['higher', 'lower']
self.mode = mode
if self.mode == 'higher':
self._check = self._check_higher
else:
self._check = self._check_lower
self.patience = patience
self.tolerance = tolerance
self.counter = 0
self.filename = filename
self.best_score = None
self.early_stop = False
def _check_higher(self, score, prev_best_score):
# return (score > prev_best_score)
return score / prev_best_score > 1 + self.tolerance
def _check_lower(self, score, prev_best_score):
# return (score < prev_best_score)
return prev_best_score / score > 1 + self.tolerance
def step(self, score, model):
if self.best_score is None:
self.best_score = score
self.save_checkpoint(model)
elif self._check(score, self.best_score):
self.best_score = score
self.save_checkpoint(model)
self.counter = 0
else:
self.counter += 1
print(
f'EarlyStopping counter: {self.counter} out of {self.patience}')
if self.counter >= self.patience:
self.early_stop = True
return self.early_stop
def save_checkpoint(self, model):
'''Saves model when the metric on the validation set gets improved.'''
torch.save({'model_state_dict': model.state_dict()}, self.filename) # 保存网络中的参数, 速度快,占空间少, 以字典格式存储
def load_checkpoint(self, model):
'''Load model saved with early stopping.'''
model.load_state_dict(torch.load(self.filename)['model_state_dict'])
class Meter(object):
def __init__(self):
self.mask = []
self.y_pred = []
self.y_true = []
def update(self, y_pred, y_true, mask):
self.y_pred.append(y_pred.detach().cpu())
self.y_true.append(y_true.detach().cpu())
self.mask.append(mask.detach().cpu())
def roc_precision_recall_score(self):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
n_data, n_tasks = y_true.shape
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0].numpy()
task_y_pred = y_pred[:, task][task_w != 0].numpy()
precision, recall, _thresholds = precision_recall_curve(task_y_true, task_y_pred, pos_label=1)
scores.append(auc(recall, precision))
return scores
def roc_auc_score(self):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
# Todo: support categorical classes
# This assumes binary case only
y_pred = torch.sigmoid(y_pred) # 求得为正例的概率
n_tasks = y_true.shape[1]
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0].numpy()
task_y_pred = y_pred[:, task][task_w != 0].numpy()
scores.append(roc_auc_score(task_y_true, task_y_pred))
return scores
def l1_loss(self, reduction):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
n_tasks = y_true.shape[1]
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0]
task_y_pred = y_pred[:, task][task_w != 0]
scores.append(torch.nn.functional.l1_loss(task_y_true, task_y_pred, reduction=reduction).item())
return scores
def rmse(self):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
n_data, n_tasks = y_true.shape
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0]
task_y_pred = y_pred[:, task][task_w != 0]
scores.append(np.sqrt(torch.nn.functional.mse_loss(task_y_pred, task_y_true).cpu().item()))
return scores
def mae(self):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
n_data, n_tasks = y_true.shape
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0].numpy()
task_y_pred = y_pred[:, task][task_w != 0].numpy()
scores.append(mean_absolute_error(task_y_true, task_y_pred))
return scores
def r2(self):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
n_data, n_tasks = y_true.shape
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0].numpy()
task_y_pred = y_pred[:, task][task_w != 0].numpy()
scores.append(r2_score(task_y_true, task_y_pred))
return scores
def Rp(self):
mask = torch.cat(self.mask, dim=0)
y_pred = torch.cat(self.y_pred, dim=0)
y_true = torch.cat(self.y_true, dim=0)
n_data, n_tasks = y_true.shape
scores = []
for task in range(n_tasks):
task_w = mask[:, task]
task_y_true = y_true[:, task][task_w != 0].numpy()
task_y_pred = y_pred[:, task][task_w != 0].numpy()
scores.append(pearsonr(task_y_true, task_y_pred)[0])
return scores
def compute_metric(self, metric_name, reduction='mean'):
if metric_name == 'roc_auc':
return self.roc_auc_score()
if metric_name == 'l1':
return self.l1_loss(reduction)
if metric_name == 'prc_auc':
return self.roc_precision_recall_score()
if metric_name == 'rmse':
return self.rmse()
if metric_name == 'mae':
return self.mae()
if metric_name == 'r2':
return self.r2()
if metric_name == 'rp':
return self.Rp()
class MyLoss(nn.Module):
def __init__(self, alph):
super(MyLoss, self).__init__()
self.alph = alph
def forward(self, input, target):
sum_xy = torch.sum(torch.sum(input * target))
sum_x = torch.sum(torch.sum(input))
sum_y = torch.sum(torch.sum(target))
sum_x2 = torch.sum(torch.sum(input * input))
sum_y2 = torch.sum(torch.sum(target * target))
n = input.size()[0]
pcc = (n * sum_xy - sum_x * sum_y) / torch.sqrt((n * sum_x2 - sum_x * sum_x) * (n * sum_y2 - sum_y * sum_y))
return self.alph*(1-torch.abs(pcc)) + (1-self.alph)*torch.nn.functional.mse_loss(input, target)
class FocalLoss(nn.Module):
def __init__(self, alpha=1, gamma=2, logits=False, reduce=True):
super(FocalLoss, self).__init__()
self.alpha = alpha
self.gamma = gamma
self.logits = logits
self.reduce = reduce
def forward(self, inputs, targets):
if self.logits:
BCE_loss = F.binary_cross_entropy_with_logits(inputs, targets, reduce=False)
else:
BCE_loss = F.binary_cross_entropy(inputs, targets, reduce=False)
pt = torch.exp(-BCE_loss)
F_loss = self.alpha * (1-pt)**self.gamma * BCE_loss
if self.reduce:
return torch.mean(F_loss)
else:
return F_loss
N_atom_features = 28
def set_cuda_visible_device(ngpus):
import subprocess
import os
empty = []
for i in range(8):
command = 'nvidia-smi -i '+str(i)+' | grep "No running" | wc -l'
output = subprocess.check_output(command, shell=True).decode("utf-8")
#print('nvidia-smi -i '+str(i)+' | grep "No running" | wc -l > empty_gpu_check')
if int(output)==1:
empty.append(i)
if len(empty)<ngpus:
print ('avaliable gpus are less than required')
exit(-1)
cmd = ''
for i in range(ngpus):
cmd+=str(empty[i])+','
return cmd
def initialize_model(model, device, load_save_file=False):
if load_save_file:
model.load_state_dict(torch.load(load_save_file))
else:
for param in model.parameters():
if param.dim() == 1:
continue
nn.init.constant(param, 0)
else:
#nn.init.normal(param, 0.0, 0.15)
nn.init.xavier_normal_(param)
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
model = nn.DataParallel(model)
model.to(device)
return model
def one_of_k_encoding(x, allowable_set):
if x not in allowable_set:
raise Exception("input {0} not in allowable set{1}:".format(x, allowable_set))
#print list((map(lambda s: x == s, allowable_set)))
return list(map(lambda s: x == s, allowable_set))
def one_of_k_encoding_unk(x, allowable_set):
"""Maps inputs not in the allowable set to the last element."""
if x not in allowable_set:
x = allowable_set[-1]
return list(map(lambda s: x == s, allowable_set))
def atom_feature(m, atom_i, i_donor, i_acceptor):
atom = m.GetAtomWithIdx(atom_i)
return np.array(one_of_k_encoding_unk(atom.GetSymbol(),
['C', 'N', 'O', 'S', 'F', 'P', 'Cl', 'Br', 'B', 'H']) +
one_of_k_encoding_unk(atom.GetDegree(), [0, 1, 2, 3, 4, 5]) +
one_of_k_encoding_unk(atom.GetTotalNumHs(), [0, 1, 2, 3, 4]) +
one_of_k_encoding_unk(atom.GetImplicitValence(), [0, 1, 2, 3, 4, 5]) +
[atom.GetIsAromatic()]) # (10, 6, 5, 6, 1) --> total 28