Repository navigation
Handle rank-deficient input across all backends - #224
Conversation
Update: folded backend parity and documentation inPer review direction, this is no longer a PyTorch-only change. All backends, one shared policyRank detection now lives in
Default changed to a relative floor
The absolute floor is unit-dependent, and it is not only MEG that suffers. Average-referenced EEG -- an everyday step -- sits at Well-conditioned data is unaffected: real EEG sits at 5e-4, eight orders above the floor. Re-verified bit-identical against main after the refactor. Documentation
The differences table is the answer to "which ones are different and why, in one look", and it is now the documented home for any future divergence. |
Closes #223.
Ports the Fortran reference's numerical-rank handling, which pamica had dropped. Rank-deficient input (Maxwell-filtered MEG, average-referenced or interpolated EEG) previously died with
nan_llon iteration 0, or silently fitted a full-rank model to rank-deficient data. Reported in #221 by a user applying pamica to 306-channel Elekta MEG at numerical rank ~70.What Fortran does, and now we do
numeigs = min(pcakeep, count(eigs > mineig))mineig, default1e-15nw = numeigsn_channelsfixed fromX.shape[0]Spinv(nx, numeigs)get_sensor_mixing_matrix()The reduced sphere follows Fortran's construction exactly, including the
do_approx_spherebranch that symmetrizes the reduced whitening by the orthogonal polar factor of the leadingnumeigsblock ofV^T(amica15.f90:483-490).Parity
Full-rank data keep every eigenvalue, so the reduction path never fires and the existing code paths are untouched. Verified bit-identical before/after on the bundled sample EEG across single-model, Newton-enabled, and two-model fits (log-likelihood, sphere sum, mixing-matrix sum, and the first five LL-history entries all compare equal by
repr).Improving on the reference
mineigis an absolute eigenvalue floor, so it is unit-dependent. Two consequences, both handled:MEG in Tesla has covariance eigenvalues ~1e-26, all below
1e-15. Fortran would computenumeigs = 0and proceed; pamica raises aValueErrornaming the cause and the fix rather than fitting an empty model.The absolute floor is a poor rank detector, because numerical-zero eigenvalues straddle it. On real EEG projected to rank 20:
mineig=1e-15(Fortran default)mineig_rel=1e-9mineig_relis opt-in and replaces the absolute floor when set, so the default stays Fortran-exact while the better detector is one argument away.Also fixed
pcakeep=N, do_approx_sphere=False— the genuinely dimension-reducing path — raisedRuntimeError: The size of tensor a (N) must match the size of tensor b (n_channels)._preprocessreturned(N, T)while every parameter was still sized to the input channel count. Covered by a regression test.Tests
pamica/tests/torch_tests/test_ng_rank_deficient.py, 7 tests, real sample EEG only. Rank deficiency is produced by projecting the real recording onto its own leading subspace — the same rank-reducing linear operator SSS applies to MEG — so no synthetic data is involved.Follow-ups (not in this PR)
numpy_implandmlx_implstill lack rank handling.mne_compatsets a unit pre-whitener (ica.pre_whitener_ = np.ones((n_ch, 1))), so mixed magnetometer/gradiometer data are not channel-type scaled. Since the scaling determines which directions survive rank truncation, this matters for MEG; tracked in Support for Maxwell-filtered Elekta MEG data #221.