Skip to content

Feature/adapt data #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 30, 2024
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
24 changes: 24 additions & 0 deletions autolens/imaging/fit_imaging.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import numpy as np
from typing import Dict, List, Optional

Expand Down Expand Up @@ -181,6 +182,29 @@ def galaxy_model_image_dict(self) -> Dict[ag.Galaxy, np.ndarray]:

return {**galaxy_blurred_image_2d_dict, **galaxy_linear_obj_image_dict}

@property
def subtracted_images_of_galaxies_dict(self) -> Dict[ag.Galaxy, np.ndarray]:
"""
A dictionary which associates every galaxy in the tracer with its `subtracted image`.

A subtracted image of a galaxy is the data where all other galaxy images are subtracted from it, therefore
showing how a galaxy appears in the data in the absence of all other galaxies.

This is used to visualize the contribution of each galaxy in the data.
"""

subtracted_images_of_galaxies_dict = {}

for (galaxy, galaxy_model_image) in self.galaxy_model_image_dict.items():
subtracted_images_of_galaxies_dict[galaxy] = copy.copy(self.dataset.data)

for (galaxy, galaxy_model_image) in self.galaxy_model_image_dict.items():
for (galaxy_other, galaxy_model_image_other) in self.galaxy_model_image_dict.items():
if galaxy != galaxy_other:
subtracted_images_of_galaxies_dict[galaxy] -= galaxy_model_image_other

return subtracted_images_of_galaxies_dict

@property
def model_images_of_planes_list(self) -> List[aa.Array2D]:
"""
Expand Down
4 changes: 2 additions & 2 deletions test_autolens/analysis/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,14 +366,14 @@ def test___image_dict(analysis_imaging_7x7):
analysis=analysis_imaging_7x7,
)

image_dict = result.image_galaxy_dict
image_dict = result.model_image_galaxy_dict

assert isinstance(image_dict[str(("galaxies", "lens"))], Array2D)
assert isinstance(image_dict[str(("galaxies", "source"))], Array2D)

result.instance.galaxies.lens = al.Galaxy(redshift=0.5)

image_dict = result.image_galaxy_dict
image_dict = result.model_image_galaxy_dict

assert (image_dict[str(("galaxies", "lens"))].native == np.zeros((7, 7))).all()
assert isinstance(image_dict[str(("galaxies", "source"))], Array2D)
136 changes: 106 additions & 30 deletions test_autolens/imaging/test_fit_imaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,81 @@ def test__galaxy_model_image_dict(masked_imaging_7x7):
assert (fit.galaxy_model_image_dict[g2] == np.zeros(9)).all()


def test__subtracted_image_of_galaxies_dict(masked_imaging_7x7):

# 2 Planes with Summed Galaxies

g0 = al.Galaxy(
redshift=0.5,
bulge=al.lp.Sersic(intensity=1.0),
)
g1 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=2.0))
g2 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=3.0))

tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

fit = al.FitImaging(dataset=masked_imaging_7x7, tracer=tracer)

g0_image = g0.blurred_image_2d_from(
grid=masked_imaging_7x7.grid,
blurring_grid=masked_imaging_7x7.blurring_grid,
convolver=masked_imaging_7x7.convolver
)

g1_image = g1.blurred_image_2d_from(
grid=masked_imaging_7x7.grid,
blurring_grid=masked_imaging_7x7.blurring_grid,
convolver=masked_imaging_7x7.convolver
)

g2_image = g2.blurred_image_2d_from(
grid=masked_imaging_7x7.grid,
blurring_grid=masked_imaging_7x7.blurring_grid,
convolver=masked_imaging_7x7.convolver
)

assert fit.subtracted_images_of_galaxies_dict[g0] == pytest.approx(
masked_imaging_7x7.data - g1_image - g2_image, 1.0e-4
)
assert fit.subtracted_images_of_galaxies_dict[g1] == pytest.approx(
masked_imaging_7x7.data - g0_image - g2_image, 1.0e-4
)
assert fit.subtracted_images_of_galaxies_dict[g2] == pytest.approx(
masked_imaging_7x7.data - g0_image - g1_image, 1.0e-4
)

# 3 Planes

g0 = al.Galaxy(
redshift=0.5,
bulge=al.lp.Sersic(intensity=1.0),
mass_profile=al.mp.IsothermalSph(einstein_radius=1.0),
)
g1 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=2.0))
g2 = al.Galaxy(redshift=2.0, bulge=al.lp.Sersic(intensity=3.0))

tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

fit = al.FitImaging(dataset=masked_imaging_7x7, tracer=tracer)

blurred_image_2d_list = tracer.blurred_image_2d_list_from(
grid=masked_imaging_7x7.grid,
convolver=masked_imaging_7x7.convolver,
blurring_grid=masked_imaging_7x7.blurring_grid,
)

assert fit.subtracted_images_of_galaxies_dict[g0] == pytest.approx(
masked_imaging_7x7.data - blurred_image_2d_list[1] - blurred_image_2d_list[2], 1.0e-4
)
assert fit.subtracted_images_of_galaxies_dict[g1] == pytest.approx(
masked_imaging_7x7.data - blurred_image_2d_list[0] - blurred_image_2d_list[2], 1.0e-4
)
assert fit.subtracted_images_of_galaxies_dict[g2] == pytest.approx(
masked_imaging_7x7.data - blurred_image_2d_list[0] - blurred_image_2d_list[1], 1.0e-4
)



def test__model_images_of_planes_list(masked_imaging_7x7):

g0 = al.Galaxy(
Expand Down Expand Up @@ -384,6 +459,36 @@ def test__model_images_of_planes_list(masked_imaging_7x7):
)


def test__subtracted_images_of_planes_list(masked_imaging_7x7_no_blur):

g0 = al.Galaxy(redshift=0.5, bulge=al.lp.Sersic(intensity=1.0))

g1 = al.Galaxy(redshift=0.75, bulge=al.lp.Sersic(intensity=2.0))

g2 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=3.0))

tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

fit = al.FitImaging(dataset=masked_imaging_7x7_no_blur, tracer=tracer)

assert fit.subtracted_images_of_planes_list[0].slim[0] == pytest.approx(0.200638, 1.0e-4)
assert fit.subtracted_images_of_planes_list[1].slim[0] == pytest.approx(0.360511, 1.0e-4)
assert fit.subtracted_images_of_planes_list[2].slim[0] == pytest.approx(0.520383, 1.0e-4)

g0 = al.Galaxy(redshift=0.5, bulge=al.lp.Sersic(intensity=1.0))

g1 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=2.0))

g2 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=3.0))

tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

fit = al.FitImaging(dataset=masked_imaging_7x7_no_blur, tracer=tracer)

assert fit.subtracted_images_of_planes_list[0].slim[0] == pytest.approx(0.200638, 1.0e-4)
assert fit.subtracted_images_of_planes_list[1].slim[0] == pytest.approx(0.840127, 1.0e-4)


def test___unmasked_blurred_images(masked_imaging_7x7):

g0 = al.Galaxy(
Expand Down Expand Up @@ -424,36 +529,6 @@ def test___unmasked_blurred_images(masked_imaging_7x7):
).all()


def test__subtracted_images_of_planes_list(masked_imaging_7x7_no_blur):

g0 = al.Galaxy(redshift=0.5, bulge=al.lp.Sersic(intensity=1.0))

g1 = al.Galaxy(redshift=0.75, bulge=al.lp.Sersic(intensity=2.0))

g2 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=3.0))

tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

fit = al.FitImaging(dataset=masked_imaging_7x7_no_blur, tracer=tracer)

assert fit.subtracted_images_of_planes_list[0].slim[0] == pytest.approx(0.200638, 1.0e-4)
assert fit.subtracted_images_of_planes_list[1].slim[0] == pytest.approx(0.360511, 1.0e-4)
assert fit.subtracted_images_of_planes_list[2].slim[0] == pytest.approx(0.520383, 1.0e-4)

g0 = al.Galaxy(redshift=0.5, bulge=al.lp.Sersic(intensity=1.0))

g1 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=2.0))

g2 = al.Galaxy(redshift=1.0, bulge=al.lp.Sersic(intensity=3.0))

tracer = al.Tracer.from_galaxies(galaxies=[g0, g1, g2])

fit = al.FitImaging(dataset=masked_imaging_7x7_no_blur, tracer=tracer)

assert fit.subtracted_images_of_planes_list[0].slim[0] == pytest.approx(0.200638, 1.0e-4)
assert fit.subtracted_images_of_planes_list[1].slim[0] == pytest.approx(0.840127, 1.0e-4)


def test__tracer_linear_light_profiles_to_light_profiles(masked_imaging_7x7):

g0 = al.Galaxy(redshift=0.5, bulge=al.lp.Sersic(intensity=1.0))
Expand All @@ -478,6 +553,7 @@ def test__tracer_linear_light_profiles_to_light_profiles(masked_imaging_7x7):
assert tracer.galaxies[1].bulge.intensity == pytest.approx(-371.061130, 1.0e-4)
assert tracer.galaxies[2].bulge.intensity == pytest.approx(0.08393533428, 1.0e-4)


def test__preloads__refit_with_new_preloads(masked_imaging_7x7):

g0 = al.Galaxy(
Expand Down