Skip to content

Add annotation x rank-reduction and sphere-guard tests - #282

Merged
neuromechanist merged 3 commits into
devfrom
feature/issue-261-test-residue
Aug 23, 2026
Merged

neuromechanist merged 3 commits into
devfrom
feature/issue-261-test-residue

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #261.

Adds the two automated tests the fresh-context reviews of #252 and #256 flagged as residual (both judged low-probability, neither blocking merge at the time).

pamica/tests/mne_tests/test_annotation_rank_reduction.py fits AMICAICA with reject_by_annotation=True combined with rank reduction (explicit pcakeep and automatic mineig_rel numerical-rank detection) on the bundled annotated eeglab_data.set, pinning that the fit completes, n_components_ reflects the reduced rank, good_sample_mask_/get_model_probability stay timeline-aligned with NaN at rejected samples, pmi() matches a manual computation, and the exported MNE ICA's pca_components_ is rank-sized and orthonormal -- the exact production configuration the external MEG tester runs by hand on Maxwell-filtered rank-deficient data plus bad_* annotations.

pamica/tests/torch_tests/test_ng_sharing.py gets a direct test that AMICATorchNG.get_sensor_mixing_matrix() raises the same clear RuntimeError on a non-finite sphere that the internal _identify_shared_comps() route already pinned; this is the twin of the NumPy backend's existing test_non_finite_sphere_fails_loudly, which already covered this route for AMICA_NumPy.

Non-blocking defect found (not fixed here; test-only PR)

While prototyping the auto-rank variant, AMICAICA(...).mir() on a fit whose rank reduction came from automatic mineig_rel detection (no explicit pcakeep/pcadb) raises an opaque numpy.linalg.LinAlgError: Last 2 dimensions of the array must be square instead of the intended, documented ValueError ("mir() is incompatible with PCA reduction").

Root cause: AMICATorchNG._pca_reduced() (pamica/torch_impl/core.py) only checks self.pcakeep is not None or self.pcadb is not None, not the sphere's actual shape, so an auto-detected rank reduction slips past the guard. Repro:

import numpy as np, mne
from pamica.mne_compat import AMICAICA
raw = mne.io.read_raw_eeglab("pamica/sample_data/eeglab_data.set", preload=True)
picked = raw.copy().pick("data", exclude="bads")
x = picked.get_data(); x -= x.mean(axis=1, keepdims=True)
u = np.linalg.svd(x, full_matrices=False)[0][:, :16]
raw_low = mne.io.RawArray(u @ (u.T @ x), picked.info.copy())
fitted = AMICAICA(device="cpu", verbose=False).fit(raw_low, max_iter=12)
fitted.mir(raw_low)  # LinAlgError instead of the documented ValueError

No test was written pinning this failure mode, since that would encode the bug as expected behavior; left for a follow-up fix.

Test plan

  • uv run pytest pamica/tests/mne_tests/ pamica/tests/torch_tests/test_ng_sharing.py -q --no-cov (99 passed)
  • uv run pytest -m "not slow" -n auto (487 passed, 21 skipped, 1 xfailed)
  • uv run ruff check . / uv run ruff format --check . / uv run ty check (all clean)

@neuromechanist
neuromechanist merged commit d5eca61 into dev Aug 23, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-261-test-residue branch August 23, 2026 17:52
This was referenced Aug 24, 2026
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.

Follow-up test coverage from the PR #252/#256 review residuals

1 participant