-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.py
75 lines (63 loc) · 2.55 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
import numpy as np
import torch
from scipy import sparse
import os.path
import time
import torch.nn as nn
from ase import Atoms, Atom
#from rdkit.Contrib.SA_Score.sascorer import calculateScore
#from rdkit.Contrib.SA_Score.sascorer
#import deepchem as dc
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(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