Skip to content

Phase 8: split Environment into spec + FittedEnvironment#38

Open
edeno wants to merge 2 commits into
test-coverage-gapsfrom
environment-spec-fitted-split
Open

Phase 8: split Environment into spec + FittedEnvironment#38
edeno wants to merge 2 commits into
test-coverage-gapsfrom
environment-spec-fitted-split

Conversation

@edeno

@edeno edeno commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

Phase 8 (final phase) of the review-findings stack. Eliminates the Environment footgun the full-repo review flagged: a single 1700-LOC dataclass that fused input parameters with eleven _-suffixed Optional fitted fields plus an _is_fitted flag, so a freshly-constructed Environment could be read as if fitted (returning Nones), and downstream code guarded with scattered if env.X_ is None: raise checks.

Stacked on #37 (test-coverage-gaps). Review/merge #29#30#33#34#35#36#37 first.

What changed

  • Environment is now purely the user-facing input specification — its constructor signature and public name are unchanged, so existing user code and notebooks keep working (from non_local_detector import Environment).
  • Environment.fit_place_grid(position) returns a new FittedEnvironment instead of mutating self in place. The resolved spatial grid (edges_, place_bin_centers_, is_track_interior_, distance_between_nodes_, ...) and the spatial-query methods (get_bin_ind, get_manifold_distances, get_distances_to_interior_bins, get_direction, plot_grid, save_environment) live on FittedEnvironment.
  • The _is_fitted flag and the eleven Optional fitted fields are gone — "fitted" is now a type, not a mutable flag.
  • models/base.py:initialize_environments captures the returned FittedEnvironment into self.environments (re-fitting from the original spec so re-fit is correct). Detector usage is unchanged.
  • Likelihood/transition boundary guards use getattr(env, "place_bin_centers_", None) so an unfitted spec still yields the helpful "fit first" ValueError rather than a raw AttributeError.
  • make_nD_track_graph_from_environment takes explicit arrays instead of a half-built self.
  • FittedEnvironment exported at the package top level alongside Environment.

Decisions / scope

  • Public name kept as Environment (per maintainer decision) rather than renaming to EnvironmentSpec — avoids breaking every downstream analysis and notebook. Achieves the plan's goal without the breaking rename.
  • FittedEnvironment is a regular (non-frozen) dataclass — the existing lazy _bin_distance_matrix_ cache is set after construction, which frozen=True would forbid. Immutable in spirit (no _is_fitted, no Optional-then-mutate).
  • Deferred (documented, not regressions): frozen continuous-transition dataclasses (8.8, requires removing self.environment assignments first); notebook updates (plan scoped to a follow-up); full is-None guard sweep in internal-only call sites (distance2D, deep models/base.py) — those are reached only post-fit.

Test plan

  • golden regression — 4 pass, bit-identical (refactor is structural, not numerical)
  • snapshot — 8 pass, no diffs
  • new test_environment_spec.py — 9 contract tests (spec has no fitted attrs; fit returns FittedEnvironment; fit does not mutate spec; spec hashable; read-only forwarded params)
  • broad pytest -m "not slow"907 passed, 5 skipped, 1 xfailed, 0 failures
  • ruff check + ruff format --check clean

🤖 Generated with Claude Code

edeno and others added 2 commits May 28, 2026 16:28
Environment is now purely the user-facing input specification (constructor
signature and public name unchanged). Environment.fit_place_grid(position)
returns a new FittedEnvironment instead of mutating self in place; the resolved
spatial grid and the spatial-query methods (get_bin_ind, get_manifold_distances,
get_distances_to_interior_bins, get_direction, plot_grid, save_environment) live
on FittedEnvironment. The _is_fitted flag and the eleven _-suffixed Optional
fields on Environment are gone -- "fitted" is now a type, not a mutable flag, so
a freshly-constructed Environment can no longer be read as if it were fitted.

- environment.py: Environment holds only inputs + validation + fit_place_grid;
  new FittedEnvironment dataclass carries the fitted fields, forwards spec
  parameters read-only, and owns the grid methods. make_nD_track_graph_from_environment
  now takes explicit arrays instead of a half-built self.
- models/base.py: initialize_environments captures the returned FittedEnvironment
  into self.environments, re-fitting from the original spec so re-fit is correct.
- Boundary guards in the likelihood/transition entry points use
  getattr(env, "place_bin_centers_", None) so an unfitted spec still produces the
  helpful "fit first" ValueError rather than a raw AttributeError.
- FittedEnvironment exported at the package top level.
- Tests updated for the immutable spec->fitted flow; new test_environment_spec.py
  covers the contract (spec has no fitted attrs, fit returns FittedEnvironment,
  fit does not mutate the spec, spec is hashable).

Golden regression and snapshot outputs are bit-identical; the refactor is
structural, not numerical.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…den types, strengthen tests

- make_nD_track_graph_from_environment: restore the is_track_interior None guard
  dropped during the signature refactor, so a user-supplied is_track_interior on
  a non-track-graph environment again raises the clear ValueError instead of a
  bare AttributeError (matches prior behavior; the deeper feature gap that the
  mask is not wired into is_track_interior_ is pre-existing).
- Remove stale "Raises RuntimeError if not fitted" docstring lines from
  get_manifold_distances and get_distances_to_interior_bins (those guards are
  gone by design); narrow get_direction's RuntimeError doc to the N-D-graph case.
- Widen the Environments type alias to include FittedEnvironment (detectors may
  receive a pre-fitted environment).
- Strengthen test_does_not_mutate_spec to check every fitted attribute leaks
  onto the spec, not just one (dataclass __eq__ only compares declared fields).
- Add tests for the new code paths: re-fit reuses the original spec (env.spec
  extraction in initialize_environments) and a bare spec into a transition
  raises the helpful "must have defined place bin centers" ValueError.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant