-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_pipeline.py
415 lines (367 loc) · 15 KB
/
test_pipeline.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""Test functionality of the pipeline class."""
from __future__ import annotations
import tempfile
import unittest
from itertools import combinations
from pathlib import Path
from typing import Any
import numpy as np
import pandas as pd
from joblib import Memory
from sklearn.base import BaseEstimator
from sklearn.calibration import CalibratedClassifierCV
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
from sklearn.model_selection import GridSearchCV
from sklearn.tree import DecisionTreeClassifier
from molpipeline import ErrorFilter, FilterReinserter, Pipeline, PostPredictionWrapper
from molpipeline.any2mol import AutoToMol, SmilesToMol
from molpipeline.mol2any import MolToMorganFP, MolToRDKitPhysChem, MolToSmiles
from molpipeline.mol2mol import (
ChargeParentExtractor,
EmptyMoleculeFilter,
MetalDisconnector,
SaltRemover,
)
from molpipeline.utils.json_operations import recursive_from_json, recursive_to_json
from molpipeline.utils.matrices import are_equal
from tests import TEST_DATA_DIR
from tests.utils.execution_count import get_exec_counted_rf_regressor
from tests.utils.fingerprints import make_sparse_fp
TEST_SMILES = ["CC", "CCO", "COC", "CCCCC", "CCC(-O)O", "CCCN"]
FAULTY_TEST_SMILES = ["CCCXAS", "", "O=C(O)C(F)(F)F"]
CONTAINS_OX = [0, 1, 1, 0, 1, 0]
FP_RADIUS = 2
FP_SIZE = 2048
EXPECTED_OUTPUT = make_sparse_fp(TEST_SMILES, FP_RADIUS, FP_SIZE)
_RANDOM_STATE = 67056
class PipelineTest(unittest.TestCase):
"""Unit test for the functionality of the pipeline class."""
def test_fit_transform_single_core(self) -> None:
"""Test if the generation of the fingerprint matrix works as expected.
Returns
-------
None
"""
# Create pipeline
smi2mol = SmilesToMol()
mol2morgan = MolToMorganFP(radius=FP_RADIUS, n_bits=FP_SIZE)
pipeline = Pipeline(
[
("smi2mol", smi2mol),
("morgan", mol2morgan),
]
)
# Run pipeline
matrix = pipeline.fit_transform(TEST_SMILES)
# Compare with expected output
self.assertTrue(are_equal(EXPECTED_OUTPUT, matrix))
def test_sklearn_pipeline(self) -> None:
"""Test if the pipeline can be used in a sklearn pipeline.
Returns
-------
None
"""
smi2mol = SmilesToMol()
mol2morgan = MolToMorganFP(radius=FP_RADIUS, n_bits=FP_SIZE)
d_tree = DecisionTreeClassifier()
s_pipeline = Pipeline(
[
("smi2mol", smi2mol),
("morgan", mol2morgan),
("decision_tree", d_tree),
]
)
s_pipeline.fit(TEST_SMILES, CONTAINS_OX)
predicted_value_array = s_pipeline.predict(TEST_SMILES)
for pred_val, true_val in zip(predicted_value_array, CONTAINS_OX):
self.assertEqual(pred_val, true_val)
def test_sklearn_pipeline_parallel(self) -> None:
"""Test if the pipeline can be used in a sklearn pipeline.
Returns
-------
None
"""
smi2mol = SmilesToMol()
mol2morgan = MolToMorganFP(radius=FP_RADIUS, n_bits=FP_SIZE)
d_tree = DecisionTreeClassifier()
s_pipeline = Pipeline(
[
("smi2mol", smi2mol),
("morgan", mol2morgan),
("decision_tree", d_tree),
],
n_jobs=2,
)
s_pipeline.fit(TEST_SMILES, CONTAINS_OX)
out = s_pipeline.predict(TEST_SMILES)
self.assertEqual(len(out), len(CONTAINS_OX))
for pred_val, true_val in zip(out, CONTAINS_OX):
self.assertEqual(pred_val, true_val)
def test_salt_removal(self) -> None:
"""Test if salts are correctly removed from molecules.
Returns
-------
None
"""
smiles_with_salt_list = ["CCO-[Na]", "CCC(=O)[O-].[Li+]", "CCC(=O)-O-[K]"]
smiles_without_salt_list = ["CCO", "CCC(=O)O", "CCC(=O)O"]
smi2mol = SmilesToMol()
disconnect_metal = MetalDisconnector()
salt_remover = SaltRemover()
empty_mol_filter = EmptyMoleculeFilter()
remove_charge = ChargeParentExtractor()
mol2smi = MolToSmiles()
salt_remover_pipeline = Pipeline(
[
("smi2mol", smi2mol),
("disconnect_metal", disconnect_metal),
("salt_remover", salt_remover),
("empty_mol_filter", empty_mol_filter),
("remove_charge", remove_charge),
("mol2smi", mol2smi),
]
)
generated_smiles = salt_remover_pipeline.transform(smiles_with_salt_list)
for generated_smiles, smiles_without_salt in zip(
generated_smiles, smiles_without_salt_list
):
self.assertEqual(generated_smiles, smiles_without_salt)
def test_json_generation(self) -> None:
"""Test that the json representation of a pipeline can be loaded back into a pipeline.
Returns
-------
None
"""
# Create pipeline
smi2mol = SmilesToMol()
metal_disconnector = MetalDisconnector()
salt_remover = SaltRemover()
physchem = MolToRDKitPhysChem()
pipeline_element_list = [
smi2mol,
metal_disconnector,
salt_remover,
physchem,
]
m_pipeline = Pipeline(
[
("smi2mol", smi2mol),
("metal_disconnector", metal_disconnector),
("salt_remover", salt_remover),
("physchem", physchem),
]
)
# Convert pipeline to json
json_str = recursive_to_json(m_pipeline)
# Recreate pipeline from json
loaded_pipeline: Pipeline = recursive_from_json(json_str)
self.assertTrue(isinstance(loaded_pipeline, Pipeline))
# Compare pipeline elements
for loaded_element, original_element in zip(
loaded_pipeline.steps, pipeline_element_list
):
if loaded_element[1] == "passthrough":
self.assertEqual(loaded_element[1], original_element)
continue
loaded_params = loaded_element[1].get_params()
original_params = original_element.get_params()
for key, value in loaded_params.items():
if isinstance(value, BaseEstimator):
self.assertEqual(type(value), type(original_params[key]))
else:
self.assertEqual(loaded_params[key], original_params[key])
def test_fit_transform_record_remove_nones(self) -> None:
"""Test if the generation of the fingerprint matrix works as expected.
Returns
-------
None
"""
smi2mol = SmilesToMol()
salt_remover = SaltRemover()
mol2morgan = MolToMorganFP(radius=FP_RADIUS, n_bits=FP_SIZE)
empty_mol_filter = EmptyMoleculeFilter()
remove_none = ErrorFilter.from_element_list(
[smi2mol, salt_remover, mol2morgan, empty_mol_filter]
)
# Create pipeline
pipeline = Pipeline(
[
("smi2mol", smi2mol),
("salt_remover", salt_remover),
("empty_mol_filter", empty_mol_filter),
("morgan", mol2morgan),
("remove_none", remove_none),
],
)
# Run pipeline
matrix = pipeline.fit_transform(TEST_SMILES + FAULTY_TEST_SMILES)
# Compare with expected output (Which is the same as the output without the faulty smiles)
self.assertTrue(are_equal(EXPECTED_OUTPUT, matrix))
def test_gridsearchcv(self) -> None:
"""Test if the MolPipeline can be used in sklearn's GridSearchCV."""
descriptor_elements_to_test: list[dict[str, Any]] = [
{
"name": "morgan",
"element": MolToMorganFP(),
"param_grid": {"morgan__n_bits": [64, 128], "morgan__radius": [1, 2]},
},
{
"name": "physchem",
"element": MolToRDKitPhysChem(),
"param_grid": {
"physchem__descriptor_list": [
["HeavyAtomMolWt"],
["HeavyAtomMolWt", "HeavyAtomCount"],
]
},
},
]
for test_data_dict in descriptor_elements_to_test:
name = test_data_dict["name"]
element = test_data_dict["element"]
param_grid = test_data_dict["param_grid"]
# set up a pipeline that trains a random forest classifier on morgan fingerprints
pipeline = Pipeline(
[
("auto2mol", AutoToMol()),
(name, element),
("estimator", RandomForestClassifier()),
],
n_jobs=1,
)
# define the hyperparameter space to try out
grid_space = {
"estimator__n_estimators": [1, 5],
"estimator__random_state": [_RANDOM_STATE],
}
grid_space.update(param_grid)
grid_search_cv = GridSearchCV(
estimator=pipeline,
param_grid=grid_space,
cv=2,
scoring="roc_auc",
n_jobs=1,
)
grid_search_cv.fit(
X=TEST_SMILES,
y=CONTAINS_OX,
)
for k, value in param_grid.items():
self.assertIn(grid_search_cv.best_params_[k], value)
def test_caching(self) -> None:
"""Test if the caching gives the same results and is faster on the second run."""
molecule_net_logd_df = pd.read_csv(
TEST_DATA_DIR / "molecule_net_logd.tsv.gz", sep="\t", nrows=20
)
prediction_list = []
for cache_activated in [False, True]:
pipeline = get_exec_counted_rf_regressor(_RANDOM_STATE)
with tempfile.TemporaryDirectory() as temp_dir:
if cache_activated:
cache_dir = Path(temp_dir) / ".cache"
mem = Memory(location=cache_dir, verbose=0)
else:
mem = Memory(location=None, verbose=0)
pipeline.memory = mem
# Run fitting 1
pipeline.fit(
molecule_net_logd_df["smiles"].tolist(),
molecule_net_logd_df["exp"].tolist(),
)
# Get predictions
prediction = pipeline.predict(molecule_net_logd_df["smiles"].tolist())
prediction_list.append(prediction)
# Reset the last step with an untrained model
pipeline.steps[-1] = (
"rf",
RandomForestRegressor(random_state=_RANDOM_STATE, n_jobs=1),
)
# Run fitting 2
pipeline.fit(
molecule_net_logd_df["smiles"].tolist(),
molecule_net_logd_df["exp"].tolist(),
)
# Get predictions
prediction = pipeline.predict(molecule_net_logd_df["smiles"].tolist())
prediction_list.append(prediction)
n_transformations = pipeline.named_steps["mol2concat"].n_transformations
if cache_activated:
# Fit is called twice, but the transform is only called once, since the second run is cached
self.assertEqual(n_transformations, 1)
else:
self.assertEqual(n_transformations, 2)
mem.clear(warn=False)
for pred1, pred2 in combinations(prediction_list, 2):
self.assertTrue(np.allclose(pred1, pred2))
def test_gridsearch_cache(self) -> None:
"""Run a short GridSearchCV and check if the caching and not caching gives the same results."""
h_params = {
"rf__n_estimators": [1, 2],
}
# First without caching
data_df = pd.read_csv(
TEST_DATA_DIR / "molecule_net_logd.tsv.gz", sep="\t", nrows=20
)
best_param_dict = {}
prediction_dict = {}
for cache_activated in [True, False]:
pipeline = get_exec_counted_rf_regressor(_RANDOM_STATE)
with tempfile.TemporaryDirectory() as temp_dir:
cache_dir = Path(temp_dir) / ".cache"
if cache_activated:
mem = Memory(location=cache_dir, verbose=0)
else:
mem = Memory(location=None, verbose=0)
pipeline.memory = mem
grid_search_cv = GridSearchCV(
estimator=pipeline,
param_grid=h_params,
cv=2,
scoring="neg_mean_squared_error",
n_jobs=1,
error_score="raise",
refit=True,
pre_dispatch=1,
)
grid_search_cv.fit(data_df["smiles"].tolist(), data_df["exp"].tolist())
best_param_dict[cache_activated] = grid_search_cv.best_params_
prediction_dict[cache_activated] = grid_search_cv.predict(
data_df["smiles"].tolist()
)
mem.clear(warn=False)
self.assertEqual(best_param_dict[True], best_param_dict[False])
self.assertTrue(np.allclose(prediction_dict[True], prediction_dict[False]))
class PipelineCompatibilityTest(unittest.TestCase):
"""Test if the pipeline is compatible with other sklearn functionalities."""
def test_calibrated_classifier(self) -> None:
"""Test if the pipeline can be used with a CalibratedClassifierCV."""
smi2mol = SmilesToMol()
mol2morgan = MolToMorganFP(radius=FP_RADIUS, n_bits=FP_SIZE)
d_tree = DecisionTreeClassifier()
error_filter = ErrorFilter(filter_everything=True)
s_pipeline = Pipeline(
[
("smi2mol", smi2mol),
("morgan", mol2morgan),
("error_filter", error_filter),
("decision_tree", d_tree),
(
"error_replacer",
PostPredictionWrapper(
FilterReinserter.from_error_filter(error_filter, np.nan)
),
),
]
)
calibrated_pipeline = CalibratedClassifierCV(
s_pipeline, cv=2, ensemble=True, method="isotonic"
)
calibrated_pipeline.fit(TEST_SMILES, CONTAINS_OX)
predicted_value_array = calibrated_pipeline.predict(TEST_SMILES)
predicted_proba_array = calibrated_pipeline.predict_proba(TEST_SMILES)
self.assertIsInstance(predicted_value_array, np.ndarray)
self.assertIsInstance(predicted_proba_array, np.ndarray)
self.assertEqual(predicted_value_array.shape, (len(TEST_SMILES),))
self.assertEqual(predicted_proba_array.shape, (len(TEST_SMILES),))
if __name__ == "__main__":
unittest.main()