Skip to content

Phase 10: write the sphere column-major - #337

Merged
neuromechanist merged 11 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-336-phase10-sphere-order
Sep 23, 2026
Merged

neuromechanist merged 11 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-336-phase10-sphere-order

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Summary

write_amicaout (pamica/numpy_impl/load.py), the writer shared by every backend's
write_amica_output (AMICATorchNG, AMICAMLXNG, the AMICA wrapper) and by the
NumPy backend's own _write_results, wrote the square sphere matrix S in C order,
while the Fortran reference (amica15.f90 near :2423) and both readers
(EEGLAB's loadmodout15.m and pamica's loadmodout) read it column-major.
The default symmetric zero-phase component analysis (ZCA) sphere is its own transpose to about 1e-17,
which is why this was invisible until do_approx_sphere=False produced a genuinely asymmetric sphere.
The exported sphere then came back exactly transposed.

S is now written order="F" in both the square and rank-reduced branches of write_amicaout,
and load_results reads a square sphere the same way.
The rank-reduced (padded) branch was already correct; only the square branch and its reader counterpart changed.
The incorrect "byte-identical to the Fortran reference" rationale for the old C-order write is replaced
with a comment stating the actual rule: S is column-major, like every other EEGLAB-read 2-D file here
(c, alpha, mu, sbeta, rho, comp_list).
A is the one 2-D file that stays C order, and for a different reason: loadmodout15.m never reads it
(it derives A from W and S instead), so its on-disk layout is only a pamica-internal
writer/reader contract, not the Fortran/EEGLAB one.

Existing output directories: a full-rank directory written with do_approx_sphere=False by an
earlier pamica has S transposed. EEGLAB always read that transposed already (this fix does not touch
EEGLAB's own reading); pamica's corrected loadmodout/load_results now also read it transposed, with
no error raised, since there is no on-disk version marker and this repo carries no compatibility shim.
Re-run write_amica_output to regenerate any such directory. The default (symmetric) sphere and a
rank-reduced (pcakeep) fit were unaffected.

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

Before / after (bundled sample, torch, 3 iterations, do_approx_sphere=False)

before after
max|S_loaded - S| 0.51 0.0
max|S_loaded - S.T| 0.0 0.51

Independent orientation check against the real Fortran binary

pamica/tests/test_native_engine.py::test_native_engine_asymmetric_sphere_reads_the_right_orientation
runs AMICANative with do_approx_sphere=0 (a plain kwarg the engine forwards into input.param;
amica15.f90's param parser has a matching case('do_approx_sphere') branch,
amica15.f90:3448-3455, over the exact sphering path, amica15.f90:495-508) on the real bundled sample,
against the reference binary itself, with no dependence on pamica's writer at all. Measured:

quantity value
max|S - S.T| 0.296 (genuinely asymmetric)
max|S @ cov @ S.T - I| 1.9e-14 (whitens, float64 round-off)
max|S.T @ cov @ S - I| 135 (does not whiten -- the check is not vacuous)

where cov is the population covariance of the mean-centered data (/N, matching Fortran's
DSYRK/cnt and torch.cov(..., correction=0), not NumPy/torch's default /(N-1)).

Test plan

  • Replaced test_square_sphere_bytes_are_unchanged with test_sphere_is_written_and_read_column_major,
    parametrized over do_approx_sphere, with an explicit asymmetry assertion so the test cannot pass vacuously.
  • Strengthened the existing rank-reduced tests in test_amicaout_reduced.py with exact byte-level
    and loadmodout round-trip assertions against the fitted sphere.
  • Added pamica/tests/test_sphere_export_order.py: a cross-backend (torch, NumPy, MLX) round-trip test,
    parametrized over do_approx_sphere and pcakeep, checking loadmodout
    (and, for NumPy, load_results) against each backend's own reported sphere, bit-exactly
    (assert_array_equal) on every backend including MLX (the file holds a float32-cast sphere
    upcast to float64, and that upcast is exact). MLX's get_sphere() -- a different, higher-precision
    float64 sphere computed before the float32 cast -- is checked separately with an explicitly-labeled
    assert_allclose (measured max_abs=1.4e-8, max_rel=5.9e-8, unrelated to the column-major bug).
  • Added test_native_engine_asymmetric_sphere_reads_the_right_orientation in test_native_engine.py:
    an independent orientation check against the real Fortran binary (see table above), gated the same
    way as the file's other end-to-end tests (PAMICA_NATIVE_BINARY).
  • Updated test_write_amica_output_bytes in test_amica_ng_wrapper.py to read S column-major.
  • Confirmed the new/changed tests fail on the pre-fix code (verified by restoring the pre-fix
    load.py/data.py): 8 failures --
    test_amicaout_reduced.py::test_sphere_is_written_and_read_column_major[True],
    [False];
    test_amica_ng_wrapper.py::test_write_amica_output_bytes;
    test_sphere_export_order.py::test_loadmodout_sphere_matches_backend[torch-True-full],
    [torch-False-full], [numpy-True-full], [numpy-False-full], [mlx-False-full].
    The reduced (pcakeep) cells and mlx-True-full cannot fail pre-fix: the padded branch was
    already column-major, and MLX's float32 cast rounds the ZCA sphere's ~1e-17 asymmetry to exactly
    zero (a C-order read of a bit-symmetric matrix is indistinguishable from a column-major one there).
  • Full export/persistence suite across all backends (test_amicaout_reduced.py, test_sphere_export_order.py,
    test_mlx_export.py, test_amica_ng_wrapper.py, test_ng_backend.py, test_numpy_outdir.py,
    test_numpy_cli.py, test_numpy_share_comps.py, test_sample_data.py,
    test_sensor_mixing_cross_backend.py, test_numpy_viz.py, test_numpy_restarts.py,
    test_native_engine.py): all green.
  • uv run ruff check --fix . && uv run ruff format ., uv run ty check, uv run typos on changed files: all clean.
  • uv run pytest pamica/tests -q --no-cov -n 4 -m "not slow" (full suite, AMICA_RUN_FORTRAN=1 and
    PAMICA_NATIVE_BINARY set): 1401 passed, 4 skipped, 1 xfailed, 0 failed (775.87s).
  • docs/changelog.md, docs/guides/validation.md and docs/guides/eeglab.md updated to describe the
    actual column-major rule for S, name the shared writer and its call sites, and warn about
    regenerating existing do_approx_sphere=False output.

@neuromechanist
neuromechanist merged commit 353a748 into feature/issue-324-epic-mlx-first-class Sep 23, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-336-phase10-sphere-order branch September 23, 2026 04:11
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