Skip to content

fix: sparse imaging inversion uses profile-subtracted data - #681

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/sparse-inversion-subtracted-data
Nov 9, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/sparse-inversion-subtracted-data

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Closes #680.

Summary

InversionImagingSparse.psf_weighted_data built the data vector D from sparse_operator.weight_map, which is data / noise**2 baked from the original image when apply_sparse_operator ran. A fit with a regular light profile hands the inversion the profile-subtracted data via DatasetInterface, so every sparse-operator imaging fit with a regular (non-linear) light profile plus a pixelization inverted the unsubtracted image, silently.

psf_weighted_data is now formed from the inversion's own data / noise_map. The operator's noise-only curvature terms are still reused. The numba sparse path already did this.

Implementation note: the first version scattered the slim weights into a native map (.at[rows, cols].set). It reproducibly hung the vmapped likelihood of autolens_workspace_test/scripts/imaging/jax_likelihood/rectangular_mge.py on CPU (0 of 3 runs completed; main passed). The shipped version is gather-only. A static [N, Ky, Kx] neighbour slim-index table is built once with NumPy from the mask, then gathers data / noise**2 with a zero sentinel. That script now passes 4 of 4 runs (~29s, the same as main).

End-to-end through al.FitImaging (31x31, regular Sersic lens + 12x12 rectangular source):

sparse log_evidence dense log_evidence chi² sparse / dense
main −69075.3 +277.98 131797 / 819.16
this PR +277.98 +277.98 (Δ 1.1e-13) 819.16 / 819.16
no-light-profile control (both) −65826.40 −65826.40 identical

Interferometer sparse path: checked, and it does not share this seam. DatasetInterface carries sparse_dirty_image / data_term, PyAutoGalaxy and PyAutoLens fit_interferometer pass the profile-subtracted ones, and both PyAutoArray readers (interferometer/sparse.py, inversion_interferometer_matrix_free_util.py) prefer them over the operator's cached values. No change.

API Changes

None to existing public behaviour, other than the bug fix: sparse imaging inversions now honour the interface's data. Two internal helpers were added to inversion_imaging_util. ImagingSparseOperator.weight_map is still built but no longer read by the inversion.
See full details below.

Test Plan

  • New witness test__sparse__profile_subtracted_data__matches_mapping_inversion[True|False]: sparse vs dense agree on D, chi-squared and log_evidence to 1e-10 with a regular light profile. The False leg is the all-linear control. The True leg fails on main.
  • New ..._matches_mapping_inversion__jax_jit: JAX eager and jax.jit (data traced) D agree with dense to 1e-10. Fails on main.
  • New test__psf_weighted_data_via_slim__matches_native_weight_map_path: the gather helper equals the old native-map path to 1e-14.
  • Full suite: 2298 passed, 1 skipped, 4 xfailed (main baseline: 2294 / 1 / 4).
  • autolens_workspace_test with PYAUTO_TEST_MODE=1: misc/jax_assertions/fit_imaging_sparse_operator.py, imaging/jax_likelihood/{rectangular, rectangular_rtu, rectangular_dspl, rectangular_dspl_rtu, rectangular_mge, delaunay_mge}.py, imaging/convolution_over_sampled.py all pass. Pinned likelihoods are unchanged (these fit all-linear models).
  • imaging/jax_likelihood/rectangular_mge_rtu.py hits the 300s cap in its vmap step on main as well (2 of 2 runs here, CPU container). This is pre-existing and not caused by this PR.
Full API Changes (for automation & release notes)

Added

  • autoarray.inversion.inversion.imaging.inversion_imaging_util.psf_weighted_data_neighbour_indexes_from(native_index_for_slim_index, shape_native, kernel_shape_native): static [N, Ky, Kx] slim-index table for a PSF kernel centred on each unmasked pixel. The sentinel N marks masked or out-of-bounds pixels.
  • autoarray.inversion.inversion.imaging.inversion_imaging_util.psf_weighted_data_via_slim_from(data, noise_map, kernel_native, neighbour_indexes, xp=np): gather-only psf_weighted_data from slim data and noise-map.

Changed Behaviour

  • InversionImagingSparse.psf_weighted_data (and hence data_vector, reconstruction, chi-squared, log_evidence) now uses the inversion dataset's data / noise_map instead of sparse_operator.weight_map. Results are unchanged when data is the operator's original image.

Migration

  • None required.

Generated by the PyAutoLabs agent workflow.


Generated by Claude Code

InversionImagingSparse.psf_weighted_data read sparse_operator.weight_map,
which is data/noise**2 baked from the ORIGINAL image when the operator was
built. A fit with a regular light profile hands the inversion the
profile-subtracted data via DatasetInterface, so the sparse data vector D
inverted the unsubtracted image (chi-squared/log_evidence badly wrong).

psf_weighted_data is now formed from the inversion's own data/noise_map via
a gather-only helper: a static [N, Ky, Kx] neighbour slim-index table (built
once with NumPy from the mask) gathers data/noise**2 with a zero sentinel.
A native-array scatter (.at[].set) formulation reproducibly hung the
rectangular_mge vmap likelihood on CPU; the gather form does not.

Witness: sparse vs dense agree on D, chi-squared and log_evidence to 1e-10
with a regular light profile (NumPy, JAX eager and jax.jit); the all-linear
control is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016nhQ8MFwfRFgwBVbYjP9Xd
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Nov 8, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit c020cc2 into main Nov 9, 2026
3 checks passed
@Jammy2211
Jammy2211 deleted the feature/sparse-inversion-subtracted-data branch November 9, 2026 10:43
@pyautolabs-bot pyautolabs-bot Bot removed the pending-release PR queued for the next release build label Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: sparse imaging inversion ignores profile-subtracted data

2 participants