From bd5b0ab285a970c56730a59c8a5de32198cbb691 Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Wed, 13 Dec 2023 11:38:16 -0800 Subject: [PATCH 1/7] pass max_mols to Draw.MolsToGridImage --- datamol/viz/_viz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/datamol/viz/_viz.py b/datamol/viz/_viz.py index 91a88216..b340d1f5 100644 --- a/datamol/viz/_viz.py +++ b/datamol/viz/_viz.py @@ -129,6 +129,7 @@ def to_image( highlightAtomLists=_highlight_atom, highlightBondLists=_highlight_bond, drawOptions=draw_options, + maxMols=max_mols, **_kwargs, ) From 32410cfc37987d7c62e7b30d5eb795ae68683d66 Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Wed, 13 Dec 2023 12:43:51 -0800 Subject: [PATCH 2/7] only pass max_mols to Draw.MolsToGridImage when in an IPython display --- datamol/viz/_viz.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/datamol/viz/_viz.py b/datamol/viz/_viz.py index b340d1f5..1b6f2eb5 100644 --- a/datamol/viz/_viz.py +++ b/datamol/viz/_viz.py @@ -3,6 +3,7 @@ from typing import Tuple from typing import Optional from typing import Any +from IPython.core.getipython import get_ipython from rdkit.Chem import Draw @@ -120,18 +121,27 @@ def to_image( else: _kwargs[k] = v - image = Draw.MolsToGridImage( - mols, - legends=legends, - molsPerRow=n_cols, - useSVG=use_svg, - subImgSize=mol_size, - highlightAtomLists=_highlight_atom, - highlightBondLists=_highlight_bond, - drawOptions=draw_options, - maxMols=max_mols, + # Check if we are in a Jupyter notebook or IPython display context + in_notebook = get_ipython() is not None + + # Create a dictionary of arguments for the MolsToGridImage function + draw_args = { + "mols": mols, + "legends": legends, + "molsPerRow": n_cols, + "useSVG": use_svg, + "subImgSize": mol_size, + "highlightAtomLists": _highlight_atom, + "highlightBondLists": _highlight_bond, + "drawOptions": draw_options, **_kwargs, - ) + } + + # Conditionally add the maxMols argument if in a notebook + if in_notebook: + draw_args["maxMols"] = max_mols + + image = Draw.MolsToGridImage(**draw_args) if outfile is not None: image_to_file(image, outfile, as_svg=use_svg) From b9caf6edce40e61eb68b2f878df177d8e32fd13d Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Wed, 13 Dec 2023 14:17:08 -0800 Subject: [PATCH 3/7] cleanup --- datamol/viz/_viz.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/datamol/viz/_viz.py b/datamol/viz/_viz.py index 1b6f2eb5..c934c3fd 100644 --- a/datamol/viz/_viz.py +++ b/datamol/viz/_viz.py @@ -122,26 +122,23 @@ def to_image( _kwargs[k] = v # Check if we are in a Jupyter notebook or IPython display context + # If so, conditionally add the maxMols argument in_notebook = get_ipython() is not None - # Create a dictionary of arguments for the MolsToGridImage function - draw_args = { - "mols": mols, - "legends": legends, - "molsPerRow": n_cols, - "useSVG": use_svg, - "subImgSize": mol_size, - "highlightAtomLists": _highlight_atom, - "highlightBondLists": _highlight_bond, - "drawOptions": draw_options, - **_kwargs, - } - - # Conditionally add the maxMols argument if in a notebook if in_notebook: - draw_args["maxMols"] = max_mols - - image = Draw.MolsToGridImage(**draw_args) + _kwargs["maxMols"] = max_mols + + image = Draw.MolsToGridImage( + mols, + legends=legends, + molsPerRow=n_cols, + useSVG=use_svg, + subImgSize=mol_size, + highlightAtomLists=_highlight_atom, + highlightBondLists=_highlight_bond, + drawOptions=draw_options, + **_kwargs, + ) if outfile is not None: image_to_file(image, outfile, as_svg=use_svg) From 4494a5a71c73f4a65ecd667f01c6fc27b8fb9479 Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Thu, 1 Feb 2024 22:34:54 -0800 Subject: [PATCH 4/7] addressed comments --- datamol/viz/_viz.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/datamol/viz/_viz.py b/datamol/viz/_viz.py index c934c3fd..73c89385 100644 --- a/datamol/viz/_viz.py +++ b/datamol/viz/_viz.py @@ -3,7 +3,7 @@ from typing import Tuple from typing import Optional from typing import Any -from IPython.core.getipython import get_ipython +from loguru import logger from rdkit.Chem import Draw @@ -23,6 +23,7 @@ def to_image( highlight_bond: Optional[List[List[int]]] = None, outfile: Optional[str] = None, max_mols: int = 32, + max_mols_ipython: int = 50, copy: bool = True, indices: bool = False, bond_indices: bool = False, @@ -45,6 +46,7 @@ def to_image( highlight_bond: The bonds to highlight. outfile: Path where to save the image (local or remote path). max_mols: The maximum number of molecules to display. + max_mols_ipython: The maximum number of molecules to display when running within an IPython environment. copy: Whether to copy the molecules or not. indices: Whether to draw the atom indices. bond_indices: Whether to draw the bond indices. @@ -123,10 +125,13 @@ def to_image( # Check if we are in a Jupyter notebook or IPython display context # If so, conditionally add the maxMols argument - in_notebook = get_ipython() is not None + in_notebook = dm.viz.utils.is_ipython_session() if in_notebook: - _kwargs["maxMols"] = max_mols + _kwargs["maxMols"] = max_mols_ipython + if max_mols > max_mols_ipython: + logger.warning(f"You have set max_mols to {max_mols}, which is higher than max_mols_ipython ({max_mols_ipython}). " + "Consider increasing max_mols_ipython if you want to display all molecules in an IPython environment.") image = Draw.MolsToGridImage( mols, From ea8ebde0c17453476b50d71a2350f6454c2fe2b1 Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Thu, 1 Feb 2024 22:36:11 -0800 Subject: [PATCH 5/7] black fomatting --- datamol/viz/_viz.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/datamol/viz/_viz.py b/datamol/viz/_viz.py index 73c89385..834a770e 100644 --- a/datamol/viz/_viz.py +++ b/datamol/viz/_viz.py @@ -130,8 +130,10 @@ def to_image( if in_notebook: _kwargs["maxMols"] = max_mols_ipython if max_mols > max_mols_ipython: - logger.warning(f"You have set max_mols to {max_mols}, which is higher than max_mols_ipython ({max_mols_ipython}). " - "Consider increasing max_mols_ipython if you want to display all molecules in an IPython environment.") + logger.warning( + f"You have set max_mols to {max_mols}, which is higher than max_mols_ipython ({max_mols_ipython}). " + "Consider increasing max_mols_ipython if you want to display all molecules in an IPython environment." + ) image = Draw.MolsToGridImage( mols, From c51690e0f3f7a90c5b003eedc1c2293a98ad7aab Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Thu, 1 Feb 2024 23:19:47 -0800 Subject: [PATCH 6/7] black fomatting v2 --- datamol/data/__init__.py | 41 +++++++++++++++------------------------- datamol/io.py | 9 +++------ 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/datamol/data/__init__.py b/datamol/data/__init__.py index b2e40418..990ab55a 100644 --- a/datamol/data/__init__.py +++ b/datamol/data/__init__.py @@ -66,18 +66,15 @@ def open_datamol_data_file( @overload -def freesolv(as_df: Literal[True] = True) -> pd.DataFrame: - ... +def freesolv(as_df: Literal[True] = True) -> pd.DataFrame: ... @overload -def freesolv(as_df: Literal[False] = False) -> List[Mol]: - ... +def freesolv(as_df: Literal[False] = False) -> List[Mol]: ... @overload -def freesolv(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: - ... +def freesolv(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: ... def freesolv(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: @@ -102,18 +99,17 @@ def freesolv(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: @overload -def cdk2(as_df: Literal[True] = True, mol_column: Optional[str] = "mol") -> pd.DataFrame: - ... +def cdk2(as_df: Literal[True] = True, mol_column: Optional[str] = "mol") -> pd.DataFrame: ... @overload -def cdk2(as_df: Literal[False] = False, mol_column: Optional[str] = "mol") -> List[Mol]: - ... +def cdk2(as_df: Literal[False] = False, mol_column: Optional[str] = "mol") -> List[Mol]: ... @overload -def cdk2(as_df: bool = True, mol_column: Optional[str] = "mol") -> Union[List[Mol], pd.DataFrame]: - ... +def cdk2( + as_df: bool = True, mol_column: Optional[str] = "mol" +) -> Union[List[Mol], pd.DataFrame]: ... def cdk2(as_df: bool = True, mol_column: Optional[str] = "mol"): @@ -130,20 +126,17 @@ def cdk2(as_df: bool = True, mol_column: Optional[str] = "mol"): @overload -def solubility(as_df: Literal[True] = True, mol_column: Optional[str] = "mol") -> pd.DataFrame: - ... +def solubility(as_df: Literal[True] = True, mol_column: Optional[str] = "mol") -> pd.DataFrame: ... @overload -def solubility(as_df: Literal[False] = False, mol_column: Optional[str] = "mol") -> List[Mol]: - ... +def solubility(as_df: Literal[False] = False, mol_column: Optional[str] = "mol") -> List[Mol]: ... @overload def solubility( as_df: bool = True, mol_column: Optional[str] = "mol" -) -> Union[List[Mol], pd.DataFrame]: - ... +) -> Union[List[Mol], pd.DataFrame]: ... def solubility(as_df: bool = True, mol_column: Optional[str] = "mol"): @@ -184,13 +177,11 @@ def solubility(as_df: bool = True, mol_column: Optional[str] = "mol"): @overload -def chembl_drugs(as_df: Literal[True] = True) -> pd.DataFrame: - ... +def chembl_drugs(as_df: Literal[True] = True) -> pd.DataFrame: ... @overload -def chembl_drugs(as_df: Literal[False] = False) -> List[Mol]: - ... +def chembl_drugs(as_df: Literal[False] = False) -> List[Mol]: ... def chembl_drugs(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: @@ -210,13 +201,11 @@ def chembl_drugs(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: @overload -def chembl_samples(as_df: Literal[True] = True) -> pd.DataFrame: - ... +def chembl_samples(as_df: Literal[True] = True) -> pd.DataFrame: ... @overload -def chembl_samples(as_df: Literal[False] = False) -> List[Mol]: - ... +def chembl_samples(as_df: Literal[False] = False) -> List[Mol]: ... def chembl_samples(as_df: bool = True) -> Union[List[Mol], pd.DataFrame]: diff --git a/datamol/io.py b/datamol/io.py index d5b1d0a8..5e27ae03 100644 --- a/datamol/io.py +++ b/datamol/io.py @@ -116,8 +116,7 @@ def read_sdf( max_num_mols: Optional[int] = ..., discard_invalid: bool = ..., n_jobs: Optional[int] = ..., -) -> List[Mol]: - ... +) -> List[Mol]: ... @overload @@ -134,8 +133,7 @@ def read_sdf( max_num_mols: Optional[int] = ..., discard_invalid: bool = ..., n_jobs: Optional[int] = ..., -) -> pd.DataFrame: - ... +) -> pd.DataFrame: ... @overload @@ -152,8 +150,7 @@ def read_sdf( max_num_mols: Optional[int] = ..., discard_invalid: bool = ..., n_jobs: Optional[int] = ..., -) -> Union[List[Mol], pd.DataFrame]: - ... +) -> Union[List[Mol], pd.DataFrame]: ... def read_sdf( From ec2d34d7c2e3bdf9e523760e68680d90502aff0c Mon Sep 17 00:00:00 2001 From: "Kyle M. Kovary" Date: Fri, 2 Feb 2024 10:29:18 -0800 Subject: [PATCH 7/7] bumped black version from 23 to 24 --- .github/workflows/code-check.yml | 2 +- binder/environment.yml | 2 +- env.yml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/code-check.yml b/.github/workflows/code-check.yml index 885e7ef4..0af8e81b 100644 --- a/.github/workflows/code-check.yml +++ b/.github/workflows/code-check.yml @@ -24,7 +24,7 @@ jobs: - name: Install black run: | - pip install black>=23 + pip install black>=24 - name: Lint run: black --check . diff --git a/binder/environment.yml b/binder/environment.yml index 3e874544..18efc11e 100644 --- a/binder/environment.yml +++ b/binder/environment.yml @@ -38,7 +38,7 @@ dependencies: - pytest >=6.0 - pytest-cov - pytest-xdist - - black >=23 + - black >=24 - jupyterlab - mypy - codecov diff --git a/env.yml b/env.yml index 8e9e28fd..4f935845 100644 --- a/env.yml +++ b/env.yml @@ -38,7 +38,7 @@ dependencies: - pytest >=6.0 - pytest-cov - pytest-xdist - - black >=23 + - black >=24 - ruff - jupyterlab - mypy