Skip to content

Phase 3: full-decomposition cross-backend equivalence - #93

Merged
neuromechanist merged 6 commits into
feature/issue-84-epic-xplat-benchmarkfrom
feature/issue-87-phase3-decomp-equiv
Jul 9, 2026
Merged

neuromechanist merged 6 commits into
feature/issue-84-epic-xplat-benchmarkfrom
feature/issue-87-phase3-decomp-equiv

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Adds benchmarks/benchmark_decompose.py: fits every backend to a full decomposition (2000
iters) on real EEG and compares the recovered ICs across backends -- the epic's headline
"result" deliverable. Answers whether pyAMICA is a drop-in EEGLAB AMICA replacement.

Closes #87
Part of epic #84

What changed

  • benchmark_decompose.py -- per-backend fit + extract (time, LL, unmixing W, mixing A);
    --compare Hungarian-matches W (sign/permutation-invariant) across backends. Two figures:
    a square cross-backend equivalence matrix, and MNE IC scalp-map topomaps ordered by
    back-projected variance (EEGLAB convention, IC1 = highest variance).
  • Tests pyAMICA/tests/test_decompose_equiv.py -- the matcher recovers corr=1.0 under
    permutation + sign flip, and low for unrelated components (no data/GPU/binary needed).
  • Findings + figures in .context/issue-84/phase3_decomp_equivalence.md.

Results (real ds002718, 147k frames = k30 at 70ch, 2000 iters)

  • Device- and precision-invariant: all six torch/MLX backends (torch-cpu, cuda, mps, MLX ×
    f32, f64) recover identical ICs (cross-backend |corr| = 1.000). Same answer on any device
    at any precision.
  • Equivalence tracks the k factor: 0.997 at k=574 (16ch) down to 0.898 at k=30 (70ch);
    native-Fortran matches the torch/MLX cluster fully at high k, and differs only on the weakly
    determined k=30 components (clock-seed local optimum, not a bug -- Fortran-vs-Fortran is 0.972).
  • Total decompose time @70ch: MLX ~5 min, CUDA ~6, Fortran ~10, torch-cpu ~30, MPS ~35.

Test plan

  • uv run pytest pyAMICA/tests/test_decompose_equiv.py -- 3 pass.
  • Validated end-to-end on both machines (Mac: MLX/Fortran/MPS/torch-cpu; hallu: Fortran/CUDA):
    52 backend x channel runs, figures generated. ruff clean.

Follow-ups (issues filed)

@neuromechanist

Copy link
Copy Markdown
Member Author

Review addressed (code + comment, Sonnet)

Both agents verified the W/A extraction conventions empirically (reconstructed x = A @ (W @ x) to ~1e-16 across all three backends). Fixed:

  • Critical (topomap): the scalp map indexed the wrong axis AND used whitened-space A. Now indexes column j and de-spheres (recompute symmetric-ZCA sphere from the data, project W, pinv(U)), so the topomaps are true EEGLAB-style sensor-space scalp maps -- they went from scrambled to proper dipolar topographies. Equivalence table was unaffected (uses W).
  • _compare n<2 crash (single result at the top channel count, a real merge scenario) -> guarded with a clear message + early return.
  • Doc fixes: removed the nonexistent --dtypes flag reference, corrected the "typically native-fortran" reference-backend comment (it's glob order), and split the Fortran timing by machine (hallu ~4.4 min vs Mac ~10 min; added the hallu torch-cpu-f64 ~67 min oversubscription note).

Everything else (W conventions, _match_correlation, _variance_order sphere, thread save/restore, the headline/k-factor numbers) verified correct. 3 tests pass; ruff clean.

@neuromechanist
neuromechanist merged commit cd1a3d8 into feature/issue-84-epic-xplat-benchmark Jul 9, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-87-phase3-decomp-equiv branch July 9, 2026 02:47
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