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 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9ac44ce
starting to add caching to inertia moment calculation during the simu…
m-rauen Nov 26, 2024
37ad0e3
correct caching problem of not handle mutable objects
m-rauen Dec 3, 2024
014b59d
move ignore_unhashable to _misc module + trying to resolve circular i…
m-rauen Dec 3, 2024
7216147
circular import solved + movem from pkg_resources to importlib
m-rauen Dec 4, 2024
184a193
updated gitignore
m-rauen Dec 4, 2024
ddb4846
change ignore to copy strategy for cache + add docstring
m-rauen Dec 4, 2024
edae933
change maxsize of copy_unhashable
m-rauen Dec 5, 2024
bcce554
changing hash strategy to eliminate errors
m-rauen Dec 10, 2024
da26124
starting to add caching to inertia moment calculation during the simu…
m-rauen Nov 26, 2024
88b20e5
correct caching problem of not handle mutable objects
m-rauen Dec 3, 2024
07da1be
move ignore_unhashable to _misc module + trying to resolve circular i…
m-rauen Dec 3, 2024
6660655
circular import solved + movem from pkg_resources to importlib
m-rauen Dec 4, 2024
687d7c5
updated gitignore
m-rauen Dec 4, 2024
98be111
change ignore to copy strategy for cache + add docstring
m-rauen Dec 4, 2024
ca3e412
change maxsize of copy_unhashable
m-rauen Dec 5, 2024
6c1f0ad
changing hash strategy to eliminate errors
m-rauen Dec 10, 2024
70ae12e
changed scipy.misc.derivative to findiff.Diff (scipy derivative remov…
m-rauen Jan 28, 2025
37d2915
changed scipy.misc.derivative to findiff.Diff (scipy derivative remov…
m-rauen Jan 28, 2025
e9ce98d
resolve funky CI + cache corrected implemented
m-rauen Jan 28, 2025
8a32b23
added 'findiff' to the poetrylock file, since it's the new derivative…
m-rauen Jan 28, 2025
557092c
updated poetrylock file + cleaned some code
m-rauen Jan 28, 2025
29df5d7
testing funky CI
m-rauen Jan 28, 2025
61bde8b
testing
m-rauen Jan 29, 2025
f2298e4
added derivative func to misc module instead of using findiff
m-rauen Jan 31, 2025
3d638ac
forgetted to call _misc._derivative in the API module
m-rauen Jan 31, 2025
d426bdf
solved bugs from tests after adding cache to 'inertia' func + deleted…
m-rauen Mar 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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