Skip to content

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

Closed

Description

@Jammy2211

Overview

A model where one parameter is an arithmetic combination of priors shared by two components (e.g. stars.mass = (1 - f) * m, halo.mass = f * m) evaluates correctly, but search.fit dies before sampling: paths.save_all writing model.json raises TypeError: Object of type UniformPrior is not JSON serializable. Found 2026-11-16 in the stars_dm_slope science project.

Reproduced on main @ 3ba1e6c: json.dumps(model.dict()) raises the TypeError; the offending path is …mass.left.left.prior. That is the NegativePrior that 1 - f builds.

Plan

  • ModifiedPrior.dict (NegativePrior, AbsolutePrior, Log, …) serialises its operand only when it is an AbstractPriorModel; a plain Prior is a ModelObject but not an AbstractPriorModel, so the raw prior object is written. Check ModelObject, as CompoundPrior.dict already does.
  • Past that, search.fit hits a second error on the same path: ModifiedPrior.cls returns self.prior.cls, which a plain Prior lacks (AttributeError from superscripts/prior_class_dict). Return float for a plain prior, as CompoundPrior does.
  • CompoundPrior.dict does not save its operand attribute names (found by frame inspection, e.g. self, m). They are part of the model identifier, so a reloaded compound model gets a different identifier. Save left_name/right_name and restore them in from_dict. Dicts without them still load.
  • Unit tests: build a two-component model sharing compound priors, write it to JSON, reload it, and check the prior count, the identifier and the instance values; serialise a NegativePrior of a plain prior; load an old-format dict.

effective level: supervised; merge mode: auto-merge on green if the Witness passes after a merge slot
Tier: glance (declared). In this shift the task ends at PR-open; the close-out session runs the witness.

Detailed plan

Work Classification: Library (PyAutoFit only)
Worktree root: ~/Code/PyAutoLabs-wt/compound-prior-model-json/
Branch: feature/compound-prior-model-json

Files:

  • autofit/mapper/prior/arithmetic/compound.py: ModifiedPrior.dict isinstance check → ModelObject; ModifiedPrior.cls → float for a non-model prior; CompoundPrior.dict adds left_name/right_name; CompoundPrior.from_dict calls the new _set_operand_names.
  • test_autofit/serialise/test_compound_model.py (new).
  • test_autofit/serialise/test_sum_prior.py and test_assertions.py: their exact-dict fixtures now pop the two name keys, since the frame-inspected names depend on the call stack.

Witness: a model with a compound prior shared by two components writes model.json, reloads to an equal model, and search.fit starts without a TypeError.

Out of scope (follow-ups):

  • 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. This happens with or without compound priors; 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.

Activity

  1. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR open: #1783, ended at PR-open (shift 2026-11-18-pm, supervised, labelled decision-taken).

    The PR fixes four defects on the search.fit → model.json path:

    • ModifiedPrior.dict wrote a raw Prior instead of its dict.
    • ModifiedPrior.cls failed for a plain prior.
    • CompoundPrior operand names were not saved, so the identifier changed on reload.
    • "modified" had no autonerves parser, so model.sigma = -f silently reloaded as the default prior.

    Gate:

    • tests: pass.
    • smoke: 203/0.
    • review: CLEAN.
    • independent adversary: CLEAN. It found the parser defect.
    • Heart dev-ship: GREEN.

    Workspace impact: (iii) none. No public names changed and no workspace scripts need edits. The serialised compound-prior dict only gains two keys.

    Follow-ups, out of scope and pre-existing, are listed in the PR: prior-id reorder on load, and call-stack-dependent operand names.

  2. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped: #1783 merged 2026-11-18T17:40Z (merge 45e8cd3) by human /prm after shift 2026-11-18-pm (unattended merge refused for the decision-taken label). Compound-prior models now write model.json and reload with the same identifier. Pending release (PyAutoFit is in the published set).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions