Skip to content

Rank-deficient input: no mineig detection, model dimension not reduced to numeigs #223

Closed

Description

@neuromechanist

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:

  1. Automatic rank detection (line 395):

    numeigs = min(pcakeep, count(eigs > mineig))

    mineig = 1.0e-15 by default (amica15_header.f90:66). This fires regardless of pcakeep, so rank deficiency is caught even with default settings.

  2. Model dimension follows the kept rank (line 545):

    nw = numeigs

    AMICA then estimates numeigs sources, not nx.

  3. Pseudo-inverse of the sphere for back-projection (lines 550-560): Spinv is (nx, numeigs), computed by SVD, and maps the decomposition back to the original nx sensor space.

What pamica does

  • No mineig equivalent anywhere (grep -rn mineig pamica/*.py is empty).
  • n_channels is 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 0

The 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=20 alone 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=False is 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
    
    _preprocess returns (20, T) from sphere = inv_sqrt @ V.T while every parameter is sized 32.

Proposed fix

  1. Port mineig (default 1e-15) and compute numeigs = min(pcakeep, count(eigs > mineig)) in _preprocess.
  2. Set the model dimension from numeigs rather than X.shape[0], so n_comps follows the kept rank. This fixes the do_approx_sphere=False crash as a side effect.
  3. Carry Spinv on the model and have get_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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions