Skip to content

Add pop_runica icatype pamica through eeg_pamica - #617

Merged
neuromechanist merged 17 commits into
developfrom
feature/pamica-backend
Nov 14, 2026
Merged

neuromechanist merged 17 commits into
developfrom
feature/pamica-backend

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

pop_runica(EEG, icatype='pamica') decomposes with pAMICA's PyTorch engine through the new eeg_pamica, fills the EEGLAB ICA fields, and records the run in EEG['etc']['pamica'].
The fit runs in float64 on the CPU with one thread and seed 5489 by default, so the same seed gives identical weights (tested with numpy.array_equal).
icaweights @ icasphere is the unmixing pAMICA applies, icasphere is get_sphere(), icawinv is its pseudo-inverse, and pop_runica sorts components by variance by default, as for the other engines, reorder='off' keeps pAMICA's order, and eeg_pamica called directly keeps pAMICA's order by default.
did_converge is true exactly when pAMICA's stop_reason_ is neither max_iter nor degenerate, and it never reads converged_; a degenerate fit raises PamicaFitError and sets no ICA field.
A missing pamica or torch raises ImportError naming pip install "eegprep[pamica]".
New optional extra pamica = ["pamica==0.4.0"], part of all; the lock moves torch 2.12.0 to 2.12.1 (pAMICA's floor) and adds pamica and json5.
Docs: the pop_runica help, the ICA and installation user guide pages, and the changelog.
Card: PX-03.

Interpretations to confirm:

  • Settings not passed take eegprep's runamica15 defaults as runamica._AMICA_DEFAULTS states them, for every name pAMICA's torch backend takes whose value matters in a run without rejection: the nine named in the plan plus max_decs (pAMICA maxdecs), newtrate, rholratefact, invsigmin and invsigmax. block_size stays pAMICA's 131072. Rank and pcadb are left to pcakeep.
  • icaact is icaweights @ icasphere @ data as for the other engines, so it differs from pAMICA's transform by one constant per component (pAMICA removes the channel mean). With do_mean=False the two are equal within 1e-10, and the test checks both.
  • .github/workflows/test.yml sets EEGPREP_REQUIRE_AMICA on the macOS job, not the ubuntu job. The ubuntu-22.04 and Windows jobs skip every AMICA test today (AMICA binary not functional on this platform, in all six ubuntu jobs of the last four runs), while the macOS job runs them. Where the variable is set, the E1 test fails instead of skipping.

Rule E1 is a test (test_e1_reports_pamica_against_amica_on_the_sample_recording, marked slow, 2 hour timeout) that prints one line E1 min=... median=... amari=... pamica_s=... amica_s=... | reference min>=0.95 median>=0.99 amari<=0.05 and asserts nothing about the numbers (see "E1 is informational").
It skips with "the shipped AMICA binary cannot run on this host" when the binary cannot run, and is not run locally: the shipped macOS binary is x86_64.
The macOS job of this pull request prints the E1 line.
Measured here, not E1: two pAMICA runs differing only in seed on sample_data/eeglab_data.set (2000 iterations, 32 channels, about 6.5 minutes each, one thread) give min 0.526, median 0.936, Amari 1.51 with the EEGLAB-valued defaults, and min 0.944, median 0.999, Amari 0.234 with pAMICA's own defaults.

E1 is informational:

  • The macOS job ran E1 twice on the same code. The first attempt printed E1 min=0.9966 median=1.0000 amari=0.0473, which meets the reference values, and the rerun printed E1 min=0.5339 median=0.9842 amari=0.9963, which does not.
  • The shipped AMICA binary seeds its random start from the clock, so the comparison depends on that start. pamica itself is deterministic for a fixed seed.
  • The owner decided to accept this: E1 runs and prints its line without asserting, and the job still fails if the AMICA binary cannot run where EEGPREP_REQUIRE_AMICA is set.
  • The decomposition is computed by the pamica package, not by EEGLAB's AMICA binary, so EEG['etc']['pamica'] now also records backend (the pamica class that ran, AMICATorchNG) and pipeline (pamica.AMICA(backend='torch').fit) beside the version and the settings that reproduce the run. A test rebuilds the fit from the record and gets numpy.array_equal weights.
  • The docs claim only what the tests check: identical weights for a repeated run and for a replay from the record, on one host, in float64 on the CPU with one thread. Another platform, torch version, GPU or thread count is not covered.

Review of 2026-11-14 (eegprep's Claude review, which saw head 04e8de4):

  • Important, E1 will most likely fail macOS CI and adds 13+ minutes to each macOS job. Done: E1 is informational (it prints its line and asserts nothing, see "E1 is informational"), and it now runs in its own job E1 report (macOS) (needs: lint, macos-latest, Python 3.12, the test job's checkout and uv sync --python 3.12 --all-extras --group dev, EEGPREP_REQUIRE_AMICA=1, running only test_e1_reports_pamica_against_amica_on_the_sample_recording). The test job deselects that test with --deselect, so the macOS test job is back to its old length (about 6 to 8 minutes in earlier runs, about 30 minutes with E1 inside), and every run still prints the E1 line. The thresholds, the statistics helper and the defaults are unchanged.
    On head 55ec52f (run 43630266238) the main macOS leg took 6 minutes (7 minutes 28 seconds, 8 minutes and 6 minutes 10 seconds in three earlier runs) and the E1 job took 28 minutes 36 seconds and printed E1 min=0.9995 median=1.0000 amari=0.0218 pamica_s=690.2 amica_s=942.0 | reference min>=0.95 median>=0.99 amari<=0.05; every job of that run passed.
  • Nit, ordering claim in the user guide. Fixed in an earlier round: pop_runica sorts components by variance by default, reorder="off" keeps pAMICA's order, and eeg_pamica called directly keeps it by default.
  • Parity note, runamica15 settings with no counterpart in the mapping. The eeg_pamica docstring now lists them, checked against _AMICA_DEFAULTS and pAMICA 0.4.0's constructor: lratefact, rholrate, minrho, maxrho and newt_ramp are pAMICA arguments whose values equal eegprep's; numrej (pAMICA maxrej, 1 against 3), pcadb, blk_min, blk_step and blk_max differ and keep pAMICA's values; decwindow and 26 other names (write*, update_*, load_* and similar) have no pAMICA argument.

Gates run on this branch:

  • uv sync --python 3.12 --all-extras --group dev
  • ruff check . and ruff format --check .
  • ty check
  • uv lock --check
  • ./pre-commit.py --changed-from origin/develop
  • pytest tests/test_pop_runica_pamica.py tests/test_eeg_amica.py: 42 passed, 2 skipped (E1 and the AMICA binary tests)
  • pytest tests/test_package_help.py tests/test_optional_extras.py tests/test_public_api_examples.py: 28 passed, 1 deselected
  • each test was shown to fail when the line it guards is mutated (the two thread-limit lines each keep torch at one thread alone on the development host, so only removing both fails a test)

Not run: the full pytest tests (CI runs it) and the EEGLAB reference suite (eeglab-reference.yml); pop_runica gains one branch and no existing path changes.
tests/test_sample_data_pop_functions.py has four ..._without_warning_noise failures on the macOS development host from numpy's matmul warnings in clean_asr; they fail identically on develop.
Not changed: eeg_pamica is not in the lazy exports or the API autosummary, and EEG['etc']['pamica'] is not copied back when chanind or a dataset list is used (the same holds for etc['amica']).

EEGLAB reference run (AGENTS.md)

AGENTS.md asks for one run of .github/workflows/eeglab-reference.yml on the branch of a core-function change; pop_runica gains one additive branch here.
The run for this branch (https://github.com/sccn/eegprep/actions/runs/43633199456, on 55ec52f) was cancelled once the cause was known from the run of a sibling branch cut from the same develop commit (https://github.com/sccn/eegprep/actions/runs/43631767812): all seven shards fail at test setup with Failed: EEGLAB is at 8ac485f654d6bbb1a6acb8dc9ef3f2eaf3d409ba; expected pinned d0963be9d81acc5b66e84f8d3d7c89e3ce127cc4 (for example core: 2273 passed, 7 skipped, 127 errors).
Cause: it is on develop, not in this pull request: develop records the src/eegprep/eeglab submodule at 8ac485f6 while tests/eeglab_tests/__init__.py pins d0963be9 (issue #622). This pull request changes neither value.
The owner, as the human approver, decided on 2026-11-14 to merge with this explanation recorded.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Code review skipped — your organization has no extra usage available to pay for this review.

If your organization's extra usage balance is empty, an organization admin can add extra usage credits at claude.ai/admin-settings/usage. If its monthly spend limit was reached, an admin can raise it on the same page. If neither applies, contact Anthropic support.

Once extra usage is available, someone with write access to this repository can comment @claude review on this pull request to trigger a review.

@claude

claude Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @neuromechanist's task in 1m 35s —— View job


Code review

  • Overall assessment: The wrapper is clean and correct. I checked icaweights @ icasphere against transform, the pcakeep shapes (they match AMICA's loadmodout15 k×k / k×nchan layout), raising before any ICA field is set on a degenerate stop, restoring the thread limit, the install error naming the extra, and the history command. Two things to settle before merge: E1 is now a test that fails CI on macOS, and the user guide gets the ordering wrong.
  • Highest-risk area: the E1 test now runs and can fail on every macOS CI job.
  • Merge recommendation: Needs more context. Wait for the macOS E1 result before merging.

Blocking

None.

Important

  • E1 will most likely fail on macOS CI (inline), at tests/test_pop_runica_pamica.py:367-371 and .github/workflows/test.yml:218.
    • Why it's run: CI runs pytest tests with no slow filter, and EEGPREP_REQUIRE_AMICA=1 turns a skip into a failure.
    • Why it may fail: the PR body reports that two pAMICA runs differing only in seed give Amari 1.51 / min 0.526 with these EEGLAB-valued defaults. pAMICA is not stable across a seed change, so getting within Amari ≤ 0.05 of Fortran AMICA, which starts from a different initialization, looks unlikely. That leaves a check that stays red, and each macOS job grows by roughly 13 minutes or more.
    • Fix: look at the E1 line printed by this PR's macOS job. If it fails, move E1 to an opt-in job such as workflow_dispatch or eeglab-reference.yml, or revisit the defaults (the body shows pAMICA's own defaults are far more seed-stable). Don't touch the thresholds.

Nits

  • Ordering claim in the user guide (inline), at docs/source/user_guide/ica_rejection.rst:59. It says components stay in pAMICA's order unless reorder="on". But pop_runica defaults to reorder='on', so the example right above it returns variance-sorted components. The PR body makes the same claim. Only eeg_pamica itself keeps pAMICA's order by default.

Test gaps

None. The tests exercise real fits, and the replay through etc['pamica'] is a good way to check that the record is complete.

EEGLAB parity notes

  • Mapping the defaults from _AMICA_DEFAULTS is reasonable. EEGLAB runamica15 settings with no pAMICA counterpart in this mapping (lratefact, rholrate, minrho, maxrho, newt_ramp, decwindow) fall back to pAMICA's own values. I couldn't check pAMICA 0.4.0's backend signature because pamica isn't installed in this runner, so the claim that "these are the only ones that matter" rests on the author's check. Listing the unmapped names in the eeg_pamica docstring would make that explicit for users.
  • Per AGENTS.md (Testing), pop_runica is a core function. The new branch is additive and doesn't change the existing paths, but whether eeglab-reference.yml is still needed is for the human approver to decide.

Checked for correctness bugs, EEGLAB parity, data structure compatibility, changed-behavior tests, and CLAUDE.md/AGENTS.md compliance.

A setting you do not pass takes the value EEGLAB's ``runamica15`` uses, such as ``lrate=0.05`` and ``do_newton=True``.
The exception is ``block_size``, which is pAMICA's own 131072, because EEGLAB's 128 is the block each thread of the Fortran program processes.
Pass ``pcakeep`` to keep fewer components than channels.
``icaweights @ icasphere`` is the unmixing pAMICA applies, and components stay in pAMICA's order unless ``reorder="on"`` sorts them by variance.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (docs vs. behavior): pop_runica defaults to reorder='on' (

options: Any = None,
reorder: str | bool = "on",
chanind: Any = None,
), so the example call just above (pop_runica(EEG, icatype="pamica", gui=False, max_iter=2000)) returns components sorted by variance, not in pAMICA's order. As written, this sentence suggests the opposite. pAMICA's own order is kept only through eeg_pamica directly (sortcomps='off') or with reorder="off".

Suggested change
``icaweights @ icasphere`` is the unmixing pAMICA applies, and components stay in pAMICA's order unless ``reorder="on"`` sorts them by variance.
``icaweights @ icasphere`` is the unmixing pAMICA applies. ``pop_runica`` sorts components by variance by default; pass ``reorder="off"`` to keep pAMICA's order.

Comment thread tests/test_pop_runica_pamica.py Outdated
Comment on lines +367 to +371
def test_e1_pamica_matches_amica_on_the_sample_recording(capsys):
if not is_amica_available():
if os.environ.get("EEGPREP_REQUIRE_AMICA") == "1":
pytest.fail("EEGPREP_REQUIRE_AMICA=1 but the shipped AMICA binary cannot run on this host")
pytest.skip("the shipped AMICA binary cannot run on this host")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important (CI risk): CI runs pytest tests with no -m "not slow" filter, and the workflow now sets EEGPREP_REQUIRE_AMICA=1 on macOS (

- name: Run Python tests
# The shipped AMICA binary launches on the macOS runner and not on the ubuntu-22.04 or Windows
# runners. Where it launches, the E1 test in tests/test_pop_runica_pamica.py fails instead of skipping.
env:
EEGPREP_REQUIRE_AMICA: ${{ runner.os == 'macOS' && '1' || '' }}
run: |
uv run --no-sync pytest tests
). So this 2-hour-budget test runs on every macOS job, and if it fails the job goes red. The PR body's own measurement makes a pass doubtful: two pAMICA runs that differ only in seed, with these EEGLAB-valued defaults, give min 0.526, median 0.936, Amari 1.51. If pAMICA is that far from itself under a seed change, a run from a different initialization (Fortran AMICA) is unlikely to land within Amari ≤ 0.05.

Suggested fix: wait for the macOS E1 line on this PR before merging. If it fails, don't keep a known-failing required check. Either move E1 to an opt-in job (e.g., workflow_dispatch or the reference workflow) or reconsider the defaults. Pre-registered thresholds must not be loosened.

@neuromechanist
neuromechanist merged commit edf694c into develop Nov 14, 2026
11 checks passed
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.

1 participant