Skip to content

Phase 3: shared params-file reader for every backend - #327

Merged
neuromechanist merged 11 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-304-phase3-params-surface
Sep 22, 2026
Merged

neuromechanist merged 11 commits into
feature/issue-324-epic-mlx-first-classfrom
feature/issue-304-phase3-params-surface

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

pamica/fortran_params.py gains read_params_file, the single params-file entry point every backend now uses: it content-sniffs JSON vs. the literal Fortran input.param text and returns pamica's canonical keys either way, applying one alias table (JSON_ALIAS_TO_CANONICAL) to a JSON file's own schema spellings (min_grad_norm, max_decs, numrej, num_mix_comps, share_int); a file carrying both an alias and its canonical key raises ValueError naming both.

writestep/do_history/histstep move from FORTRAN_UNSUPPORTED_KEYS to translated (identity) keys: the reader now translates what any pamica backend can honor (the legacy NumPy backend already implements periodic on-disk checkpointing under these names), and each consumer is responsible for warning about what it cannot apply.

AMICA.from_params_file (the PyTorch wrapper) now calls read_params_file instead of its own inline JSON/Fortran sniffing, so sample_params.json's own max_decs/min_grad_norm/share_int settings are now applied (see "Behavior changes" below).

AMICA_NumPy (pamica/numpy_impl/core.py) routes both the bundled default params.json and a user-supplied params_file through read_params_file, maps the canonical keys to this backend's own attribute spellings (min_nd -> min_grad_norm, maxdecs -> max_decs, share_iter -> share_int), and reads files/data_dim/field_dim off the same parsed dict instead of a second raw json.load. A params-file setting this backend does not consume is named in one logger.warning. The classmethod from_json_file is renamed to from_params_file (no alias left behind), and pdftype != 0 now raises NotImplementedError at construction -- this backend implements only the generalized-Gaussian source density and previously silently ignored the setting; numpy_impl/params.json's bundled default pdftype changed 1 -> 0 to match.

validate_implementations.py's own JSON-schema-to-Fortran-keyword alias table (_FORTRAN_ALIASES) and its max_decs special case are now derived from the shared reader (fortran_params.PAMICA_KEY_TO_FORTRAN_KEY) instead of duplicating the knowledge; load_sample_data reads sample_params.json through read_params_file too.

pamica/native/engine.py is left untouched: its _DEFAULT_PARAMS are correctly Fortran-spelled (out of scope per the design), and its _ALIASES table (n_models/n_mix -> Fortran keywords) maps from the wrapper-level constructor kwarg names, not pamica's canonical params-file keys, so deriving it from the shared table would not be a direct, behavior-preserving swap -- see "Deviations / notes for the lead" below.

Docs updated: docs/guides/amica-differences.md (backend table row + pdftype guard note), docs/guides/validation.md (read_params_file, the JSON alias table, the writestep/do_history/histstep move, the NumPy backend's own params-file/pdftype behavior), docs/api/numpy-backend.md (renamed classmethod, Fortran support, pdftype guard), docs/changelog.md ("Unreleased").

Part of #304 (MLX path lands with Phase 4, #313). Part of epic #324.

Behavior changes

  • AMICA.from_params_file (PyTorch wrapper) now applies sample_params.json's own max_decs/min_grad_norm/share_int settings (as maxdecs/min_nd/share_iter) instead of silently dropping them into the "not applied" warning.
  • AMICA_NumPy(params_file=...) now accepts the literal Fortran input.param text format, not just JSON (previously raised a raw json.JSONDecodeError).
  • AMICA_NumPy's unconsumed params-file settings are now named in one logger.warning instead of silently vanishing.
  • Breaking (legacy NumPy backend): AMICA_NumPy.from_json_file is renamed to from_params_file; no alias left behind.
  • Breaking (legacy NumPy backend): AMICA_NumPy(pdftype=...) with anything other than 0 now raises NotImplementedError at construction. numpy_impl/params.json's bundled default pdftype changed 1 -> 0; since the value was never read by the fit path, no previously-passing fit's numerics change.
  • validate_implementations.py --help / its own JSON-vs-Fortran comparison harness: min_nd/share_iter (from sample_params.json's min_grad_norm/share_int) now reach AMICATorchNG automatically via the canonical-keyed params dict (previously silently landed in the harness's own "ignored" warning while, on the Fortran side, write_fortran_param_file already wrote them -- an uncontrolled-comparison gap this closes as a side effect, not a regression).

Test plan

  • uv run ruff check --fix . && uv run ruff format . -- clean
  • uv run ty check -- clean
  • uv run pytest pamica/tests/test_fortran_params.py pamica/tests/test_fortran_param_forwarding.py pamica/tests/test_sample_data.py pamica/tests/test_numpy_params_file.py -q -- 85 passed, 3 xfailed (pre-existing, unrelated), 0 failed
  • uv run pytest -q -n auto (full suite) -- in progress at PR-open time; will report the final count separately
  • uv run python validate_implementations.py --help -- still works
  • Anti-drift: read_params_file(sample_params.json) and read_params_file(input.param) agree on 49 shared keys (>= 47 gate), the only difference being files
  • JSON alias/canonical conflict, non-object JSON top level, and unparseable non-JSON text each raise ValueError
  • AMICA_NumPy from input.param equals AMICA_NumPy from sample_params.json on every shared hyperparameter attribute
  • AMICA_NumPy(params_file=<input.param>, max_iter=3, ...).fit() with no explicit data runs (chdir to sample_data/, matching the Fortran binary's own path resolution)
  • AMICA_NumPy(pdftype=1|2|3|4) raises NotImplementedError; pdftype=0 constructs
  • AMICA.from_params_file(sample_params.json) applies maxdecs=3/min_nd=1e-7/share_iter=100 to the fitted AMICATorchNG, and the wrapper's "not applied" warning no longer names max_decs/min_grad_norm/share_int
  • AMICA_NumPy's unconsumed-key warning names kurt_start/num_kurt/kurt_int/num_samples for input.param; writestep/do_history/histstep from Fortran text reach AMICA_NumPy's attributes

read_params_file (issue #304) is the single params-file entry point
every backend should use: content-sniffs JSON vs Fortran input.param
text and returns canonical pamica keys either way, aliasing a JSON
file's schema spellings (min_grad_norm/max_decs/numrej/num_mix_comps/
share_int) via one table. A file with both an alias and its canonical
key raises ValueError.

writestep/do_history/histstep move from FORTRAN_UNSUPPORTED_KEYS to
translated (identity) keys: the reader translates what any backend
supports, not just the wrapper; the legacy NumPy backend already
implements periodic checkpointing under these names.
Replaces the wrapper's own inline JSON/Fortran content sniffing with
the shared reader (issue #304). Behavior change: sample_params.json's
own max_decs/min_grad_norm/share_int settings are now applied (as
maxdecs/min_nd/share_iter) instead of silently landing in the "not
applied" warning.
AMICA_NumPy(params_file=...) and load_default_params now read through
read_params_file (issue #304) instead of a bare json.load, so the
legacy backend accepts the literal Fortran input.param text too, not
just JSON. Canonical keys map to this backend's own spellings
(min_nd -> min_grad_norm, maxdecs -> max_decs, share_iter ->
share_int); files/data_dim/field_dim come off the same parsed dict
instead of a second file read. A params-file setting this backend
does not consume is named in one logger.warning.

from_json_file is renamed to from_params_file (matches the PyTorch
wrapper's name, now accepts both formats); no alias left behind.

pdftype != 0 now raises NotImplementedError at construction: this
backend implements only the generalized-Gaussian source density and
previously silently ignored the setting. numpy_impl/params.json's
bundled default pdftype changes 1 -> 0 to match; the value was never
read by the fit path, so no previously-passing fit's numerics change.
_FORTRAN_ALIASES is now derived from fortran_params.
PAMICA_KEY_TO_FORTRAN_KEY (the shared reader's single source of truth
for pamica-key -> Fortran-keyword) plus the one JSON-only share_int
addition, instead of a hand-maintained duplicate. load_sample_data
reads sample_params.json through read_params_file, so min_nd/maxdecs/
share_iter land in run_pytorch_amica's ng_kwargs via the plain
_NG_PARAMS filter; the max_decs special case is no longer needed.
docs/guides/validation.md: read_params_file, the JSON alias table, the
writestep/do_history/histstep move, and the NumPy backend's own
params-file/pdftype behavior.

docs/guides/amica-differences.md: the backend-differences table's
Fortran input.param reader row (NumPy now yes, MLX awaits epic #324
Phase 4) and the NumPy pdftype NotImplementedError.

docs/api/numpy-backend.md: the renamed from_params_file classmethod
and the pdftype guard.

docs/changelog.md: Unreleased entry covering the shared reader, the
wrapper's now-applied JSON aliases, NumPy's input.param support, the
from_json_file rename, and the NumPy pdftype guard.
cli.py's load_params() did a bare json.load, so the CLI died with
JSONDecodeError on a Fortran input.param even though AMICA(params_
file=...) already accepted it. It now calls the same
_read_numpy_keyed_params helper the constructor uses (no duplicated
key-mapping table), so both formats work; files stays unresolved
relative to the working directory, matching Fortran-binary semantics.

New test_numpy_cli.py exercises main() end to end on real bundled data
for both formats; verified to fail with JSONDecodeError on the
pre-fix load_params().
Replaces the hand-added share_int entry with a composition of
JSON_ALIAS_TO_CANONICAL (JSON alias -> canonical) and
PAMICA_KEY_TO_FORTRAN_KEY (canonical -> Fortran keyword), so
write_fortran_param_file accepts either raw JSON-schema keys or
canonical keys with no hand-maintained entry left in this module.

Adds coverage: canonical maxdecs/min_nd are written as Fortran
max_decs/min_grad_norm, and an end-to-end test feeds load_sample_
data()'s canonical-keyed params straight into write_fortran_param_
file, asserting the written file carries Fortran's own spellings and
none of the canonical ones.
NumPy: explicit kwargs win over params-file defaults (mirrors the
PyTorch wrapper's precedence test). Wrapper: fitting from input.param
names writestep/do_history/histstep in the "not applied" warning,
since AMICATorchNG has no matching constructor keyword (issue #312).
Pins FORTRAN_TO_PAMICA_KEY/FORTRAN_UNSUPPORTED_KEYS table sizes so a
future move between them forces the docstring/doc counts to be
revisited.
fortran_params.py module docstring and its mirror in validation.md
still claimed share_int was "out of scope" and surfaced only via the
"not applied" warning -- stale since read_params_file/
JSON_ALIAS_TO_CANONICAL landed; both now correctly say share_iter
needs no Fortran-side rename and the JSON alias table handles
share_int on the way in.

Reworded four "silently landed in the not applied warning" claims
(amica.py, changelog.md, validation.md, a test docstring): the
warning already named max_decs/min_grad_norm/share_int before this
epic, it just did not apply them -- "silently" overstated that.

numpy_impl/core.py's __init__ docstring now says params_file accepts
both formats. AGENTS.md's architecture-map line for fortran_params.py
now describes it as the shared reader for the wrapper, AMICA_NumPy
and the NumPy CLI. amica-differences.md spells out "generalized
Gaussian (GG)" at first use. Applies semantic line breaks to the
Markdown prose this epic added (changelog.md, validation.md,
amica-differences.md, numpy-backend.md), without reflowing
pre-existing text.
…-class' into feature/issue-304-phase3-params-surface

# Conflicts:
#	docs/changelog.md
#	pamica/numpy_impl/core.py
@neuromechanist
neuromechanist merged commit b6ecf22 into feature/issue-324-epic-mlx-first-class Sep 22, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-304-phase3-params-surface branch September 22, 2026 23:22
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