Repository navigation
Description
Summary
pamica does not implement the Fortran reference's rank-deficiency handling. amica15.f90 detects the numerical rank of the data covariance, reduces the model to that rank, and keeps a pseudo-inverse for mapping components back to sensor space. pamica does none of the three, so rank-deficient input (Maxwell-filtered MEG, average-referenced or interpolated EEG) either dies with nan_ll or silently fits a full-rank model to rank-deficient data.
Reported by an external user applying pamica to Maxwell-filtered Elekta MEG (306 channels, numerical rank ~70) in #221.
What the Fortran does
Three pieces, all in pamica/amica15.f90:
-
Automatic rank detection (line 395):
numeigs = min(pcakeep, count(eigs > mineig))
mineig = 1.0e-15by default (amica15_header.f90:66). This fires regardless ofpcakeep, so rank deficiency is caught even with default settings. -
Model dimension follows the kept rank (line 545):
nw = numeigsAMICA then estimates
numeigssources, notnx. -
Pseudo-inverse of the sphere for back-projection (lines 550-560):
Spinvis(nx, numeigs), computed by SVD, and maps the decomposition back to the originalnxsensor space.
What pamica does
- No
mineigequivalent anywhere (grep -rn mineig pamica/*.pyis empty). n_channelsis taken from the input row count (amica.py:207) and passed to the backend (amica.py:225);n_comps = n_channels * n_models(torch_impl/core.py:541). Nothing ever resizes it to the kept rank.- No
Spinv.get_mixing_matrix()returns A in the sphered space, and callers must de-sphere by hand.
Reproducer
Real sample EEG projected onto its top-20 subspace (what SSS does to MEG):
X = load_eeglab_data("pamica/sample_data/eeglab_data.fdt", 32, 30504, dtype=np.float32).astype(np.float64)
X = X - X.mean(1, keepdims=True)
U = np.linalg.svd(X, full_matrices=False)[0]
X_rd = U[:, :20] @ (U[:, :20].T @ X) # 32 channels, rank 20
AMICA(verbose=False).fit(X_rd, max_iter=30, seed=0)
# converged_=False stop_reason_='nan_ll' at iteration 0The symmetric ZCA sphere V diag(1/sqrt(lambda)) V^T inverts sqrt(lambda ~ 0) in the null directions. The nan_ll stop (#50) correctly refuses the model, but the Fortran would never have got here: it would have set numeigs = 20 and fitted a 20-source model.
Current pcakeep/pcadb do not cover this:
pcakeep=20alone keeps the symmetric ZCA form, so the sphere stays(32, 32)at rank 20 and AMICA still estimates 32 components from rank-20 data. No NaN, but 12 components are spurious.pcakeep=20, do_approx_sphere=Falseis the genuinely dimension-reducing path and crashes:RuntimeError: The size of tensor a (20) must match the size of tensor b (32) at non-singleton dimension 0_preprocessreturns(20, T)fromsphere = inv_sqrt @ V.Twhile every parameter is sized 32.
Proposed fix
- Port
mineig(default1e-15) and computenumeigs = min(pcakeep, count(eigs > mineig))in_preprocess. - Set the model dimension from
numeigsrather thanX.shape[0], son_compsfollows the kept rank. This fixes thedo_approx_sphere=Falsecrash as a side effect. - Carry
Spinvon the model and haveget_mixing_matrix()return sensor-space maps when reduction is active, instead of requiring callers to de-sphere manually.
Note for step 1: mineig is an absolute threshold on covariance eigenvalues, so it is unit-dependent. EEG in microvolts gives eigenvalues of order 1-100 and the default works; MEG in Tesla gives ~1e-26 and every eigenvalue falls below 1e-15, which would collapse numeigs to 0. Matching Fortran exactly means inheriting that. Worth considering a relative fallback (eigs > mineig * eigs.max()) alongside the parity-faithful absolute one, and it is a further argument for channel-type scaling before sphering (#221).
Related
- Support for Maxwell-filtered Elekta MEG data #221 (the MEG report that surfaced this)
- AMICA wrapper marks is_fitted_ even on a degenerate (nan_ll/singular_ll) fit #50 (the degenerate-fit contract that catches the NaN today)
Activity
Add Newton coverage: 3-model, rank-reduced, three-way (#284)