Skip to content

Phase 6: validation harness per backend and end-to-end workflow - #332

Merged
neuromechanist merged 20 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-315-phase6-e2e-validation
Sep 23, 2026
Merged

neuromechanist merged 20 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-315-phase6-e2e-validation

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Summary

validate_implementations.py --backend {torch,numpy,mlx} (or a comma-separated list, or all) compares each backend against one shared Fortran reference run with the same canonical settings; the default stays torch and prints the same report as before (diffed).
NumPy receives the settings through its own _CANONICAL_TO_NUMPY_KEY table, PyTorch and MLX through AMICA(backend=...); an explicit --backend also prints and saves a per-backend summary with runtimes (parity_summary.md), and --backend mlx without MLX exits with status 2 and the install hint.
The harness now resolves its bundled inputs from its own location, so it runs from any working directory.

New pamica/tests/mne_tests/test_end_to_end_workflow.py mirrors our own pipeline on the bundled EEG for torch and MLX: average reference, AMICAICA with pcakeep = n_channels - 1, get_sources vs transform, a one-component exclusion (exact back-projection, residual kept, rank drops by one), EEGLAB export reloaded by loadmodout with the padded sphere, AMICA.save/load, an input.param-driven fit, and torch vs MLX Hungarian-matched sources >= 0.999 (measured 0.999999999). Not marked slow (about 2 to 5 s).

Fix: AMICA_NumPy.get_sensor_mixing_matrix was missing the transpose the other two backends apply, so its columns were rows of the true mixing matrix (about 10% off the torch maps after five iterations, and not an inverse of get_weights() @ sphere); a new cross-backend test pins the agreement (4.6e-12 full rank, 9.5e-11 at pcakeep=20).

Behavior change (legacy backend): AMICA_NumPy now writes files only when given an outdir (default None, like torch and MLX); an explicit outdir (keyword, params file, or the CLI's --outdir, whose default stays output) writes exactly as before.

Fix: the legacy pamica.numpy_impl.viz helpers had the same orientation slip (stored-A columns drawn as maps; activations from raw data with no transpose, sphere or mean); they now plot what get_sensor_mixing_matrix and transform return, and load_results reads a rank-reduced fit's padded sphere.

Docs: a short Apple Silicon (MLX) route in docs/getting-started.md that links to the backends guide's full workflow; per-backend harness usage and parity rows in docs/guides/validation.md; docs/guides/amica-differences.md records do_sphere=False (reference scales channels, pamica does not; #328) and NumPy refit warm-start (related to #312); AGENTS.md, changelog, native-backend and testing pages updated.

Cleanups: 35 British spellings fixed in 18 files; seven torch_impl/core.py:<line> citations (plus a stale pamica.py:802-813) converted to symbol references; the suite no longer writes into the repository (a session-scoped conftest.py fixture runs each session from a temporary directory, and the slow tests write under tmp_path); stale 2025 debug logs at the repository root removed.

Part of epic #324; resolves #315 (closes when the epic merges to dev).

Parity rows (harness defaults: seed 42, max_iter 100, sample_params.json; Apple M4 Pro, 2026-09-22; reference: v0.3.3 native amica15-macos-arm64)

Backend Precision Final LL LL difference from Fortran Mean matched corr Min matched corr Amari distance Runtime (s) Expected bar
Fortran (reference) float64 -3.411274 10.0 reference
PyTorch (AMICA) float64 -3.411245 0.000029 0.9992 0.9935 0.0037 17.7 corr > 0.95, Amari < 0.05, LL diff < 0.005
NumPy (AMICA_NumPy) float64 -3.411246 0.000029 0.9992 0.9934 0.0037 32.8 PyTorch bar, LL within 1e-5 of PyTorch
MLX (AMICA(backend="mlx")) float32 -3.411242 0.000032 0.9992 0.9935 0.0037 3.4 PyTorch bar, LL within 1e-4 of PyTorch

Test plan

  • uv run ruff check --fix . && uv run ruff format ., uv run ty check, typos: clean
  • uv run pytest pamica/tests -q --no-cov -n 4 -m "not slow": 1374 passed, 17 skipped, 1 xfailed; the session working directory is empty afterwards (nothing in the suite writes to its working directory any more)
  • validate_implementations.py --backend all against the v0.3.3 native binary: all three backends meet their bar (table above); default run output diffed identical to the pre-change run
  • AMICA_RUN_FORTRAN=1 per-backend parity test (test_backend_meets_its_parity_bar_against_fortran): 3 passed
  • Slow and Fortran-gated tests whose output paths changed (test_ng_backend end-to-end x2, test_ng_pdf_families x5, test_sample_data_cli, test_sample_data_scikit): 7 passed, 2 xfailed as before
  • New sensor-mixing test fails without the NumPy fix (4 cases) and passes with it
  • New test_numpy_outdir.py (default writes nothing, explicit and params-file outdir write as before; fails on the old code) and a CLI test that a run without --outdir still writes ./output
  • New test_numpy_viz.py compares the plotting helpers' maps, histograms and reconstructions with the live accessors, full rank and pcakeep=20 (10 passed; 8 fail on the old code)
  • Harness: literal default run pinned (one report, no summary), format_parity_summary rendered from real runs, no-MLX exit over torch,mlx and all
  • mkdocs build --strict (scratch docs environment); new anchors checked in the built site

neuromechanist and others added 19 commits September 22, 2026 18:05
validate_implementations.py gains --backend {torch,numpy,mlx}, a comma-separated list, or all. Each backend runs the bundled sample with the harness's canonical params (NumPy through its own key map, MLX through AMICAMLXNG) against one shared reference run; an explicit --backend adds a parity summary table with runtime. The default torch-only run prints the same report as before (diffed).

Tested: dispatch and argument-parsing tests (no binary), and the AMICA_RUN_FORTRAN-gated per-backend parity test against the v0.3.3 native binary (3 passed).
With Phase 4 merged, the torch and MLX runs share one AMICA(backend=...) path, so the MLX row exercises the wrapper a user calls. Numbers unchanged (same summary table as the direct AMICAMLXNG run).

Tested: harness dispatch tests (29 passed); all-backend run against the v0.3.3 native binary.
35 British spellings in comments, docstrings and Markdown (behaviour, colour, neighbour, cancelled, licence, optimiser, ...). Proper nouns (Centre de Recherche...) and 'analogue' left as is; no identifiers touched.

Tested: ruff, typos.
Seven torch_impl/core.py:<line> citations outside the MLX module pointed at drifted lines; they now name the method (or are dropped where the sentence already names it). Also replaces a stale pamica.py:802-813 citation (a file renamed in #34) in AMICATorchNG._newton_direction. Fortran line citations unchanged.

Tested: ruff.
getting-started gains a complete Apple Silicon (MLX) section (install, fit/transform/save/load, params file, AMICAICA, precision). The validation guide documents running the harness per backend and records the per-backend parity rows with their expected bars. The differences page records do_sphere=False (#328) and NumPy refit warm-start (#312), both verified against the code. AGENTS.md, changelog and the native-backend and testing pages follow.

Tested: mkdocs build --strict (scratch env); anchors checked in the built site.
An audit of the non-slow suite found 63 tests (13 files) writing ./output via AMICA_NumPy's default outdir; the slow CLI, end-to-end and pdf-family tests wrote test_output, _ng_e2e_tmp and scratch_amica_parity into the repo. A session-scoped autouse fixture now runs each session (per xdist worker) from a temporary directory, the harness resolves its bundled inputs from its own location, and the slow tests write under tmp_path. Stale .gitignore entries removed.

Tested: full non-slow suite (1335 passed, nothing written to the repo); coverage reports still land in the repo root with and without xdist.
New mne_tests/test_end_to_end_workflow.py mirrors our own pipeline on the bundled EEG: average reference, AMICAICA with pcakeep = n_channels - 1 on each backend, get_sources vs transform, a single-component exclusion (exact back-projection, residual kept, rank drops by one), EEGLAB export reloaded by loadmodout with the padded sphere, AMICA save/load, an input.param-driven fit, and torch vs MLX Hungarian-matched sources >= 0.999. Not marked slow (about 5 s); MLX tests skip without MLX or an Apple GPU.

Tested: 12 passed locally (torch and MLX); measured tolerances recorded in the module.
AMICA_NumPy.get_sensor_mixing_matrix returned pinv(sphere) @ stored A without the transpose the torch and MLX backends apply (the stored W is the unmixing transposed, issue #24), so its columns were rows of the true mixing matrix: about 10% off the torch maps after five iterations and no inverse of get_weights() @ sphere. New cross-backend test pins torch/NumPy agreement (4.6e-12 full rank, 9.5e-11 at pcakeep=20), MLX agreement at float32 tolerance, and the identity on every backend.

Tested: new test fails without the fix (4 NumPy cases) and passes with it (10 passed); backend-guard and share-comps suites green.
pytorch_debug_test/out.txt and pytorch_output_test/out.txt were run logs committed in August 2025 by the long-removed basic PyTorch backend; nothing reads them. The only reference, a typos exclusion for the first directory, goes with them.

Tested: typos.
States why the rows name an explicit release binary instead of the resolver default (its cached 'latest' is never refreshed) and how to fetch it.

Tested: mkdocs build --strict.
AMICA_NumPy defaulted outdir to ./output, so every fit wrote out.txt at construction, writestep checkpoints, history and its final results into the caller's working directory. The default is now outdir=None, which writes nothing (as torch and MLX never do unless asked); an explicit outdir (keyword, params file, or the CLI's --outdir, whose own default stays output) writes exactly as before. Behavior change recorded in the changelog and the NumPy API page; the conftest chdir stays as defense in depth.

Tested: new test_numpy_outdir.py (default leaves the working directory empty with every write path on; explicit and params-file outdir write as before; fails on the old code) and a CLI test that a run without --outdir still writes ./output; NumPy suites (107 passed).
plot_components drew stored-A columns (rows of the true mixing matrix) as mixing vectors, and it and plot_pdf_fits built activations as stored W times raw data (no transpose, sphere or mean); plot_model_comparison skipped the sphere. One helper, _component_maps_and_sources, now computes exactly what get_sensor_mixing_matrix and transform return, and all three plots use it. load_results reads a rank-reduced fit's zero-padded sphere instead of raising.

Tested: new test_numpy_viz.py compares the helper and the plotted lines/histograms with the live accessors, full rank and pcakeep=20 (10 passed; 8 fail on the old code).
Pins the literal default run (no --backend: one validation_report.txt, no parity_summary.md, exit 0), renders format_parity_summary with populated rows from two real runs (torch as the stand-in reference, NumPy through compare_results) and checks every numeric column, and runs the no-MLX exit over both 'torch,mlx' and 'all'.

Tested: 32 passed, 3 skipped (the AMICA_RUN_FORTRAN-gated parity test).
unlabelled and relabelled, missed by the first sweep's word-boundary match. Test names containing 'honour' (identifiers) are left as is.

Tested: typos, ruff.
The MLX section duplicated the backends guide's end-to-end and precision sections; it is now install, AMICA(backend="mlx") fit/transform, an AMICAICA one-liner and the float32 note, linking to the guide for the full workflow. The install heading no longer uses a bare GPU abbreviation.

Tested: mkdocs build --strict; linked anchors checked.
progress_summary.md's harness line now matches AGENTS.md (every backend, #315), and the testing guide explains the conftest session chdir and how a test resolves repository paths.

Tested: mkdocs build --strict.
default_reference_binary's docstring is now the single full description of the reference-binary resolution order; the --native-engine help summarizes it and points there. The native-backend page runs the harness with uv run.

Tested: --help renders; harness tests (32 passed).
The NumPy-vs-torch sensor-map gate (1e-9) failed on the macOS arm64 runner at 1.3e-9: the backends start within 1e-13 and EM amplifies the gap about sixfold per iteration, so five iterations was platform-sensitive. Two iterations keep the same gate with a 1e-12 gap while the pre-fix transposed maps still differ by 2.6-3%. Tested locally: module passes.
@neuromechanist
neuromechanist merged commit e38aa11 into feature/issue-324-epic-mlx-first-class Sep 23, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-315-phase6-e2e-validation branch September 23, 2026 02:55
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