Skip to content

Commit

Permalink
Back propagate parameters to earlier analyses. Set axis limits and ti…
Browse files Browse the repository at this point in the history
…cks/labels intelligently.
  • Loading branch information
blakeNaccarato committed Aug 1, 2023
1 parent bce79f5 commit ab9ac25
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions data/docs.dvc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
outs:
- md5: 72ef5d33e00054c32cf29812ebe52ff0.dir
size: 3485382
- md5: 29f21802e3cd8d82a796750710d8fb82.dir
size: 3430183
nfiles: 11
hash: md5
path: docs
37 changes: 36 additions & 1 deletion src/boilercv/format.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# type: ignore pyright 1.1.308, local/CI differences, below
from collections.abc import Mapping
from contextlib import contextmanager
from typing import Any
from typing import Any, Literal

import matplotlib as mpl
import numpy as np
import pandas as pd
from IPython.core.display import Markdown, Math # type: ignore
from IPython.display import display # type: ignore
from matplotlib import pyplot as plt
from matplotlib.axis import XAxis, YAxis
from matplotlib.ticker import MaxNLocator
from sympy import FiniteSet
from sympy.printing.latex import latex

Expand Down Expand Up @@ -39,6 +43,37 @@ def math_mod(expr, long_frac_ratio=3, **kwargs):
# * PLOTTING


def smart_set_lim(
ax: plt.Axes,
axis: Literal["x", "y", "z"],
limit: tuple[float, float],
prec: int = 0,
):
"""Set axis limits with smart precision and tick handling.
If axis limits exceed one, simply set the limits. Otherwise, set limits and
automatically choose the minimum necessary label format precision to represent the
limits. Limit the number of major ticks so as not to repeat tick labels given the
format precision.
Args:
ax: The axes object to operate on.
axis: The axis (e.g. "x" or "y") to operate on.
limit: The axes limits to apply.
prec: The maximum precision for the major ticks. Default is 0.
"""
getattr(ax, f"set_{axis}lim")(*limit)
if np.max(limit) > 1:
return
if prec == 0:
prec = int(np.min(np.floor(np.log10(np.array(limit)[np.array(limit) != 0]))))
axis_to_set: XAxis | YAxis = getattr(ax, f"{axis}axis")
axis_to_set.set_major_formatter(f"{{:#.{-prec}f}}".format)
axis_to_set.set_major_locator(
MaxNLocator(int(np.squeeze(np.diff(limit)) * 10**-prec))
)


@contextmanager
def manual_subplot_spacing():
"""Context manager that allows custom spacing of subplots."""
Expand Down
6 changes: 3 additions & 3 deletions src/boilercv/pre_repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
sink=sink, enqueue=True, format=("<green>{time:mm:ss}</green> | {message}")
)
logger = logger.opt(colors=not REPLAY)
logger.info("<yellow>START</yellow> pre_repro")
if not VERBOSE_LOG:
logger.disable("__main__")


async def main():
Expand Down Expand Up @@ -366,6 +363,9 @@ def __await__(self):
# * -------------------------------------------------------------------------------- * #

if __name__ == "__main__":
logger.info("<yellow>START</yellow> pre_repro")
if not VERBOSE_LOG:
logger.disable("__main__")
asyncio.run(main())
logger.enable("__main__")
logger.info("<green>FINISH</green> pre_repro")

0 comments on commit ab9ac25

Please sign in to comment.