Skip to content

Commit ee5637d

Browse files
Jammy2211Jammy2211
authored andcommitted
remove image_mesh from pixelization
1 parent 3932325 commit ee5637d

8 files changed

Lines changed: 145 additions & 182 deletions

File tree

autoarray/inversion/inversion/settings.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ def __init__(
1717
no_regularization_add_to_curvature_diag_value: float = None,
1818
use_w_tilde_numpy: bool = False,
1919
use_source_loop: bool = False,
20-
image_mesh_min_mesh_pixels_per_pixel=None,
21-
image_mesh_min_mesh_number: int = 5,
22-
image_mesh_adapt_background_percent_threshold: float = None,
23-
image_mesh_adapt_background_percent_check: float = 0.8,
2420
tolerance: float = 1e-8,
2521
maxiter: int = 250,
2622
):
@@ -46,19 +42,6 @@ def __init__(
4642
which exploit sparsity to do the calculation normally in a more efficient way).
4743
use_source_loop
4844
Shhhh its a secret.
49-
image_mesh_min_mesh_pixels_per_pixel
50-
If not None, the image-mesh must place this many mesh pixels per image pixels in the N highest weighted
51-
regions of the adapt data, or an `InversionException` is raised. This can be used to force the image-mesh
52-
to cluster large numbers of source pixels to the adapt-datas brightest regions.
53-
image_mesh_min_mesh_number
54-
The value N given above in the docstring for `image_mesh_min_mesh_pixels_per_pixel`, indicating how many
55-
image pixels are checked for having a threshold number of mesh pixels.
56-
image_mesh_adapt_background_percent_threshold
57-
If not None, the image-mesh must place this percentage of mesh-pixels in the background regions of the
58-
`adapt_data`, where the background is the `image_mesh_adapt_background_percent_check` masked data pixels
59-
with the lowest values.
60-
image_mesh_adapt_background_percent_check
61-
The percentage of masked data pixels which are checked for the background criteria.
6245
tolerance
6346
For an interferometer inversion using the linear operators method, sets the tolerance of the solver
6447
(this input does nothing for dataset data and other interferometer methods).
@@ -73,14 +56,6 @@ def __init__(
7356
self._no_regularization_add_to_curvature_diag_value = (
7457
no_regularization_add_to_curvature_diag_value
7558
)
76-
self.image_mesh_min_mesh_pixels_per_pixel = image_mesh_min_mesh_pixels_per_pixel
77-
self.image_mesh_min_mesh_number = image_mesh_min_mesh_number
78-
self.image_mesh_adapt_background_percent_threshold = (
79-
image_mesh_adapt_background_percent_threshold
80-
)
81-
self.image_mesh_adapt_background_percent_check = (
82-
image_mesh_adapt_background_percent_check
83-
)
8459

8560
self.tolerance = tolerance
8661
self.maxiter = maxiter

autoarray/inversion/mock/mock_mesh.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,3 @@ def image_plane_mesh_grid_from(
4343
return adapt_data * self.image_plane_mesh_grid
4444

4545
return self.image_plane_mesh_grid
46-
47-
@property
48-
def requires_image_mesh(self):
49-
return False

autoarray/inversion/mock/mock_pixelization.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ def __init__(
77
self,
88
mesh=None,
99
regularization=None,
10-
image_mesh=None,
1110
mapper=None,
1211
image_plane_mesh_grid=None,
1312
):
1413
super().__init__(
15-
mesh=mesh, regularization=regularization, image_mesh=image_mesh
14+
mesh=mesh, regularization=regularization
1615
)
1716

1817
self.mapper = mapper

autoarray/inversion/pixelization/image_mesh/abstract.py

Lines changed: 1 addition & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -65,131 +66,3 @@ def mesh_pixels_per_image_pixels_from(
6566
)
6667

6768
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()

autoarray/inversion/pixelization/image_mesh/hilbert.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,20 @@ def __init__(
212212
weight_power
213213
The power the weight values are raised too, which allows more pixels to be drawn from the higher weight
214214
regions of the adapt image.
215+
216+
image_mesh_min_mesh_pixels_per_pixel
217+
If not None, the image-mesh must place this many mesh pixels per image pixels in the N highest weighted
218+
regions of the adapt data, or an `InversionException` is raised. This can be used to force the image-mesh
219+
to cluster large numbers of source pixels to the adapt-datas brightest regions.
220+
image_mesh_min_mesh_number
221+
The value N given above in the docstring for `image_mesh_min_mesh_pixels_per_pixel`, indicating how many
222+
image pixels are checked for having a threshold number of mesh pixels.
223+
image_mesh_adapt_background_percent_threshold
224+
If not None, the image-mesh must place this percentage of mesh-pixels in the background regions of the
225+
`adapt_data`, where the background is the `image_mesh_adapt_background_percent_check` masked data pixels
226+
with the lowest values.
227+
image_mesh_adapt_background_percent_check
228+
The percentage of masked data pixels which are checked for the background criteria.
215229
"""
216230

217231
super().__init__(
@@ -285,3 +299,132 @@ def image_plane_mesh_grid_from(
285299
)
286300

287301
return mesh_grid
302+
303+
304+
def check_mesh_pixels_per_image_pixels(
305+
self, mask: Mask2D, mesh_grid: Grid2DIrregular, settings: SettingsInversion
306+
):
307+
"""
308+
Checks the number of mesh pixels in every image pixel and raises an `InversionException` if there are fewer
309+
mesh pixels inside a certain number of image-pixels than the input settings.
310+
311+
This allows a user to force a model-fit to use image-mesh's which cluster a large number of mesh pixels to
312+
the brightest regions of the image data (E.g. the highst weighted regions).
313+
314+
The check works as follows:
315+
316+
1) Compute the 2D array of the number of mesh pixels in every masked data image pixel.
317+
2) Find the number of mesh pixels in the N data pixels with the larger number of mesh pixels, where N is
318+
given by `settings.image_mesh_min_mesh_number`. For example, if `settings.image_mesh_min_mesh_number=5` then
319+
the number of mesh pixels in the 5 data pixels with the most data pixels is computed.
320+
3) Compare the lowest value above to the value `settings.image_mesh_min_mesh_pixels_per_pixel`. If the value is
321+
below this value, raise an `InversionException`.
322+
323+
Therefore, by settings `settings.image_mesh_min_mesh_pixels_per_pixel` to a value above 1 the code is forced
324+
to adapt the image mesh enough to put many mesh pixels in the brightest image pixels.
325+
326+
Parameters
327+
----------
328+
mask
329+
The mask of the dataset being analysed, which the pixelization grid maps too. The number of
330+
mesh pixels mapped inside each of this mask's image-pixels is returned.
331+
mesh_grid
332+
The image mesh-grid computed by the class which adapts to the data's mask. The number of image mesh pixels
333+
that fall within each of the data's mask pixels is returned.
334+
settings
335+
The inversion settings, which have the criteria dictating if the image-mesh has clustered enough or if
336+
an exception is raised.
337+
"""
338+
339+
if os.environ.get("PYAUTOFIT_TEST_MODE") == "1":
340+
return
341+
342+
if settings is not None:
343+
if settings.image_mesh_min_mesh_pixels_per_pixel is not None:
344+
mesh_pixels_per_image_pixels = self.mesh_pixels_per_image_pixels_from(
345+
mask=mask, mesh_grid=mesh_grid
346+
)
347+
348+
indices_of_highest_values = np.argsort(mesh_pixels_per_image_pixels)[
349+
-settings.image_mesh_min_mesh_number :
350+
]
351+
lowest_mesh_pixels = np.min(
352+
mesh_pixels_per_image_pixels[indices_of_highest_values]
353+
)
354+
355+
if lowest_mesh_pixels < settings.image_mesh_min_mesh_pixels_per_pixel:
356+
raise exc.InversionException()
357+
358+
return mesh_grid
359+
360+
def check_adapt_background_pixels(
361+
self,
362+
mask: Mask2D,
363+
mesh_grid: Grid2DIrregular,
364+
adapt_data: Optional[np.ndarray],
365+
settings: SettingsInversion,
366+
):
367+
"""
368+
Checks the number of mesh pixels in the background of the image-mesh and raises an `InversionException` if
369+
there are fewer mesh pixels in the background than the input settings.
370+
371+
This allows a user to force a model-fit to use image-mesh's which cluster a minimum number of mesh pixels to
372+
the faintest regions of the image data (E.g. the lowest weighted regions). This prevents too few image-mesh
373+
pixels being allocated to the background of the data.
374+
375+
The check works as follows:
376+
377+
1) Find all pixels in the background of the `adapt_data`, which are N pixels with the lowest values, where N is
378+
a percentage given by `settings.image_mesh_adapt_background_percent_check`. If N is 50%, then the half of
379+
pixels in `adapt_data` with the lowest values will be checked.
380+
2) Sum the total number of mesh pixels in these background pixels, thereby estimating the number of mesh pixels
381+
assigned to background pixels.
382+
3) Compare this value to the total number of mesh pixels multiplied
383+
by `settings.image_mesh_adapt_background_percent_threshold` and raise an `InversionException` if the number
384+
of mesh pixels is below this value, meaning the background did not have sufficient mesh pixels in it.
385+
386+
Therefore, by setting `settings.image_mesh_adapt_background_percent_threshold` the code is forced
387+
to adapt the image mesh in a way that places many mesh pixels in the background regions.
388+
389+
Parameters
390+
----------
391+
mask
392+
The mask of the dataset being analysed, which the pixelization grid maps too. The number of
393+
mesh pixels mapped inside each of this mask's image-pixels is returned.
394+
mesh_grid
395+
The image mesh-grid computed by the class which adapts to the data's mask. The number of image mesh pixels
396+
that fall within each of the data's mask pixels is returned.
397+
adapt_data
398+
A image which represents one or more components in the masked 2D data in the image-plane.
399+
settings
400+
The inversion settings, which have the criteria dictating if the image-mesh has clustered enough or if
401+
an exception is raised.
402+
"""
403+
404+
if os.environ.get("PYAUTOFIT_TEST_MODE") == "1":
405+
return
406+
407+
if settings is not None:
408+
if settings.image_mesh_adapt_background_percent_threshold is not None:
409+
pixels = mesh_grid.shape[0]
410+
411+
pixels_in_background = int(
412+
mask.shape_slim * settings.image_mesh_adapt_background_percent_check
413+
)
414+
415+
indices_of_lowest_values = np.argsort(adapt_data)[:pixels_in_background]
416+
mask_background = np.zeros_like(adapt_data, dtype=bool)
417+
mask_background[indices_of_lowest_values] = True
418+
419+
mesh_pixels_per_image_pixels = self.mesh_pixels_per_image_pixels_from(
420+
mask=mask, mesh_grid=mesh_grid
421+
)
422+
423+
mesh_pixels_in_background = sum(
424+
mesh_pixels_per_image_pixels[mask_background]
425+
)
426+
427+
if mesh_pixels_in_background < (
428+
pixels * settings.image_mesh_adapt_background_percent_threshold
429+
):
430+
raise exc.InversionException()

autoarray/inversion/pixelization/mesh/abstract.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ def mesh_grid_from(
112112
):
113113
raise NotImplementedError
114114

115-
@property
116-
def requires_image_mesh(self):
117-
return True
118-
119115
def __str__(self):
120116
return "\n".join(["{}: {}".format(k, v) for k, v in self.__dict__.items()])
121117

autoarray/inversion/pixelization/mesh/rectangular.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,6 @@ def mesh_grid_from(
151151
xp=xp,
152152
)
153153

154-
@property
155-
def requires_image_mesh(self):
156-
return False
157-
158154

159155
class RectangularSource(RectangularMagnification):
160156

0 commit comments

Comments
 (0)