Repository navigation
Phase 10: write the sphere column-major - #337
Merged
neuromechanist merged 11 commits intoSep 23, 2026
Merged
neuromechanist merged 11 commits into
neuromechanist merged 11 commits into
Conversation
neuromechanist
merged commit Sep 23, 2026
353a748
into
feature/issue-324-epic-mlx-first-class
8 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
write_amicaout(pamica/numpy_impl/load.py), the writer shared by every backend'swrite_amica_output(AMICATorchNG,AMICAMLXNG, theAMICAwrapper) and by theNumPy backend's own
_write_results, wrote the square sphere matrixSin C order,while the Fortran reference (amica15.f90 near :2423) and both readers
(EEGLAB's
loadmodout15.mand pamica'sloadmodout) 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=Falseproduced a genuinely asymmetric sphere.The exported sphere then came back exactly transposed.
Sis now writtenorder="F"in both the square and rank-reduced branches ofwrite_amicaout,and
load_resultsreads 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:
Sis column-major, like every other EEGLAB-read 2-D file here(
c,alpha,mu,sbeta,rho,comp_list).Ais the one 2-D file that stays C order, and for a different reason:loadmodout15.mnever reads it(it derives
AfromWandSinstead), so its on-disk layout is only a pamica-internalwriter/reader contract, not the Fortran/EEGLAB one.
Existing output directories: a full-rank directory written with
do_approx_sphere=Falseby anearlier pamica has
Stransposed. EEGLAB always read that transposed already (this fix does not touchEEGLAB's own reading); pamica's corrected
loadmodout/load_resultsnow also read it transposed, withno error raised, since there is no on-disk version marker and this repo carries no compatibility shim.
Re-run
write_amica_outputto regenerate any such directory. The default (symmetric) sphere and arank-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)max|S_loaded - S|max|S_loaded - S.T|Independent orientation check against the real Fortran binary
pamica/tests/test_native_engine.py::test_native_engine_asymmetric_sphere_reads_the_right_orientationruns
AMICANativewithdo_approx_sphere=0(a plain kwarg the engine forwards intoinput.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:
max|S - S.T|max|S @ cov @ S.T - I|max|S.T @ cov @ S - I|where
covis the population covariance of the mean-centered data (/N, matching Fortran'sDSYRK/cntandtorch.cov(..., correction=0), not NumPy/torch's default/(N-1)).Test plan
test_square_sphere_bytes_are_unchangedwithtest_sphere_is_written_and_read_column_major,parametrized over
do_approx_sphere, with an explicit asymmetry assertion so the test cannot pass vacuously.test_amicaout_reduced.pywith exact byte-leveland
loadmodoutround-trip assertions against the fitted sphere.pamica/tests/test_sphere_export_order.py: a cross-backend (torch, NumPy, MLX) round-trip test,parametrized over
do_approx_sphereandpcakeep, checkingloadmodout(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 sphereupcast to float64, and that upcast is exact). MLX's
get_sphere()-- a different, higher-precisionfloat64 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).test_native_engine_asymmetric_sphere_reads_the_right_orientationintest_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).test_write_amica_output_bytesintest_amica_ng_wrapper.pyto readScolumn-major.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 andmlx-True-fullcannot fail pre-fix: the padded branch wasalready 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).
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 typoson changed files: all clean.uv run pytest pamica/tests -q --no-cov -n 4 -m "not slow"(full suite,AMICA_RUN_FORTRAN=1andPAMICA_NATIVE_BINARYset): 1401 passed, 4 skipped, 1 xfailed, 0 failed (775.87s).docs/changelog.md,docs/guides/validation.mdanddocs/guides/eeglab.mdupdated to describe theactual column-major rule for
S, name the shared writer and its call sites, and warn aboutregenerating existing
do_approx_sphere=Falseoutput.