Skip to content

fix: matrix cache silently corrupts matrices when view/sensor/surface names collide - #65

Open
silaskalmbach wants to merge 1 commit into
LBNL-ETA:mainfrom
silaskalmbach:fix/matrix-cache-key-collision
Open

fix: matrix cache silently corrupts matrices when view/sensor/surface names collide#65
silaskalmbach wants to merge 1 commit into
LBNL-ETA:mainfrom
silaskalmbach:fix/matrix-cache-key-collision

Conversation

@silaskalmbach

Copy link
Copy Markdown
Contributor

save_matrices keyed all per-entity matrices by bare entity name:
'{name}_sky_matrix' (TwoPhase, views AND sensors), '{name}_window_matrix'
(ThreePhase: views, sensors AND surfaces; FivePhase: views and sensors) and
'{name}_sun_direct_matrix' (FivePhase). When two groups share a name -- e.g.
a view and a sensor both named after the zone -- the later group overwrote
the earlier one in the .npz, and load_matrices restored the wrong array into
the survivor without any error. Downstream this manifests as degenerate
view matrices and noise-floor DGP values with no exception.

Namespace the keys per group (view_/sensor_/surface_) and stamp the file
with '_schema_version' = 2. Legacy (v1) files remain loadable when their
entity names do not collide; a colliding legacy file now raises ValueError
with a pointer to regenerate, instead of silently returning corrupt data.

Adds tests: roundtrips with colliding names for all three phase methods
(shapes and contents preserved), legacy-file compatibility, and the
legacy-collision error path.

… names collide

save_matrices keyed all per-entity matrices by bare entity name:
'{name}_sky_matrix' (TwoPhase, views AND sensors), '{name}_window_matrix'
(ThreePhase: views, sensors AND surfaces; FivePhase: views and sensors) and
'{name}_sun_direct_matrix' (FivePhase). When two groups share a name -- e.g.
a view and a sensor both named after the zone -- the later group overwrote
the earlier one in the .npz, and load_matrices restored the wrong array into
the survivor without any error. Downstream this manifests as degenerate
view matrices and noise-floor DGP values with no exception.

Namespace the keys per group (view_/sensor_/surface_) and stamp the file
with '_schema_version' = 2. Legacy (v1) files remain loadable when their
entity names do not collide; a colliding legacy file now raises ValueError
with a pointer to regenerate, instead of silently returning corrupt data.

Adds tests: roundtrips with colliding names for all three phase methods
(shapes and contents preserved), legacy-file compatibility, and the
legacy-collision error path.
Copilot AI review requested due to automatic review settings July 15, 2026 09:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes silent matrix-cache corruption when view/sensor/surface entity names collide by namespacing cache keys per group and introducing a cache schema marker to distinguish legacy files.

Changes:

  • Add schema v2 cache format with group-namespaced keys (view_, sensor_, surface_) and write _schema_version = 2 into newly saved caches.
  • Update matrix-cache load paths to support both v2 caches and collision-free legacy (v1) caches; legacy caches with colliding names now raise ValueError instead of silently loading corrupt data.
  • Add regression tests covering v2 roundtrips with colliding names across Two/Three/Five-phase methods plus legacy compatibility/error-path behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
frads/methods.py Introduces schema v2 namespacing for matrix-cache keys, adds _schema_version, and updates load/save logic to avoid collisions and detect legacy caches.
tests/test_matrix_cache.py Adds regression tests for collision-safe roundtrips and legacy cache handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread frads/methods.py
Comment on lines +562 to +563
if "_schema_version" in mdata:
return "view_", "sensor_", "surface_"
@taoning

taoning commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Thanks. I'd probably try to do something cleaner and bump the cache format that would require users to regenerate all the matrices. I will keep this open for now while working on a solution.

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.

3 participants