Skip to content

Commit

Permalink
BREAK: remove perform_cached_doit() function (#97)
Browse files Browse the repository at this point in the history
* DOC: import `simplify_latex_rendering` from `io`
  • Loading branch information
redeboer authored Mar 17, 2024
1 parent 31d573e commit 145e9e5
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 60 deletions.
3 changes: 2 additions & 1 deletion docs/comparison/d2kkk.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@
"from tensorwaves.data.rng import TFUniformRealNumberGenerator\n",
"from tensorwaves.data.transform import SympyDataTransformer\n",
"\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder, simplify_latex_rendering\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder\n",
"from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain\n",
"from ampform_dpd.io import (\n",
" as_markdown_table,\n",
" aslatex,\n",
" get_readable_hash,\n",
" perform_cached_lambdify,\n",
" simplify_latex_rendering,\n",
")\n",
"from ampform_dpd.spin import filter_parity_violating_ls, generate_ls_couplings\n",
"\n",
Expand Down
3 changes: 2 additions & 1 deletion docs/comparison/jpsi2phipipi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@
"from tensorwaves.data.rng import TFUniformRealNumberGenerator\n",
"from tensorwaves.data.transform import SympyDataTransformer\n",
"\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder, simplify_latex_rendering\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder\n",
"from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain\n",
"from ampform_dpd.io import (\n",
" as_markdown_table,\n",
" aslatex,\n",
" get_readable_hash,\n",
" perform_cached_lambdify,\n",
" simplify_latex_rendering,\n",
")\n",
"from ampform_dpd.spin import filter_parity_violating_ls, generate_ls_couplings\n",
"\n",
Expand Down
3 changes: 2 additions & 1 deletion docs/comparison/jpsi2pipipi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@
"from tensorwaves.data.rng import TFUniformRealNumberGenerator\n",
"from tensorwaves.data.transform import SympyDataTransformer\n",
"\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder, simplify_latex_rendering\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder\n",
"from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain\n",
"from ampform_dpd.io import (\n",
" as_markdown_table,\n",
" aslatex,\n",
" get_readable_hash,\n",
" perform_cached_lambdify,\n",
" simplify_latex_rendering,\n",
")\n",
"from ampform_dpd.spin import filter_parity_violating_ls, generate_ls_couplings\n",
"\n",
Expand Down
13 changes: 7 additions & 6 deletions docs/jpsi2ksp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@
"from tensorwaves.data.transform import SympyDataTransformer\n",
"from tqdm.auto import tqdm\n",
"\n",
"from ampform_dpd import (\n",
" DalitzPlotDecompositionBuilder,\n",
" _get_particle,\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder, _get_particle\n",
"from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain\n",
"from ampform_dpd.io import (\n",
" as_markdown_table,\n",
" aslatex,\n",
" perform_cached_lambdify,\n",
" simplify_latex_rendering,\n",
")\n",
"from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain\n",
"from ampform_dpd.io import as_markdown_table, aslatex, perform_cached_lambdify\n",
"from ampform_dpd.spin import filter_parity_violating_ls, generate_ls_couplings\n",
"\n",
"if TYPE_CHECKING:\n",
Expand Down Expand Up @@ -577,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
4 changes: 2 additions & 2 deletions docs/lc2pkpi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
"import sympy as sp\n",
"from IPython.display import Latex, Markdown\n",
"\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder, simplify_latex_rendering\n",
"from ampform_dpd import DalitzPlotDecompositionBuilder\n",
"from ampform_dpd.decay import IsobarNode, Particle, ThreeBodyDecay, ThreeBodyDecayChain\n",
"from ampform_dpd.dynamics import BreitWignerMinL\n",
"from ampform_dpd.io import as_markdown_table, aslatex\n",
"from ampform_dpd.io import as_markdown_table, aslatex, simplify_latex_rendering\n",
"from ampform_dpd.spin import filter_parity_violating_ls, generate_ls_couplings\n",
"\n",
"simplify_latex_rendering()"
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 145e9e5

Please sign in to comment.