|
11 | 11 | from autogalaxy.galaxy.galaxy import Galaxy |
12 | 12 |
|
13 | 13 |
|
| 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 | + |
14 | 67 | class AdaptImages: |
15 | 68 | def __init__( |
16 | 69 | self, |
17 | 70 | galaxy_image_dict: Optional[Dict[Galaxy, aa.Array2D]] = None, |
18 | 71 | 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, |
19 | 76 | ): |
20 | 77 | """ |
21 | 78 | Contains the adapt-images which are used to make a pixelization's mesh and regularization adapt to the |
@@ -54,6 +111,11 @@ def __init__( |
54 | 111 | self.galaxy_image_dict = galaxy_image_dict |
55 | 112 | self.galaxy_name_image_dict = galaxy_name_image_dict |
56 | 113 |
|
| 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 | + |
57 | 119 | @property |
58 | 120 | def mask(self) -> aa.Mask2D: |
59 | 121 | """ |
@@ -85,59 +147,6 @@ def model_image(self) -> aa.Array2D: |
85 | 147 |
|
86 | 148 | return adapt_model_image |
87 | 149 |
|
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 | | - |
141 | 150 | def updated_via_instance_from(self, instance, mask=None) -> "AdaptImages": |
142 | 151 | """ |
143 | 152 | 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": |
168 | 177 | """ |
169 | 178 | from autogalaxy.galaxy.galaxy import Galaxy |
170 | 179 |
|
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) |
172 | 188 |
|
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] |
175 | 191 |
|
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) |
178 | 195 |
|
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 |
182 | 197 |
|
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 | + ) |
0 commit comments