Skip to content

Commit ad559ba

Browse files
Kacper-KozubowskiKacper Kozubowski
andauthored
Add suppress_warnings support to MolToX transformers (#468)
Co-authored-by: Kacper Kozubowski <agapanthus133@gmail.com>
1 parent 43bc3ac commit ad559ba

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

skfp/preprocessing/input_output/aminoseq.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class MolFromAminoseqTransformer(BasePreprocessor):
7979
"sanitize": ["boolean"],
8080
"flavor": [Options(int, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})],
8181
"valid_only": ["boolean"],
82+
"suppress_warnings": ["boolean"],
8283
}
8384

8485
def __init__(

skfp/preprocessing/input_output/inchi.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class MolFromInchiTransformer(BasePreprocessor):
7474
"sanitize": ["boolean"],
7575
"remove_hydrogens": ["boolean"],
7676
"valid_only": ["boolean"],
77+
"suppress_warnings": ["boolean"],
7778
}
7879

7980
def __init__(
@@ -181,6 +182,9 @@ class MolToInchiTransformer(BasePreprocessor):
181182
Number of inputs processed in each batch. ``None`` divides input data into
182183
equal-sized parts, as many as ``n_jobs``.
183184
185+
suppress_warnings: bool, default=False
186+
Whether to suppress warnings and errors on saving molecules.
187+
184188
verbose : int or dict, default=0
185189
Controls the verbosity when processing molecules.
186190
If a dictionary is passed, it is treated as kwargs for ``tqdm()``,
@@ -205,18 +209,26 @@ class MolToInchiTransformer(BasePreprocessor):
205209
['InChI=1S/H2O/h1H2', 'InChI=1S/C8H10N4O2/c1-10-4-9-6-5(10)7(13)12(3)8(14)11(6)2/h4H,1-3H3']
206210
"""
207211

212+
_parameter_constraints: dict = {
213+
**BasePreprocessor._parameter_constraints,
214+
"suppress_warnings": ["boolean"],
215+
}
216+
208217
def __init__(
209218
self,
210219
n_jobs: int | None = None,
211220
batch_size: int | None = None,
221+
suppress_warnings: bool = False,
212222
verbose: int | dict = 0,
213223
):
214224
super().__init__(
215225
n_jobs=n_jobs,
216226
batch_size=batch_size,
227+
suppress_warnings=suppress_warnings,
217228
verbose=verbose,
218229
)
219230

220231
def _transform_batch(self, X: Sequence[Mol]) -> list[str]:
221-
require_mols(X)
222-
return [MolToInchi(mol) for mol in X]
232+
with no_rdkit_logs() if self.suppress_warnings else nullcontext():
233+
require_mols(X)
234+
return [MolToInchi(mol) for mol in X]

skfp/preprocessing/input_output/smiles.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class MolFromSmilesTransformer(BasePreprocessor):
7777
"sanitize": ["boolean"],
7878
"replacements": [dict, None],
7979
"valid_only": ["boolean"],
80+
"suppress_warnings": ["boolean"],
8081
}
8182

8283
def __init__(
@@ -202,6 +203,9 @@ class MolToSmilesTransformer(BasePreprocessor):
202203
Number of inputs processed in each batch. ``None`` divides input data into
203204
equal-sized parts, as many as ``n_jobs``.
204205
206+
suppress_warnings: bool, default=False
207+
Whether to suppress warnings and errors on saving molecules.
208+
205209
verbose : int or dict, default=0
206210
Controls the verbosity when processing molecules.
207211
If a dictionary is passed, it is treated as kwargs for ``tqdm()``,
@@ -234,6 +238,7 @@ class MolToSmilesTransformer(BasePreprocessor):
234238
"all_bonds_explicit": ["boolean"],
235239
"all_hs_explicit": ["boolean"],
236240
"do_random": ["boolean"],
241+
"suppress_warnings": ["boolean"],
237242
}
238243

239244
def __init__(
@@ -246,11 +251,13 @@ def __init__(
246251
do_random: bool = False,
247252
n_jobs: int | None = None,
248253
batch_size: int | None = None,
254+
suppress_warnings: bool = False,
249255
verbose: int | dict = 0,
250256
):
251257
super().__init__(
252258
n_jobs=n_jobs,
253259
batch_size=batch_size,
260+
suppress_warnings=suppress_warnings,
254261
verbose=verbose,
255262
)
256263
self.isomeric_smiles = isomeric_smiles
@@ -261,16 +268,17 @@ def __init__(
261268
self.do_random = do_random
262269

263270
def _transform_batch(self, X: Sequence[Mol]) -> list[str]:
264-
require_mols(X)
265-
return [
266-
MolToSmiles(
267-
mol,
268-
isomericSmiles=self.isomeric_smiles,
269-
kekuleSmiles=self.kekule_smiles,
270-
canonical=self.canonical,
271-
allBondsExplicit=self.all_bonds_explicit,
272-
allHsExplicit=self.all_hs_explicit,
273-
doRandom=self.do_random,
274-
)
275-
for mol in X
276-
]
271+
with no_rdkit_logs() if self.suppress_warnings else nullcontext():
272+
require_mols(X)
273+
return [
274+
MolToSmiles(
275+
mol,
276+
isomericSmiles=self.isomeric_smiles,
277+
kekuleSmiles=self.kekule_smiles,
278+
canonical=self.canonical,
279+
allBondsExplicit=self.all_bonds_explicit,
280+
allHsExplicit=self.all_hs_explicit,
281+
doRandom=self.do_random,
282+
)
283+
for mol in X
284+
]

0 commit comments

Comments
 (0)