Skip to content

Write and read reduced-rank spheres correctly - #227

Merged
neuromechanist merged 2 commits into
mainfrom
164-reduced-amicaout
Aug 15, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
164-reduced-amicaout

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

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/loadmodout under genuine num_pcs < data_dim" — and fixes the two bugs writing that test exposed.

The on-disk convention

Fortran always writes S at recl = 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 first numeigs rows. Both readers — loadmodout here and EEGLAB's loadmodout15.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) wrote n_kept*nx values where EEGLAB expects nx*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. loadmodout applies EEGLAB's conventions — rows variance-ordered via origord, A columns normalized to unit norm with the norm folded into W — 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.sources has 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.py and test_sample_data.py (the other loader consumers) pass unchanged.

@neuromechanist
neuromechanist merged commit 8bb1aa4 into main Aug 15, 2026
7 checks passed
@neuromechanist
neuromechanist deleted the 164-reduced-amicaout branch August 15, 2026 22:03
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.

fit() crashes with do_approx_sphere=False + pcakeep (PCA-reduced non-symmetric sphere)

1 participant