Skip to content

Write CLI results in Fortran binary format (#30) - #40

Merged
neuromechanist merged 4 commits into
mainfrom
30-legacy-cli-saveload-format-mismatch-npy-vs-fortran-binary
Jul 5, 2026
Merged

neuromechanist merged 4 commits into
mainfrom
30-legacy-cli-saveload-format-mismatch-npy-vs-fortran-binary

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

The legacy NumPy CLI wrote results as .npy, but loadmodout (and the tests/comparison path) expect raw Fortran-format binaries with no extension, so the round-trip raised FileNotFoundError for W. This converges pyAMICA on the single Fortran-binary format.

Changes

  • AMICA._write_results now writes raw little-endian float64 (int32 comp_list) files (gm, A, W, S, mean, c, alpha, mu, sbeta, rho, comp_list, LL) in loadmodout's byte layout, so pyAMICA output is directly comparable to and loadable by the same reader as the Fortran amicaout. comp_list is written 1-based (Fortran convention).
  • load_results now reads that raw-binary format back into AMICA's internal array shapes for the amica_viz helpers (the compressed flag is a documented no-op; the format is never .npz).
  • Removed the dead amica_data.save_results (.npy/.npz writer, no callers) to finish converging on one format.
  • Fixed a pre-existing broken import in amica_viz.plot_pdf_fits (from amica_pdf → from .amica_pdf).
  • Added a fast, non-slow smoke test test_cli_output_format_roundtrip covering the format round-trip (loadmodout + load_results shapes) and all three viz helpers on real data (also viz coverage toward Test coverage gaps: AMICA.save/load and plot_components #15).

Verification

  • Round-trip: fit → _write_results → loadmodout Hungarian-matches the Fortran reference W at min corr 0.95 / 32-of-32 bijective (150-iter fit; the full run is higher).
  • New smoke test passes (2.4s); non-slow suite green (33 passed, 1 xfailed).

Honest status on test_sample_data_cli

The #30 format bug is fixed, but I kept test_sample_data_cli (the full 2000-iter CLI-vs-Fortran integration test) xfail: with the format fixed it now gets past the load step, but the NumPy backend diverges to a non-finite likelihood ~iter 687 on the sample data and stops early, so the degraded result no longer matches Fortran. That is a separate long-fit numerical-stability bug, filed as #39 (the torch AMICATorchNG default backend is unaffected; the NumPy backend is stable and matches Fortran at ~150 iters per the #37 parity test). The xfail reason now points at #39, and the format contract itself is covered by the new smoke test.

Related

The legacy NumPy CLI wrote results as .npy, but loadmodout (and the
tests/comparison path) expect raw Fortran-format binaries with no
extension, so the round-trip raised FileNotFoundError for 'W'. Converge
on the Fortran format:

- AMICA._write_results now writes raw little-endian float64 (int32
  comp_list) files (gm, A, W, S, mean, c, alpha, mu, sbeta, rho,
  comp_list, LL) in loadmodout's layout, making pyAMICA output directly
  comparable to and loadable by the same reader as the Fortran amicaout.
  Verified: loadmodout round-trip Hungarian-matches the Fortran reference
  W at min corr 0.95 / 32-of-32 bijective (150-iter fit; the full CLI run
  is higher). comp_list is written 1-based (Fortran convention).
- load_results now reads that raw-binary format back into AMICA's internal
  array shapes for the amica_viz helpers (the compressed flag is a
  documented no-op; the format is never .npz).
- Fix a pre-existing broken import in amica_viz.plot_pdf_fits
  (from amica_pdf -> from .amica_pdf).
- Un-xfail test_sample_data_cli; add a fast non-slow smoke test covering
  the format round-trip (loadmodout + load_results shapes) and the viz
  helpers on real data (also viz coverage for #15).

Tested: new smoke test + non-slow suite green (33 passed, 1 xfailed which
is the unrelated under-convergence test_sample_data_light).
amica_data.save_results wrote the .npy/.npz format and had no callers; it
is the counterpart to the old load_results and is now inconsistent with
the raw-binary format everything reads. Removing it finishes converging on
the single Fortran-binary format.
The #30 format fix is verified by test_cli_output_format_roundtrip. The
full 2000-iter CLI integration test still fails because the NumPy backend
diverges to a non-finite likelihood ~iter 687 (separate bug #39), so keep
it xfail with an accurate reason pointing at #39 rather than #30.
@neuromechanist neuromechanist linked an issue Jul 4, 2026 that may be closed by this pull request
- AMICA.fit now does an unconditional (finite-guarded) final _write_results,
  so a run whose last iteration is not a writestep multiple still persists
  the converged result; the finite guard avoids overwriting good output with
  a #39 NaN state.
- Scope the _write_results "Fortran byte layout" claim to num_models==1 in
  the docstring/comments; multi-model output is self-consistent (loadmodout
  round-trips) but not byte-identical to Fortran (noted, ref #27).
- load_results raises a clear FileNotFoundError on an incomplete output
  directory instead of a cryptic downstream KeyError in a viz helper.
- Update the stale `compressed` docstrings in the viz helpers (legacy no-op).
- Smoke test now relies on fit's final write (exercises that path) and adds
  value-level round-trip asserts (loaded W/A/alpha/comp_list == in-memory),
  guarding transpose/axis-order regressions a shape check would miss.
- Add test_cli_subprocess_output_loadable: runs the real amica_cli entrypoint
  on a short stable config and confirms loadmodout reads the output (the
  direct #30 regression).

Tested: non-slow suite 34 passed, 1 xfailed.
@neuromechanist

Copy link
Copy Markdown
Member Author

Review (3 agents, Sonnet) + resolutions

Findings addressed in b20ba66:

Addressed

  • No value-level round-trip assertion (test, sev 7): the smoke test checked only shapes, so a transpose/axis-order bug in the W byte layout would pass. Added value asserts: loaded W/A/alpha/comp_list == the in-memory arrays. (The comment reviewer independently confirmed the round-trip is byte-exact for W/A.)
  • No real CLI-path test (test, sev 6): added test_cli_subprocess_output_loadable, which runs the actual amica_cli entrypoint (the thing that raised FileNotFoundError) on a short stable config and asserts loadmodout reads it. Fast (~5s), non-slow.
  • No final write after fit (silent-failure / test): _write_results only fired on writestep boundaries, so a run whose last iteration isn't a writestep multiple never persisted the converged result. AMICA.fit now does an unconditional final write, finite-guarded so a AMICA_NumPy long fit hits non-finite likelihood (NaN) ~iter 687 on sample data #39 NaN run doesn't overwrite the last good output. The smoke test now relies on this path.
  • Multi-model layout claim false (code-review, Important): the C-order dump is byte-identical to Fortran only for num_models==1 (the CLI/sample case). Scoped the docstring/comments honestly; multi-model output is self-consistent (loadmodout round-trips) but not byte-identical to Fortran (noted, relates to Multi-model NG partition matching vs Fortran (>0.95 cross-corr) #27).
  • load_results silently accepts incomplete dirs (silent-failure): now raises a clear FileNotFoundError listing the missing core files instead of a cryptic downstream KeyError in a viz helper.
  • Stale compressed docstrings (silent-failure): updated across the viz helpers to note it's a legacy no-op.

Skipped (with rationale)

  • _write_results write atomicity (silent-failure): a crash mid-write leaves a partial dir. Pre-existing (the old .npy writer had the same), and temp-file+rename is a broader change; the new load_results incomplete-dir check now at least fails loudly on such a dir. Worth a separate follow-up.
  • num_comps != nw*num_models (PCA reduction) shape derivation (both reviewers, low): only affects the PCA-reduced path, which is pre-existing and non-functional (data_dim is never updated after reduction, unrelated to this diff). Out of Legacy CLI save/load format mismatch (.npy vs Fortran binary) #30 scope.
  • test_sample_data_cli has no raises=: it can't distinguish the AMICA_NumPy long fit hits non-finite likelihood (NaN) ~iter 687 on sample data #39 NaN from a writer regression, but the new value asserts + CLI subprocess test now guard writer regressions directly, so the slow xfail doesn't need to.

Non-slow suite after fixes: 34 passed, 1 xfailed (the unrelated under-convergence test).

@neuromechanist
neuromechanist merged commit fa9b667 into main Jul 5, 2026
@neuromechanist
neuromechanist deleted the 30-legacy-cli-saveload-format-mismatch-npy-vs-fortran-binary branch July 5, 2026 02:15
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.

Legacy CLI save/load format mismatch (.npy vs Fortran binary)

1 participant