-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathutils.py
342 lines (306 loc) · 12.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
# -*- coding: utf-8 -*-
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import dgl
import errno
import json
import os
import torch
import torch.nn.functional as F
from dgllife.data import MoleculeCSVDataset
from dgllife.utils import SMILESToBigraph, ScaffoldSplitter, RandomSplitter
def init_featurizer(args):
"""Initialize node/edge featurizer
Parameters
----------
args : dict
Settings
Returns
-------
args : dict
Settings with featurizers updated
"""
if args['model'] in ['gin_supervised_contextpred', 'gin_supervised_infomax',
'gin_supervised_edgepred', 'gin_supervised_masking']:
from dgllife.utils import PretrainAtomFeaturizer, PretrainBondFeaturizer
args['atom_featurizer_type'] = 'pre_train'
args['bond_featurizer_type'] = 'pre_train'
args['node_featurizer'] = PretrainAtomFeaturizer()
args['edge_featurizer'] = PretrainBondFeaturizer()
return args
if args['atom_featurizer_type'] == 'canonical':
from dgllife.utils import CanonicalAtomFeaturizer
args['node_featurizer'] = CanonicalAtomFeaturizer()
elif args['atom_featurizer_type'] == 'attentivefp':
from dgllife.utils import AttentiveFPAtomFeaturizer
args['node_featurizer'] = AttentiveFPAtomFeaturizer()
else:
return ValueError(
"Expect node_featurizer to be in ['canonical', 'attentivefp'], "
"got {}".format(args['atom_featurizer_type']))
if args['model'] in ['Weave', 'MPNN', 'AttentiveFP']:
if args['bond_featurizer_type'] == 'canonical':
from dgllife.utils import CanonicalBondFeaturizer
args['edge_featurizer'] = CanonicalBondFeaturizer(self_loop=True)
elif args['bond_featurizer_type'] == 'attentivefp':
from dgllife.utils import AttentiveFPBondFeaturizer
args['edge_featurizer'] = AttentiveFPBondFeaturizer(self_loop=True)
else:
args['edge_featurizer'] = None
return args
def load_dataset(args, df):
smiles_to_g = SMILESToBigraph(add_self_loop=True, node_featurizer=args['node_featurizer'],
edge_featurizer=args['edge_featurizer'])
dataset = MoleculeCSVDataset(df=df,
smiles_to_graph=smiles_to_g,
smiles_column=args['smiles_column'],
cache_file_path=args['result_path'] + '/graph.bin',
task_names=args['task_names'],
n_jobs=args['num_workers'])
return dataset
def get_configure(model):
"""Query for the manually specified configuration
Parameters
----------
model : str
Model type
Returns
-------
dict
Returns the manually specified configuration
"""
with open('model_configures/{}.json'.format(model), 'r') as f:
config = json.load(f)
return config
def mkdir_p(path):
"""Create a folder for the given path.
Parameters
----------
path: str
Folder to create
"""
try:
os.makedirs(path)
print('Created directory {}'.format(path))
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
print('Directory {} already exists.'.format(path))
else:
raise
def init_trial_path(args):
"""Initialize the path for a hyperparameter setting
Parameters
----------
args : dict
Settings
Returns
-------
args : dict
Settings with the trial path updated
"""
trial_id = 0
path_exists = True
while path_exists:
trial_id += 1
path_to_results = args['result_path'] + '/{:d}'.format(trial_id)
path_exists = os.path.exists(path_to_results)
args['trial_path'] = path_to_results
mkdir_p(args['trial_path'])
return args
def split_dataset(args, dataset):
"""Split the dataset
Parameters
----------
args : dict
Settings
dataset
Dataset instance
Returns
-------
train_set
Training subset
val_set
Validation subset
test_set
Test subset
"""
train_ratio, val_ratio, test_ratio = map(float, args['split_ratio'].split(','))
if args['split'] == 'scaffold_decompose':
train_set, val_set, test_set = ScaffoldSplitter.train_val_test_split(
dataset, frac_train=train_ratio, frac_val=val_ratio, frac_test=test_ratio,
scaffold_func='decompose')
elif args['split'] == 'scaffold_smiles':
train_set, val_set, test_set = ScaffoldSplitter.train_val_test_split(
dataset, frac_train=train_ratio, frac_val=val_ratio, frac_test=test_ratio,
scaffold_func='smiles')
elif args['split'] == 'random':
train_set, val_set, test_set = RandomSplitter.train_val_test_split(
dataset, frac_train=train_ratio, frac_val=val_ratio, frac_test=test_ratio)
else:
return ValueError("Expect the splitting method to be 'scaffold', got {}".format(args['split']))
return train_set, val_set, test_set
def collate_molgraphs(data):
"""Batching a list of datapoints for dataloader.
Parameters
----------
data : list of 4-tuples.
Each tuple is for a single datapoint, consisting of
a SMILES, a DGLGraph, all-task labels and a binary
mask indicating the existence of labels.
Returns
-------
smiles : list
List of smiles
bg : DGLGraph
The batched DGLGraph.
labels : Tensor of dtype float32 and shape (B, T)
Batched datapoint labels. B is len(data) and
T is the number of total tasks.
masks : Tensor of dtype float32 and shape (B, T)
Batched datapoint binary mask, indicating the
existence of labels.
"""
smiles, graphs, labels, masks = map(list, zip(*data))
bg = dgl.batch(graphs)
bg.set_n_initializer(dgl.init.zero_initializer)
bg.set_e_initializer(dgl.init.zero_initializer)
labels = torch.stack(labels, dim=0)
if masks is None:
masks = torch.ones(labels.shape)
else:
masks = torch.stack(masks, dim=0)
return smiles, bg, labels, masks
def collate_molgraphs_unlabeled(data):
"""Batching a list of datapoints without labels
Parameters
----------
data : list of 2-tuples.
Each tuple is for a single datapoint, consisting of
a SMILES and a DGLGraph.
Returns
-------
smiles : list
List of smiles
bg : DGLGraph
The batched DGLGraph.
"""
smiles, graphs = map(list, zip(*data))
bg = dgl.batch(graphs)
bg.set_n_initializer(dgl.init.zero_initializer)
bg.set_e_initializer(dgl.init.zero_initializer)
return smiles, bg
def load_model(exp_configure):
if exp_configure['model'] == 'GCN':
from dgllife.model import GCNPredictor
model = GCNPredictor(
in_feats=exp_configure['in_node_feats'],
hidden_feats=[exp_configure['gnn_hidden_feats']] * exp_configure['num_gnn_layers'],
activation=[F.relu] * exp_configure['num_gnn_layers'],
residual=[exp_configure['residual']] * exp_configure['num_gnn_layers'],
batchnorm=[exp_configure['batchnorm']] * exp_configure['num_gnn_layers'],
dropout=[exp_configure['dropout']] * exp_configure['num_gnn_layers'],
predictor_hidden_feats=exp_configure['predictor_hidden_feats'],
predictor_dropout=exp_configure['dropout'],
n_tasks=exp_configure['n_tasks'])
elif exp_configure['model'] == 'GAT':
from dgllife.model import GATPredictor
model = GATPredictor(
in_feats=exp_configure['in_node_feats'],
hidden_feats=[exp_configure['gnn_hidden_feats']] * exp_configure['num_gnn_layers'],
num_heads=[exp_configure['num_heads']] * exp_configure['num_gnn_layers'],
feat_drops=[exp_configure['dropout']] * exp_configure['num_gnn_layers'],
attn_drops=[exp_configure['dropout']] * exp_configure['num_gnn_layers'],
alphas=[exp_configure['alpha']] * exp_configure['num_gnn_layers'],
residuals=[exp_configure['residual']] * exp_configure['num_gnn_layers'],
predictor_hidden_feats=exp_configure['predictor_hidden_feats'],
predictor_dropout=exp_configure['dropout'],
n_tasks=exp_configure['n_tasks']
)
elif exp_configure['model'] == 'Weave':
from dgllife.model import WeavePredictor
model = WeavePredictor(
node_in_feats=exp_configure['in_node_feats'],
edge_in_feats=exp_configure['in_edge_feats'],
num_gnn_layers=exp_configure['num_gnn_layers'],
gnn_hidden_feats=exp_configure['gnn_hidden_feats'],
graph_feats=exp_configure['graph_feats'],
gaussian_expand=exp_configure['gaussian_expand'],
n_tasks=exp_configure['n_tasks']
)
elif exp_configure['model'] == 'MPNN':
from dgllife.model import MPNNPredictor
model = MPNNPredictor(
node_in_feats=exp_configure['in_node_feats'],
edge_in_feats=exp_configure['in_edge_feats'],
node_out_feats=exp_configure['node_out_feats'],
edge_hidden_feats=exp_configure['edge_hidden_feats'],
num_step_message_passing=exp_configure['num_step_message_passing'],
num_step_set2set=exp_configure['num_step_set2set'],
num_layer_set2set=exp_configure['num_layer_set2set'],
n_tasks=exp_configure['n_tasks']
)
elif exp_configure['model'] == 'AttentiveFP':
from dgllife.model import AttentiveFPPredictor
model = AttentiveFPPredictor(
node_feat_size=exp_configure['in_node_feats'],
edge_feat_size=exp_configure['in_edge_feats'],
num_layers=exp_configure['num_layers'],
num_timesteps=exp_configure['num_timesteps'],
graph_feat_size=exp_configure['graph_feat_size'],
dropout=exp_configure['dropout'],
n_tasks=exp_configure['n_tasks']
)
elif exp_configure['model'] in ['gin_supervised_contextpred', 'gin_supervised_infomax',
'gin_supervised_edgepred', 'gin_supervised_masking']:
from dgllife.model import GINPredictor
from dgllife.model import load_pretrained
model = GINPredictor(
num_node_emb_list=[120, 3],
num_edge_emb_list=[6, 3],
num_layers=5,
emb_dim=300,
JK=exp_configure['jk'],
dropout=0.5,
readout=exp_configure['readout'],
n_tasks=exp_configure['n_tasks']
)
model.gnn = load_pretrained(exp_configure['model'])
model.gnn.JK = exp_configure['jk']
elif exp_configure['model'] == 'NF':
from dgllife.model import NFPredictor
model = NFPredictor(
in_feats=exp_configure['in_node_feats'],
n_tasks=exp_configure['n_tasks'],
hidden_feats=[exp_configure['gnn_hidden_feats']] * exp_configure['num_gnn_layers'],
batchnorm=[exp_configure['batchnorm']] * exp_configure['num_gnn_layers'],
dropout=[exp_configure['dropout']] * exp_configure['num_gnn_layers'],
predictor_hidden_size=exp_configure['predictor_hidden_feats'],
predictor_batchnorm=exp_configure['batchnorm'],
predictor_dropout=exp_configure['dropout']
)
else:
return ValueError("Expect model to be from ['GCN', 'GAT', 'Weave', 'MPNN', 'AttentiveFP', "
"'gin_supervised_contextpred', 'gin_supervised_infomax', "
"'gin_supervised_edgepred', 'gin_supervised_masking', 'NF'], "
"got {}".format(exp_configure['model']))
return model
def predict(args, model, bg):
bg = bg.to(args['device'])
if args['edge_featurizer'] is None:
node_feats = bg.ndata.pop('h').to(args['device'])
return model(bg, node_feats)
elif args['bond_featurizer_type'] == 'pre_train':
node_feats = [
bg.ndata.pop('atomic_number').to(args['device']),
bg.ndata.pop('chirality_type').to(args['device'])
]
edge_feats = [
bg.edata.pop('bond_type').to(args['device']),
bg.edata.pop('bond_direction_type').to(args['device'])
]
return model(bg, node_feats, edge_feats)
else:
node_feats = bg.ndata.pop('h').to(args['device'])
edge_feats = bg.edata.pop('e').to(args['device'])
return model(bg, node_feats, edge_feats)