Repository navigation
Write and read reduced-rank spheres correctly - #227
Merged
Merged
Conversation
Merged
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.
Closes #164. The crash half of that issue was fixed by #223/#224; this completes its second acceptance criterion — "a round-trip test for
sources/loadmodoutunder genuinenum_pcs < data_dim" — and fixes the two bugs writing that test exposed.The on-disk convention
Fortran always writes
Satrecl = 2*nbyte*nx*nx(amica15.f90:2299). The array is allocated(nx, nx)and zero-filled (amica15.f90:334), and a rank-reduced sphere occupies only its firstnumeigsrows. Both readers —loadmodouthere and EEGLAB'sloadmodout15.m— reshape to(nx, nx)and slice[:num_pcs].Two bugs
Writer emitted the wrong number of values. A reduced sphere is
(n_kept, nx), so_w("S", sphere)wroten_kept*nxvalues where EEGLAB expectsnx*nx. Now padded to(nx, nx)before writing.Reader reshaped in the wrong order.
S.reshape(nx, nx)is C-order; Fortran and MATLAB store column-major. Invisible for the symmetric-ZCA sphere — which is why it survived this long — but it puts a reduced sphere's zero pad in the wrong half.Byte compatibility preserved
The square branch deliberately keeps its C-order write. The ZCA sphere is its own transpose only to ~1e-17, so switching that branch to column-major would move bytes that are guaranteed identical to the Fortran reference (issue #92). A test asserts both the unchanged C-order bytes and that the sphere is not bit-symmetric, so the guard cannot silently become vacuous.
What the round-trip actually asserts
Not element-wise equality with
transform.loadmodoutapplies EEGLAB's conventions — rows variance-ordered viaorigord,Acolumns normalized to unit norm with the norm folded intoW— so loaded sources equal live ones under a row permutation and a per-component scale. Both are conventional; ICA sources are order- and scale-arbitrary.What must hold exactly, and is what a transposed or mis-padded sphere would break, is that the scale is constant along each row. Measured spread: 9e-14.
Worth recording: my first draft asserted element-wise equality and failed. Checking the full-rank case showed it failed identically (best-match correlation 1.0000000000 under a permutation), so the premise was wrong, not the code —
AmicaOutput.sourceshas never returned rows in the model's internal order.Tests
pamica/tests/test_amicaout_reduced.py, 4 tests, real sample EEG projected onto its own leading subspace.test_viz.pyandtest_sample_data.py(the other loader consumers) pass unchanged.