@@ -18,6 +18,7 @@ def __init__(self):
1818 """
1919 An abstract image mesh, which is used by pixelizations to determine the (y,x) mesh coordinates from image
2020 data.
21+
2122 """
2223 pass
2324
@@ -29,7 +30,6 @@ def image_plane_mesh_grid_from(
2930 self ,
3031 mask : Mask2D ,
3132 adapt_data : Optional [np .ndarray ] = None ,
32- settings : SettingsInversion = None ,
3333 ) -> Grid2DIrregular :
3434 raise NotImplementedError
3535
@@ -65,131 +65,3 @@ def mesh_pixels_per_image_pixels_from(
6565 )
6666
6767 return Array2D (values = mesh_pixels_per_image_pixels , mask = mask )
68-
69- def check_mesh_pixels_per_image_pixels (
70- self , mask : Mask2D , mesh_grid : Grid2DIrregular , settings : SettingsInversion
71- ):
72- """
73- Checks the number of mesh pixels in every image pixel and raises an `InversionException` if there are fewer
74- mesh pixels inside a certain number of image-pixels than the input settings.
75-
76- This allows a user to force a model-fit to use image-mesh's which cluster a large number of mesh pixels to
77- the brightest regions of the image data (E.g. the highst weighted regions).
78-
79- The check works as follows:
80-
81- 1) Compute the 2D array of the number of mesh pixels in every masked data image pixel.
82- 2) Find the number of mesh pixels in the N data pixels with the larger number of mesh pixels, where N is
83- given by `settings.image_mesh_min_mesh_number`. For example, if `settings.image_mesh_min_mesh_number=5` then
84- the number of mesh pixels in the 5 data pixels with the most data pixels is computed.
85- 3) Compare the lowest value above to the value `settings.image_mesh_min_mesh_pixels_per_pixel`. If the value is
86- below this value, raise an `InversionException`.
87-
88- Therefore, by settings `settings.image_mesh_min_mesh_pixels_per_pixel` to a value above 1 the code is forced
89- to adapt the image mesh enough to put many mesh pixels in the brightest image pixels.
90-
91- Parameters
92- ----------
93- mask
94- The mask of the dataset being analysed, which the pixelization grid maps too. The number of
95- mesh pixels mapped inside each of this mask's image-pixels is returned.
96- mesh_grid
97- The image mesh-grid computed by the class which adapts to the data's mask. The number of image mesh pixels
98- that fall within each of the data's mask pixels is returned.
99- settings
100- The inversion settings, which have the criteria dictating if the image-mesh has clustered enough or if
101- an exception is raised.
102- """
103-
104- if os .environ .get ("PYAUTOFIT_TEST_MODE" ) == "1" :
105- return
106-
107- if settings is not None :
108- if settings .image_mesh_min_mesh_pixels_per_pixel is not None :
109- mesh_pixels_per_image_pixels = self .mesh_pixels_per_image_pixels_from (
110- mask = mask , mesh_grid = mesh_grid
111- )
112-
113- indices_of_highest_values = np .argsort (mesh_pixels_per_image_pixels )[
114- - settings .image_mesh_min_mesh_number :
115- ]
116- lowest_mesh_pixels = np .min (
117- mesh_pixels_per_image_pixels [indices_of_highest_values ]
118- )
119-
120- if lowest_mesh_pixels < settings .image_mesh_min_mesh_pixels_per_pixel :
121- raise exc .InversionException ()
122-
123- return mesh_grid
124-
125- def check_adapt_background_pixels (
126- self ,
127- mask : Mask2D ,
128- mesh_grid : Grid2DIrregular ,
129- adapt_data : Optional [np .ndarray ],
130- settings : SettingsInversion ,
131- ):
132- """
133- Checks the number of mesh pixels in the background of the image-mesh and raises an `InversionException` if
134- there are fewer mesh pixels in the background than the input settings.
135-
136- This allows a user to force a model-fit to use image-mesh's which cluster a minimum number of mesh pixels to
137- the faintest regions of the image data (E.g. the lowest weighted regions). This prevents too few image-mesh
138- pixels being allocated to the background of the data.
139-
140- The check works as follows:
141-
142- 1) Find all pixels in the background of the `adapt_data`, which are N pixels with the lowest values, where N is
143- a percentage given by `settings.image_mesh_adapt_background_percent_check`. If N is 50%, then the half of
144- pixels in `adapt_data` with the lowest values will be checked.
145- 2) Sum the total number of mesh pixels in these background pixels, thereby estimating the number of mesh pixels
146- assigned to background pixels.
147- 3) Compare this value to the total number of mesh pixels multiplied
148- by `settings.image_mesh_adapt_background_percent_threshold` and raise an `InversionException` if the number
149- of mesh pixels is below this value, meaning the background did not have sufficient mesh pixels in it.
150-
151- Therefore, by setting `settings.image_mesh_adapt_background_percent_threshold` the code is forced
152- to adapt the image mesh in a way that places many mesh pixels in the background regions.
153-
154- Parameters
155- ----------
156- mask
157- The mask of the dataset being analysed, which the pixelization grid maps too. The number of
158- mesh pixels mapped inside each of this mask's image-pixels is returned.
159- mesh_grid
160- The image mesh-grid computed by the class which adapts to the data's mask. The number of image mesh pixels
161- that fall within each of the data's mask pixels is returned.
162- adapt_data
163- A image which represents one or more components in the masked 2D data in the image-plane.
164- settings
165- The inversion settings, which have the criteria dictating if the image-mesh has clustered enough or if
166- an exception is raised.
167- """
168-
169- if os .environ .get ("PYAUTOFIT_TEST_MODE" ) == "1" :
170- return
171-
172- if settings is not None :
173- if settings .image_mesh_adapt_background_percent_threshold is not None :
174- pixels = mesh_grid .shape [0 ]
175-
176- pixels_in_background = int (
177- mask .shape_slim * settings .image_mesh_adapt_background_percent_check
178- )
179-
180- indices_of_lowest_values = np .argsort (adapt_data )[:pixels_in_background ]
181- mask_background = np .zeros_like (adapt_data , dtype = bool )
182- mask_background [indices_of_lowest_values ] = True
183-
184- mesh_pixels_per_image_pixels = self .mesh_pixels_per_image_pixels_from (
185- mask = mask , mesh_grid = mesh_grid
186- )
187-
188- mesh_pixels_in_background = sum (
189- mesh_pixels_per_image_pixels [mask_background ]
190- )
191-
192- if mesh_pixels_in_background < (
193- pixels * settings .image_mesh_adapt_background_percent_threshold
194- ):
195- raise exc .InversionException ()
0 commit comments