-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.py
312 lines (268 loc) · 11.3 KB
/
main.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
from typing import List, Dict, Any
import numpy as np
import random
from scipy.stats import pearsonr
from sklearn.metrics import (
f1_score,
roc_auc_score,
mean_squared_error,
mean_absolute_error,
)
from sklearn.utils.class_weight import compute_class_weight
from gzip_utils import (
molnet_loader,
schneider_loader,
adme_loader,
pdbbind_loader,
local_molnet_loader,
write_table,
)
from moleculenet_config_nok import get_all_tests
from molzip import (
ZipRegressor,
ZipClassifier,
RDKitRegressor,
RDKitClassifier,
ZipClassifier_CV,
ZipRegressor_CV,
)
from molzip.featurizers import ZipFeaturizer
from molzip.compressors import GzipCompressor
def benchmark(configs: List[Dict[str, Any]]) -> None:
results = []
regressor_CV, regressor = ZipRegressor_CV(), ZipRegressor()
classifier_CV, classifier = ZipClassifier_CV(), ZipClassifier()
for config in configs:
print(f"Loading dataset {config['dataset']}...")
is_k_set = "k" in config
n_bins = config["bins"] if "bins" in config else 128
zip_featurizer = ZipFeaturizer(n_bins)
# loader = molnet_loader
loader = local_molnet_loader
if config["dataset"] in ["schneider"]:
loader = schneider_loader
if config["dataset"].startswith("adme"):
loader = adme_loader
if config["dataset"] == "pdbbind":
loader = pdbbind_loader
if "result_props" not in config:
config["result_props"] = {}
run_results = []
for i_n in range(config["n"]):
print(f"Running split {i_n}...")
random.seed(i_n)
tasks, X_train, y_train, X_valid, y_valid, X_test, y_test = loader(
config["dataset"],
splitter=config["splitter"],
task_name=config["task_name"] if "task_name" in config else None,
properties=config["properties"] if "properties" in config else None,
reload=False,
transformers=[],
)
if "transforms" in config:
for transform in config["transforms"]:
X_train = transform(X_train)
X_valid = transform(X_valid)
X_test = transform(X_test)
if config["task"] in ["classification", "classification_vec"]:
# Get class weights
class_weights = []
if config["is_imbalanced"]:
for y_task in y_train.T:
class_weights.append(
compute_class_weight(
"balanced",
classes=np.array(sorted(list(set(y_task)))),
y=y_task,
)
)
if config["task"] == "classification_vec":
X_train, nan_inds_train = zip_featurizer(X_train)
X_valid, nan_inds_valid = zip_featurizer(X_valid)
X_test, nan_inds_test = zip_featurizer(X_test)
# Some features are nan (because rdkit failed to compute partial charges for some molecules)
if len(nan_inds_train) > 0:
y_train = np.delete(y_train, nan_inds_train, axis=0)
if len(nan_inds_valid) > 0:
y_valid = np.delete(y_valid, nan_inds_valid, axis=0)
if len(nan_inds_test) > 0:
y_test = np.delete(y_test, nan_inds_test, axis=0)
k = config.get("k", 0)
if k == 0:
k, valid_preds = classifier_CV.fit_predict(
X_train,
y_train,
X_valid,
class_weights,
config.get("compressor", GzipCompressor()),
)
test_preds = classifier.fit_predict(
X_train,
y_train,
X_test,
k,
class_weights,
config.get("compressor", GzipCompressor()),
)
else:
k = config.get("k", 0)
if k == 0:
k, valid_preds = classifier_CV.fit_predict(
X_train,
y_train,
X_valid,
class_weights,
config.get("compressor", GzipCompressor()),
)
test_preds = classifier.fit_predict(
X_train,
y_train,
X_test,
k,
class_weights,
config.get("compressor", GzipCompressor()),
)
# Compute metrics
if not is_k_set:
try:
valid_auroc = roc_auc_score(
y_valid, valid_preds, multi_class="ovr"
)
except:
valid_auroc = 0.0
valid_f1 = f1_score(
y_valid,
valid_preds,
average="micro",
)
try:
test_auroc = roc_auc_score(y_test, test_preds, multi_class="ovr")
except:
test_auroc = 0.0
test_f1 = f1_score(
y_test,
test_preds,
average="micro",
)
print(f"\n{config['dataset']} ({len(tasks)} tasks)")
print(config)
if not is_k_set:
print(
f"Valid AUROC: {valid_auroc}, Valid F1: {valid_f1} , Test AUROC: {test_auroc}, Test F1: {test_f1}"
)
run_results.append(
[valid_auroc, valid_f1, 0, test_auroc, test_f1, 0]
)
else:
print(f"Test AUROC: {test_auroc}, Test F1: {test_f1}")
run_results.append([0, 0, 0, test_auroc, test_f1, 0])
else:
if config["task"] == "regression":
k = config.get("k", 0)
if k == 0:
k, valid_preds = regressor_CV.fit_predict(
X_train,
y_train,
X_valid,
config.get("compressor", GzipCompressor()),
)
test_preds = regressor.fit_predict(
X_train,
y_train,
X_test,
k,
config.get("compressor", GzipCompressor()),
)
elif config["task"] == "regression_vec":
X_train, nan_inds_train = zip_featurizer(X_train)
X_valid, nan_inds_valid = zip_featurizer(X_valid)
X_test, nan_inds_test = zip_featurizer(X_test)
# Some features are nan (because rdkit failed to compute partial charges for some molecules)
if len(nan_inds_train) > 0:
y_train = np.delete(y_train, nan_inds_train).reshape(-1, 1)
if len(nan_inds_valid) > 0:
y_valid = np.delete(y_valid, nan_inds_valid).reshape(-1, 1)
if len(nan_inds_test) > 0:
y_test = np.delete(y_test, nan_inds_test).reshape(-1, 1)
k = config.get("k", 0)
if k == 0:
k, valid_preds = regressor_CV.fit_predict(
X_train,
y_train,
X_valid,
config.get("compressor", GzipCompressor()),
)
test_preds = regressor.fit_predict(
X_train,
y_train,
X_test,
k,
config.get("compressor", GzipCompressor()),
)
else:
raise ValueError(f"Unknown task {config['task']}")
# Compute metrics
if not is_k_set:
valid_r = pearsonr(y_valid.flatten(), valid_preds.flatten())
valid_rmse = mean_squared_error(y_valid, valid_preds, squared=False)
valid_mae = mean_absolute_error(
y_valid,
valid_preds,
)
test_r = pearsonr(y_test.flatten(), test_preds.flatten())
test_rmse = mean_squared_error(y_test, test_preds, squared=False)
test_mae = mean_absolute_error(y_test, test_preds)
print(f"\n{config['dataset']} ({len(tasks)} tasks)")
print(config)
print("best k valid", k)
config["k"] = k
if not is_k_set:
print(
f"Valid R: {valid_r[0]}, Valid RMSE: {valid_rmse}, Valid MAE: {valid_mae}, Test R: {test_r[0]}, Test RMSE: {test_rmse}, Test MAE: {test_mae}"
)
run_results.append(
[
valid_rmse,
valid_mae,
valid_r[0],
test_rmse,
test_mae,
test_r[0],
]
)
else:
print(
f"Test R: {test_r[0]}, Test RMSE: {test_rmse}, Test MAE: {test_mae}"
)
run_results.append(
[
0,
0,
0,
test_rmse,
test_mae,
test_r[0],
]
)
run_results = np.array(run_results)
results_means = np.mean(run_results, axis=0)
results_stds = np.std(run_results, axis=0)
results.append(
(
config,
{
"valid_auroc": f"{round(results_means[0], 3)} +/- {round(results_stds[0], 3)}",
"valid_f1": f"{round(results_means[1], 3)} +/- {round(results_stds[1], 3)}",
"valid_r": f"{round(results_means[2], 3)} +/- {round(results_stds[2], 3)}",
"test_auroc": f"{round(results_means[3], 3)} +/- {round(results_stds[3], 3)}",
"test_f1": f"{round(results_means[4], 3)} +/- {round(results_stds[4], 3)}",
"test_r": f"{round(results_means[5], 3)} +/- {round(results_stds[5], 3)}",
"best_k_valid": k,
},
)
)
write_table(results)
def main():
benchmark(get_all_tests())
if __name__ == "__main__":
main()