Repository navigation
Description
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)resolvespathto its alias group (every path naming the same prior, from the cachedSamples.paths=model.all_paths) and reads each sample's value under whichever alias is present inkwargs— the ruleSample.parameter_lists_for_pathsalready uses.Result.projected_modelis unchanged; it inherits the fix.- Unit tests: a model with one prior shared by two paths (two Gaussians with the same
centreprior) —values_for_pathfor both aliases, andprojected_modelprojects 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
autofit/non_linear/samples/samples.pyvalues_for_path: build an{alias: group}map fromself.paths(alias groups ordered like the sample columns); for each sample returnkwargs[a]for the first aliasaof the group present, raisingKeyError("Could not find any of the following keys in kwargs ...")in the same format asSample.parameter_lists_for_pathswhen none is present. A path not in the model keeps the current direct lookup.- Tests in
test_autofit/non_linear/samples/forvalues_for_pathon a shared prior (both aliases return the same values). - Test beside the existing
projected_modeltests (test_autofit/graphical/test_search_factor_optimiser.py/test_unification.py): aResultwhose model shares a prior across two paths projects without a KeyError, and the shared prior is one prior in the projected model. - Run the full
test_autofitsuite; 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 samecentreprior object) andresult.projected_modelreturns a model whose shared prior is projected once, with no KeyError; andScience/development/ep_gnfw_slopepython 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_0Fix 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
Jammy2211 commentedon Nov 18, 2026
Library PR opened: #1778 (labelled pending-release).
Samples.values_for_pathnow resolves a shared prior's path to its alias group and reads whichever alias each sample stores, soResult.projected_modelno longer raisesKeyError.values_for_path/projected_modelfound, so no workspace work is needed (option iii).test_autofit4291 passed, 2 skipped, 1 xfailed; autofit_workspace smoke 8/8 scripts + 2/2 notebooks passed; Heart dev-ship GREEN.Next: human
/prmto merge. Still to confirm after merge: theep_gnfw_slopeEP smoke from the Witness.Merge pull request #1778 from PyAutoLabs/feature/ep-projected-model-s…
Jammy2211 commentedon Nov 18, 2026
Shipped in #1778 (merged
fea8c01b, 2026-11-18).Samples.values_for_pathnow reads a prior shared across several model paths under whichever alias the sample's kwargs hold, so the EP projected model no longer raisesKeyError. Tests: fulltest_autofit(4291 passed), autofit_workspace smoke 8/8 scripts + 2/2 notebooks. Released in the next PyAutoFit release (pending-release). Follow-up: run theep_gnfw_slopeN=3 EP smoke to confirm the science unblock.