Skip to content

Commit

Permalink
fix errors and add no_rdkit_log decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
maclandrol committed Jun 9, 2024
1 parent 176fce9 commit 96bb89c
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 35 deletions.
4 changes: 3 additions & 1 deletion datamol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"parallelized_with_batches": "datamol.utils",
"JobRunner": "datamol.utils",
"fs": "datamol.utils",
# log
# data
"freesolv": "datamol.data",
"cdk2": "datamol.data",
"solubility": "datamol.data",
Expand All @@ -39,6 +39,7 @@
"enable_rdkit_log": "datamol.log",
"disable_rdkit_log": "datamol.log",
"without_rdkit_log": "datamol.log",
"no_rdkit_log": "datamol.log",
# mol
"PERIODIC_TABLE": "datamol.mol",
"TRIPLE_BOND": "datamol.mol",
Expand Down Expand Up @@ -233,6 +234,7 @@ def __dir__():
from .log import enable_rdkit_log
from .log import disable_rdkit_log
from .log import without_rdkit_log
from .log import no_rdkit_log

from .mol import PERIODIC_TABLE
from .mol import TRIPLE_BOND
Expand Down
2 changes: 2 additions & 0 deletions datamol/descriptors/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

from .. import Mol
from ..convert import from_smarts
from ..log import no_rdkit_log
from .._version import is_lower_than_current_rdkit_version


@no_rdkit_log
def _sasscorer(mol: Mol):
sys.path.append(os.path.join(RDConfig.RDContribDir, "SA_Score"))
try:
Expand Down
8 changes: 4 additions & 4 deletions datamol/fp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"topological": {
"includeChirality": False,
"torsionAtomCount": 4,
"countSimulation": False,
"countSimulation": True,
"countBounds": None,
"fpSize": 2048,
"atomInvariantsGenerator": None,
Expand All @@ -86,7 +86,7 @@
"maxDistance": 30,
"includeChirality": False,
"use2D": True,
"countSimulation": False,
"countSimulation": True,
"countBounds": None,
"fpSize": 2048,
"atomInvariantsGenerator": None,
Expand Down Expand Up @@ -159,7 +159,7 @@
"topological-count": {
"includeChirality": False,
"torsionAtomCount": 4,
"countSimulation": False,
"countSimulation": True,
"countBounds": None,
"fpSize": 2048,
"atomInvariantsGenerator": None,
Expand All @@ -169,7 +169,7 @@
"maxDistance": 30,
"includeChirality": False,
"use2D": True,
"countSimulation": False,
"countSimulation": True,
"countBounds": None,
"fpSize": 2048,
"atomInvariantsGenerator": None,
Expand Down
51 changes: 51 additions & 0 deletions datamol/log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rdkit import RDLogger
from rdkit import rdBase
from functools import wraps


class without_rdkit_log:
Expand Down Expand Up @@ -71,3 +72,53 @@ def enable_rdkit_log():
"""Enable all rdkit logs."""
for log_level in RDLogger._levels:
rdBase.EnableLog(log_level)


def no_rdkit_log(
func=None,
*,
mute_errors: bool = True,
mute_warning: bool = True,
mute_info: bool = True,
mute_debug: bool = True,
enable: bool = True,
):
"""Decorator to disable RDKit logs.
This decorator can be used to suppress RDKit logs when executing a specific function.
By default, all log levels (error, warning, info, and debug) are muted.
Args:
mute_errors : Whether to mute error logs (default is True).
mute_warning : Whether to mute warning logs (default is True).
mute_info : Whether to mute info logs (default is True).
mute_debug : Whether to mute debug logs (default is True).
enable: Whether to enable the log muting (default is True). If set to False, no logs will be muted.
Example:
```python
@no_rdkit_log()
def example_function():
# Your function code here
pass
example_function() # RDKit logs won't show during this function's execution
```
"""

if func is None:
return lambda f: no_rdkit_log(
f,
mute_errors=mute_errors,
mute_warning=mute_warning,
mute_info=mute_info,
mute_debug=mute_debug,
enable=enable,
)

@wraps(func)
def wrapper(*args, **kwargs):
with without_rdkit_log(mute_errors, mute_warning, mute_info, mute_debug, enable):
return func(*args, **kwargs)

return wrapper
432 changes: 405 additions & 27 deletions docs/tutorials/Descriptors.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies:
- scikit-learn

# Chemistry
- rdkit >=2021.03
- rdkit >=2024.03.3
- selfies

# Optional deps
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies = [
"packaging",
"typing-extensions",
"importlib-resources",
"rdkit",
"rdkit>=2024.03.3",
]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_all_fps():
"erg": {"size": 315, "bits_sum": 23.4},
"estate": {"size": 79, "bits_sum": 13},
"avalon-count": {"size": 512, "bits_sum": 168},
"ecfp-count": {"size": 2048, "bits_sum": 35},
"ecfp-count": {"size": 2048, "bits_sum": 42},
"fcfp-count": {"size": 2048, "bits_sum": 35},
"topological-count": {"size": 2048, "bits_sum": 19},
"atompair-count": {"size": 2048, "bits_sum": 78},
Expand Down
15 changes: 15 additions & 0 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import datamol as dm


@dm.no_rdkit_log
def no_log_to_mol(smiles):
return dm.to_mol(smiles)


def check_logs_are_shown(capfd):
smiles = "fake_smiles"
dm.to_mol(smiles)
Expand All @@ -17,10 +22,20 @@ def check_logs_are_not_shown(capfd):
assert err == ""


def check_logs_are_not_shown_deco(capfd):
smiles = "fake_smiles"
no_log_to_mol(smiles)
_, err = capfd.readouterr()
assert err == ""


@pytest.mark.skip_platform("win")
def test_rdkit_log(capfd):
"""Test multiple rdkit log scenarios."""

check_logs_are_shown(capfd)
check_logs_are_not_shown_deco(capfd)

check_logs_are_shown(capfd)
with dm.without_rdkit_log():
check_logs_are_not_shown(capfd)
Expand Down

0 comments on commit 96bb89c

Please sign in to comment.