Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: inertia benefit from caching + move from pkg_resources to importlib.metada #714

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions overreact/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

__docformat__ = "restructuredtext"

import pkg_resources as _pkg_resources
from importlib.metadata import version

from overreact.api import (
get_enthalpies,
Expand Down Expand Up @@ -48,7 +48,7 @@
"unparse_reactions",
]

__version__ = _pkg_resources.get_distribution(__name__).version
__version__ = version(__name__)
__license__ = "MIT" # I'm too lazy to get it from setup.py...

__headline__ = "📈 Create and analyze chemical microkinetic models built from computational chemistry data."
Expand Down
33 changes: 32 additions & 1 deletion overreact/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,45 @@

import contextlib
from functools import lru_cache as cache
from copy import deepcopy

import numpy as np
from scipy.stats import cauchy, norm

import overreact as rx
from overreact import _constants as constants


def make_hashable(obj):
if isinstance(obj, np.ndarray):
return tuple(obj.ravel())
elif isinstance(obj, (list, set)):
return tuple(map(make_hashable, obj))
else:
return obj

def copy_unhashable(maxsize=128, typed=False):
"""Creates a copy of the arrays received by lru_cache and make them hashable, therefore maintaining the arrays to be passed and caching prototypes of those arrays.

Insipired by:
<https://stackoverflow.com/a/54909677/21189559>

Parameters
----------
maxsize : int
typed : bool
If true, function arguments of different types will be cached separately.

Returns
--------
function
"""
def decorator(func):
cached_func = cache(maxsize=maxsize, typed=typed)(func)
def wrapper(*args, **kwargs):
return deepcopy(cached_func(*args, **kwargs))
return wrapper
return decorator

def _find_package(package):
"""Check if a package exists without importing it.

Expand Down
4 changes: 3 additions & 1 deletion overreact/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import overreact as rx
from overreact import _constants as constants
from overreact import _misc as _misc

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1679,8 +1680,9 @@ def gyradius(atommasses, atomcoords, method="iupac"):
else:
msg = f"unavailable method: '{method}'"
raise ValueError(msg)



@rx._misc.copy_unhashable()
def inertia(atommasses, atomcoords, align=True):
r"""Calculate primary moments and axes from the inertia tensor.

Expand Down
Loading