Skip to content

feat: AnalysisWeak — weak lensing modeling (weak series step 4) #579

Closed

Description

@Jammy2211

Overview

Step 4 of the weak-lensing series (PyAutoMind/z_features/weak_shear.md): add al.AnalysisWeak so a non-linear search can fit lens mass models to a WeakDataset shear catalogue, mirroring the established model API of imaging/point, plus a workspace scripts/weak/modeling.py tutorial. This is the keystone of the weak-lensing home-straight push — series steps 5 (likelihood_function), 7 (Abell 2744 real data) and 8 (combined strong+weak via summed analyses) all depend on it.

Science scope: cluster/group-scale weak shear complementing strong lensing (Niemiec et al. 2020 hybrid-Lenstool; Oguri et al. 2012 SGAS) — not cosmic shear or galaxy-galaxy lensing.

Plan

  • Add autolens/weak/model/ mirroring autolens/point/model/ (the right template — FitWeak already follows FitPoint's standalone pattern): AnalysisWeak, ResultWeak, VisualizerWeak, PlotterWeak.
  • AnalysisWeak.log_likelihood_function builds a Tracer from the model instance and returns FitWeak(dataset, tracer).log_likelihood.
  • Wire on-the-fly visualization through the existing nine weak plotters (subplot_weak_dataset, subplot_fit_weak, subplot_fit_quick), with new weak_dataset / fit_weak sections in the packaged config/visualize/plots.yaml.
  • Export al.AnalysisWeak in autolens/__init__.py; NumPy-only unit tests mirroring test_autolens/point/model/.
  • Ship library PR first; workspace follow-up adds scripts/weak/modeling.py (judgment-tier tutorial prose, style of scripts/imaging/modeling.py) and mirrors the new plots.yaml keys into workspace config.
Detailed implementation plan

Affected Repositories

  • PyAutoLens (primary — library)
  • autolens_workspace (follow-up — workspace tutorial + config mirror)

Branch Survey

Repository Current Branch Dirty?
./PyAutoLens main clean
./autolens_workspace main dirty (7 pre-existing regenerated dataset files + stray dataset.fits, unrelated to this task)

Suggested branch: feature/weak-modeling
Worktree: ~/Code/PyAutoLabs-wt/weak-modeling/
Note: autolens_workspace is currently claimed by task lenstool-example. Library work proceeds first (combined workflow); the workspace follow-up uses the parallel-worktree pattern proven twice in this series (scripts/weak/ has zero file overlap with lenstool work) if the claim still stands at ship time.

Implementation Steps

  1. autolens/weak/model/__init__.py — empty, matching point/model.
  2. autolens/weak/model/analysis.py — class AnalysisWeak(AgAnalysis, AnalysisLens):
    • Visualizer = VisualizerWeak, Result = ResultWeak class attributes.
    • __init__(self, dataset: WeakDataset, cosmology=None, title_prefix=None, use_jax=False, **kwargs). use_jax=False default: FitWeak is NumPy (cached_property + np.asarray, no xp threading); JAX support is deliberate future work (needs pytree registration like AnalysisPoint._register_fit_point_pytrees) — do NOT bolt it on here.
    • log_likelihood_function(instance) → self.fit_from(instance).log_likelihood.
    • fit_from(instance) → tracer_via_instance_from(instance) → FitWeak(dataset=self.dataset, tracer=tracer).
    • save_attributes(paths) → ag.output_to_json(self.dataset, paths._files_path / "dataset.json") (round-trip works since the weak-dataset-from-json fix).
  3. autolens/weak/model/result.py — ResultWeak(Result) mirroring ResultPoint (13 lines): fallback uniform grid property + max_log_likelihood_fit via analysis.fit_from(self.instance).
  4. autolens/weak/model/plotter.py — PlotterWeak(Plotter) mirroring PlotterPoint: dataset_weak() → subplot_weak_dataset behind plot_setting(section=["weak_dataset"], ...); fit_weak(fit, quick_update=False) → subplot_fit_weak / subplot_fit_quick behind plot_setting(section=["fit_weak"], ...).
  5. autolens/weak/model/visualizer.py — VisualizerWeak(af.Visualizer) mirroring VisualizerPoint (107 lines): visualize_before_fit plots the dataset; visualize plots the fit from the instance, forwarding quick_update.
  6. Config: add to autolens/config/visualize/plots.yaml (after the point_dataset/fit_point_dataset block, same comment style):
    • weak_dataset: with subplot_dataset: true
    • fit_weak: with subplot_fit: true
  7. Exports: autolens/__init__.py — from .weak.model.analysis import AnalysisWeak (alongside the existing weak exports at lines 128-130).
  8. Tests (NumPy-only, no JAX per repo rules): test_autolens/weak/test_analysis.py (+ plotter test if cheap) mirroring test_autolens/point/model/test_analysis_point.py:
    • log_likelihood_function(instance) == FitWeak(dataset, tracer).log_likelihood for a SimulatorShearYX(seed=1) dataset and Isothermal instance.
    • noise-free round-trip: simulator tracer fitted to its own noise-free output gives chi_squared == 0 (bit-exact, both use shear_yx_2d_via_hessian_from).
    • save_attributes writes a loadable dataset.json (al.from_json round-trip).
    • Use direct module imports in tests (the al.plot.X pytest recursion gotcha from step 2 applies).
  9. Run full python -m pytest test_autolens/ before PR.

Workspace follow-up (second PR, after library merges)

  1. autolens_workspace/scripts/weak/modeling.py — Nautilus fit of an Isothermal lens to the dataset/weak/simple dataset, prose in the style of scripts/imaging/modeling.py (judgment-tier tutorial prose per WORKFLOW.md).
  2. Mirror the new weak_dataset / fit_weak plots.yaml sections into autolens_workspace/config/visualize/plots.yaml (workspace configs override packaged defaults).

Key Files

  • autolens/point/model/{analysis,result,plotter,visualizer}.py — the template (233/13/97/107 lines).
  • autolens/weak/fit.py — FitWeak, already exposes log_likelihood.
  • autolens/weak/plot/{weak_dataset_plots,fit_weak_plots}.py — the nine existing plot helpers + subplot_fit_quick.
  • autolens/config/visualize/plots.yaml — visualization settings read by plot_setting.

Autonomy

--auto launch 2026-07-09, effective level supervised (header supervised, feature cap supervised): plan-to-issue (this issue), mechanical stretches proceed, ship sign-off parks as a batched question here, merge stays human. No Heart YELLOW acknowledgement was given at launch. Brain Feature Agent scored this too-large and suggested a 4-phase split; consolidated to the series' proven 2-PR shape (library, then workspace) — recorded here for post-hoc review.

Original Prompt

Click to expand starting prompt

We are now going to add weak lensing modeling.

First, we need to create the analysis.py module, so inspect @autolens_workspace/scripts/weak and
@PyAutoLens/autolens/imaging/model . We are basically going to make everything weak does from here a "mirror" of
the imaging model API (and also interferoter.)

The log_likelihod_function is in particular important, we already have the codw which creates the shear field
show in the @autolens_workspace/scripts/weak/simulator.py file and we can turn it into a fit in
@PyAutoLens/autolens/imaging/fit.py, so shouldnt be hard to work out the pattern and whats needed here.

I believe everything else (plotter.py, result.py, visualizer.py) should be relatively straight forward to work
out by simply copying the patterns and logic from the imaging module, but keep an eye out for unexpected tricky
parts.

Finally, read @autolens_workspace/scripts/imaging/modeling.py and then make an equivalent for
weak lensing.

Activity

  1. Jammy2211 commented on Jul 9, 2026

    @Jammy2211
    CollaboratorAuthor

    Ship sign-off requested (supervised checkpoint — run parked here)

    What was done. Both halves of this task are implemented, validated and committed locally on feature/weak-modeling in the task worktree — nothing has been pushed. Library: autolens/weak/model/{analysis,result,plotter,visualizer}.py mirroring point/model, al.AnalysisWeak export, weak_dataset/fit_weak plots.yaml sections, 5 new NumPy-only tests (commit 8a0733d47, +448 lines). Workspace: scripts/weak/modeling.py tutorial + config mirror (commit 58f42a9a, +273 lines), developed on a parallel autolens_workspace worktree per the series precedent (lenstool-example's claim touches only root metadata files — zero overlap with scripts/weak/).

    Validation.

    • PyAutoLens full suite: 347 passed (includes 32 weak tests, 5 new) in the task worktree.
    • Visualizer smoke: visualize_before_fit + visualize (full + quick_update) wrote subplot_weak_dataset.png, subplot_fit_weak.png, fit_quick.png, galaxies.png.
    • End-to-end: scripts/weak/modeling.py ran a real Nautilus fit (~7 min CPU): recovered einstein_radius = 1.556 (1.476–1.631) vs truth 1.6, centre at origin, ell_comps within ~1σ for 200 galaxies at σ=0.3 shape noise. The search's own VisualizerWeak produced the on-the-fly subplots in output/.../image/.

    Heart: YELLOW — reasons: workspace validation 5-failed report of 2026-07-08 11:25 (known stale per the morning triage; zero_contour fixes were merged but never re-validated), 58 stale parked scripts, autolens_assistant version pin behind, install verification not run, no release validation for current source, PyAutoMind open PR 9d old. No YELLOW acknowledgement was given at launch, so the run parks rather than ships (per AUTONOMY.md checkpoint 3).

    The question. Approve shipping? On approval the run will: push both branches, open the PyAutoLens PR (pending-release, body below) and the autolens_workspace PR (library-first merge gate noted in its body), append the autonomy-log row. If you want changes, comment them here and the next session resumes from active.md.


    Draft PyAutoLens PR body

    Summary

    Adds al.AnalysisWeak so a non-linear search can fit lens mass models to a WeakDataset shear catalogue — step 4 (the keystone) of the weak-lensing series (#579). autolens/weak/model/ mirrors autolens/point/model/: FitWeak already follows FitPoint's standalone pattern, and AnalysisWeak.log_likelihood_function builds a Tracer from the instance and returns FitWeak(dataset, tracer).log_likelihood. VisualizerWeak/PlotterWeak wire the nine existing weak quiver plotters into on-the-fly search visualization.

    API Changes

    Added only — no existing symbol removed, renamed or changed.

    • al.AnalysisWeak(dataset, cosmology=, title_prefix=, use_jax=False) — new Analysis class for WeakDataset fits. use_jax defaults to False (FitWeak is NumPy-only; JAX needs pytree registration — deliberate future work).
    • New packaged config sections weak_dataset / fit_weak in config/visualize/plots.yaml (workspaces should mirror — done in the companion workspace PR).
      See full details below.

    Test Plan

    • Full test_autolens/ suite in the task worktree: 347 passed.
    • New test_autolens/weak/test_analysis.py: analysis-vs-FitWeak likelihood equality, noise-free zero-chi-squared round trip, wrong-model likelihood ordering, save_attributes dataset.json round trip.
    • End-to-end Nautilus fit via autolens_workspace/scripts/weak/modeling.py recovers simulator truth within 1σ.

    Validation checklist (--auto run — plan was not pre-approved)

    • Effective level: supervised (header: supervised, cap: feature → supervised)
    • Plan: on the issue (feat: AnalysisWeak — weak lensing modeling (weak series step 4) #579), written at start, unmodified since (Brain suggested a 4-phase split; consolidated to the series' proven 2-PR shape)
    • Gate: tests 347-pass · smoke pending sign-off · review pending sign-off · Heart YELLOW (no launch ack → parked at sign-off)
    • Human: plan sound in hindsight?
    • Human: diff matches plan (no scope creep)?
    • Human: merge, amend, or reject — then log the outcome
    Full API Changes (for automation & release notes)

    Added

    • autolens.weak.model.analysis.AnalysisWeak (exported as al.AnalysisWeak) — fits lens mass models to WeakDataset shear catalogues; Result = ResultWeak, Visualizer = VisualizerWeak.
    • autolens.weak.model.result.ResultWeak — Result subclass with fallback uniform grid and max_log_likelihood_fit (a FitWeak).
    • autolens.weak.model.plotter.PlotterWeak — dataset_weak() / fit_weak(quick_update=) behind plot_setting sections weak_dataset / ["fit", "fit_weak"].
    • autolens.weak.model.visualizer.VisualizerWeak — af.Visualizer for search-time output (dataset subplot before fit; fit subplot + tracer/galaxies during).
    • Config: weak_dataset.subplot_dataset: true and fit_weak: {} in packaged config/visualize/plots.yaml.

    Migration

    • None required — purely additive.

    🤖 Generated with Claude Code


    Draft autolens_workspace PR body

    Summary

    Adds scripts/weak/modeling.py — the weak-lensing model-fit tutorial completing the simulate → fit → model core workflow of the series — and mirrors the new weak_dataset/fit_weak visualize/plots.yaml sections into the workspace config. Companion to the PyAutoLens AnalysisWeak PR (library-first merge gate: merge the library PR before this one).

    Scripts Changed

    • scripts/weak/modeling.py (new) — Nautilus Isothermal fit to the simulated WeakDataset via al.AnalysisWeak; auto-simulates the dataset if missing; validated end-to-end (truth recovered within 1σ, ~7 min CPU).
    • config/visualize/plots.yaml — adds weak_dataset / fit_weak sections mirroring the new packaged defaults.

    🤖 Generated with Claude Code

  2. Jammy2211 commented on Jul 9, 2026

    @Jammy2211
    CollaboratorAuthor

    Ship approved — PRs open 🚀

    Thanks for the sign-off! Both branches are pushed and the PRs are open with pending-release:

    Workspace impact: the API change is Added-only (al.AnalysisWeak), so no migration is needed anywhere — the new demo script in #241 is the only downstream consumer. Heart's YELLOW reason set was acknowledged as part of this approval.

    Merge order: #580 first, then #241, then this issue can close. Once merged, the weak series home straight continues: step 5 (likelihood_function.py) and step 6 (shear-profile + Kaiser-Squires visualization) unblock immediately; step 7 (Abell 2744 real data) and step 8 (combined strong+weak) build on this Analysis class.

  3. Jammy2211 commented on Jul 9, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped ✅

    Both PRs are merged (library-first order respected):

    The weak-lensing series now has its keystone: simulate → fit → model all work end to end, and steps 5 (likelihood_function), 6 (shear-profile/Kaiser-Squires visualization), 7 (Abell 2744 real data), 8 (combined strong+weak) and 9 (PYAUTO_SMALL_DATASETS smoke support) are unblocked. Continuing the home straight under --auto: step 6 next, then 5.

    Closing this issue.

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