Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions autolens/imaging/fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]:
"""
Expand All @@ -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]:
"""
Expand Down
15 changes: 7 additions & 8 deletions autolens/interferometer/fit_interferometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
"""
Expand All @@ -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.
Expand Down
28 changes: 12 additions & 16 deletions autolens/interferometer/model/result.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
),
)
12 changes: 12 additions & 0 deletions autolens/lens/to_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 21 additions & 0 deletions test_autolens/imaging/test_fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
58 changes: 54 additions & 4 deletions test_autolens/interferometer/test_fit_interferometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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))
)
Loading