Repository navigation
fix: sparse imaging inversion uses profile-subtracted data - #681
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #680.
Summary
InversionImagingSparse.psf_weighted_databuilt the data vectorDfromsparse_operator.weight_map, which isdata / noise**2baked from the original image whenapply_sparse_operatorran. A fit with a regular light profile hands the inversion the profile-subtracted data viaDatasetInterface, so every sparse-operator imaging fit with a regular (non-linear) light profile plus a pixelization inverted the unsubtracted image, silently.psf_weighted_datais now formed from the inversion's owndata/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 ofautolens_workspace_test/scripts/imaging/jax_likelihood/rectangular_mge.pyon 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 gathersdata / noise**2with 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):Interferometer sparse path: checked, and it does not share this seam.
DatasetInterfacecarriessparse_dirty_image/data_term, PyAutoGalaxy and PyAutoLensfit_interferometerpass 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 toinversion_imaging_util.ImagingSparseOperator.weight_mapis still built but no longer read by the inversion.See full details below.
Test Plan
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. TheFalseleg is the all-linear control. TheTrueleg fails on main...._matches_mapping_inversion__jax_jit: JAX eager andjax.jit(data traced) D agree with dense to 1e-10. Fails on main.test__psf_weighted_data_via_slim__matches_native_weight_map_path: the gather helper equals the old native-map path to 1e-14.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.pyall pass. Pinned likelihoods are unchanged (these fit all-linear models).imaging/jax_likelihood/rectangular_mge_rtu.pyhits 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 sentinelNmarks 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-onlypsf_weighted_datafrom slim data and noise-map.Changed Behaviour
InversionImagingSparse.psf_weighted_data(and hencedata_vector, reconstruction, chi-squared, log_evidence) now uses the inversion dataset'sdata/noise_mapinstead ofsparse_operator.weight_map. Results are unchanged whendatais the operator's original image.Migration
Generated by the PyAutoLabs agent workflow.
Generated by Claude Code