-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_neural_fingerprint_predictions.py
315 lines (283 loc) · 9.79 KB
/
04_neural_fingerprint_predictions.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
"""Run ML experiments on the Tox21 dataset."""
# pylint: disable=invalid-name
# pylint: enable=invalid-name
import argparse
import logging
from pathlib import Path
from typing import Any
import joblib
import numpy as np
import pandas as pd
from lightning import pytorch as pl
from lightning.pytorch.utilities import disable_possible_user_warnings
from molpipeline.any2mol import SmilesToMol
from molpipeline.error_handling import ErrorFilter, FilterReinserter
from molpipeline.estimators.chemprop.models import ChempropClassifier
from molpipeline.estimators.chemprop.neural_fingerprint import ChempropNeuralFP
from molpipeline.mol2any.mol2chemprop import MolToChemprop
from molpipeline.pipeline import Pipeline
from molpipeline.post_prediction import PostPredictionWrapper
from sklearn.base import BaseEstimator
from sklearn.calibration import CalibratedClassifierCV
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV, LeaveOneGroupOut, LeavePGroupsOut
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from tqdm.auto import tqdm
def parse_args() -> argparse.Namespace:
"""Parse command line arguments.
Returns
-------
argparse.Namespace
Parsed command line arguments.
"""
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument(
"--n_jobs",
type=int,
default=16,
help="Number of jobs to use for training.",
)
argument_parser.add_argument(
"--endpoint",
type=str,
help="Endpoint to train on.",
)
argument_parser.add_argument(
"--nn_calibration",
type=str,
default="isotonic",
help="Calibration method for neural networks.",
)
args = argument_parser.parse_args()
return args
def define_chemprop_pipeline(
n_jobs: int, calibration_method: str = "isotonic"
) -> CalibratedClassifierCV:
"""Define the Chemprop pipeline.
Parameters
----------
n_jobs : int
Number of jobs to use for training.
Returns
-------
Pipeline
Chemprop pipeline.
"""
trainer = pl.Trainer(
logger=False,
enable_checkpointing=False,
max_epochs=50,
enable_model_summary=False,
callbacks=[],
enable_progress_bar=False,
val_check_interval=0.0,
)
error_filter = ErrorFilter(filter_everything=True)
pipeline = Pipeline(
[
("smi2mol", SmilesToMol()),
("error_filter", error_filter),
("mol2graph", MolToChemprop()),
(
"chemprop",
ChempropClassifier(
n_jobs=n_jobs,
lightning_trainer=trainer,
model__message_passing__dropout_rate=0.2,
),
),
(
"error_replacer",
PostPredictionWrapper(
FilterReinserter.from_error_filter(error_filter, fill_value=np.nan)
),
),
],
n_jobs=1,
memory=joblib.Memory(),
)
calibrated_pipeline = CalibratedClassifierCV(
estimator=pipeline,
method=calibration_method,
cv=5,
n_jobs=1,
ensemble=False,
)
return calibrated_pipeline
def define_models(n_jobs: int) -> dict[str, tuple[BaseEstimator, dict[str, list[Any]]]]:
"""Define the models to train.
Parameters
----------
n_jobs : int
Number of jobs to use for training.
Returns
-------
dict[str, tuple[Pipeline, dict[str, list[Any]]]]
Dictionary of model names and tuples of the model pipeline and the
hyperparameter grid.
"""
knn_model = KNeighborsClassifier(n_neighbors=9, n_jobs=n_jobs)
knn_hyperparams: dict[str, Any] = {}
svc_model = SVC(probability=True)
svc_hyperparams = {
"C": np.power(5.0, np.arange(-4, 4)),
}
rf_model = RandomForestClassifier(
n_estimators=1024,
n_jobs=n_jobs,
)
rf_hyperparams = {
"max_depth": [4, 16, None],
}
cal_rf_model = CalibratedClassifierCV(
estimator=RandomForestClassifier(
n_estimators=1024,
n_jobs=n_jobs,
),
method="sigmoid",
cv=5,
n_jobs=1,
ensemble=False,
)
cal_rf_hyperparams = {
"estimator__max_depth": [4, 16, None],
}
model_dict = {
"KNN": (knn_model, knn_hyperparams),
"SVC": (svc_model, svc_hyperparams),
"RF": (rf_model, rf_hyperparams),
"Calibrated RF": (cal_rf_model, cal_rf_hyperparams),
}
return model_dict
def compile_pipeline(
model: BaseEstimator, neural_encoder: ChempropNeuralFP
) -> Pipeline:
"""Compile the model and the neural encoder into a pipeline.
Parameters
----------
model : BaseEstimator
The model to use.
neural_encoder : ChempropNeuralFP
The neural encoder to use.
Returns
-------
Pipeline
The compiled pipeline.
"""
error_filter = ErrorFilter(filter_everything=True)
error_replacer = FilterReinserter.from_error_filter(error_filter, np.nan)
return Pipeline(
[
("smi2mol", SmilesToMol()),
("error_filter", error_filter),
("mol2graph", MolToChemprop()),
("neural_encoder", neural_encoder),
("model", model),
("error_replacer", PostPredictionWrapper(error_replacer)),
],
n_jobs=1,
memory=joblib.Memory(),
)
def main() -> None: # pylint: disable=too-many-locals
"""Run ML experiments on the Tox21 dataset."""
disable_possible_user_warnings()
logging.getLogger("lightning.pytorch.utilities.rank_zero").setLevel(logging.ERROR)
logging.getLogger("lightning.pytorch.accelerators.cuda").setLevel(logging.ERROR)
args = parse_args()
data_path = Path(__file__).parents[1] / "data"
endpoint_df = pd.read_csv(
data_path
/ "intermediate_data"
/ "presplit_data"
/ f"presplit_data_{args.endpoint}.tsv",
sep="\t",
)
split_strategy_list = [
"Random",
"Agglomerative clustering",
]
model_dict = define_models(args.n_jobs)
splitter = LeavePGroupsOut(1)
prediction_df_list = []
for split_strategy in tqdm(split_strategy_list, desc="Split strategy"):
iter_splits = splitter.split(
endpoint_df["smiles"].tolist(),
groups=endpoint_df[split_strategy].tolist(),
)
n_splits = splitter.get_n_splits(
endpoint_df["smiles"].tolist(),
groups=endpoint_df[split_strategy].tolist(),
)
for trial, (train_idx, test_idx) in tqdm(
enumerate(iter_splits), desc="Split", leave=False, total=n_splits
):
chemprop_model = define_chemprop_pipeline(
args.n_jobs, calibration_method=args.nn_calibration
)
chemprop_model.fit(
endpoint_df.iloc[train_idx]["smiles"].tolist(),
endpoint_df.label.to_numpy()[train_idx],
)
test_df = endpoint_df.iloc[test_idx].copy()
test_df["proba"] = chemprop_model.predict_proba(test_df.smiles.tolist())[
:, 1
]
test_df["prediction"] = chemprop_model.predict(test_df.smiles.tolist())
test_df["endpoint"] = args.endpoint
test_df["trial"] = trial
test_df["Split strategy"] = split_strategy
test_df["model"] = "Calibrated Chemprop"
prediction_df_list.append(test_df)
uncalibrated_chemprop = chemprop_model.calibrated_classifiers_[0].estimator
test_df = endpoint_df.iloc[test_idx].copy()
test_df["proba"] = uncalibrated_chemprop.predict_proba(
test_df.smiles.tolist()
)[:, 1]
test_df["prediction"] = uncalibrated_chemprop.predict(
test_df.smiles.tolist()
)
test_df["endpoint"] = args.endpoint
test_df["trial"] = trial
test_df["Split strategy"] = split_strategy
test_df["model"] = "Chemprop"
prediction_df_list.append(test_df)
chemprop_encoder = uncalibrated_chemprop[ # pylint: disable=no-member
"chemprop"
].to_encoder()
for model_name, (model, hyperparams) in tqdm(
model_dict.items(), desc="Model", leave=False
):
model = compile_pipeline(model, chemprop_encoder)
hparams = {"model__" + k: v for k, v in hyperparams.items()}
model = GridSearchCV(
model,
hparams,
cv=LeaveOneGroupOut(),
scoring="balanced_accuracy",
n_jobs=1,
)
train_df = endpoint_df.iloc[train_idx]
test_df = endpoint_df.iloc[test_idx].copy()
model.fit(
train_df.smiles.tolist(),
train_df.label.tolist(),
groups=train_df[split_strategy].tolist(),
)
test_df["proba"] = model.predict_proba(test_df.smiles.tolist())[:, 1]
test_df["prediction"] = model.predict(test_df.smiles.tolist())
test_df["endpoint"] = args.endpoint
test_df["trial"] = trial
test_df["Split strategy"] = split_strategy
test_df["model"] = model_name
prediction_df_list.append(test_df)
prediction_df = pd.concat(prediction_df_list)
save_path = (
data_path
/ "intermediate_data"
/ "model_predictions"
/ f"neural_fingerprint_predictions_{args.nn_calibration}_{args.endpoint}.tsv.gz"
)
prediction_df.to_csv(save_path, sep="\t", index=False)
if __name__ == "__main__":
main()