Skip to content

fix: EP projection KeyError for a prior shared across model paths #1773

Closed

Description

@Jammy2211

Overview

Result.projected_model — the EP step that projects a factor's posterior samples back onto the mean field — raises KeyError for any prior shared by several model paths. Samples.values_for_path(path) reads sample.kwargs[path], but kwargs holds a shared prior only under its last alias while path_priors_tuples names it by its first. Every EP AnalysisFactor whose model has an MGE source (mge_model_from ties one centre / ell_comps prior across all Gaussians) therefore crashes after its first factor search. Non-EP fits never call projected_model.

Plan

  • Samples.values_for_path(path) resolves path to its alias group (every path naming the same prior, from the cached Samples.paths = model.all_paths) and reads each sample's value under whichever alias is present in kwargs — the rule Sample.parameter_lists_for_paths already uses.
  • Result.projected_model is unchanged; it inherits the fix.
  • Unit tests: a model with one prior shared by two paths (two Gaussians with the same centre prior) — values_for_path for both aliases, and projected_model projects the shared prior once without a KeyError.
  • Validation: rerun the downstream N=3 EP smoke test (ep_gnfw_slope: scripts/ep.py --total 3 --max_steps 2 --smoke) against the branch.
  • Sibling samples_with_paths_shared_prior_factor_graph (with_paths / without_paths, same root cause, different code path) stays a separate task.
  • Tier: judge — merge mode: human /prm

Workflow note: the worktree conflict guard flagged PyAutoFit as claimed by the docs task three-autos-public-framing. The human overrode it on 2026-11-13: the files don't overlap and this fix gets its own worktree.

Detailed implementation plan

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./fit/PyAutoFit main (362ee74, = origin/main) clean

Suggested branch: feature/ep-projected-model-shared-prior (worktree ~/Code/PyAutoLabs-wt/ep-projected-model-shared-prior/)

Implementation Steps

  1. autofit/non_linear/samples/samples.py values_for_path: build an {alias: group} map from self.paths (alias groups ordered like the sample columns); for each sample return kwargs[a] for the first alias a of the group present, raising KeyError("Could not find any of the following keys in kwargs ...") in the same format as Sample.parameter_lists_for_paths when none is present. A path not in the model keeps the current direct lookup.
  2. Tests in test_autofit/non_linear/samples/ for values_for_path on a shared prior (both aliases return the same values).
  3. Test beside the existing projected_model tests (test_autofit/graphical/test_search_factor_optimiser.py / test_unification.py): a Result whose model shares a prior across two paths projects without a KeyError, and the shared prior is one prior in the projected model.
  4. Run the full test_autofit suite; then the downstream EP smoke test.

Key Files

  • autofit/non_linear/samples/samples.py — values_for_path (the fix)
  • autofit/non_linear/result.py — projected_model (the caller; unchanged)
  • autofit/non_linear/samples/sample.py — parameter_lists_for_paths (the existing alias rule to mirror)

Original Prompt

Click to expand starting prompt

autofit: EP projection (Result.projected_model) raises KeyError for a prior shared across paths

Type: bug
Target: autofit
Repos:

  • @PyAutoFit
    Themes:
  • expectation-propagation
  • samples
  • factor-graph
    Difficulty: small
    Autonomy: supervised
    Priority: high
    Status: formalised
    Consequence: judge
    Witness: a unit test fits/constructs Samples for a model with one prior shared by two paths (e.g. two Gaussians with the same centre prior object) and result.projected_model returns a model whose shared prior is projected once, with no KeyError; and Science/development/ep_gnfw_slope python scripts/ep.py --total 3 --max_steps 2 --smoke (MGE source, 20 Gaussians sharing one centre/ell_comps prior) gets past the first factor's projection and completes both EP sweeps.
    Review-minutes: 10
    Unattended: never
    Lane: any
    Filed: 2026-11-13

Found 2026-11-13 in the ep_gnfw_slope science project's first EP smoke test (50
stellar + gNFW twins of Euclid DR1 lenses, one shared inner slope). Blocks that project.

What happens

Result.projected_model (autofit/non_linear/result.py, ~line 500) loops over
self.samples.model.path_priors_tuples and calls self.samples.values_for_path(path),
which does sample.kwargs[path] (samples/samples.py ~line 247). For a prior shared by
several paths, path_priors_tuples names it by its first path, but Sample.kwargs
holds it only under its last path (dict overwrite when the kwargs are built). Result:

KeyError: ('galaxies', 'galaxy_1', 'bulge', 'profile_list', '0', 'centre', 'centre_0')

after the factor's Nautilus search completes (kwargs carry profile_list.19.centre.centre_0).
Any EP AnalysisFactor whose model has an MGE source triggers it, because
al.model_util.mge_model_from ties one centre and one ell_comps prior across all
Gaussians. So EP with an MGE source has never been able to complete a factor step.
Ordinary (non-EP) fits never call projected_model, which is why nobody hit it before.

Repro (local PyAutoFit main 362ee74 == origin/main):

import common  # ep_gnfw_slope/scripts
from autofit.non_linear.samples.sample import Sample
m = common.model_from(common.simulated_twin_from(common.sample_lens_ids()[0]))
s = Sample.from_lists(m, [list(m.physical_values_from_prior_medians)], [0.0], [0.0], [1.0])[0]
# path_priors_tuples -> ...profile_list.0.centre.centre_0 ; s.kwargs -> ...profile_list.19.centre.centre_0

Fix direction

Same root cause as the sibling draft samples_with_paths_shared_prior_factor_graph.md
(kwargs keep only the last alias of a shared prior). Prefer one root fix that both inherit:
make Samples.values_for_path (and the kwargs lookup used by parameter_lists) accept any
alias of a shared prior, via model.all_paths, instead of patching projected_model alone.
Add a unit test with a prior shared across two paths covering values_for_path and
projected_model.

Activity

  1. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR opened: #1778 (labelled pending-release).

    • Samples.values_for_path now resolves a shared prior's path to its alias group and reads whichever alias each sample stores, so Result.projected_model no longer raises KeyError.
    • API changes: none, internal only. No workspace callers of values_for_path/projected_model found, so no workspace work is needed (option iii).
    • Tests: full test_autofit 4291 passed, 2 skipped, 1 xfailed; autofit_workspace smoke 8/8 scripts + 2/2 notebooks passed; Heart dev-ship GREEN.

    Next: human /prm to merge. Still to confirm after merge: the ep_gnfw_slope EP smoke from the Witness.

  2. added a commit that references this issue on Nov 18, 2026
  3. Jammy2211 commented on Nov 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped in #1778 (merged fea8c01b, 2026-11-18). Samples.values_for_path now reads a prior shared across several model paths under whichever alias the sample's kwargs hold, so the EP projected model no longer raises KeyError. Tests: full test_autofit (4291 passed), autofit_workspace smoke 8/8 scripts + 2/2 notebooks. Released in the next PyAutoFit release (pending-release). Follow-up: run the ep_gnfw_slope N=3 EP smoke to confirm the science unblock.

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