Skip to content

feat: weak-lensing shear visualization (WeakDataset plotters) #496

Closed

Description

@Jammy2211

Overview

Step 2 of the weak gravitational lensing series. Step 1 (PyAutoLens #472) added the WeakDataset class and SimulatorShearYX simulator, plus the workspace simulator script autolens_workspace/scripts/weak/simulator.py with two # TODO(2_visualization.md) placeholders. This task adds the visualization layer that fills those placeholders — module-level aplt.subplot_weak_dataset and aplt.plot_shear_yx_2d helpers, drawn as quivers of headless line segments (the standard weak-lensing-paper convention since shear is a spin-2 quantity).

Plan

  • Add a weak/plot/ package inside PyAutoLens with module-level plot helpers for shear catalogues (single-panel and a 2×2 subplot mosaic).
  • Re-export the new helpers into the aplt namespace through autolens.plot.__init__, mirroring how subplot_imaging_dataset is exposed.
  • Replace the # TODO(2_visualization.md) placeholders in autolens_workspace/scripts/weak/simulator.py with real aplt calls and a docstring prose block.
  • Add unit tests under PyAutoLens/test_autolens/weak/plot/ that assert the plotters run and write the expected output files, mirroring test_imaging_plotters.py.
Detailed implementation plan

Affected Repositories

  • PyAutoLens (primary — new plot module + tests)
  • autolens_workspace (follow-up — replace TODO placeholders in weak/simulator.py)

Work Classification

Library, with workspace follow-up after the library PR merges.

Branch Survey

Repository Current Branch Dirty?
PyAutoLens main clean
autolens_workspace main dirty (unrelated dataset/** / start_here.py noise — scripts/weak/ clean)

Suggested branch: feature/weak-visualization
Worktree root: ~/Code/PyAutoLabs-wt/weak-visualization/ (created later by /start_library, once use-pathlib ships)

Blocked by

use-pathlib currently holds PyAutoLens via ~/Code/PyAutoLabs-wt/use-pathlib (along with PyAutoConf, PyAutoFit, PyAutoArray, PyAutoGalaxy). This task is queued in PyAutoPrompt/planned.md and can start once use-pathlib ships and releases the PyAutoLens worktree.

Design choice: functions, not a class

The prompt asks for a WeakDatasetPlotter "analogous to ImagingPlotter", but ImagingPlotter is not a class in this codebase — it's a set of module-level functions in PyAutoArray/autoarray/dataset/plot/imaging_plots.py (subplot_imaging_dataset, subplot_imaging_dataset_list, fits_imaging) re-exported into the aplt namespace via PyAutoLens/autolens/plot/__init__.py:14. The point analogue (autolens/point/plot/point_dataset_plots.py:7) follows the same functional pattern. We match that — module-level functions, not a class.

Implementation Steps

  1. New plot module — PyAutoLens/autolens/weak/plot/ (matching autolens/point/plot/):

    • weak_dataset_plots.py with module-level functions:
      • plot_shear_yx_2d(shear_yx, ax=None, ...) — single-panel quiver. Use shear_yx.ellipticities + shear_yx.phis to derive (u, v) = (mag·cos(phi), mag·sin(phi)), then ax.quiver(x, y, u, v, mag, pivot="middle", headwidth=0, headlength=0, headaxislength=0). Do not index [:, 0] / [:, 1] directly — phis / ellipticities are the abstraction (per prompt point 5, insulating against the ShearYX2D[:,0]=γ₂, [:,1]=γ₁ storage convention pinned by PyAutoGalaxy PR Jax to Main!  #366).
      • plot_ellipticities(shear_yx, ax=None, ...) — color-coded scatter of |γ|, reuses autoarray.plot.grid.plot_grid with color_array.
      • plot_phis(shear_yx, ax=None, ...) — color-coded scatter of φ, cyclic colormap.
      • plot_noise_map(noise_map, positions, ax=None, ...) — color-coded scatter of per-galaxy noise σ.
      • subplot_weak_dataset(dataset, ...) — 2×2 mosaic: shear field | noise map / |γ| | φ. Uses subplots, subplot_save, conf_subplot_figsize, tight_layout from autoarray.plot.utils.
  2. aplt namespace wiring — edit PyAutoLens/autolens/plot/__init__.py to import the 5 new helpers (insert next to subplot_point_dataset at line 66).

  3. Workspace script update — replace the TODO block at autolens_workspace/scripts/weak/simulator.py:109-113 with a """__Visualize__""" docstring (which is the workspace's notebook-cell convention — PyAutoBuild/autobuild/generate.py converts these to # %% markdown cells) followed by aplt.subplot_weak_dataset(dataset=dataset, output_path=dataset_path, output_format="png") and aplt.plot_shear_yx_2d(shear_yx=dataset.shear_yx, output_path=str(dataset_path), output_filename="shear_yx", output_format="png"). Keep the existing print(dataset.info) line.

  4. Unit tests — PyAutoLens/test_autolens/weak/plot/test_weak_dataset_plots.py. Build a small 4-galaxy ShearYX2DIrregular fixture, then assert each plot helper writes the expected file under plot_patch.paths (the plot_patch fixture comes from PyAutoLens/test_autolens/conftest.py, already used by test_autolens/lens/plot/ and test_autolens/point/plot/). Five tests total — one per helper.

Key Files

  • New: PyAutoLens/autolens/weak/plot/__init__.py — empty package marker.
  • New: PyAutoLens/autolens/weak/plot/weak_dataset_plots.py — 5 functions described above.
  • New: PyAutoLens/test_autolens/weak/plot/__init__.py — empty.
  • New: PyAutoLens/test_autolens/weak/plot/test_weak_dataset_plots.py — 5 tests.
  • Edit: PyAutoLens/autolens/plot/__init__.py — add 5 imports.
  • Edit: autolens_workspace/scripts/weak/simulator.py:109-113 — replace TODO block.

Reused utilities (no need to re-implement)

  • subplots, subplot_save, save_figure, conf_subplot_figsize, tight_layout, apply_labels, apply_extent — autoarray.plot.utils
  • plot_grid — autoarray.plot.grid (scalar-coloured scatter for the |γ| / φ / noise panels)
  • AbstractShearField.{ellipticities, phis, semi_major_axes, semi_minor_axes} — autogalaxy/util/shear_field.py:55
  • WeakDataset.{positions, extent_from} — autolens/weak/dataset.py:75,95

Verification

  1. NUMBA_CACHE_DIR=/tmp/numba_cache MPLCONFIGDIR=/tmp/matplotlib python -m pytest test_autolens/weak/ -v — all 5 new plot tests + existing dataset/simulator tests pass.
  2. After workspace follow-up: cd autolens_workspace && python scripts/weak/simulator.py — produces dataset/weak/simple/dataset_weak.png and shear_yx.png. Visually confirm tangential alignment around the lens centre at (0, 0) (consistent with the Isothermal mass profile).
  3. JAX cleanliness: grep "^import jax\|^from jax" PyAutoLens/autolens/weak/plot/*.py — no matches (the xp opt-in pattern; the plotters don't need JAX).
  4. Library-first merge gate: workspace edit ships only after the PyAutoLens PR merges (/ship_library → /ship_workspace).

Original Prompt

Click to expand starting prompt

Step 2 of the weak gravitational lensing series. Step 1 (1_simulator.md, shipped via PyAutoLens issue #472) added the WeakDataset data class and the SimulatorShearYX simulator, plus a workspace simulator script at @autolens_workspace/scripts/weak/simulator.py with # TODO(2_visualization.md) placeholders where the plots should go. This prompt adds the visualization layer.

The natural matplotlib primitive for shear fields is plt.quiver — each shear vector (gamma_2, gamma_1) at position (y, x) becomes an arrow whose length is the shear magnitude |gamma| = sqrt(gamma_1**2 + gamma_2**2) and whose orientation is the position angle phi = 0.5 * arctan2(gamma_2, gamma_1). For weak-lensing science papers the convention is usually to draw "headless" line segments rather than arrows (since shear is a spin-2 quantity, not a vector — a 180-degree rotation maps shear to itself). Quiver supports this via headwidth=0, headlength=0.

The ShearYX2D data structures already expose what you need: .ellipticities, .semi_major_axes, .semi_minor_axes, .phis, and .elliptical_patches. Use these directly where useful.

Please:

  1. Look at how plotters are structured in @PyAutoLens/autolens/plot and how aplt.subplot_imaging_dataset, aplt.plot_array, and similar helpers are wired through autolens.plot and autoarray.plot. The same module structure should host weak-lensing visualization.

  2. Add a WeakDatasetPlotter (analogous to ImagingPlotter) which can produce:

    • A scatter / quiver plot of the shear field on top of the source-galaxy positions.
    • A subplot mosaic showing the shear, its noise map, and (if useful) the ellipticities and position angles.
    • A subplot_weak_dataset helper at the aplt namespace level mirroring aplt.subplot_imaging_dataset.
  3. Replace the # TODO(2_visualization.md) placeholders in @autolens_workspace/scripts/weak/simulator.py with real calls to the new plotters, plus a # %% markdown cell explaining what's being shown.

  4. Add unit tests for the plotter (just check it runs without raising under the workspace test mode flags, like the existing ImagingPlotter tests do).

  5. The ShearYX2D [:, 0] = gamma_2, [:, 1] = gamma_1 storage convention pinned by PyAutoGalaxy PR Jax to Main!  #366 must be respected by the plotters. Use .phis / .ellipticities rather than indexing directly, so the plotters keep working if downstream we ever swap the storage order.

Good prior art outside the codebase: most weak-lensing papers (e.g. KiDS, DES) draw shear fields as a regular grid of headless line segments scaled by |gamma|. The quiver arguments pivot="middle", headwidth=0, headlength=0, headaxislength=0 produce exactly that.

Activity

  1. Jammy2211 commented on May 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created — 2026-05-18

    PR

    Status

    Workspace changes needed. Next: /start_workspace

    API Changes

    Five new module-level helpers in autolens.weak.plot.weak_dataset_plots, re-exported into the aplt namespace:

    • plot_shear_yx_2d — headless-quiver of the shear field
    • plot_ellipticities, plot_phis — scalar-coloured scatter
    • plot_noise_map — per-galaxy noise scatter
    • subplot_weak_dataset — 2×2 mosaic

    Pure additions, no removals or signature changes. The workspace follow-up replaces two # TODO(2_visualization.md) placeholders in autolens_workspace/scripts/weak/simulator.py with real aplt calls.

  2. added a commit that references this issue on May 18, 2026
  3. Jammy2211 commented on May 18, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped — 2026-05-18

    PRs

    Summary

    Step 2 of the weak-lensing series. Added a new autolens.weak.plot package with five module-level matplotlib helpers (plot_shear_yx_2d, plot_ellipticities, plot_phis, plot_noise_map, subplot_weak_dataset), all re-exported into the aplt namespace. The plotters render shear vectors as headless quiver segments — the spin-2 convention used across weak-lensing papers (KiDS, DES) since shear is unchanged under 180° rotation. The workspace simulator script's two # TODO(2_visualization.md) placeholders were filled with real aplt calls; scripts/weak/simulator.py now writes subplot_weak_dataset.png and shear_yx.png alongside the existing dataset / tracer JSON outputs.

    Session Notes

    • The plotters access the shear field exclusively through .ellipticities and .phis — never raw [:, 0] / [:, 1] indexing — so the [γ₂, γ₁] storage convention pinned by PyAutoGalaxy PR Jax to Main!  #366 stays encapsulated. If the storage order ever changes, only the property definitions in AbstractShearField need updating.
    • .phis returns degrees (not radians), so plot_shear_yx_2d converts via np.deg2rad before passing to quiver.
    • Tests use direct module imports (from autolens.weak.plot.weak_dataset_plots import ...) because al.plot.X hits a pre-existing recursion in autolens/__init__.py's __getattr__ under pytest's attribute-access flow. The point/imaging tests already follow this pattern; aplt-via-namespace still works fine from workspace scripts and notebooks.
    • Workspace work was done in a parallel autolens_workspace worktree alongside cluster-scaling-members (which held the registered claim). Zero file overlap (scripts/weak/ vs scripts/cluster/), so the parallelism was safe — bypassed the helper's task-level conflict check for the ~30-min window.
    • Next in the weak-lensing series: weak/3_fit.md (FitWeak class), weak/4_modeling.md (AnalysisWeak), weak/5_likelihood_function.md (tutorial). Each will be issued one at a time when its predecessor is close to shipping (per [[feedback_no_bulk_issue_queues]]).
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