Skip to content

Commit

Permalink
[Misc Utils] New module
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatRSingh committed Nov 30, 2023
1 parent 442a3f1 commit baf3370
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
3 changes: 3 additions & 0 deletions deepchem/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

logger = logging.getLogger(__name__)

from deepchem.utils.misc_utils import indent
from deepchem.utils.misc_utils import shape2str

from deepchem.utils.batch_utils import batch_coulomb_matrix_features

from deepchem.utils.attribute_utils import set_attr
Expand Down
6 changes: 2 additions & 4 deletions deepchem/utils/differentiation_utils/linop.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
from abc import abstractmethod
from contextlib import contextmanager
from scipy.sparse.linalg import LinearOperator as spLinearOperator
from deepchem.utils.differentiation_utils import EditableModule
from deepchem.utils.differentiation_utils import get_bcasted_dims
from deepchem.utils.differentiation_utils import shape2str
from deepchem.utils.differentiation_utils import indent
from deepchem.utils.differentiation_utils import EditableModule, get_bcasted_dims
from deepchem.utils import shape2str, indent

__all__ = ["LinearOperator"]

Expand Down
40 changes: 0 additions & 40 deletions deepchem/utils/differentiation_utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,43 +131,3 @@ def assert_runtime(cond, msg=""):
"""
if not cond:
raise RuntimeError(msg)


# repr helper functions
def indent(s, nspace):
"""give indentation of the second line and next lines
Parameters
----------
s: str
The string to be indented.
nspace: int
The number of spaces to be indented.
Returns
-------
str
The indented string.
"""
spaces = " " * nspace
lines = [spaces + c if i > 0 else c for i, c in enumerate(s.split("\n"))]
return "\n".join(lines)


def shape2str(shape):
"""Convert the shape to string representation.
It also nicely formats the shape to be readable.
Parameters
----------
shape: Sequence[int]
The shape to be converted to string representation.
Returns
-------
str
The string representation of the shape.
"""
return "(%s)" % (", ".join([str(s) for s in shape]))
45 changes: 45 additions & 0 deletions deepchem/utils/misc_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Utilities for miscellaneous tasks.
"""


def indent(s, nspace):
"""Gives indentation of the second line and next lines.
It is used to format the string representation of an object.
Which might be containing multiples objects in it.
Usage: LinearOperator
Parameters
----------
s: str
The string to be indented.
nspace: int
The number of spaces to be indented.
Returns
-------
str
The indented string.
"""
spaces = " " * nspace
lines = [spaces + c if i > 0 else c for i, c in enumerate(s.split("\n"))]
return "\n".join(lines)


def shape2str(shape):
"""Convert the shape to string representation.
It also nicely formats the shape to be readable.
Parameters
----------
shape: Sequence[int]
The shape to be converted to string representation.
Returns
-------
str
The string representation of the shape.
"""
return "(%s)" % (", ".join([str(s) for s in shape]))
12 changes: 11 additions & 1 deletion docs/source/api_reference/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,14 @@ for additional information regarding equivariance and Deepchem's support for equ

.. autofunction:: deepchem.utils.equivariance_utils.change_basis_real_to_complex

.. autofunction:: deepchem.utils.equivariance_utils.wigner_D
.. autofunction:: deepchem.utils.equivariance_utils.wigner_D

Miscellaneous Utilities
-----------------------

The utilities here are used for miscellaneous purposes.
Initial usecases are for improving the printing format of __repr__.

.. autofunction:: deepchem.utils.misc_utils.indent

.. autofunction:: deepchem.utils.equivariance_utils.shape2str

0 comments on commit baf3370

Please sign in to comment.