Skip to content

Commit

Permalink
BREAK: remove perform_cached_doit() function
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer committed Mar 17, 2024
1 parent 9473f86 commit a386881
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
2 changes: 1 addition & 1 deletion docs/jpsi2ksp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"The {meth}`~sympy.core.basic.Basic.doit` operation can be cached to disk with {func}`.perform_cached_doit`. We do this twice, once for the unfolding of the {attr}`~.AmplitudeModel.intensity` expression and second for the substitution and unfolding of the {attr}`~.AmplitudeModel.amplitudes`. Note that we could also have unfolded the intensity and substituted the amplitudes with {attr}`~.AmplitudeModel.full_expression`, but then the unfolded {attr}`~.AmplitudeModel.intensity` expression is not cached."
"The {meth}`~sympy.core.basic.Basic.doit` operation can be cached to disk with {func}`~ampform.sympy.perform_cached_doit`. We do this twice, once for the unfolding of the {attr}`~.AmplitudeModel.intensity` expression and second for the substitution and unfolding of the {attr}`~.AmplitudeModel.amplitudes`. Note that we could also have unfolded the intensity and substituted the amplitudes with {attr}`~.AmplitudeModel.full_expression`, but then the unfolded {attr}`~.AmplitudeModel.intensity` expression is not cached."
]
},
{
Expand Down
52 changes: 3 additions & 49 deletions src/ampform_dpd/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
from os.path import abspath, dirname, expanduser
from textwrap import dedent
from typing import TYPE_CHECKING, Iterable, Mapping, Sequence, overload
from warnings import warn

import cloudpickle
import sympy as sp
from ampform.io import aslatex
from ampform.sympy import (
perform_cached_doit, # noqa: F401 # pyright:ignore[reportUnusedImport]
)
from tensorwaves.function.sympy import create_function, create_parametrized_function

from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain
Expand Down Expand Up @@ -217,54 +219,6 @@ def _create_markdown_table_row(items: Iterable):
return "| " + " | ".join(f"{i}" for i in items) + " |\n"


def perform_cached_doit(
unevaluated_expr: sp.Expr, directory: str | None = None
) -> sp.Expr:
"""Perform :code:`doit()` on an `~sympy.core.expr.Expr` and cache result to disk.
The cached result is fetched from disk if the hash of the original expression is the
same as the hash embedded in the filename.
Args:
unevaluated_expr: A `sympy.Expr <sympy.core.expr.Expr>` on which to call
:code:`doit()`.
directory: The directory in which to cache the result. If `None`, the cache
directory will be put under the home directory, or to the path specified by
the environment variable :code:`SYMPY_CACHE_DIR`.
.. tip:: For a faster cache, set `PYTHONHASHSEED
<https://docs.python.org/3/using/cmdline.html#envvar-PYTHONHASHSEED>`_ to a
fixed value.
.. seealso:: :func:`perform_cached_lambdify`
.. deprecated:: 0.2
Use :func:`ampform.sympy.perform_cached_doit` instead. See `ComPWA/ampform#24
<https://github.com/ComPWA/ampform-dpd/issues/24>`_
"""
msg = """
Use ampform.sympy.perform_cached_doit from AmpForm-DPD v0.2 onwards. See
https://github.com/ComPWA/ampform-dpd/issues/24
"""
warn(dedent(msg), category=PendingDeprecationWarning)
if directory is None:
main_cache_dir = _get_main_cache_dir()
directory = abspath(f"{main_cache_dir}/.sympy-cache")
h = get_readable_hash(unevaluated_expr)
filename = f"{directory}/{h}.pkl"
if os.path.exists(filename):
with open(filename, "rb") as f:
return pickle.load(f)
_LOGGER.warning(
f"Cached expression file {filename} not found, performing doit()..."
)
unfolded_expr = unevaluated_expr.doit()
os.makedirs(dirname(filename), exist_ok=True)
with open(filename, "wb") as f:
pickle.dump(unfolded_expr, f)
return unfolded_expr


@overload
def perform_cached_lambdify(
expr: sp.Expr,
Expand Down

0 comments on commit a386881

Please sign in to comment.