Skip to content

viz/interactive.py: dedup repeated status Div, log-transform closure, and 3D mesh cache key (+ first unit tests) #1518

Open

Description

@sbryngelson

toolchain/mfc/viz/interactive.py repeats three small, self-contained pieces of logic that are pure functions and trivially extractable. Dedup'ing them removes a live copy-paste hazard (change one copy, forget the others) and is a good opportunity to add the first automated tests for this module.

This is the low-risk subset carved out of the larger _update decomposition (tracked separately) — it touches no callback control flow and is unit-testable.

1. Status html.Div built 4× verbatim

Lines 1971, 2120, 2470, 2626 are the same 8-child html.Div (step {step} / · shape {raw.shape} / Br / min+value / max+value, same _YELLOW/_MUTED/_BLUE/_RED styles), differing only in the min/max local names.
→ add _status_div(step, shape, dmin, dmax) -> html.Div and call it at all four sites.

2. Log/identity transform closure _tf redefined 4×

Lines 379, 386 (inside _prefetch_3d_mesh) and 1853, 1861 (inside _update):

def _tf(arr):
    return np.where(arr > 0, np.log10(np.maximum(arr, 1e-300)), np.nan)
# else
def _tf(arr):
    return arr

→ add _log_transform(log: bool) returning the transform (and the cmin/cmax pair, since sites 379/386 inline-compute those right next to the closure — extract them together so the cmin/cmax duplication doesn't get left behind). The 1e-300 floor should become a module constant _LOG_FLOOR at the same time.

3. 11-arg 3D mesh cache-key tuple repeated

The literal (step, var, mode, log, vmin_in, vmax_in, iso_min_frac, iso_max_frac, iso_n, vol_min_frac, vol_max_frac) appears at 350 (_prefetch_3d_mesh) and 432 (_get_cached_3d_mesh), and is passed positionally from _update at ~1890.
→ add _mesh3_key(...) -> tuple (or a small namedtuple) and use it at all three sites.

Tests

toolchain/mfc/viz/test_viz.py currently has no coverage of interactive.py at all. Add a few unit tests for _status_div, _log_transform, and _mesh3_key — these would be the first tests for this module and make the dedup verifiable.

Behavior preservation / risk: all three are pure-function extractions with no control-flow change; toolchain-only, no golden-file impact.


Filed from a repo-wide code-cleanliness review; verified against master @ 40dde5e.

Code references

Activity

  1. laolmstead commented on Oct 26, 2026

    @laolmstead

    I'd like to work on this issue. This is my first contribution to MFC. I'll start by writing the unit tests that capture the expected behavior, then implement the fix against them. I'll post back once I have a PR ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions