Repository navigation
fix: compound-prior models write model.json and reload with the same identifier - #1783
Merged
Merged
Conversation
…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>
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
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 insearch.fitbefore sampling:paths.save_allwritingmodel.jsonraisedTypeError: 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:ModifiedPrior.dictserialised its operand only when it was anAbstractPriorModel. A plainPrioris aModelObject, so the raw prior object was written (1 - fbuildsSumPrior(NegativePrior(f), 1)). It now checksModelObject, asCompoundPrior.dictalready did.search.fitnext failed insuperscripts/prior_class_dict, becauseModifiedPrior.clsreturnedself.prior.cls, which a plainPriorlacks. It now returnsfloatfor a plain prior, asCompoundPriordoes.CompoundPrior.dictdid 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 asleft_name/right_nameand restored infrom_dict. Dicts without them still load."modified"dict type had no parser registered with autonerves. A modified prior set directly on a model (model.sigma = -f) raised aKeyErrorinside 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_nameandright_name. Oldmodel.jsonfiles 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:search.fitruns (Drawer);paths.save_allwrites amodel.jsonthat reloads with the same identifier;test_sum_prior.pyandtest_assertions.pyfixtures now check the name keys exist and compare the rest. The frame-inspected names depend on the call stack: under pytest the0operand is namedi.Full suite
test_autofit -n 6: 4277 passed. 18 failed and 19 errored on sharedtest_autofit/outputand database files while other processes used the same worktree. All 33 of those tests, run serially, pass on both this branch andorigin/main(330 passed each).End-to-end: a
stars_dm_slope-shaped model runssearch.fit(Drawer and DynestyStatic).model.jsonis written, andAggregator.from_directoryreloads 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)
Decision taken (flagged)
CompoundPriorgainsleft_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.TypeErrorto fixes 2 and 4 above. Fix 2 blocks the samesearch.fit(the science model uses(1.0 - f_dm) * m_ap). Fix 4 would otherwise make the newly writtenmodel.jsonreload a different model without any error.Follow-ups (not in this PR, pre-existing)
Prior.from_dictgives 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_nameframe-walking makes compound operand names, and so identifiers, depend on the call stack.🤖 Generated with Claude Code