Repository navigation
Description
Overview
ell_comps priors are an independent box per component, so 21.5 % of the prior area is non-physical; JAX tracer-mode validation is a no-op there, so gradient lanes settle at |e| ≥ 1 (20.1 % of MultiStart lane best points campaign-wide, 0 of 246 hit lanes; positions-on doubles it — autolens_profiling#182). PyAutoFit already has a class-declared, traceable constraint channel (autofit/mapper/prior_model/constraint.py) and PyAutoGalaxy's EllProfile already declares the disk constraint — but MultiStartGradient only counts violations. Decision (Option 1): give the declaration projectable structure and make the clipper project onto the disk. Reparameterisation (Option 2) rejected for migration cost.
Plan
- New
__model_ball_constraints__class hook (sibling of__model_constraint__, duck-typed) →[(("ell_comps",), radius)];EllProfiledeclares radius =convert.ELL_COMPS_MAGNITUDE_CLAMP(0.999) — NOT1−margin, which sits in the dead-gradient annulus [0.999, 1). AbstractModel.ball_constraint_index_pairs()→ static(i0, i1, radius)viaTuplePrior.prior_tuples+priors_ordered_by_ididentity map.- New
ClipperPriorBoxJoint(ClipperPriorBox): box clip first, then jittable radial shrink (xp.where,xp.maximum(r, tiny)— no NaN at r=0, no grad poisoning),clipped_maskTrue on both pair members so momentum resets. Opt-in subclass ⇒ existingClipperPriorBoxidentifiers unchanged; pin__identifier_fields__=("margin","strict_epsilon")on the base (no-op today). - Guard/warn in
AbstractBFGS._bounds_from(a ball is not a scipyBounds). - PyAutoGalaxy: widen
analysis/dataset.pysave_resultscatch toSamplesException/FitException(mirror PyAutoLens#713) — separate commit. - Quantify on
imaging/mge/hstCPU (twosweep.pyarms, n_starts 16) — report fraction ≥1.0 and ≥0.999. validate_ell_compsuntouched.
Detailed implementation plan
Work Classification
Library (PyAutoFit primary, PyAutoGalaxy).
Affected Repositories
- PyAutoFit (primary)
- PyAutoGalaxy
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main | clean |
| ./PyAutoGalaxy | main | clean |
Suggested branch: feature/ell-comps-disk-constraint
Worktree root: ~/Code/PyAutoLabs-wt/ell-comps-disk-constraint/
Implementation Steps
autofit/mapper/prior_model/constraint.py—MODEL_BALL_CONSTRAINT = "__model_ball_constraints__",declares_ball_constraints(cls),ball_constraints_for(cls).autofit/mapper/prior_model/abstract.py(besideconstrained_model_tuples~:836) —ball_constraint_index_pairs(self), cached.autofit/non_linear/clipper.py—ClipperPriorBoxJoint; docstring "Two consumers" extended; base__identifier_fields__pinned.autofit/__init__.py(~:80-82) — export.autofit/non_linear/search/mle/bfgs/search.py:117-147— guard/warn on a joint clipper..../multi_start_gradient/search.py:1315-1319— optional joint-clip count in the log line.autogalaxy/profiles/geometry_profiles.py(~:240) —__model_ball_constraints__ = ((("ell_comps",), convert.ELL_COMPS_MAGNITUDE_CLAMP),).autogalaxy/analysis/analysis/dataset.py(~:178) — exception widening + test.- Fix stale comment
autolens_profiling/scripts/misc/searches/clipper_campaign.py:23-24(clipper DOES enter the identifier) — follow-up PR, not this one.
Tests
test_autofit/non_linear/test_clipper.pyTestJointBall: (−1,−1)→radius r angle-preserved; interior bit-identical; (0,0) not NaN; mask on both members; batched input; xp=np only (+ optional importorskip jax jit/grad-finite).- multi_start_gradient: lane seeded at |e|=1.4 is ≤ radius after one step.
test_autogalaxy/profiles/test_model_constraint.py: every EllProfile subclass declares the ball;validate_ell_compsstandalone unchanged.test_autogalaxy/analysis/analysis/test_analysis_dataset.py: mirrored save_results test.
Key Files
PyAutoFit/autofit/non_linear/clipper.py,autofit/mapper/prior_model/{constraint,abstract}.py,autofit/non_linear/search/mle/bfgs/search.pyPyAutoGalaxy/autogalaxy/profiles/geometry_profiles.py,autogalaxy/convert.py(CLAMP),autogalaxy/profiles/validate.py(untouched),autogalaxy/analysis/analysis/dataset.py
Original Prompt
Click to expand starting prompt
Claude Development Prompt: Joint ell_comps Disk Constraint
Type: feature
Target: PyAutoGalaxy
Difficulty: medium
Autonomy: supervised
Priority: normal
Status: formalised
Issued: 2026-08-27
Goal
Give the elliptical-components parametrisation a constraint the searches can
actually honour, so a gradient MAP optimizer cannot spend its budget — or
finish — outside the physical region.
ell_comps = (e1, e2) is physical only inside the unit disk |e| < 1. Every
profile's default priors are two INDEPENDENT box priors on e1 and e2
(typically [-1, 1] each), which is a SQUARE. The disk covers pi/4 of that
square, so 21.5% of the prior box is non-physical by area, and nothing in
the search stops a lane from sitting there.
What was measured (autolens_profiling, 2026-08-27 harvest, issue #182)
- The existing validation is a no-op where it matters.
validate_ell_comps
fires on standalone profile construction, but not on the tracer-mode path a
MultiStart lane actually evaluates — so a lane at|e| >= 1is never told. - 20.1% of MultiStart lane best points end at
|e| >= 1(1,252 of 6,240
lane best points across the recorded campaign). - 0 of 246 lanes that HIT the target basin end there. The two populations
are cleanly separated: ending outside the disk is a property of failed lanes,
never of successful ones. That is what makes this a searchability defect
rather than a cosmetic one — the wasted 20% is wasted budget, not a wasted
answer. - Positions-on roughly DOUBLES the out-of-disk fraction (17% -> 29% of best
points on the Phase-4 diagnostic arms). The positions penalty pushes lanes
into the corners the box permits and the geometry does not.
Full write-ups: autolens_profiling/results/notes/inference/ phase_03_prodigy_reliability/RESULTS.md and phase_04_positions/RESULTS.md.
Options to weigh (this prompt does not pick one)
- A joint disk assertion the clipper honours. Express
e1^2 + e2^2 < 1as
a constraint the search's prior-box clipper can project onto, rather than as
a post-hoc validation that raises. The clipper already insets a box; a
ball projection is the same operation in a different norm. Cheapest change,
keeps the existing parameter names and every recorded identifier stable, and
is the only option that helps a search already in flight. - Reparameterise. Sample in a coordinate whose whole domain is physical —
e.g.(magnitude, angle)with a bounded magnitude, or a squashing map onto
the disk. Removes the problem by construction, but changes the model's
parameter set, and therefore everytarget_id, prior config and recorded
posterior that namesell_comps_0/ell_comps_1. It needs a migration story
before it needs an implementation.
Hard constraint
Never make validate_ell_comps fire on tracers. Raising from the
tracer-mode path turns a 20%-of-lanes condition into a 20%-of-lanes crash in
the middle of multi-hour GPU fits, and would make a MultiStart search die on
exactly the lanes it is supposed to clip and move on from. The fix belongs in
the search's constraint handling (option 1) or in the parametrisation (option
2) — not in a validator that converts a survivable state into an exception.
Requirements
- Decide between the two options above with the evidence in the write-ups
named, and record the decision before implementing. - Whatever lands, add a test that a lane placed at
|e| >= 1is handled by the
chosen mechanism (projected, or unrepresentable) rather than merely detected. - Quantify the effect on the measured 20.1% figure on at least one cell before
claiming the defect is closed. - Do not change
validate_ell_comps's standalone-construction behaviour.
Out of scope
- The positions-penalty scaling question (Gate B pt 2,
autolens_profiling
issue feature/pickle #182) — the doubling is evidence FOR this task, not a task itself. - Re-running any autolens_profiling campaign.
Activity
Jammy2211 commentedon Aug 28, 2026
Library PR Created
Option 1 implemented (a joint disk constraint the clipper honours), as
recorded before implementing. Option 2 (reparameterise) was not taken: it changes
the model's parameter set and therefore every
target_id, prior config andrecorded posterior naming
ell_comps_0/ell_comps_1, and it cannot help a searchalready in flight.
96dad51911d8ae08b9Merge order: PyAutoFit#1538 first. PyAutoGalaxy#589 declares geometry the
PyAutoFit PR introduces the machinery to read, and its tests reference
ball_constraint_index_pairs()/af.ClipperPriorBoxJoint.What landed
__model_ball_constraints__— a class-declared((path, radius), ...), besidethe existing
__model_constraint__. The measure says how far outside; the ballsays what the region is, which is what a search needs to project.
AbstractPriorModel.ball_constraint_index_pairs()— resolves declarations to(index_0, index_1, radius)indices into the physical parameter vector; staticgeometry, cached, resolved outside the step loop.
af.ClipperPriorBoxJoint— opt-inClipperPriorBoxsubclass: box clip, thena radial shrink onto each declared ball. Jittable (double-
wheresogradisfinite at the origin;
xp.maximum(r, tiny)divisor floor). Masks bothmembers of a moved pair, so MultiStartGradient's momentum reset zeroes the
outward velocity in both coordinates rather than letting the pair spiral back out.
EllProfile.__model_ball_constraints__with radius =ELL_COMPS_MAGNITUDE_CLAMP(0.999), not
1 - margin: 0.999–1.0 is a dead-gradient annulus where theaxis-ratio conversion saturates, so projecting there would move a lane from a
region the model rejects into one the optimizer cannot leave.
model (a ball is not a
scipy.optimize.Bounds), and joint clipper + ascaler/bijector (neither maps a disk to a disk) — the latter at construction, so
a multi-hour fit does not die a minute in.
AnalysisDataset.save_resultscatch widened to(AttributeError, af.exc.SamplesException, af.exc.FitException), mirroringPyAutoLens#713 / PyAutoFit#1535.
Requirements from the prompt
|e| >= 1is handled rather than merelydetected" — done, at three levels: the clipper (
test_clipper.py), the searchstep contract (
test_multi_start_gradient.py, including that the defaultClipperNone/ClipperPriorBoxdemonstrably cannot move the same lane), andend-to-end on a real
Isothermallens model (test_model_constraint.py).200,000 uniform draws from the
ell_compsprior box of anIsothermallensmodel: 21.57% outside the disk before projection (analytic
1 - pi/4=21.46%) → 0.00% after. The canonical point
(0.9, 0.9),|e| = 1.2728,projects to
|e| = 0.999000with the angle preserved and both members masked.validate_ell_compsfire on tracers" — honoured; the validatoris byte-unchanged and a test class now pins its standalone-construction
behaviour (rejects
|e| > 1and the corner(0.8, 0.8), accepts the saturatingannulus, returns early for a non-concrete magnitude).
validate_ell_comps's standalone-construction behaviour" —honoured, as above.
Tests
test_autofit/(full)test_autogalaxy/(full)test_autolens/(downstream, against this branch)New tests: 17 in PyAutoFit
test_clipper.py, 15 intest_model_constraint.py,3 in
test_multi_start_gradient.py; 11 in PyAutoGalaxy. Unit tests arenumpy-only — the traced
jit/grad/vmapbehaviour was verifiedout-of-band (all finite at the origin, at the projected point and interior).
black: the set of files it would reformat is identical before and after thebranch (pre-existing, untouched).
Identifier stability, measured across checkouts. The same script on
main(
cbd30065) and on the branch gives identical hashes forMultiStartAdam+ClipperPriorBox(45e493b6…),MultiStartAdam+ClipperNone(
e1e937f9…),LBFGS+ClipperPriorBox(1caa475d…) andClipperPriorBox()alone (
5f855685…), andClipperPriorBox.project/bounds_from_modelreturnbit-identical values.
ClipperPriorBoxJoint()hashes differently, as it must.Heart
pyauto-heart readinessis RED, single red reason:release validation FAILED (stage integrate)— the known, human-authorisedoverride for this task, unrelated to these branches. Shipped to PR-open only
under that override; nothing merged.
Workspace impact — option (iii), no impact
API changes are additive and opt-in.
ClipperPriorBoxappears in no user-facingworkspace script (
autofit_workspace,autogalaxy_workspace,autolens_workspace,autolens_workspace_test,HowToLens,HowToFit— zero matches); the onlyconsumer is
autolens_profiling/scripts/misc/searches/, whose usages are all stillvalid since nothing was removed or renamed.
max_log_likelihood_galaxiescallsites in
autogalaxy_workspaceare unaffected — onlysave_results' internalcatch widened. No
no_run.yamlentries touch the changed symbols.A
/smoke_testconfirmation is owed at merge time rather than now: merge isalready blocked by the Heart override, and no workspace script exercises the new
opt-in clipper, so a smoke run today would not touch the changed code path.
Next
Human review of both PRs → merge PyAutoFit#1538 first, then PyAutoGalaxy#589.
A follow-up could add an
autolens_workspaceexample configuringaf.MultiStartAdam(clipper=af.ClipperPriorBoxJoint())— option (i) — but nothingrequires it.
🤖 Generated with Claude Code
https://claude.ai/code/session_011joies4k5TdRqezPUK8YET
Merge pull request #1538 from PyAutoLabs/feature/ell-comps-disk-const…
Jammy2211 commentedon Aug 28, 2026
Shipped
Option 1 of the prompt — a joint disk constraint the clipper honours — was the
one implemented; the reparameterisation (option 2) was not taken, since it re-keys
every
ell_comps_0/ell_comps_1identifier and prior config and needs a migrationstory first.
What changed
PyAutoFit (#1538) — the structural half of the constraint machinery, beside the
existing
__model_constraint__violation measure:__model_ball_constraints__: a class-declared((path, radius), ...)naming atuple prior confined to a disk. Duck-typed like
__model_constraint__, so aprofile library declares its own geometry without inheriting from PyAutoFit.
AbstractPriorModel.ball_constraint_index_pairs()resolves declarations to(index_0, index_1, radius)triples into the physical parameter vector, cachedunder an underscore-prefixed
__dict__key (theparameterizationconvention).af.ClipperPriorBoxJoint: an opt-inClipperPriorBoxsubclass that clips thebox, then radially shrinks each declared pair onto its ball. Jittable — the factor
is a
where, the radius is compared squared, and the double-whereidiom keepsgradfinite at the origin. Both members of a moved pair are masked, soMultiStartGradientzeroes the outward momentum in both coordinates.AbstractBFGSrefuses a joint clipper ona model declaring a ball (a ball is not a
scipy.optimize.Bounds), andAbstractMultiStartGradientrefuses one alongside a non-default scaler orbijector at construction (neither change of variables maps a disk to a disk).
ClipperPriorBox.__identifier_fields__is pinnedto
("margin", "strict_epsilon"), so existing search identifiers and outputdirectories stay byte-identical.
PyAutoGalaxy (#589):
EllProfile.__model_ball_constraints__ = ((("ell_comps",), convert.ELL_COMPS_MAGNITUDE_CLAMP),)— declared once at
EllProfile, so it reaches every elliptical light and massprofile. The radius is the clamp (0.999), not
1 - margin: between 0.999 and1.0 the axis-ratio conversion saturates and the likelihood is flat radially, so a
lane projected into that annulus would have nothing to climb back out on.
Spherical subclasses inherit the declaration but pin
ell_compsto an instance,so no pair resolves and nothing is projected.
validate_ell_compsis unchanged, per the prompt's hard constraint — the fixlives in the search's constraint handling, never in a validator that converts a
survivable state into an exception.
AnalysisDataset.save_results' catch to(AttributeError, af.exc.SamplesException, af.exc.FitException), mirroringPyAutoLens#713: writing an optional output file must never kill a completed fit
before
paths.completed()(PyAutoFit#1535).Workspace
No workspace smoke run was needed: the clipper is opt-in and no workspace script
selects it, so every existing script keeps the default
ClipperPriorBoxand itsbyte-identical identifiers.