-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate common model and notebook functionality in from boilerdata
- Loading branch information
1 parent
a63fa6b
commit c027a49
Showing
8 changed files
with
107 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
"""Model fits.""" | ||
|
||
import warnings | ||
from collections.abc import Mapping, Sequence | ||
from functools import partial | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Notebook helpers.""" | ||
|
||
from typing import Any | ||
|
||
import pandas as pd | ||
from IPython.core.display import Markdown, Math | ||
from IPython.display import display | ||
from sympy import FiniteSet | ||
from sympy.printing.latex import latex | ||
|
||
|
||
def set_format(): | ||
"""Set up formatting for interactive notebook sessions. | ||
The triple curly braces in the f-string allows the format function to be dynamically | ||
specified by a given float specification. The intent is clearer this way, and may be | ||
extended in the future by making `float_spec` a parameter. | ||
""" | ||
float_spec = ":#.4g" | ||
pd.options.display.min_rows = pd.options.display.max_rows = 50 | ||
pd.options.display.float_format = f"{{{float_spec}}}".format | ||
|
||
|
||
def disp_named(*args: tuple[Any, str]): | ||
"""Display objects with names above them.""" | ||
for elem, name in args: | ||
display(Markdown(f"##### {name}")) | ||
display(elem) | ||
|
||
|
||
def disp_free(title, eqn, **kwargs): | ||
disp(title, eqn, **kwargs) | ||
disp("Free symbols", FiniteSet(*eqn.rhs.free_symbols), **kwargs) | ||
|
||
|
||
def disp(title, *exprs, **kwargs): | ||
print(f"{title}:") | ||
display(*(math_mod(expr, **kwargs) for expr in exprs)) | ||
|
||
|
||
def math_mod(expr, long_frac_ratio=3, **kwargs): | ||
return Math(latex(expr, long_frac_ratio=long_frac_ratio, **kwargs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Paths and modules.""" | ||
|
||
from collections.abc import Iterable | ||
from pathlib import Path | ||
from re import compile | ||
from types import ModuleType | ||
|
||
|
||
def get_package_dir(package: ModuleType) -> Path: | ||
return Path(next(iter(package.__path__))) | ||
|
||
|
||
def map_stages(stages_dir: Path, package_dir: Path) -> dict[str, Path]: | ||
"""Map stage module names to their paths.""" | ||
stages: dict[str, Path] = {} | ||
for path in walk_module_paths(stages_dir, package_dir, glob="[!__]*.[py ipynb]*"): | ||
module = get_module_rel(get_module(path, package_dir), stages_dir.name) | ||
stages[module.replace(".", "_")] = path | ||
return stages | ||
|
||
|
||
def walk_module_paths( | ||
package: Path, top: Path, suffix: str = ".py", glob: str | None = None | ||
) -> Iterable[Path]: | ||
"""Walk modules from a given submodule path and the top level library directory.""" | ||
for directory in ( | ||
package, | ||
*[ | ||
path | ||
for path in package.iterdir() | ||
if path.is_dir() and "__" not in str(path.relative_to(top.parent)) | ||
], | ||
): | ||
yield from sorted(directory.glob(glob or f"[!__]*{suffix}")) | ||
|
||
|
||
def get_module(module: Path, package: Path) -> str: | ||
"""Get module name given the submodule path and the top level library directory.""" | ||
return ( | ||
str(module.relative_to(package.parent).with_suffix("")) | ||
.replace("\\", ".") | ||
.replace("/", ".") | ||
) | ||
|
||
|
||
def walk_modules(package: Path, top: Path, suffix: str = ".py") -> Iterable[str]: | ||
"""Walk modules from a given submodule path and the top level library directory.""" | ||
for module in walk_module_paths(package, top, suffix): | ||
yield get_module(module, top) | ||
|
||
|
||
def get_module_rel(module: str, relative: str) -> str: | ||
"""Get module name relative to another module.""" | ||
return compile(rf".*{relative}\.").sub(repl="", string=module) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""Types used throughout this package.""" | ||
"""Types.""" | ||
|
||
from typing import Literal, TypeVar | ||
|
||
|