Repository navigation
[core] Stop mutating caller EEG and fix ICA sign-flip invariant (Fable 5 Phase 2) - #202
Conversation
…iants Part of the Fable 5 Findings epic (#193); addresses issue #195. Make side-effect-free functions actually side-effect-free by copying at entry, and fix the ICA sign-normalization invariant break. - Deep-copy EEG at entry in eeg_runica, eeg_amica, load_amica_model, eeg_picard, pop_select, pop_headplot, pop_clean_rawdata, clean_windows and clean_asr; copy the data array at entry in runica and clean_asr; build pop_saveset's MATLAB structures from copies so the caller keeps 0-based urchan/urevent; deep-copy the clean_artifacts high-pass snapshot. - eeg_rpsd no longer reseeds the global NumPy RNG (uses a local RandomState). - coords_to_mm / coords_RAS_to_ALS no longer mutate the caller's array. - eeg_from_data no longer silently transposes a tall 2-D array on a shape>shape guess; it raises and asks for nbchan when orientation is ambiguous. - posact sign-flips now negate the matching icaweights row and icawinv column directly instead of recomputing icaweights via pinv(icawinv), which had folded the non-identity sphere into the weights; icasphere is left untouched, preserving icawinv == pinv(icaweights @ icasphere) and icaact == icaweights @ icasphere @ data. eeg_amica and eeg_picard now flatten epoched data in Fortran order to match eeg_runica and EEGLAB. - Adds regression tests asserting inputs are unchanged and the ICA invariants hold.
|
🤖 Specification (PR >500 LOC; full per-finding detail in #195, epic #193) Problem. Many functions that should be side-effect-free mutated the caller's live EEG dict/arrays: Approach. Copy-on-entry discipline ( Tests. Adds regression tests per fix: the caller's Note. For |
|
Claude finished @suraj-ranganath's task in 3m 21s —— View job Code review
I reviewed the source diff for correctness, EEGLAB parity, import hygiene, and the regression tests. The copy-on-entry discipline and the ICA fix look correct. BlockingNone. ImportantNone. Nits
Test gapsNone that block. The added regressions are meaningful and non-tautological — EEGLAB parity notes
Checks performed
|
The mutation regression test snapshotted icaweights via list(), which breaks when the shared tests/fixtures.py fixture (Phase 1) provides a pre-ICA dataset with icaweights=None. Assert the caller's ICA fields are the exact same objects after the call instead, which is what 'does not mutate' means and works for both a None and an array-valued icaweights.
clean_asr now returns a fresh dataset; reword the three comments that still described it mutating EEG in place (AGENTS.md: delete stale comments in touched code).
Make side-effect-free functions actually side-effect-free by copying at entry across pop_saveset, pop_select, pop_headplot, eeg_runica/eeg_amica/eeg_picard, runica, clean_asr, clean_windows, clean_artifacts, pop_clean_rawdata, eeg_rpsd and the BIDS coords helpers, so operations a researcher believes are read-only no longer corrupt the in-memory dataset. The ICA posact sign-flip now negates the icaweights row directly instead of recomputing via pinv(icawinv), preserving the icaweights/icasphere factorization; eeg_amica and eeg_picard flatten epoched data in Fortran order to match eeg_runica. eeg_from_data raises instead of silently transposing ambiguous 2-D input. Adds input-unchanged and ICA-invariant regression tests.
Fixes #195