Skip to content

fix: sparse imaging inversion ignores profile-subtracted data #680

Closed

Description

@Jammy2211

Overview

InversionImagingSparse.psf_weighted_data builds the data vector D from dataset.sparse_operator.weight_map, which is data / noise**2 baked from the original image when apply_sparse_operator ran. FitImaging hands the inversion a DatasetInterface whose data is the profile-subtracted image, but that subtracted data never reaches D. So any sparse-operator imaging fit with a regular light profile plus a pixelized source inverts the unsubtracted image: D is off by about 20x and chi-squared and log_evidence are badly wrong, all silently. Fits with no regular light profile are unaffected.

Plan

  • Build psf_weighted_data from the inversion's own (interface) data and noise_map instead of the operator's baked weight map. This matches what the numba sparse path already does.
  • Leave the operator's precomputed curvature terms alone. They depend only on the noise-map, which the fit does not change.
  • Add a regression test with a regular light profile plus a pixelized source, built on an operator made from the original image. Sparse and dense inversions must agree on D, chi-squared and log_evidence to 1e-10. A no-profile control must stay unchanged.
  • Interferometer sparse path: checked, and it does not share this seam. The interface carries an explicit sparse_dirty_image and data_term, PyAutoGalaxy and PyAutoLens fit_interferometer pass the subtracted ones, and both readers in PyAutoArray (interferometer/sparse.py, inversion_interferometer_matrix_free_util.py) prefer them over the operator's cached values. No change there.
  • Tier: glance (declared). Merge mode for this session: human /prm, by maintainer instruction.
Detailed implementation plan

Affected Repositories

  • PyAutoArray (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoArray main clean

Suggested branch: feature/sparse-inversion-subtracted-data

Note: PyAutoArray is also claimed in Mind by magnification-maps-wiring (PyAutoLens#818). By maintainer override, this task runs in parallel on its own branch. The diffs don't overlap (inversion/imaging/sparse.py).

Implementation Steps

  1. autoarray/inversion/inversion/imaging/sparse.py — psf_weighted_data: build the native weight map from self.data / self.noise_map**2. Scatter the slim values to native via mask.derive_indexes.native_for_slim, with zeros off-mask, using numpy or JAX through self._xp. Then pass that map to inversion_imaging_util.psf_weighted_data_from.
  2. test_autoarray/inversion/inversion/imaging/ — witness test: (a) sparse operator built from the original image, then the inversion run on a DatasetInterface with subtracted data. D, chi-squared and log_evidence must match InversionImagingMapping to 1e-10. (b) Control with unmodified data stays equal.
  3. Run the PyAutoArray unit suite plus the relevant autolens_workspace_test sparse/pixelization scripts.

Key Files

  • autoarray/inversion/inversion/imaging/sparse.py — seam
  • autoarray/inversion/inversion/imaging/inversion_imaging_util.py — psf_weighted_data_from, ImagingSparseOperator.from_ (bakes weight_map)
  • autoarray/inversion/inversion/imaging_numba/sparse.py — reference: already uses self.data
  • autoarray/inversion/inversion/interferometer/sparse.py — checked, honours sparse_dirty_image

Original Prompt

Click to expand starting prompt

Mind prompt: draft/bug/autoarray/sparse_inversion_ignores_profile_subtracted_image.md (Priority: high, Consequence: glance, Witness: a regression test with a regular light profile + pixelized source where the sparse-operator and dense inversions agree on D, chi-squared and log_evidence to 1e-10).

Symptom. InversionImagingSparse.psf_weighted_data reads dataset.sparse_operator.weight_map, which is data / noise**2 baked at apply_sparse_operator time from the ORIGINAL image. FitImaging.galaxies_to_inversion swaps data for profile_subtracted_image in the DatasetInterface but passes the operator through unchanged, so the sparse data vector D never sees the subtraction.

Measured 2026-09-11 (400-px rectangular mesh, true-Sersic regular lens light + pixelized source):

leg inversion class chi2 log_evidence
sparse (apply_sparse_operator) InversionImagingSparse 154083 −38713
dense InversionImagingMapping 16239 +31051

max|D_sparse − D_dense| / max|D_dense| = 19.8. The control with no regular light profile agrees to 2.2e-16, so all-linear models — and today's production GPU runs, which fit the plain dataset — are unaffected. Any apply_sparse_operator user with a regular lens light (SLaM after light[1] with the light fixed, foreground-subtracted fits) is silently wrong.

Fix. Build psf_weighted_data from the interface's (subtracted) data — rebuild or pass the subtracted weight map into the operator rather than reading the one baked from the original image — and add the regression test named in the witness. Check whether the interferometer sparse path has the same seam.

Activity

  1. Jammy2211 commented on Nov 8, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR created: #681

    • Fix: InversionImagingSparse.psf_weighted_data now uses the inversion dataset's (profile-subtracted) data / noise_map through a gather-only helper. A first scatter-based version reproducibly hung the rectangular_mge.py vmap likelihood on CPU, so it was replaced (details in the PR).
    • Witness: sparse vs dense agree on D, chi-squared and log_evidence to 1e-10 with a regular light profile (NumPy, JAX eager, jax.jit), and the no-profile control is unchanged. End-to-end al.FitImaging: sparse log_evidence goes from −69075 (main) to +277.98, matching dense to 1e-13.
    • Interferometer: no seam. The subtracted sparse_dirty_image / data_term are already passed by Galaxy and Lens and honoured in PyAutoArray.
    • Tests: unit suite 2298 passed / 1 skipped / 4 xfailed. Relevant autolens_workspace_test imaging sparse scripts pass. rectangular_mge_rtu.py hangs in its vmap on main too, so that's pre-existing.
    • Workspace impact: (iii) none required. Optional (i): a workspace_test assertion for regular-light + sparse parity, since no current script covers that path. The maintainer decides.
    • Process note: PyAutoArray is also claimed by magnification-maps-wiring (PyAutoLens#818). This task runs in parallel under an explicit human override given 2026-11-08 in session https://claude.ai/code/session_016nhQ8MFwfRFgwBVbYjP9Xd. The files don't overlap.
    • Merge: human /prm. The declared tier is glance, but the maintainer asked to review every PR.

    Generated by Claude Code

  2. Jammy2211 commented on Nov 9, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped — 2026-11-09

    • Merged: fix: sparse imaging inversion uses profile-subtracted data #681 (merge commit c020cc2), via /prm with CI 3/3 legs green.
    • Sparse imaging inversions now build their data vector from the profile-subtracted data. Witness: sparse vs dense agree on D, chi-squared and log_evidence to 1e-10, and the no-profile control is unchanged.
    • Pending release: PyAutoArray (pending-release label on the PR).
    • Not changed: the interferometer sparse path, which already honoured the subtracted dirty image / data term.

    Generated by Claude Code

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