Skip to content

Commit

Permalink
Merge pull request #195 from softboiler/194-migrate-stabilized-morph-…
Browse files Browse the repository at this point in the history
…functionality-to-boilercv

Migrate stabilized `Morph` functionality to `boilercv`
  • Loading branch information
blakeNaccarato authored May 22, 2024
2 parents 7e2303b + 2ff7f7b commit d9729e1
Show file tree
Hide file tree
Showing 19 changed files with 210 additions and 188 deletions.
1 change: 1 addition & 0 deletions changelog/194.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Migrate stabilized `Morph` functionality to `boilercv`
17 changes: 9 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from hashlib import sha256
from pathlib import Path

from ruamel.yaml import YAML
from sphinx.application import Sphinx

from boilercv_docs import DOCS, PYPROJECT, chdir_docs
from boilercv_docs.intersphinx import get_ispx, get_rtd, get_url
from boilercv_docs.nbs import init_nb_env
Expand All @@ -12,8 +15,6 @@
from boilercv_pipeline.correlations.dimensionless_bubble_diameter.morphs import (
EQUATIONS,
)
from ruamel.yaml import YAML
from sphinx.application import Sphinx

# ! Initialization
patch_nbs()
Expand Down Expand Up @@ -45,17 +46,17 @@
"""Package version."""
# ! Intersphinx and related
ISPX_MAPPING: dict[str, IspxMappingValue] = {
**{pkg: get_rtd(pkg) for pkg in ["myst_parser", "nbformat", "numpydoc"]},
**{pkg: get_rtd(pkg) for pkg in ["myst_parser", "nbformat", "numpydoc", "tomlkit"]},
**{pkg: get_rtd(pkg, latest=True) for pkg in ["pyqtgraph"]},
"jupyterbook": get_url("jupyterbook.org/en"),
"numpy": get_url("numpy.org/doc"),
"matplotlib": get_url("matplotlib.org"),
"pytest": get_url("docs.pytest.org/en"),
"sympy": get_url("docs.sympy.org", latest=True),
"colorcet": get_ispx("https://colorcet.holoviz.org/"),
"cv2": get_ispx("docs.opencv.org/2.4"),
"python": get_ispx("docs.python.org/3"),
"pandas": get_ispx("pandas.pydata.org/docs"),
"colorcet": get_ispx("https://colorcet.holoviz.org/"),
"python": get_ispx("docs.python.org/3"),
}
"""Intersphinx mapping."""
TIPPY_RTD_URLS = [
Expand Down Expand Up @@ -142,7 +143,7 @@ def dpath(path: Path, rel: Path = DOCS) -> str:
"sphinxcontrib.towncrier",
]
suppress_warnings = [
"autodoc2.dup_item" # "Duplicate items in boilercv_tests.test_morph
"autodoc2.dup_item" # "Duplicate items in boilercv_tests.test_morphs
]
# ! Theme
html_title = PACKAGE
Expand Down Expand Up @@ -250,15 +251,15 @@ def dpath(path: Path, rel: Path = DOCS) -> str:
(r"py:.*", r"_pytest\..+"),
(r"py:.*", r"boilercore\..+"),
(r"py:.*", r"numpy\.typing\..+"),
(r"py:.*", r"tomlkit\.container\..+"),
# ? sympy: https://github.com/sympy/sympy/issues/17619#issuecomment-536781620
(r"py:.*", r"sympy\..+"),
(r"py:.*", r"pydantic\..+"), # ? https://github.com/pydantic/pydantic/issues/1339
(r"py:.*", r"PySide6\..+"), # ? https://bugreports.qt.io/browse/PYSIDE-2215
# ? TypeAlias: https://github.com/sphinx-doc/sphinx/issues/10785
(r"py:class", rf"{PACKAGE}.*\.types\..+"),
(r"py:class", rf"{PACKAGE}_pipeline\.captivate\.previews\..+"),
(r"py:class", rf"{PACKAGE}_pipeline\.equations\..+"),
(r"py:class", rf"{PACKAGE}_tests\.test_morph\..+"),
(r"py:.*", rf"{PACKAGE}_tests\.test_morphs\..+"),
# ? Until done with Pydantic v1
(r"py:.*", r"pydantic\.v1\..+"),
]
Expand Down
8 changes: 4 additions & 4 deletions docs/experiments/e230920_subcool/find_centers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
"\n",
"paths = init()\n",
"\n",
"from boilercv_docs.nbs import HIDE, nowarn, style_df\n",
"from boilercv_pipeline.experiments.e230920_subcool import GBC, bounded_ax\n",
"from boilercv_pipeline.experiments.e240215_plotting import cool, warm\n",
"from boilercv_pipeline.sets import get_contours_df, get_dataset\n",
"from geopandas import GeoDataFrame, points_from_xy\n",
"from matplotlib.pyplot import subplots\n",
"from myst_nb import glue\n",
Expand All @@ -45,6 +41,10 @@
"from shapely import LinearRing\n",
"\n",
"from boilercv.images import scale_bool\n",
"from boilercv_docs.nbs import HIDE, nowarn, style_df\n",
"from boilercv_pipeline.experiments.e230920_subcool import GBC, bounded_ax\n",
"from boilercv_pipeline.experiments.e240215_plotting import cool, warm\n",
"from boilercv_pipeline.sets import get_contours_df, get_dataset\n",
"\n",
"COMPARE_WITH_TRACKPY = True\n",
"\"\"\"Whether to get centers using the Trackpy approach.\"\"\"\n",
Expand Down
8 changes: 4 additions & 4 deletions docs/experiments/e230920_subcool/find_objects.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@
"\n",
"paths = init()\n",
"\n",
"from boilercv_docs.nbs import HIDE, nowarn, style_df\n",
"from boilercv_pipeline.experiments.e230920_subcool import GBC, bounded_ax\n",
"from boilercv_pipeline.experiments.e240215_plotting import cool, warm\n",
"from boilercv_pipeline.sets import get_contours_df, get_dataset\n",
"from geopandas import GeoDataFrame, points_from_xy\n",
"from numpy import pi, sqrt\n",
"from pandas import DataFrame, IndexSlice, NamedAgg\n",
"from seaborn import scatterplot\n",
"from shapely import LinearRing, Polygon\n",
"\n",
"from boilercv.images import scale_bool\n",
"from boilercv_docs.nbs import HIDE, nowarn, style_df\n",
"from boilercv_pipeline.experiments.e230920_subcool import GBC, bounded_ax\n",
"from boilercv_pipeline.experiments.e240215_plotting import cool, warm\n",
"from boilercv_pipeline.sets import get_contours_df, get_dataset\n",
"\n",
"COMPARE_WITH_TRACKPY = True\n",
"\"\"\"Whether to get objects using the Trackpy approach.\"\"\"\n",
Expand Down
5 changes: 3 additions & 2 deletions docs/experiments/e230920_subcool/find_tracks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"\n",
"paths = init()\n",
"\n",
"from numpy import diff, linalg\n",
"from pandas import DataFrame, read_hdf\n",
"\n",
"from boilercv_docs.nbs import HIDE, display_dataframe_with_math, nowarn, style_df\n",
"from boilercv_pipeline.experiments.e230920_subcool import (\n",
" GBC,\n",
Expand All @@ -43,8 +46,6 @@
" transform_cols,\n",
")\n",
"from boilercv_pipeline.sets import get_dataset\n",
"from numpy import diff, linalg\n",
"from pandas import DataFrame, read_hdf\n",
"\n",
"with nowarn(capture=True):\n",
" from trackpy import link, quiet\n",
Expand Down
34 changes: 17 additions & 17 deletions docs/experiments/e230920_subcool/find_tracks_trackpy.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@
"\n",
"paths = init()\n",
"\n",
"from matplotlib.figure import Figure\n",
"from matplotlib.pyplot import subplot_mosaic, subplots\n",
"from numpy import diff, linalg, logspace\n",
"from pandas import CategoricalDtype, DataFrame, read_hdf\n",
"from seaborn import lineplot, move_legend, scatterplot\n",
"\n",
"from boilercv.data import apply_to_img_da\n",
"from boilercv.dimensionless_params import (\n",
" fourier,\n",
" jakob,\n",
" kinematic_viscosity,\n",
" prandtl,\n",
" reynolds,\n",
" thermal_diffusivity,\n",
")\n",
"from boilercv.images import scale_bool\n",
"from boilercv.images.cv import Op, Transform, transform\n",
"from boilercv_docs.nbs import HIDE, nowarn, style_df\n",
"from boilercv_pipeline.correlations.dimensionless_bubble_diameter import (\n",
" florschuetz_chao_1965,\n",
Expand All @@ -47,23 +64,6 @@
"from boilercv_pipeline.experiments.e240215_plotting import cool, warm\n",
"from boilercv_pipeline.models.params import PARAMS\n",
"from boilercv_pipeline.sets import get_dataset\n",
"from matplotlib.figure import Figure\n",
"from matplotlib.pyplot import subplot_mosaic, subplots\n",
"from numpy import diff, linalg, logspace\n",
"from pandas import CategoricalDtype, DataFrame, read_hdf\n",
"from seaborn import lineplot, move_legend, scatterplot\n",
"\n",
"from boilercv.data import apply_to_img_da\n",
"from boilercv.dimensionless_params import (\n",
" fourier,\n",
" jakob,\n",
" kinematic_viscosity,\n",
" prandtl,\n",
" reynolds,\n",
" thermal_diffusivity,\n",
")\n",
"from boilercv.images import scale_bool\n",
"from boilercv.images.cv import Op, Transform, transform\n",
"\n",
"with nowarn(capture=True):\n",
" from trackpy import batch, link, locate, quiet\n",
Expand Down
5 changes: 3 additions & 2 deletions docs/experiments/e230920_subcool/get_thermal_data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
"\n",
"paths = init()\n",
"\n",
"from boilercv_docs.nbs import HIDE\n",
"from boilercv_pipeline.experiments.e230920_subcool import ALL_THERMAL_DATA\n",
"from pandas import DataFrame, read_csv\n",
"from seaborn import scatterplot\n",
"\n",
"from boilercv_docs.nbs import HIDE\n",
"from boilercv_pipeline.experiments.e230920_subcool import ALL_THERMAL_DATA\n",
"\n",
"BOILING = 97.33 # (C) inferred from mean pressure over the time span\n",
"TIME = \"time\"\n",
"BASE_TEMP = \"T0cal (C)\"\n",
Expand Down
30 changes: 15 additions & 15 deletions docs/experiments/e230920_subcool/plot_tracks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@
"from boilercv_docs.nbs import init\n",
"\n",
"paths = init()\n",
"from matplotlib.figure import Figure\n",
"from matplotlib.pyplot import subplot_mosaic, subplots\n",
"from numpy import diff, log10, logspace\n",
"from pandas import DataFrame, Series, read_hdf\n",
"from seaborn import lineplot, move_legend, scatterplot\n",
"\n",
"from boilercv.dimensionless_params import (\n",
" fourier,\n",
" jakob,\n",
" kinematic_viscosity,\n",
" prandtl,\n",
" reynolds,\n",
" thermal_diffusivity,\n",
")\n",
"from boilercv.images import scale_bool\n",
"from boilercv_docs.nbs import HIDE\n",
"from boilercv_pipeline.correlations.dimensionless_bubble_diameter import (\n",
" akiyama_1973,\n",
Expand All @@ -57,21 +72,6 @@
")\n",
"from boilercv_pipeline.experiments.e240215_plotting import cool, warm12\n",
"from boilercv_pipeline.sets import get_dataset\n",
"from matplotlib.figure import Figure\n",
"from matplotlib.pyplot import subplot_mosaic, subplots\n",
"from numpy import diff, log10, logspace\n",
"from pandas import DataFrame, Series, read_hdf\n",
"from seaborn import lineplot, move_legend, scatterplot\n",
"\n",
"from boilercv.dimensionless_params import (\n",
" fourier,\n",
" jakob,\n",
" kinematic_viscosity,\n",
" prandtl,\n",
" reynolds,\n",
" thermal_diffusivity,\n",
")\n",
"from boilercv.images import scale_bool\n",
"\n",
"S = Series\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
from pathlib import Path
from re import sub
from string import whitespace
from typing import Annotated, Any, ClassVar, Generic, Self, TypeAlias, overload
from typing import Any, ClassVar, Generic, Self, overload

from numpy import linspace, pi
from pydantic import BaseModel, Field, PlainValidator, model_validator
from pydantic import BaseModel, Field, model_validator
from pydantic_core import PydanticUndefinedType
from sympy import Basic, symbols, sympify
from tomlkit import parse
from tomlkit.container import Container
from tomlkit.items import Item

from boilercv.morphs import BaseMorph, Morph
from boilercv_pipeline.correlations.dimensionless_bubble_diameter.types import (
LOCALS,
Eq,
Expectation,
Expr,
FormsRepl,
JsonStrPlainSerializer,
K,
Kind,
Leaf,
Locals,
Node,
Param,
Repl,
Sym,
V,
kinds,
params,
syms,
solve_syms,
)
from boilercv_pipeline.equations import BaseMorph, Morph

base = Path(__file__).with_suffix(".toml")
EQUATIONS_TOML = base.with_stem("equations")
Expand All @@ -49,8 +49,6 @@
for find, repl in {"{0}": r"\o", "{b0}": r"\b0"}.items()
)
"""Replacements to make after parsing LaTeX from PNGs."""
PARAMS = Morph[Param, Sym](dict(zip(params, syms, strict=True)))
"""Parameters."""
KWDS = Morph[Param, Expectation]({
"bubble_fourier": linspace(start=0.0, stop=5.0e-3, num=10),
"bubble_jakob": 1.0,
Expand Down Expand Up @@ -125,23 +123,6 @@ class Forms(DefaultMorph[Kind, str]):
DefaultMorph.register(Forms)


def validate_expr(v: Basic | str):
"""Validate expression."""
return (
v
if isinstance(v, Basic)
else sympify(v, locals=LOCALS.model_dump(), evaluate=False)
)


Expr: TypeAlias = Annotated[
Basic, PlainValidator(validate_expr), JsonStrPlainSerializer
]
"""Expression."""
solve_syms: tuple[Sym, ...] = ("Fo_0", "beta")
"""Symbols to solve for."""


class Soln(BaseModel):
"""All solutions."""

Expand All @@ -159,20 +140,6 @@ class Solns(DefaultMorph[Sym, Soln]):
DefaultMorph.register(Solns)


Locals: TypeAlias = Morph[Sym, Expr]
"""Locals."""
LOCALS = Locals(
dict(
zip(
syms,
symbols(list(PARAMS.values()), nonnegative=True, real=True, finite=True),
strict=False,
)
)
)
"""Local variables."""


def set_equation_forms(i: Forms, symbols: Locals) -> Forms:
"""Set equation forms."""
return i.pipe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from numpy import float64
from numpy.typing import NDArray
from pydantic import PlainSerializer, PlainValidator
from sympy import Basic, symbols, sympify

from boilercv.morphs import Morph

T = TypeVar("T")
K = TypeVar("K")
Expand Down Expand Up @@ -45,6 +48,8 @@ class Repl(NamedTuple, Generic[T]):
"""Symbol."""
syms: tuple[Sym, ...] = get_args(Sym)
"""Symbols."""
solve_syms: tuple[Sym, ...] = ("Fo_0", "beta")
"""Symbols to solve for."""
FormsRepl: TypeAlias = Repl[Kind]
"""Forms replacements."""
Param: TypeAlias = Literal[
Expand All @@ -71,3 +76,33 @@ class Repl(NamedTuple, Generic[T]):
"""Leaf node."""
Node: TypeAlias = dict[Any, "Node | Leaf"]
"""General node."""


def validate_expr(v: Basic | str):
"""Validate expression."""
return (
v
if isinstance(v, Basic)
else sympify(v, locals=LOCALS.model_dump(), evaluate=False)
)


Expr: TypeAlias = Annotated[
Basic, PlainValidator(validate_expr), JsonStrPlainSerializer
]
"""Expression."""

Locals: TypeAlias = Morph[Sym, Expr]
"""Locals."""
PARAMS = Morph[Param, Sym](dict(zip(params, syms, strict=True)))
"""Parameters."""
LOCALS = Locals(
dict(
zip(
syms,
symbols(list(PARAMS.values()), nonnegative=True, real=True, finite=True),
strict=False,
)
)
)
"""Local variables."""
Loading

0 comments on commit d9729e1

Please sign in to comment.