Repository navigation
Description
Overview
Builds on the PyAutoGalaxy latent infrastructure shipped in PyAutoGalaxy #441 by adding the lensing-specific latents that need a Tracer. PyAutoLens's AnalysisImaging does NOT inherit the PyAutoGalaxy LATENT_KEYS / compute_latent_variables via MRO — they live on parallel imaging Analysis classes (autolens.imaging.model.AnalysisImaging vs autogalaxy.imaging.model.AnalysisImaging), not the shared dataset base — so this PR adds an explicit dispatch layer in PyAutoLens with its own registry.
After this ships, sub-prompt #3 (euclid migration) can drop the bespoke LATENT_KEYS + compute_latent_variables in euclid_strong_lens_modeling_pipeline/util.py:306-490 and inherit the library catalogue.
Plan
- Add
autolens/analysis/latent.pyfor the one tracer-derived latent (effective_einstein_radius, delegating to PyAutoGalaxy'sLensCalc.einstein_radius_jit_fromso its closure cache is reused). - Add
autolens/imaging/model/latent.pyfor image-derived lensing latents —total_lens_flux_mujy,total_source_flux_mujy,total_lensed_source_flux_mujy,magnification. Helpersab_mag_via_flux_from/flux_mujy_via_ab_mag_fromare imported from PyAutoGalaxy (no duplication). - Add
autolens/config/latent.yamlwith all 5 keys default OFF (same reasoning as Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441:magzerorequirement +compute_latent_samplesruns on every fit, so default-on would crash existing users). - Wire
AnalysisImagingwith aLATENT_KEYS@propertyreading fromconf.instance["latent"]and acompute_latent_variables(parameters, model)method dispatching through a localLATENT_FUNCTIONSregistry. PyAutoLens defines its own registry (no re-export of PyAutoGalaxy'stotal_galaxy_0_flux_mujy— the lens-light equivalent istotal_lens_flux_mujy, named in the lensing vocabulary). - Apply the same
NotImplementedError-when-empty short-circuit as Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441 so autofit'sexcept NotImplementedError: return Noneskips the latent pipeline cleanly. - Preserve
LATENT_BATCH_MODE = "jit"(inherited viaautogalaxy/analysis/analysis/dataset.py:28—ZeroSolverin the Einstein-radius helper is vmap-incompatible). - Unit tests for each latent function + AnalysisImaging end-to-end + a spy/mock test confirming
effective_einstein_radiusactually callsLensCalc.einstein_radius_jit_from.
Detailed implementation plan
Affected Repositories
- PyAutoLens (primary)
Work Classification
Library
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoLens | main | CLAUDE.md + README.md modified (pre-existing PyAutoPaper section adds — unrelated) |
Suggested branch: feature/latent-module-autolens
Worktree root: ~/Code/PyAutoLabs-wt/latent-module-autolens/ (created later by /start_library)
Implementation Steps
-
Create
autolens/analysis/latent.pywitheffective_einstein_radius(fit, magzero, xp=np):- JAX path:
LensCalc.from_mass_obj(fit.tracer).einstein_radius_jit_from(init_guess=fixed_fan)wherefixed_fan = jnp.array([[1.0, 0.0], [0.0, 1.0], [-1.0, 0.0], [0.0, -1.0]])(matches euclidutil.py:502-509). - NumPy path:
LensCalc.from_mass_obj(fit.tracer).einstein_radius_from(grid=fit.dataset.grids.lp). - Returns
xp.nanonValueError/AttributeError. - Cache the
(closure, solver)pair properly per memoryfeedback_jax_closure_cache_busts. The cache lives insideLensCalc(lines 1580-1586) — just don't construct a freshLensCalceach call within the same Analysis.
- JAX path:
-
Create
autolens/imaging/model/latent.pywith the four image-derived latents. Each function takes(fit, magzero, xp=np). Usefrom autogalaxy.imaging.model.latent import ab_mag_via_flux_from, flux_mujy_via_ab_mag_from.total_lens_flux_mujy(fit, magzero, xp=np)—fit.galaxy_image_dict[fit.galaxies[0]], sum, → AB mag → µJy. RaisesValueErrorifmagzerois None (memoryfeedback_no_silent_guards).total_lensed_source_flux_mujy(fit, magzero, xp=np)—fit.galaxy_image_dict[fit.tracer.galaxies[-1]](image-plane image of source after lensing).total_source_flux_mujy(fit, magzero, xp=np)—fit.tracer.galaxies[-1].image_2d_from(grid=fit.dataset.grids.lp)(source-plane intrinsic image).magnification(fit, magzero, xp=np)— ratio oftotal_lensed_source_flux_mujy / total_source_flux_mujy. magzero param accepted but unused; dimensionless ratio.
-
Create
autolens/config/latent.yaml— all 5 keys defaultfalse. Each key gets a comment explaining what it computes and that it requiresmagzero(exceptmagnificationwhich is dimensionless). -
Wire
AnalysisImaging(autolens/imaging/model/analysis.py):- Build a single
LATENT_FUNCTIONSregistry:{**lens_analysis_latents, **lens_imaging_latents}where the two source modules each export a per-module dict. LATENT_KEYS@property: readsconf.instance["latent"](lens-side latent.yaml), filters to enabled keys present inLATENT_FUNCTIONS.compute_latent_variables(parameters, model): raisesNotImplementedErrorwhenLATENT_KEYSis empty; otherwise dispatches throughLATENT_FUNCTIONS[k](**context)forkinLATENT_KEYS, returning a tuple aligned to keys. Context dict:{"fit": fit_from(instance_from_vector(parameters)), "magzero": self.kwargs.get("magzero"), "xp": self._xp}.
- Build a single
-
Unit tests at
test_autolens/analysis/test_latent.pyandtest_autolens/imaging/model/test_latent.py:- Per-latent against known toy models (numpy only — memory
feedback_no_jax_in_unit_tests). effective_einstein_radiusspy test asserting it calls intoLensCalc.einstein_radius_jit_from/einstein_radius_from(mock or monkeypatch).- On/off filtering via explicit
yaml_configdict. - Order preservation across registry → LATENT_KEYS → return tuple.
- End-to-end through
AnalysisImaging.compute_latent_variableswith at least one key enabled in the test config mirror. compute_latent_variablesraisesNotImplementedErrorwhenLATENT_KEYS == [].test_autolens/config/latent.yamlmirror with at least one latent enabled so the end-to-end test exercises the dispatch path.
- Per-latent against known toy models (numpy only — memory
Key Files
autolens/analysis/latent.py— new module (effective_einstein_radius)autolens/imaging/model/latent.py— new module (4 image-derived latents)autolens/config/latent.yaml— new configautolens/imaging/model/analysis.py— wireAnalysisImaging(parallel to PyAutoGalaxy's PR Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441 pattern)test_autolens/config/latent.yaml— test mirrortest_autolens/analysis/test_latent.py— new teststest_autolens/imaging/model/test_latent.py— new tests
Constraints to preserve
LATENT_BATCH_MODE = "jit"inherited viaautogalaxy/analysis/analysis/dataset.py:28— do not change.- All yaml keys snake_case-lowercase (memory
feedback_autoconf_lowercases_yaml_keys). - Default OFF in library yaml to avoid the
magzeroregression that Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441 caught. - Reuse
LensCalc.einstein_radius_jit_fromrather than reimplementing theZeroSolverloop.
Original Prompt
Click to expand starting prompt
Add first-class lensing latent-variable modules to PyAutoLens
Context
Parent epic: PyAutoPrompt/z_features/latent_refactor.md.
Depends on the PyAutoGalaxy spine in autogalaxy/latent_module.md (shipped as PyAutoGalaxy #441).
PyAutoGalaxy gets galaxy-level / image-flux latents. This task adds the lensing-specific latents that need a Tracer (mass model + sources): magnification, effective Einstein radius, lensed source flux, total source flux. Today these live in euclid_strong_lens_modeling_pipeline/util.py:306-490 as part of a bespoke AnalysisImaging subclass. After this task lands, the euclid pipeline (sub-prompt #3) can drop its custom subclass and inherit the curated set.
Task
-
Create
autolens/analysis/latent.py— tracer-derived, dataset-agnostic latents.effective_einstein_radiusmust delegate toLensCalc.einstein_radius_jit_from()atPyAutoGalaxy/autogalaxy/operate/lens_calc.py:1520. -
Create
autolens/imaging/model/latent.py— lensing imaging-derived latents that need aFitImaging:total_lens_flux_mujy,total_lensed_source_flux_mujy,total_source_flux_mujy,magnification. Useab_mag_via_flux_fromandflux_mujy_via_ab_mag_fromimported from PyAutoGalaxy (shipped in Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441). -
Create
autolens/config/latent.yaml— flat dict oflatent_key: bool. Mirrorautolens/config/output.yaml. Per Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441's regression learning, default everything OFF. -
Wire
AnalysisImaging(autolens/imaging/model/analysis.py): readconfig/latent.yaml, buildLATENT_KEYSvia@property, dispatchcompute_latent_variablesthrough a localLATENT_FUNCTIONSregistry composed from the analysis-latent module and the imaging-latent module. PreserveLATENT_BATCH_MODE = "jit"(inherited viaautogalaxy/analysis/analysis/dataset.py:28). RaiseNotImplementedErrorwhenLATENT_KEYS == []so autofit skips cleanly. -
Unit tests at
test_autolens/analysis/test_latent.pyandtest_autolens/imaging/model/test_latent.py. Spy/mock test thateffective_einstein_radiuscalls intoLensCalc.einstein_radius_jit_from. No JAX in unit tests.
Where to look
- PyAutoFit hook (do not modify):
autofit/non_linear/analysis/analysis.py:34, 170, 285. - Einstein radius JAX helper:
PyAutoGalaxy/autogalaxy/operate/lens_calc.py:1520-1537and the closure cache at 1580-1586. LATENT_BATCH_MODEconstraint:PyAutoGalaxy/autogalaxy/analysis/analysis/dataset.py:28.- Reference implementation:
euclid_strong_lens_modeling_pipeline/util.py:306-490. - PyAutoGalaxy mirror (now shipped):
PyAutoGalaxy/autogalaxy/imaging/model/latent.py,autogalaxy/config/latent.yaml,autogalaxy/imaging/model/analysis.pyLATENT_KEYSproperty andcompute_latent_variablesmethod.
Suggested branch
feature/latent-module-autolens
Notes
- Lens-side latent is named
total_lens_flux_mujy(nottotal_galaxy_0_flux_mujyfrom PyAutoGalaxy). Per-user direction during /start_dev — PyAutoLens uses lensing-vocabulary names, doesn't re-export PyAutoGalaxy's registry. - Default OFF in library yaml to avoid the
magzeroregression that PyAutoGalaxy Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441 caught. - Per memory
feedback_jax_closure_cache_busts: when delegating toeinstein_radius_jit_from, ensure the same(closure, solver)pair is reused across calls. - Per memory
feedback_no_silent_guards: ifmagzerois missing when an enabled latent needs it, raise loudly.
Activity
Jammy2211 commentedon May 23, 2026
Smoke Test Results — 2026-05-23
All 43 smoke tests passed across 6 workspaces. 1 pre-existing skip.
Worktree verified:
/home/jammy/Code/PyAutoLabs-wt/latent-module-autolens/PyAutoLens/autolens/__init__.py(sibling libraries autofit, autoarray, autogalaxy also resolved through worktree root)
Pre-existing skip (unchanged from before this PR):
autolens_workspace_test—database/scrape/general.py— listed inno_run.yamlasNEEDS_FIX 2026-04-27:TypeError: __hash__ method should return an integerinabstract_fit.linear_light_profile_intensity_dictduring subplot rendering after search completes. Not introduced by this PR.Wall-clock total: ~222 seconds (all 6 workspaces run in parallel, scripts within each workspace also parallel).
Note on
aggregator/fit_interferometer.pyandaggregator/tracer.py: logs containWARNING - Failed to load latent variables— this is an expected autofit aggregator WARNING in test mode (no sampler internals to load), not an error. Both scripts exit cleanly with "All ... aggregator tests passed."Jammy2211 commentedon May 23, 2026
Shipped — 2026-05-23
PR
Summary
Second sub-prompt of the latent_refactor epic landed. PyAutoLens now ships five lensing latents (
total_lens_flux_mujy,total_lensed_source_flux_mujy,total_source_flux_mujy,magnification,effective_einstein_radius) inautolens/analysis/latent.py, plusautolens/config/latent.yamltoggle layer andAnalysisImagingdispatch (LATENT_KEYS @Property + compute_latent_variables with NotImplementedError short-circuit). All five default OFF — existing fits unchanged. Library tests 311/311; cross-workspace smoke 43/43 across 6 workspaces with the worktree active.Session Notes
autolens/analysis/latent.py(tracer-only) +autolens/imaging/model/latent.py(image-derived), but the user flagged that all five latents are dataset-agnostic — they usefit.tracer/fit.galaxy_image_dict/fit.dataset.grids.lp, APIs shared betweenFitImagingandFitInterferometer(both PyAutoGalaxy and PyAutoLens versions). Collapsed to a singleautolens/analysis/latent.pyso futureAnalysisInterferometerwiring can reuse the registry without duplication.total_lens_flux_mujy(per user direction), NOT re-exported astotal_galaxy_0_flux_mujyfrom PyAutoGalaxy — PyAutoLens owns its full registry with lensing-vocabulary names.super().compute_latent_variables()and append aperture values).compute_latent_samplesruns on every fit (latent_after_fit: trueis autofit default), so default-on would crash users withoutmagzero. Same regression-avoidance pattern as PyAutoGalaxy Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441.feedback_autoconf_lowercases_yaml_keysandfeedback_latent_modules_colocate_with_model_apimemories from PyAutoGalaxy Drop Binder URLs, pin Colab URLs to workspace tag 2026.4.13.6 #441 carried over cleanly — no new memories from this task.PyAutoPrompt/z_features/latent_refactor.md: euclid migration (scipy.integrate Requires LowLevelCallable #3, ready), autofit_workspace tutorial expand (scipy.spatial.Voronoi does not compute list of neighbouring points of each Voronoi cell #4), autogalaxy_workspace + autolens_workspace tutorials (KMeans initialize seed points #5, Speed up computation of F matrix via clustering / Voronoi grid information? #6), smoke test (List of neighbors of each Voronoi cell #7), profiling package (Variable PSF convolution kernel size to speed up convolution step #8), prior-mapping investigation (IDEA: Perform weight KMeans clustering in the image-plane #9).feat: first-class lensing latent variable API in PyAutoLens