Skip to content

Commit 585e340

Browse files
authored
Merge pull request #258 from Jammy2211/feature/image_mesh_jax
Feature/image mesh jax
2 parents a332f22 + b720648 commit 585e340

22 files changed

Lines changed: 294 additions & 336 deletions

File tree

autogalaxy/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
from .analysis import model_util
5858
from .analysis.adapt_images.adapt_images import AdaptImages
59-
from .analysis.adapt_images.adapt_image_maker import AdaptImageMaker
59+
from .analysis.adapt_images.adapt_images import galaxy_name_image_dict_via_result_from
6060
from . import aggregator as agg
6161
from . import exc
6262
from . import plot

autogalaxy/aggregator/agg_util.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import annotations
2+
import numpy as np
23
from typing import List, Optional
34

5+
from autoconf.fitsable import flip_for_ds9_from
46
from autoconf.fitsable import ndarray_via_hdu_from
57

68
import autofit as af
@@ -140,9 +142,24 @@ def adapt_images_from(
140142

141143
galaxy_name_image_dict[value.header["EXTNAME"].lower()] = adapt_image
142144

145+
galaxy_name_image_plane_mesh_grid_dict = {}
146+
147+
for i, value in enumerate(fit.value(name="adapt_image_plane_mesh_grids")[1:]):
148+
149+
adapt_image_plane_mesh_grid = aa.Grid2DIrregular(
150+
values=flip_for_ds9_from(value.data.astype("float")),
151+
)
152+
153+
galaxy_name_image_plane_mesh_grid_dict[value.header["EXTNAME"].lower()] = (
154+
adapt_image_plane_mesh_grid
155+
)
156+
143157
instance = fit.model.instance_from_prior_medians(ignore_assertions=True)
144158

145-
adapt_images = AdaptImages(galaxy_name_image_dict=galaxy_name_image_dict)
159+
adapt_images = AdaptImages(
160+
galaxy_name_image_dict=galaxy_name_image_dict,
161+
galaxy_name_image_plane_mesh_grid_dict=galaxy_name_image_plane_mesh_grid_dict,
162+
)
146163

147164
adapt_images = adapt_images.updated_via_instance_from(
148165
instance=instance,

autogalaxy/analysis/adapt_images/adapt_image_maker.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

autogalaxy/analysis/adapt_images/adapt_images.py

Lines changed: 92 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,68 @@
1111
from autogalaxy.galaxy.galaxy import Galaxy
1212

1313

14+
def galaxy_name_image_dict_via_result_from(
15+
result, use_model_images: bool = False
16+
) -> "AdaptImages":
17+
"""
18+
Returns the adapt-images from a non-linear search result.
19+
20+
For model-fitting, the adapt-images are typically setup using the maximum log likelihood model of the
21+
previous model-fit. This means the model-fitting is used to cleanly deblend the light of the different
22+
galaxies in the image (e.g. separate the lens light from the source light).
23+
24+
This method uses attributes of a result (e.g. dictionary mapping galaxy instances to their model-images)
25+
to create the adapt-images.
26+
27+
This can use either:
28+
29+
- The model image of each galaxy in the best-fit model.
30+
- The subtracted image of each galaxy in the best-fit model, where the subtracted image is the dataset
31+
minus the model images of all other galaxies.
32+
33+
Certain models produce galaxy-images with negative flux values (e.g. a pixelization), which can cause
34+
numerical issues with the adaptive schemes. To prevent this, we set a minimum flux value for each
35+
galaxy-image, which is a fraction of the maximum flux value of that image defined via a config file.
36+
37+
Parameters
38+
----------
39+
result
40+
The result of a previous model-fit, which contains the model-image of each galaxy.
41+
use_model_images
42+
If True, the model images of the galaxies are used to create the adapt images. If False, the subtracted
43+
images of the galaxies are used.
44+
45+
Returns
46+
-------
47+
The adapt-images, which are the model-image of each galaxy inferred via the previous model-fit.
48+
"""
49+
adapt_minimum_percent = conf.instance["general"]["adapt"]["adapt_minimum_percent"]
50+
51+
galaxy_name_image_dict = {}
52+
53+
for path, galaxy in result.path_galaxy_tuples:
54+
if use_model_images:
55+
galaxy_image = result.model_image_galaxy_dict[path]
56+
else:
57+
galaxy_image = result.subtracted_signal_to_noise_map_galaxy_dict[path]
58+
59+
minimum_galaxy_value = adapt_minimum_percent * np.max(galaxy_image.array)
60+
galaxy_image[galaxy_image < minimum_galaxy_value] = minimum_galaxy_value
61+
62+
galaxy_name_image_dict[path] = galaxy_image
63+
64+
return galaxy_name_image_dict
65+
66+
1467
class AdaptImages:
1568
def __init__(
1669
self,
1770
galaxy_image_dict: Optional[Dict[Galaxy, aa.Array2D]] = None,
1871
galaxy_name_image_dict: Optional[Dict[Tuple[str, ...], aa.Array2D]] = None,
72+
galaxy_image_plane_mesh_grid_dict: Optional[Dict[Galaxy, aa.Array2D]] = None,
73+
galaxy_name_image_plane_mesh_grid_dict: Optional[
74+
Dict[Tuple[str, ...], aa.Grid2DIrregular]
75+
] = None,
1976
):
2077
"""
2178
Contains the adapt-images which are used to make a pixelization's mesh and regularization adapt to the
@@ -54,6 +111,11 @@ def __init__(
54111
self.galaxy_image_dict = galaxy_image_dict
55112
self.galaxy_name_image_dict = galaxy_name_image_dict
56113

114+
self.galaxy_image_plane_mesh_grid_dict = galaxy_image_plane_mesh_grid_dict
115+
self.galaxy_name_image_plane_mesh_grid_dict = (
116+
galaxy_name_image_plane_mesh_grid_dict
117+
)
118+
57119
@property
58120
def mask(self) -> aa.Mask2D:
59121
"""
@@ -85,59 +147,6 @@ def model_image(self) -> aa.Array2D:
85147

86148
return adapt_model_image
87149

88-
@classmethod
89-
def from_result(cls, result, use_model_images: bool = False) -> "AdaptImages":
90-
"""
91-
Returns the adapt-images from a non-linear search result.
92-
93-
For model-fitting, the adapt-images are typically setup using the maximum log likelihood model of the
94-
previous model-fit. This means the model-fitting is used to cleanly deblend the light of the different
95-
galaxies in the image (e.g. separate the lens light from the source light).
96-
97-
This method uses attributes of a result (e.g. dictionary mapping galaxy instances to their model-images)
98-
to create the adapt-images.
99-
100-
This can use either:
101-
102-
- The model image of each galaxy in the best-fit model.
103-
- The subtracted image of each galaxy in the best-fit model, where the subtracted image is the dataset
104-
minus the model images of all other galaxies.
105-
106-
Certain models produce galaxy-images with negative flux values (e.g. a pixelization), which can cause
107-
numerical issues with the adaptive schemes. To prevent this, we set a minimum flux value for each
108-
galaxy-image, which is a fraction of the maximum flux value of that image defined via a config file.
109-
110-
Parameters
111-
----------
112-
result
113-
The result of a previous model-fit, which contains the model-image of each galaxy.
114-
use_model_images
115-
If True, the model images of the galaxies are used to create the adapt images. If False, the subtracted
116-
images of the galaxies are used.
117-
118-
Returns
119-
-------
120-
The adapt-images, which are the model-image of each galaxy inferred via the previous model-fit.
121-
"""
122-
adapt_minimum_percent = conf.instance["general"]["adapt"][
123-
"adapt_minimum_percent"
124-
]
125-
126-
galaxy_name_image_dict = {}
127-
128-
for path, galaxy in result.path_galaxy_tuples:
129-
if use_model_images:
130-
galaxy_image = result.model_image_galaxy_dict[path]
131-
else:
132-
galaxy_image = result.subtracted_signal_to_noise_map_galaxy_dict[path]
133-
134-
minimum_galaxy_value = adapt_minimum_percent * np.max(galaxy_image.array)
135-
galaxy_image[galaxy_image < minimum_galaxy_value] = minimum_galaxy_value
136-
137-
galaxy_name_image_dict[path] = galaxy_image
138-
139-
return AdaptImages(galaxy_name_image_dict=galaxy_name_image_dict)
140-
141150
def updated_via_instance_from(self, instance, mask=None) -> "AdaptImages":
142151
"""
143152
Returns adapt-images which have been updated to map galaxy instances instead of galaxy names.
@@ -168,16 +177,37 @@ def updated_via_instance_from(self, instance, mask=None) -> "AdaptImages":
168177
"""
169178
from autogalaxy.galaxy.galaxy import Galaxy
170179

171-
galaxy_image_dict = {}
180+
galaxy_image_dict = None
181+
182+
if self.galaxy_name_image_dict is not None:
183+
184+
galaxy_image_dict = {}
185+
186+
for galaxy_name, galaxy in instance.path_instance_tuples_for_class(Galaxy):
187+
galaxy_name = str(galaxy_name)
172188

173-
for galaxy_name, galaxy in instance.path_instance_tuples_for_class(Galaxy):
174-
galaxy_name = str(galaxy_name)
189+
if galaxy_name in self.galaxy_name_image_dict:
190+
galaxy_image_dict[galaxy] = self.galaxy_name_image_dict[galaxy_name]
175191

176-
if galaxy_name in self.galaxy_name_image_dict:
177-
galaxy_image_dict[galaxy] = self.galaxy_name_image_dict[galaxy_name]
192+
if mask is not None:
193+
for key, image in galaxy_image_dict.items():
194+
galaxy_image_dict[key] = aa.Array2D(values=image, mask=mask)
178195

179-
if mask is not None:
180-
for key, image in galaxy_image_dict.items():
181-
galaxy_image_dict[key] = aa.Array2D(values=image, mask=mask)
196+
galaxy_image_plane_mesh_grid_dict = None
182197

183-
return AdaptImages(galaxy_image_dict=galaxy_image_dict)
198+
if self.galaxy_name_image_plane_mesh_grid_dict is not None:
199+
200+
galaxy_image_plane_mesh_grid_dict = {}
201+
202+
for galaxy_name, galaxy in instance.path_instance_tuples_for_class(Galaxy):
203+
galaxy_name = str(galaxy_name)
204+
205+
if galaxy_name in self.galaxy_name_image_plane_mesh_grid_dict:
206+
galaxy_image_plane_mesh_grid_dict[galaxy] = (
207+
self.galaxy_name_image_plane_mesh_grid_dict[galaxy_name]
208+
)
209+
210+
return AdaptImages(
211+
galaxy_image_dict=galaxy_image_dict,
212+
galaxy_image_plane_mesh_grid_dict=galaxy_image_plane_mesh_grid_dict,
213+
)

autogalaxy/analysis/analysis/dataset.py

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import autofit as af
88
import autoarray as aa
99

10-
from autogalaxy.analysis.adapt_images.adapt_image_maker import AdaptImageMaker
1110
from autogalaxy.analysis.adapt_images.adapt_images import AdaptImages
1211
from autogalaxy.cosmology.lensing import LensingCosmology
1312
from autogalaxy.analysis.analysis.analysis import Analysis
@@ -22,7 +21,7 @@ class AnalysisDataset(Analysis):
2221
def __init__(
2322
self,
2423
dataset: Union[aa.Imaging, aa.Interferometer],
25-
adapt_image_maker: Optional[AdaptImageMaker] = None,
24+
adapt_images: Optional[AdaptImages] = None,
2625
cosmology: LensingCosmology = None,
2726
settings_inversion: aa.SettingsInversion = None,
2827
preloads: aa.Preloads = None,
@@ -41,8 +40,8 @@ def __init__(
4140
----------
4241
dataset
4342
The dataset that is the model is fitted too.
44-
adapt_image_maker
45-
Makes the adapt-model image and galaxies images of a previous result in a model-fitting pipeline, which are
43+
adapt_images
44+
The adapt-model image and galaxies images of a previous result in a model-fitting pipeline, which are
4645
used by certain classes for adapting the analysis to the properties of the dataset.
4746
cosmology
4847
The Cosmology assumed for this analysis.
@@ -61,26 +60,12 @@ def __init__(
6160
)
6261

6362
self.dataset = dataset
64-
self.adapt_image_maker = adapt_image_maker
65-
self._adapt_images = None
63+
self.adapt_images = adapt_images
6664

6765
self.settings_inversion = settings_inversion or aa.SettingsInversion()
6866

6967
self.title_prefix = title_prefix
7068

71-
@property
72-
def adapt_images(self):
73-
74-
if self._adapt_images is not None:
75-
return self._adapt_images
76-
77-
if self.adapt_image_maker is None:
78-
return None
79-
80-
self._adapt_images = self.adapt_image_maker.adapt_images
81-
82-
return self._adapt_images
83-
8469
def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):
8570
"""
8671
This function is called immediately before the non-linear search begins and performs final tasks and checks
@@ -119,10 +104,6 @@ def modify_before_fit(self, paths: af.DirectoryPaths, model: af.Collection):
119104

120105
self.dataset.grids.border_relocator
121106

122-
if self.adapt_image_maker is not None:
123-
if not paths.is_complete:
124-
self._adapt_images = self.adapt_image_maker.adapt_images
125-
126107
super().modify_before_fit(paths=paths, model=model)
127108

128109
return self

autogalaxy/analysis/chaining_util.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ def source_custom_model_from(result: Result, source_is_model: bool = False) -> a
109109
if source_is_model:
110110
pixelization = af.Model(
111111
aa.Pixelization,
112-
image_mesh=result.instance.galaxies.source.pixelization.image_mesh,
113112
mesh=result.instance.galaxies.source.pixelization.mesh,
114113
regularization=result.model.galaxies.source.pixelization.regularization,
115114
)
@@ -122,7 +121,6 @@ def source_custom_model_from(result: Result, source_is_model: bool = False) -> a
122121

123122
pixelization = af.Model(
124123
aa.Pixelization,
125-
image_mesh=result.instance.galaxies.source.pixelization.image_mesh,
126124
mesh=result.instance.galaxies.source.pixelization.mesh,
127125
regularization=result.instance.galaxies.source.pixelization.regularization,
128126
)

0 commit comments

Comments
 (0)