Skip to content

fix: compound-prior models write model.json and reload with the same identifier - #1783

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/compound-prior-model-json
Nov 18, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/compound-prior-model-json

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

A model where one parameter is an arithmetic combination of priors shared by two components (stars.mass = (1 - f) * m, halo.mass = f * m) failed in search.fit before sampling: paths.save_all writing model.json raised TypeError: Object of type UniformPrior is not JSON serializable. Fixes #1782.

Four defects sat on that path, all in how compound and modified priors (NegativePrior, AbsolutePrior, Log, …) serialise:

  1. ModifiedPrior.dict serialised its operand only when it was an AbstractPriorModel. A plain Prior is a ModelObject, so the raw prior object was written (1 - f builds SumPrior(NegativePrior(f), 1)). It now checks ModelObject, as CompoundPrior.dict already did.
  2. With the dict fixed, search.fit next failed in superscripts / prior_class_dict, because ModifiedPrior.cls returned self.prior.cls, which a plain Prior lacks. It now returns float for a plain prior, as CompoundPrior does.
  3. CompoundPrior.dict did not save its operand attribute names. These names are found by frame inspection and are part of the model identifier, so a reloaded compound model had a different identifier. The names are now written as left_name/right_name and restored in from_dict. Dicts without them still load.
  4. The "modified" dict type had no parser registered with autonerves. A modified prior set directly on a model (model.sigma = -f) raised a KeyError inside the model loader, which swallowed it and silently kept the default prior. "modified" is now registered alongside "compound". The independent adversary leg found this one.

API Changes

None to public names. The serialised form of a compound prior gains two keys, left_name and right_name. Old model.json files without them still load.

Test Plan

  • New test_autofit/serialise/test_compound_model.py, 11 tests. With the source changes reverted, 6 fail and 3 error:

    • a shared compound-prior model JSON round-trips with the same prior count, identifier and instance values;
    • a modified prior set directly on a model round-trips;
    • search.fit runs (Drawer);
    • paths.save_all writes a model.json that reloads with the same identifier;
    • an old-format dict loads.
  • test_sum_prior.py and test_assertions.py fixtures now check the name keys exist and compare the rest. The frame-inspected names depend on the call stack: under pytest the 0 operand is named i.

  • Full suite test_autofit -n 6: 4277 passed. 18 failed and 19 errored on shared test_autofit/output and database files while other processes used the same worktree. All 33 of those tests, run serially, pass on both this branch and origin/main (330 passed each).

  • End-to-end: a stars_dm_slope-shaped model runs search.fit (Drawer and DynestyStatic). model.json is written, and Aggregator.from_directory reloads the model with prior_count 2 and an equal identifier.

  • Smoke (pyauto-heart smoke --root <task worktree>): autofit, autogalaxy, autolens, autolens_test, euclid and howtolens workspaces, plus autocti_workspace scripts, 203 PASS / 0 FAIL. The run then timed out at 50 min while pip-building an isolated env for a further CTI workspace, outside the default six.

Validation checklist (--auto run — plan was not pre-approved)

  • Effective level: supervised (header: safe; merge-mode: supervised in a shift). Tier: glance, declared.
  • Plan: on the issue (fix: compound-prior models cannot be written to model.json, so search.fit fails #1782), written at start. Scope grew in two places, both flagged below.
  • Gate:
    • tests: the 4277 that passed, plus the contention failures passing serially (see the test plan). Downstream library suites are n/a: no public names changed.
    • smoke: 203/0 across the six default workspaces.
    • review: CLEAN on ec11c36, from an independent Opus session. Its FINDINGS on 52343c7 were fixed in ec11c36.
    • adversary: CLEAN on ec11c36, from Sonnet, a different model. It found fix 4.
    • Heart: dev-ship scoped to PyAutoFit, GREEN. Shown, non-blocking:
      • manifest drift: end-at-deliverable blocks (46);
      • manifest drift: generated hooks (2);
      • release validation incomplete: no rehearsal for current source.
  • Human: plan sound in hindsight?
  • Human: diff matches plan (no scope creep)?
  • Human: merge, amend, or reject — then log the outcome

Decision taken (flagged)

  • The serialised dict format of CompoundPrior gains left_name/right_name. The plan said "serialise the operands and the operator" but did not settle the format. Saving the names is what makes "reloads to an equal model" hold by identifier.
  • Scope grew past the reported TypeError to fixes 2 and 4 above. Fix 2 blocks the same search.fit (the science model uses (1.0 - f_dm) * m_ap). Fix 4 would otherwise make the newly written model.json reload a different model without any error.

Follow-ups (not in this PR, pre-existing)

  • Prior.from_dict gives priors fresh ids in load order, so any model whose priors were created out of path order reloads with a different parameter order. Samples load by path name, so the aggregator is unaffected.
  • retrieve_name frame-walking makes compound operand names, and so identifiers, depend on the call stack.

🤖 Generated with Claude Code

Jammy2211 and others added 2 commits November 18, 2026 15:04
…e same identifier

A model such as stars.mass = (1 - f) * m, halo.mass = f * m failed in
search.fit when paths.save_all wrote model.json:
TypeError: Object of type UniformPrior is not JSON serializable.

- ModifiedPrior.dict (NegativePrior, AbsolutePrior, Log, ...) serialised its
  operand only when it was an AbstractPriorModel; a plain Prior is a
  ModelObject, so the raw prior was written. Check ModelObject, as
  CompoundPrior.dict already does.
- ModifiedPrior.cls returned self.prior.cls, which a plain Prior lacks, so
  search.fit then failed in superscripts/prior_class_dict. Return float for a
  plain prior, as CompoundPrior does.
- CompoundPrior.dict now saves the operand attribute names and from_dict
  restores them. They are part of the model identifier, so a reloaded model
  keeps its identifier. Dicts written before this load as before.

Fixes #1782

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The "modified" dict type (NegativePrior, AbsolutePrior, Log, ...) had no
parser registered with autonerves, so a model attribute such as
model.sigma = -f fell through to a class_path lookup, raised KeyError,
and the model loader silently kept the default prior. Register
ModelObject.from_dict for "modified" as it is for "compound".

Tests: a modified prior on a model round-trips with the same type,
prior count and identifier; search.fit runs on a shared compound-prior
model; paths.save_all writes a model.json that reloads with the same
identifier.

Found by the independent adversary review.

Refs #1782

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jammy2211 Jammy2211 added pending-release PR queued for the next release build decision-taken labels Nov 18, 2026
@Jammy2211 Jammy2211 added pending-release PR queued for the next release build decision-taken labels Nov 18, 2026
@Jammy2211
Jammy2211 merged commit 45e8cd3 into main Nov 18, 2026
6 checks passed
@Jammy2211
Jammy2211 deleted the feature/compound-prior-model-json branch November 18, 2026 17:40
@pyautolabs-bot pyautolabs-bot Bot removed the pending-release PR queued for the next release build label Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: compound-prior models cannot be written to model.json, so search.fit fails

1 participant