-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_moltop.py
272 lines (245 loc) · 7.96 KB
/
main_moltop.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
import argparse
import numpy as np
import pandas as pd
from feature_engine.selection import DropConstantFeatures
from data_loading import (
DATASET_NAMES,
load_dataset,
DATASET_TASK_TYPES,
load_dataset_splits,
)
from feature_extraction import extract_features
from models import tune_hyperparameters, get_model, evaluate_model
from utils import ensure_bool
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument(
"--dataset_name",
choices=[
"all",
"all_fast",
"ogbg-molbace",
"ogbg-molbbbp",
"ogbg-molhiv",
"ogbg-molmuv",
"ogbg-molclintox",
"ogbg-molsider",
"ogbg-moltox21",
"ogbg-moltoxcast",
],
default="all",
help=(
"Dataset name. You can either provide dataset name from "
"MoleculeNet (via OGB), or use one of the following options: "
"'all_fast' to run on MoleculeNet apart from MUV and ToxCast; "
"'all' to run on the entire MoleculeNet; "
),
)
parser.add_argument(
"--degree_features",
type=ensure_bool,
default=True,
help="Use degree features based on LDP?",
)
parser.add_argument(
"--edge_betweenness",
type=ensure_bool,
default=True,
help="Add normalized edge betweenness centrality?",
)
parser.add_argument(
"--rand_index",
type=ensure_bool,
default=True,
help="Add normalized Adjusted Rand Index?",
)
parser.add_argument(
"--scan_structural_score",
type=ensure_bool,
default=True,
help="Add SCAN Structural Similarity Score?",
)
parser.add_argument(
"--atom_types",
type=ensure_bool,
default=True,
help="Add atom types features?",
)
parser.add_argument(
"--bond_types",
type=ensure_bool,
default=True,
help="Add bond types features?",
)
parser.add_argument(
"--n_bins",
type=lambda x: int(x) if x.isnumeric() else x,
default="median",
help=(
"Number of bins for aggregation. Either a number or 'median' "
"to use median number of atoms in training molecules."
),
)
parser.add_argument(
"--model_hyperparams",
choices=[
"optimized",
"LTP_default",
"tune",
],
default="optimized",
help=(
"Which hyperparameters to use for Random Forest: "
"'optimized' for features tuned on validation sets of MoleculeNet, "
"'LTP_default' for values suggested in LTP paper, "
"'tune' to perform hyperparameter tuning."
),
)
parser.add_argument(
"--use_valid_for_testing",
type=ensure_bool,
default=False,
help="Use validation split for testing? Only for MoleculeNet datasets!",
)
parser.add_argument(
"--use_full_training_data",
type=ensure_bool,
default=True,
help=(
"Use both training and validation splits for training? "
"Only for MoleculeNet datasets!"
),
)
parser.add_argument(
"--verbose",
type=ensure_bool,
default=False,
help="Should print out verbose output?",
)
return parser.parse_args()
def perform_experiment(
dataset_name: str,
degree_features: bool,
edge_betweenness: bool,
rand_index: bool,
scan_structural_score: bool,
atom_types: bool,
bond_types: bool,
n_bins: int | str,
model_hyperparams: str,
use_valid_for_testing: bool,
use_full_training_data: bool,
verbose: bool,
) -> tuple[float, float, float, float]:
dataset = load_dataset(dataset_name)
task_type = DATASET_TASK_TYPES[dataset_name]
train_idxs, test_idxs = load_dataset_splits(
dataset_name, use_valid_for_testing, use_full_training_data
)
if n_bins == "median":
nodes_nums = [data.num_nodes for data in dataset[train_idxs]]
n_bins = int(np.median(nodes_nums))
if verbose:
print(f"Selected {n_bins} histogram bins")
X = extract_features(
dataset,
degree_features=degree_features,
edge_betweenness=edge_betweenness,
rand_index=rand_index,
scan_structural_score=scan_structural_score,
atom_types=atom_types,
bond_types=bond_types,
n_bins=n_bins,
verbose=verbose,
)
X_train = X[train_idxs, :]
X_test = X[test_idxs, :]
dropper = DropConstantFeatures()
X_train = dropper.fit_transform(pd.DataFrame(X_train)).values
X_test = dropper.transform(pd.DataFrame(X_test)).values
if verbose:
constant_features = X.shape[1] - X_train.shape[1]
print(f"Eliminated {constant_features} constant features")
y = np.array(dataset.y)
if task_type == "classification":
y = y.ravel()
y_train = y[train_idxs]
y_test = y[test_idxs]
# fill NaN values with zeros for multioutput classification
y_train[np.isnan(y_train)] = 0
if model_hyperparams == "optimized":
# default values, optimized on validation sets of MoleculeNet fast datasets
hyperparams = {
"n_estimators": 1000,
"criterion": "entropy",
"min_samples_split": 10,
}
elif model_hyperparams == "LTP_default":
# default values from LTP paper
hyperparams = {"n_estimators": 500}
elif model_hyperparams == "tune":
hyperparams = tune_hyperparameters(
X_train=X_train, y_train=y_train, verbose=verbose
)
else:
raise ValueError(f"Value '{model_hyperparams}' not recognized")
test_metrics = []
params_counts = []
for random_state in range(10):
if verbose:
print(f"Starting random seed {random_state}")
model = get_model(
dataset_name=dataset_name,
random_state=random_state,
hyperparams=hyperparams,
verbose=verbose,
)
model.fit(X_train, y_train)
test_metric = evaluate_model(
dataset_name=dataset_name,
task_type=task_type,
model=model,
X_test=X_test,
y_test=y_test,
)
test_metrics.append(test_metric)
n_params = sum(tree.tree_.node_count for tree in model.estimators_)
params_counts.append(n_params)
test_metrics_mean = np.mean(test_metrics)
test_metrics_stddev = np.std(test_metrics)
params_mean = np.mean(params_counts)
params_stddev = np.std(params_counts)
return test_metrics_mean, test_metrics_stddev, params_mean, params_stddev
if __name__ == "__main__":
args = parse_args()
if args.dataset_name == "all":
datasets = DATASET_NAMES
elif args.dataset_name == "all_fast":
datasets = [
"ogbg-molbace",
"ogbg-molbbbp",
"ogbg-molhiv",
"ogbg-molclintox",
"ogbg-molsider",
"ogbg-moltox21",
]
else:
datasets = [args.dataset_name]
for dataset_name in datasets:
print(dataset_name)
test_mean, test_stddev, params_mean, params_stddev = perform_experiment(
dataset_name=dataset_name,
degree_features=args.degree_features,
edge_betweenness=args.edge_betweenness,
rand_index=args.rand_index,
scan_structural_score=args.scan_structural_score,
atom_types=args.atom_types,
bond_types=args.bond_types,
n_bins=args.n_bins,
model_hyperparams=args.model_hyperparams,
use_valid_for_testing=args.use_valid_for_testing,
use_full_training_data=args.use_full_training_data,
verbose=args.verbose,
)
print(f"AUROC: {100 * test_mean:.1f} +- {100 * test_stddev:.1f}")
print(f"Parameters: {params_mean:.2f} +- {params_stddev:.2f}")