Repository navigation
Description
Overview
Two PyAutoFit entry points materialize a stored search sample into a model instance with no FitException recovery, so a sample the model no longer accepts raises straight through to the user. This fails PyAutoHeart Workspace Smoke every scheduled night (runs 31356506626, 31992749671) on autogalaxy_workspace notebooks/guides/results/aggregator/samples.ipynb, and it holds Heart's workspace validation not passing reason open.
#1466 established the recovery contract but wrote it by hand at two call sites only — max_log_likelihood and draw_randomly_via_pdf. Everything else was left exposed.
Plan
- Make
Sample.instance_for_model(ignore_assertions=True)honour its documented contract — it currently reaches onlyaf.Model's assertion mechanism and cannot stop a downstream profile constructor from raising. - Give the shared
to_instancedecorator an explicit per-method recovery policy, so every decorated result-reading method inherits one contract instead of each call site restating it. max_log_posteriorgains the next-best-valid-sample fallback its likelihood twin already has;from_sample_indexand the marginalized methods raise a typed, explanatory error instead.- Fold fix: preserve guarded sample lifecycle #1466's two hand-written guards onto the same mechanism — one implementation, not three.
- Cover both entry points with unit tests against a synthetic rejecting model (NumPy only).
- Verify against the real failing tutorial; leave the tutorial itself untouched.
Detailed implementation plan
Affected Repositories
- PyAutoFit (primary, library-only)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main | clean |
Suggested branch: feature/stored-sample-reconstruction-guard
⚠️ Blocked at filing time.PyAutoFitis claimed by taskversion-stamp-sync-guards(PyAutoHands#235), worktree~/Code/PyAutoLabs-wt/version-stamp-sync-guards. Registered inplanned.md, notactive.md.
Implementation Steps
-
autofit/non_linear/samples/sample.py:178-212—Sample.instance_for_model.ignore_assertions=Truemust also tolerate a downstreamFitException. It cannot construct the rejected object, so decide and document what "tolerate" returns (skip-with-signal vs. sentinel); whatever it is must let afor sample in samples.sample_listloop proceed without the user hand-writing atry/except. -
autofit/non_linear/samples/interface.py:32-40—to_instancegains a recovery policy:
def to_instance(func=None, *, recover="raise"):
...
@to_instance(recover="next_valid")
def max_log_posterior(self): ...
@to_instance(recover="raise")
def from_sample_index(self, sample_index): ...recover="next_valid"— fall back to the next-best valid stored sample.recover="raise"— typedSamplesExceptionnaming the offending parameter, why it is invalid, and thatas_dict=True/as_instance=Falsereturn raw values safely.
-
samples.py:405-441/pdf.py:336-367— refactor the two hand-written fix: preserve guarded sample lifecycle #1466 guards onto the shared mechanism.draw_randomly_via_pdfkeeps its own redraw loop (it redraws from the PDF, not next-best) but should reuse the message text. -
Tests —
test_autofit/non_linear/samples/: a local class whose__init__raisesaf.exc.FitException(autofit cannot import autogalaxy). Cover both entry points, each policy, and thatas_dict=True/as_instance=Falsestay unguarded. -
Verify — run
autogalaxy_workspace/scripts/guides/results/_quick_fit.pythenscripts/guides/results/aggregator/samples.pyunder the env resolved fromconfig/build/profile_smoke.yaml. The fixture reliably yields 1–2 invalid samples per 300.
Behaviour change for release notes
ModelParameterException subclasses ValueError; SamplesException subclasses plain Exception (autofit/exc.py:57). Callers catching ValueError around these methods would stop catching it. raise ... from e preserves the cause.
Key Files
autofit/non_linear/samples/sample.py—instance_for_model, the CI failure siteautofit/non_linear/samples/interface.py— theto_instancedecoratorautofit/non_linear/samples/samples.py—max_log_likelihood,max_log_posterior,from_sample_indexautofit/non_linear/samples/pdf.py—draw_randomly_via_pdf
Do not
Do not weaken PyAutoGalaxy's validate_ell_comps, and do not edit the tutorial to route around this — the tutorial is user documentation and the guard is correct.
Original Prompt
Click to expand starting prompt
Reconstructing a stored sample raises through ignore_assertions=True
Type: bug
Target: autofit
Repos:
- PyAutoFit
Difficulty: small
Autonomy: supervised
Priority: high
Status: formalised
What this is
Two PyAutoFit entry points materialize a stored search sample into a model
instance without any FitException recovery, so a sample the model no longer
accepts raises straight through to the user:
Sample.instance_for_model(model, ignore_assertions=True)
(autofit/non_linear/samples/sample.py:178-212) — the CI failure site.
ignore_assertionsreaches onlyaf.Model's own assertion mechanism; it
cannot stop a downstream profile constructor from raising, despite the
parameter's docstring reading "If True, do not check that the instance is
valid".- The shared
to_instancedecorator (interface.py:32-40), used by
from_sample_index,max_log_posterior,median_pdf,
values_at_*_sigma,errors_at_*_sigma:
if as_instance:
return self._instance_from_vector(vector) # <-- no try/exceptPyAutoFit#1466
(b499d4367, 2026-08-11) established the recovery contract but wrote it by
hand at two call sites only — max_log_likelihood (samples.py:405-441) and
draw_randomly_via_pdf (pdf.py:336-367). Everything else was left exposed.
as_dict=True and as_instance=False never construct an object and are always
safe; only the materialization request needs the guard.
Why invalid samples are in storage at all
This is the part that makes the bug real rather than cosmetic — the stored
sample list legitimately contains points the model rejects.
PyAutoGalaxy gained a geometric constructor guard on 2026-08-09 (a366f771,
#566): validate_ell_comps rejects ell_comps whose magnitude is not below 1,
since q = (1 - f) / (1 + f) is a valid axis ratio only while f < 1. The next
day be61b8d0 (#568) made it a
ModelParameterException(ValueError, af.exc.FitException) so a search would
resample rather than die.
"Resample" is a misnomer. fitness.py:252-258 on the NumPy path:
except exc.FitException:
return self.resample_figure_of_merit # -1.0e99 for NautilusIt does not redraw — it returns a sentinel figure of merit, and the sampler
still records the point. On the JAX path (fitness.py:244-250) there is no
guard at all, and none is needed: autoarray.validate.is_concrete_scalar
returns False for tracers, so validate_ell_comps passes tracers through
unvalidated, while convert.py:86-92 clamps the magnitude to
ELL_COMPS_MAGNITUDE_CLAMP = 0.999. A JAX fit therefore explores |e| >= 1
freely and stores the raw, unclamped value.
Either way the stored parameter vector keeps a value the constructor will later
refuse. Reconstruction is where it surfaces.
The CI failure
PyAutoHeart Workspace Smoke, scheduled runs 2026-08-10 (31356506626)
and 2026-08-17 (31992749671), job run_notebooks (3.12, autogalaxy, guides) —
1 failure in 1332 checks:
autogalaxy_workspace notebooks/guides/results/aggregator/samples.ipynb
ModelParameterException: ell_comps must satisfy ell_comps[0]**2 + ell_comps[1]**2 < 1;
got (0.9781300707301511, -0.23873992910981626), magnitude 1.0068441731558715
Reproduced locally end-to-end (see below); the traceback lands on
scripts/guides/results/aggregator/samples.py:451:
for sample in samples.sample_list:
instance = sample.instance_for_model(model=samples.model, ignore_assertions=True)That loop walks every stored sample, so it fires deterministically whenever
any invalid sample exists — which is why it fails every scheduled run rather
than intermittently.
Reproduction (local, confirmed)
Ran _quick_fit.py then aggregator/samples.py under the env PyAutoHands
resolves from config/build/profile_smoke.yaml, then again under a
production-like env. Script:
scratchpad/run_smoke_repro.py.
| smoke env | production-like (JAX on, checks on) | |
|---|---|---|
| invalid stored samples | 2/300, 2/300 | 1/300, 0/300 |
| max |ell_comps| | 1.124, 1.133 | 1.115 |
| weight of every invalid sample | 0.0 |
0.0 |
| tutorial result | fails (exact CI error) | invalid samples present |
Invalid samples appear with checks enabled and JAX on, so this is not purely
a smoke-profile artifact.
A second control test (scratchpad/repro_guard_gap.py) isolates the
to_instance half against a synthetic sample list — arm A puts the invalid
sample last, arm B makes it maximum-posterior:
| call | arm A | arm B |
|---|---|---|
max_log_likelihood() |
ok — recovered | ok — recovered |
draw_randomly_via_pdf() ×50 |
ok — recovered | ok — recovered |
max_log_posterior() |
ok (not selected) | RAISED |
from_sample_index(-1) |
RAISED | ok (not selected) |
The two hand-guarded methods recover under both arms; the two decorated ones
raise as soon as they happen to select the invalid sample.
Test mode is NOT implicated (checked explicitly)
Both scripts declare ENV: full_datasets real_search, and
PyAutoHands/autohands/env_config.py:63 maps real_search to
("PYAUTO_TEST_MODE",) — a token releases (unsets) its var. The resolved
smoke env is:
PYAUTO_DISABLE_JAX=1 PYAUTO_FAST_PLOTS=1 PYAUTO_SKIP_CHECKS=1 PYAUTO_SKIP_VISUALIZATION=1
PYAUTO_TEST_MODE is absent, so no sampler bypass runs. Independently,
_build_fake_samples (abstract_search.py:1111-1171) synthesizes the prior
median ±0.1%, which cannot produce |e| = 1.007. Do not re-open this line.
Fix locus
PyAutoFit library source, both entry points:
Sample.instance_for_model— makeignore_assertions=Truehonour its stated
contract, i.e. also tolerate a downstreamFitException. The caller has
explicitly said it accepts invalid points; today the flag silently under-
delivers. Decide and document what "tolerate" returns (skip vs. sentinel) —
it cannot construct the rejected object.to_instance— an explicit per-method recovery policy, so every decorated
method inherits one contract instead of each call site restating it:
def to_instance(func=None, *, recover="raise"):
...
@to_instance(recover="next_valid")
def max_log_posterior(self): ...
@to_instance(recover="raise")
def from_sample_index(self, sample_index): ...recover="next_valid"— fall back to the next-best valid stored sample,
reusing whatmax_log_likelihoodalready implements by hand.recover="raise"— a typedSamplesExceptionnaming the offending
parameter, explaining why the point is invalid, and stating that
as_dict=True/as_instance=Falsereturn the raw values safely.
Fold #1466's two hand-written guards onto the same mechanism — one
implementation, not three.
Rejected: a uniform typed error everywhere (leaves max_log_posterior without
the fallback its likelihood twin has); threading an ignore_assertions caller
flag through to_instance (pushes a library contract gap onto every user).
Do not weaken validate_ell_comps, and do not edit the tutorial to
route around this — the tutorial is user documentation and the guard is correct.
Behaviour change to note in release notes
ModelParameterException subclasses ValueError; SamplesException
subclasses plain Exception (autofit/exc.py:57). Callers catching ValueError
around these methods would stop catching it. raise ... from e preserves the
cause.
Split out — do not fix here
Every invalid sample measured carried weight exactly 0.0, and
samples_weight_threshold: 1.0e-10 is set in both the workspace and packaged
config/output.yaml. The prune at updater.py:220 should therefore have
removed all 299 zero-weight rows — but all 300 survived in both runs, and
the "removed from samples.csv" log line never fired. PYAUTO_SKIP_CHECKS=1
explains it under smoke (samples.py:505-506 nulls the threshold) but not
the checks-on run. Suspect: the early return on FitException at
updater.py:212-215, which skips the prune entirely — unconfirmed.
Filed separately: different blast radius (every saved samples.csv, not just
this tutorial). If that prune is repaired, invalid zero-weight samples stop
being stored and this bug's trigger becomes much rarer — but the contract gap
above is still real, because a converged fit can carry an invalid sample at
non-zero weight.
Context worth carrying
The reproduction fit reports f_live=1.0000, N_eff=1 — the n_like_max=300 cap
in _quick_fit.py means the sampler never converges, so those 300 rows are
near-prior exploration points with a degenerate one-hot weight vector. Any
attempt to reason about "typical" weights from this fixture will mislead.
Superseded
An initial triage guessed this was the prior-support family
(#1484,
#1481) and attributed the
failure to from_sample_index at samples.py:319. Both were wrong; the
reproduction above supersedes them. Those issues concern the objective during
a search — this concerns reconstructing stored samples after one.
Activity
Jammy2211 commentedon Aug 17, 2026
Re-scoped after entering the worktree: the nightly CI failure is being fixed in the workspace instead, because PyAutoFit already ships the right lever.
valid_sample_instance_pairs(#1470, 2026-08-11) does exactly what the failing tutorial hand-rolls — skips only points rejected via the narrowFitExceptioncontract, keeps weights paired — and it is already used byautofit/aggregator/base.pyand byautolens_workspace's copy of the same guide. That rollout covered autolens and stopped; autogalaxy was never migrated. Fixed in PyAutoLabs/autogalaxy_workspace#210, which needs no library release (valid_sample_instance_pairsis in releasedautofit-2026.8.15.1).So the original framing here — "make
ignore_assertions=Truehonour its contract" — is not the right primary fix, and the "do not edit the tutorial" boundary in the description was wrong: using the canonical API is correct usage, not a workaround.What remains genuinely broken in this repo, and what this issue should now cover:
Samples.instances(samples.py:70-82) — materializes every stored sample viainstance_from_vectorwith no guard. Same failure mode, unguarded in the release too.to_instancedecorator (interface.py:32-40) — 14 decorated methods share it. Proven by control test: with an invalid sample present,from_sample_index(-1)andmax_log_posterior()each raise as soon as they select it, while the two hand-guarded methods (max_log_likelihood,draw_randomly_via_pdf) recover.This matters beyond one tutorial:
from_sample_indexis called inautolens_workspace(4 sites, one at index-10),autogalaxy_workspace(3),autofit_workspace,autocti_workspaceandHowToFit. Those are latent today only because which index lands on an invalid sample is luck.Agreed design is unchanged — a per-method recovery policy on the decorator (
recover="next_valid"formax_log_posterior,recover="raise"with a typedSamplesExceptionelsewhere), plus a guard onSamples.instances. Release-note item: the raise path changes the user-visible type fromModelParameterException(aValueError) toSamplesException(plainException).Jammy2211 commentedon Aug 17, 2026
Workspace half shipped: PyAutoLabs/autogalaxy_workspace#210 merged 2026-08-17T22:08Z (
1b5005c8), all 6 checks green (smoke 3.12 3m37s, 3.13 3m44s). That clears the nightly Workspace Smoke red.This issue remains open for the library hardening only —
Samples.instancesand theto_instancedecorator, per the re-scope comment above.Jammy2211 commentedon Aug 19, 2026
Library PR Created: #1504 (
pending-release, full suite 2011 passed / 16 skipped, Heart GREEN at ship).Implements the agreed design:
to_instancegains a per-method recovery policy (recover="next_valid"formax_log_posterior, mirroring the #1466max_log_likelihoodguard via a shared helper;recover="raise"→ typedSamplesExceptionforfrom_sample_indexand the marginalized methods), andSamples.instancesskips rejected stored samples viavalid_sample_instance_pairs(#1470). Two internal consumers of the old exception type were updated (mode-1 rejected-final-sample hook, updater samples-save guard). Release-notes classifier verified:breaking.Workspace impact (grep of every workspace repo): exactly one affected site —
autocti_workspace/scripts/dataset_1d/advanced/database/examples/samples.py:286catchesaf.exc.FitExceptionarounderrors_at_*_sigma(...)instance calls and would miss the newSamplesException. Latent-only (fires only when stored samples are invalid); fix is a one-line catch update. All otherFitExceptioncatches in workspaces wrap directinstance_from_vectorcalls, which are unchanged.Next:
/start_workspacefor the one-line autocti_workspace migration (behind the library-first merge gate).Jammy2211 commentedon Aug 19, 2026
Extended validation of PR #1504 (all against the worktree branch, i.e. the PR code):
cookbooks/samples.py,cookbooks/result.py)ENV: real_search, genuine searches + stored samples)guides/results/aggregator/{samples,samples_via_aggregator}.pyin autogalaxy_workspace and autolens_workspaceThe integration scripts exercise every changed method against real stored samples:
median_pdf(dict/vector/instance),errors_at_upper/lower_sigma,from_sample_index(-1)as instance,valid_sample_instance_pairs(ignore_assertions=True),draw_randomly_via_pdf. The autogalaxysamples.pyis the script whose notebook was the original nightly Workspace Smoke red.Harness note: a first targeted-run attempt reported failures in these guides, on the PR branch and on main — root-caused to the runner's CWD silently dropping the in-file
ENV: real_searchtoken, so those runs executed in test mode with tiny fake sample lists (the known degraded-profile trap). Re-run with the token honored: 4/4.Pre-existing findings (not regressions, both reproduce independent of this PR):
autocti_workspacedataset_1d/advanced/database/start_here.pyreadsdataset/.../info.json, which no simulator writes — fails atopen()before any samples API. autocti_workspace has no CI, so this has gone unnoticed.autocti_workspacedatabase/examples/samples.py:103andautolens_workspaceaggregator guides (lines 200/146) hardcodeparameter_lists[9]— breaks under any profile that stores <10 samples (test mode). Latent under real_search.These will be routed to /intake as workspace bugs. The one-line
SamplesExceptioncatch migration at autoctisamples.py:286remains the workspace follow-up for this issue.Merge pull request #1504 from PyAutoLabs/feature/stored-sample-recons…
Jammy2211 commentedon Aug 19, 2026
Shipped. Both halves are now merged:
3b37843b(all 4 CI checks green; full validation table in the previous comment).FitException-only catch site, widened to also catchSamplesException).The breaking exception-type change ships in the next autofit release via the PR's
## API Changessection (classifier-verifiedbreaking). PyAutoFit#1487 (weight-threshold prune retains zero-weight rows) remains open and deliberately untouched by this work. The two pre-existing autocti_workspace script bugs found during validation (missinginfo.jsoncontract; hardcodedparameter_lists[9]) are being routed to intake as separate workspace bugs.Closing.
fix: recover or raise cleanly when stored samples fail reconstruction (…