Phase 8: split Environment into spec + FittedEnvironment#38
Open
edeno wants to merge 2 commits into
Open
Conversation
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>
5 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 8 (final phase) of the review-findings stack. Eliminates the
Environmentfootgun the full-repo review flagged: a single 1700-LOC dataclass that fused input parameters with eleven_-suffixedOptionalfitted fields plus an_is_fittedflag, so a freshly-constructedEnvironmentcould be read as if fitted (returningNones), and downstream code guarded with scatteredif env.X_ is None: raisechecks.Stacked on #37 (
test-coverage-gaps). Review/merge #29 → #30 → #33 → #34 → #35 → #36 → #37 first.What changed
Environmentis 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 newFittedEnvironmentinstead of mutatingselfin 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 onFittedEnvironment._is_fittedflag and the elevenOptionalfitted fields are gone — "fitted" is now a type, not a mutable flag.models/base.py:initialize_environmentscaptures the returnedFittedEnvironmentintoself.environments(re-fitting from the original spec so re-fit is correct). Detector usage is unchanged.getattr(env, "place_bin_centers_", None)so an unfitted spec still yields the helpful "fit first"ValueErrorrather than a rawAttributeError.make_nD_track_graph_from_environmenttakes explicit arrays instead of a half-builtself.FittedEnvironmentexported at the package top level alongsideEnvironment.Decisions / scope
Environment(per maintainer decision) rather than renaming toEnvironmentSpec— avoids breaking every downstream analysis and notebook. Achieves the plan's goal without the breaking rename.FittedEnvironmentis a regular (non-frozen) dataclass — the existing lazy_bin_distance_matrix_cache is set after construction, whichfrozen=Truewould forbid. Immutable in spirit (no_is_fitted, no Optional-then-mutate).self.environmentassignments first); notebook updates (plan scoped to a follow-up); full is-None guard sweep in internal-only call sites (distance2D, deepmodels/base.py) — those are reached only post-fit.Test plan
test_environment_spec.py— 9 contract tests (spec has no fitted attrs; fit returnsFittedEnvironment; fit does not mutate spec; spec hashable; read-only forwarded params)pytest -m "not slow"— 907 passed, 5 skipped, 1 xfailed, 0 failuresruff check+ruff format --checkclean🤖 Generated with Claude Code