From 614584870d1be5c71e7cc7fb001d9cd7a551bfa3 Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Sun, 22 Nov 2026 17:59:36 +0000 Subject: [PATCH] fix: retain ordinary light in mixed-galaxy images --- autolens/imaging/fit_imaging.py | 19 +++--- autolens/interferometer/fit_interferometer.py | 15 +++-- autolens/interferometer/model/result.py | 28 ++++----- autolens/lens/to_inversion.py | 12 ++++ test_autolens/imaging/test_fit_imaging.py | 21 +++++++ .../interferometer/test_fit_interferometer.py | 58 +++++++++++++++++-- 6 files changed, 116 insertions(+), 37 deletions(-) diff --git a/autolens/imaging/fit_imaging.py b/autolens/imaging/fit_imaging.py index 88f79847f..4425e5ebb 100644 --- a/autolens/imaging/fit_imaging.py +++ b/autolens/imaging/fit_imaging.py @@ -31,7 +31,10 @@ from autogalaxy.abstract_fit import AbstractFitInversion from autolens.lens.tracer import Tracer -from autolens.lens.to_inversion import TracerToInversion +from autolens.lens.to_inversion import ( + TracerToInversion, + _galaxy_image_dict_with_linear_reconstruction, +) from autolens import exc @@ -220,12 +223,11 @@ def galaxy_image_dict(self) -> Dict[ag.Galaxy, np.ndarray]: xp=self._xp ) - galaxy_linear_obj_image_dict = self.galaxy_linear_obj_data_dict_from( - use_operated=False, + return _galaxy_image_dict_with_linear_reconstruction( + galaxy_image_dict=galaxy_image_2d_dict, + linear_images=self.galaxy_linear_obj_data_dict_from(use_operated=False), ) - return {**galaxy_image_2d_dict, **galaxy_linear_obj_image_dict} - @cached_property def galaxy_model_image_dict(self) -> Dict[ag.Galaxy, np.ndarray]: """ @@ -248,12 +250,11 @@ def galaxy_model_image_dict(self) -> Dict[ag.Galaxy, np.ndarray]: xp=self._xp ) - galaxy_linear_obj_image_dict = self.galaxy_linear_obj_data_dict_from( - use_operated=True, + return _galaxy_image_dict_with_linear_reconstruction( + galaxy_image_dict=galaxy_blurred_image_2d_dict, + linear_images=self.galaxy_linear_obj_data_dict_from(use_operated=True), ) - return {**galaxy_blurred_image_2d_dict, **galaxy_linear_obj_image_dict} - @functools.cached_property def subtracted_images_of_galaxies_dict(self) -> Dict[ag.Galaxy, np.ndarray]: """ diff --git a/autolens/interferometer/fit_interferometer.py b/autolens/interferometer/fit_interferometer.py index 348de8f90..cabcb168e 100644 --- a/autolens/interferometer/fit_interferometer.py +++ b/autolens/interferometer/fit_interferometer.py @@ -35,7 +35,10 @@ ) from autolens.lens.tracer import Tracer -from autolens.lens.to_inversion import TracerToInversion +from autolens.lens.to_inversion import ( + TracerToInversion, + _galaxy_image_dict_with_linear_reconstruction, +) class FitInterferometer(aa.FitInterferometer, AbstractFitInversion): @@ -365,12 +368,11 @@ def galaxy_image_dict(self) -> Dict[ag.Galaxy, np.ndarray]: grid=self.grids.lp, xp=self._xp ) - galaxy_linear_obj_image_dict = self.galaxy_linear_obj_data_dict_from( - use_operated=False + return _galaxy_image_dict_with_linear_reconstruction( + galaxy_image_dict=galaxy_image_dict, + linear_images=self.galaxy_linear_obj_data_dict_from(use_operated=False), ) - return {**galaxy_image_dict, **galaxy_linear_obj_image_dict} - @property def model_image_natural(self) -> aa.Array2D: """ @@ -380,9 +382,6 @@ def model_image_natural(self) -> aa.Array2D: (`inversion.mapped_reconstructed_data`, linear light profiles and pixelized sources) -- the real-space image whose visibilities are `model_data`. - It is built from these two terms rather than from `galaxy_image_dict`, whose entry for a galaxy with - both ordinary and linear light holds only the linear reconstruction. - It needs neither visibilities nor a transformer, so it is available on an array-free dataset (built by `Interferometer.from_stream` / `from_sparse_terms`), where it is the image the natural-weighted dirty model image `dirty_model_image_natural` is formed from. diff --git a/autolens/interferometer/model/result.py b/autolens/interferometer/model/result.py index 444f7fa5f..4499bf730 100644 --- a/autolens/interferometer/model/result.py +++ b/autolens/interferometer/model/result.py @@ -1,6 +1,9 @@ import autoarray as aa -from autogalaxy.analysis.adapt_images.adapt_images import AdaptImages +from autogalaxy.analysis.adapt_images.adapt_images import ( + AdaptImages, + galaxy_name_image_dict_via_result_from, +) from autolens.lens.tracer import Tracer from autolens.interferometer.fit_interferometer import FitInterferometer from autolens.analysis.result import ResultDataset @@ -39,20 +42,13 @@ def adapt_images_from(self, use_model_images: bool = False) -> AdaptImages: Returns the adapt-images which are used to make a pixelization's mesh and regularization adapt to the reconstructed galaxy's morphology. - This can use either: - - - The model image of each galaxy in the best-fit model. - - The subtracted image of each galaxy in the best-fit model, where the subtracted image is the dataset - minus the model images of all other galaxies. - - In **PyAutoLens** these adapt images have had lensing calculations performed on them and therefore for source - galaxies are their lensed model images in the image-plane. - - Parameters - ---------- - use_model_images - If True, the model images of the galaxies are used to create the adapt images. If False, the subtracted - images of the galaxies are used. + The adapt images use each galaxy's real-space model image in the best-fit model. + For lensed galaxies this is the image-plane model image. + The `use_model_images` argument is retained for compatibility. """ - return AdaptImages.from_result(result=self, use_model_images=True) + return AdaptImages( + galaxy_name_image_dict=galaxy_name_image_dict_via_result_from( + result=self, use_model_images=True + ), + ) diff --git a/autolens/lens/to_inversion.py b/autolens/lens/to_inversion.py index ea9fc462b..8803698b6 100644 --- a/autolens/lens/to_inversion.py +++ b/autolens/lens/to_inversion.py @@ -28,6 +28,18 @@ from autoarray.inversion.inversion.factory import inversion_from +def _galaxy_image_dict_with_linear_reconstruction( + galaxy_image_dict: Dict[ag.Galaxy, aa.Array2D], + linear_images: Dict[ag.Galaxy, aa.Array2D], +) -> Dict[ag.Galaxy, aa.Array2D]: + """Add each galaxy's reconstructed linear light to its ordinary image.""" + for galaxy, linear_image in linear_images.items(): + if galaxy in galaxy_image_dict: + linear_image = galaxy_image_dict[galaxy] + linear_image + galaxy_image_dict[galaxy] = linear_image + return galaxy_image_dict + + class TracerToInversion(ag.AbstractToInversion): def __init__( self, diff --git a/test_autolens/imaging/test_fit_imaging.py b/test_autolens/imaging/test_fit_imaging.py index 6e1090b1b..ee2e6110b 100644 --- a/test_autolens/imaging/test_fit_imaging.py +++ b/test_autolens/imaging/test_fit_imaging.py @@ -1192,3 +1192,24 @@ def test__mask_padding__off_centre_mask__same_likelihood_as_centred(): assert fit_off.log_likelihood == pytest.approx( fit_centred.log_likelihood, abs=1e-4, ) + + + +def test__galaxy_image_dicts__add_ordinary_and_linear_light(masked_imaging_7x7): + galaxy = al.Galaxy( + redshift=0.5, + bulge=al.lp.Sersic(intensity=0.1), + disk=al.lp_linear.Gaussian(sigma=1.0), + ) + fit = al.FitImaging(dataset=masked_imaging_7x7, tracer=al.Tracer(galaxies=[galaxy])) + ordinary = galaxy.bulge.image_2d_from(grid=fit.grids.lp) + linear = fit.inversion.mapped_reconstructed_data + assert np.any(ordinary.array) + assert np.any(linear.array) + + np.testing.assert_allclose( + fit.galaxy_image_dict[galaxy].array, ordinary.array + linear.array + ) + np.testing.assert_allclose( + fit.galaxy_model_image_dict[galaxy].array, fit.model_data.array + ) diff --git a/test_autolens/interferometer/test_fit_interferometer.py b/test_autolens/interferometer/test_fit_interferometer.py index 356989676..9a6e0bac7 100644 --- a/test_autolens/interferometer/test_fit_interferometer.py +++ b/test_autolens/interferometer/test_fit_interferometer.py @@ -1145,10 +1145,8 @@ def test__natural_dirty_images__galaxy_with_ordinary_and_linear_light__matches_m interferometer_7, linear_component ): """ - A galaxy with both an ordinary light profile and a linear component (a lens with a linear light profile, - or a source with a pixelization) has only its linear reconstruction in `galaxy_image_dict`; - `model_image_natural` must still include the ordinary (lensed) light, so `dirty_model_image_natural` is - the natural dirty image of the fit's model visibilities `model_data`. + The naturally weighted dirty image includes ordinary and reconstructed light and matches the + natural dirty image of the fit's model visibilities `model_data`. """ dataset = interferometer_7.apply_sparse_operator(use_jax=False) @@ -1204,3 +1202,55 @@ def test__natural_dirty_images__galaxy_with_ordinary_and_linear_light__matches_m rtol=1.0e-8, atol=1.0e-8 * np.abs(expected.array).max(), ) + + + +@pytest.mark.parametrize("array_free", [False, True]) +def test__galaxy_image_dict__adds_ordinary_and_linear_light( + interferometer_7, array_free, tmp_path +): + from types import SimpleNamespace + + from astropy.io import fits + from autonerves import conf + import autofit as af + from autogalaxy.interferometer.plot.fit_interferometer_plots import fits_galaxy_images + from autolens.interferometer.model.result import ResultInterferometer + + dataset = ( + _array_free_dataset_from(interferometer_7) if array_free else interferometer_7 + ) + galaxy = al.Galaxy( + redshift=0.5, + bulge=al.lp.Sersic(intensity=0.1), + disk=al.lp_linear.Gaussian(sigma=1.0), + ) + fit = al.FitInterferometer(dataset=dataset, tracer=al.Tracer(galaxies=[galaxy])) + ordinary = galaxy.bulge.image_2d_from(grid=fit.grids.lp) + linear = fit.inversion.mapped_reconstructed_data + assert np.any(ordinary.array) + assert np.any(linear.array) + + expected = ordinary + linear + np.testing.assert_allclose( + fit.galaxy_image_dict[galaxy].array, expected.array, rtol=1.0e-8 + ) + np.testing.assert_allclose( + fit.model_image_natural.array, expected.array, rtol=1.0e-8 + ) + + fits_galaxy_images(fit=fit, output_path=tmp_path) + with fits.open(tmp_path / "galaxy_images.fits") as hdus: + np.testing.assert_allclose(hdus["GALAXY_0"].data, expected.native_for_fits) + + instance = af.Collection(galaxies=af.Collection(galaxy=galaxy)) + result = ResultInterferometer( + samples_summary=SimpleNamespace(instance=instance), + analysis=al.AnalysisInterferometer(dataset=dataset, use_jax=False), + ) + ((path, _),) = result.path_galaxy_tuples + adapt_image = result.adapt_images_from().galaxy_name_image_dict[path] + floor = conf.instance["general"]["adapt"]["adapt_minimum_percent"] + np.testing.assert_allclose( + adapt_image.array, np.maximum(expected.array, floor * np.max(expected.array)) + )