Skip to content

Commit 89f22e7

Browse files
Jammy2211Jammy2211
authored andcommitted
image mesh refactor all unit tests pass
1 parent 8e3fa55 commit 89f22e7

File tree

3 files changed

+65
-45
lines changed

3 files changed

+65
-45
lines changed

autoarray/inversion/pixelization/image_mesh/hilbert.py

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22
import numpy as np
3+
import os
34

45
from typing import Optional
56

@@ -238,7 +239,6 @@ def image_plane_mesh_grid_from(
238239
self,
239240
mask: Mask2D,
240241
adapt_data: Optional[np.ndarray],
241-
settings: SettingsInversion = None,
242242
) -> Grid2DIrregular:
243243
"""
244244
Returns an image mesh by running the Hilbert curve on the weight map.
@@ -290,14 +290,6 @@ def image_plane_mesh_grid_from(
290290

291291
mesh_grid = Grid2DIrregular(values=np.stack((drawn_y, drawn_x), axis=-1))
292292

293-
self.check_mesh_pixels_per_image_pixels(
294-
mask=mask, mesh_grid=mesh_grid, settings=settings
295-
)
296-
297-
self.check_adapt_background_pixels(
298-
mask=mask, mesh_grid=mesh_grid, adapt_data=adapt_data, settings=settings
299-
)
300-
301293
return mesh_grid
302294

303295
def check_mesh_pixels_per_image_pixels(
@@ -344,21 +336,20 @@ def check_mesh_pixels_per_image_pixels(
344336
if os.environ.get("PYAUTOFIT_TEST_MODE") == "1":
345337
return
346338

347-
if settings is not None:
348-
if image_mesh_min_mesh_pixels_per_pixel is not None:
349-
mesh_pixels_per_image_pixels = self.mesh_pixels_per_image_pixels_from(
350-
mask=mask, mesh_grid=mesh_grid
351-
)
339+
if image_mesh_min_mesh_pixels_per_pixel is not None:
340+
mesh_pixels_per_image_pixels = self.mesh_pixels_per_image_pixels_from(
341+
mask=mask, mesh_grid=mesh_grid
342+
)
352343

353-
indices_of_highest_values = np.argsort(mesh_pixels_per_image_pixels)[
354-
-image_mesh_min_mesh_number:
355-
]
356-
lowest_mesh_pixels = np.min(
357-
mesh_pixels_per_image_pixels[indices_of_highest_values]
358-
)
344+
indices_of_highest_values = np.argsort(mesh_pixels_per_image_pixels)[
345+
-image_mesh_min_mesh_number:
346+
]
347+
lowest_mesh_pixels = np.min(
348+
mesh_pixels_per_image_pixels[indices_of_highest_values]
349+
)
359350

360-
if lowest_mesh_pixels < image_mesh_min_mesh_pixels_per_pixel:
361-
raise exc.InversionException()
351+
if lowest_mesh_pixels < image_mesh_min_mesh_pixels_per_pixel:
352+
raise exc.InversionException()
362353

363354
return mesh_grid
364355

@@ -412,27 +403,26 @@ def check_adapt_background_pixels(
412403
if os.environ.get("PYAUTOFIT_TEST_MODE") == "1":
413404
return
414405

415-
if settings is not None:
416-
if image_mesh_adapt_background_percent_threshold is not None:
417-
pixels = mesh_grid.shape[0]
406+
if image_mesh_adapt_background_percent_threshold is not None:
407+
pixels = mesh_grid.shape[0]
418408

419-
pixels_in_background = int(
420-
mask.shape_slim * image_mesh_adapt_background_percent_check
421-
)
409+
pixels_in_background = int(
410+
mask.shape_slim * image_mesh_adapt_background_percent_check
411+
)
422412

423-
indices_of_lowest_values = np.argsort(adapt_data)[:pixels_in_background]
424-
mask_background = np.zeros_like(adapt_data, dtype=bool)
425-
mask_background[indices_of_lowest_values] = True
413+
indices_of_lowest_values = np.argsort(adapt_data)[:pixels_in_background]
414+
mask_background = np.zeros_like(adapt_data, dtype=bool)
415+
mask_background[indices_of_lowest_values] = True
426416

427-
mesh_pixels_per_image_pixels = self.mesh_pixels_per_image_pixels_from(
428-
mask=mask, mesh_grid=mesh_grid
429-
)
417+
mesh_pixels_per_image_pixels = self.mesh_pixels_per_image_pixels_from(
418+
mask=mask, mesh_grid=mesh_grid
419+
)
430420

431-
mesh_pixels_in_background = sum(
432-
mesh_pixels_per_image_pixels[mask_background]
433-
)
421+
mesh_pixels_in_background = sum(
422+
mesh_pixels_per_image_pixels[mask_background]
423+
)
434424

435-
if mesh_pixels_in_background < (
436-
pixels * image_mesh_adapt_background_percent_threshold
437-
):
438-
raise exc.InversionException()
425+
if mesh_pixels_in_background < (
426+
pixels * image_mesh_adapt_background_percent_threshold
427+
):
428+
raise exc.InversionException()

autoarray/preloads.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
self,
2323
mapper_indices: np.ndarray = None,
2424
source_pixel_zeroed_indices: np.ndarray = None,
25-
image_plane_mesh_grid: np.ndarray = None,
25+
image_plane_mesh_grid_list: np.ndarray = None,
2626
linear_light_profile_blurred_mapping_matrix=None,
2727
):
2828
"""
@@ -37,6 +37,29 @@ def __init__(
3737
the source model is fixed (e.g. when fitting only the lens light), sections of the curvature matrix do not
3838
change and can be reused, avoiding redundant computation.
3939
40+
Returns a list of image-plane mesh-grids, which are image-plane grids defining the centres of the pixels of
41+
the pixelization's mesh (e.g. the centres of Voronoi pixels).
42+
43+
The `image_mesh` attribute of the pixelization object defines whether the centre of each mesh pixel are
44+
determined in the image-plane. When this is the case, the pixelization therefore has an image-plane mesh-grid,
45+
which needs to be computed before the inversion is performed.
46+
47+
This function iterates over all galaxies with pixelizations, determines which pixelizations have an
48+
`image_mesh` and for these pixelizations computes the image-plane mesh-grid.
49+
50+
It returns a list of all image-plane mesh-grids, which in the functions `mapper_from` and `mapper_galaxy_dict`
51+
are grouped into a `Mapper` object with other information required to perform the inversion using the
52+
pixelization.
53+
54+
The order of this list is not important, because the `linear_obj_galaxy_dict` function associates each
55+
mapper object (and therefore image-plane mesh-grid) with the galaxy it belongs to and is therefore used
56+
elsewhere in the code (e.g. the fit module) to match inversion results to galaxies.
57+
58+
Certain image meshes adapt their pixels to the dataset, for example congregating the pixels to the brightest
59+
regions of the image. This requires that `adapt_images` are used when setting up the image-plane mesh-grid.
60+
This function uses the `adapt_images` attribute of the `GalaxiesToInversion` object pass these images and
61+
raise an error if they are not present.
62+
4063
Parameters
4164
----------
4265
mapper_indices
@@ -77,9 +100,16 @@ def __init__(
77100

78101
self.source_pixel_zeroed_indices_to_keep = np.where(values_to_solve)[0]
79102

80-
if image_plane_mesh_grid is not None:
103+
if image_plane_mesh_grid_list is not None:
104+
105+
self.image_plane_mesh_grid_list = []
106+
107+
for image_plane_mesh_grid in image_plane_mesh_grid_list:
81108

82-
self.image_plane_mesh_grid = np.array(image_plane_mesh_grid)
109+
if image_plane_mesh_grid is not None:
110+
self.image_plane_mesh_grid_list.append(np.array(image_plane_mesh_grid))
111+
else:
112+
self.image_plane_mesh_grid_list.append(None)
83113

84114
if linear_light_profile_blurred_mapping_matrix is not None:
85115

test_autoarray/inversion/pixelization/image_mesh/test_abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test__check_adapt_background_pixels(mask, mesh_grid, image_mesh):
7676
)
7777

7878
image_mesh.check_adapt_background_pixels(
79-
mask=mask, mesh_grid=mesh_grid, adapt_data=adapt_data, settings=None
79+
mask=mask, mesh_grid=mesh_grid, adapt_data=adapt_data,
8080
)
8181

8282
image_mesh.check_adapt_background_pixels(

0 commit comments

Comments
 (0)