Skip to content

Commit

Permalink
border relocator refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Dec 16, 2024
1 parent 73d6700 commit 1fab29e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 45 deletions.
78 changes: 62 additions & 16 deletions autoarray/inversion/pixelization/border_relocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,44 @@ def __init__(self, mask: Mask2D, sub_size: Union[int, Array2D]):

self.sub_size = sub_size

@cached_property
def border_slim(self):
"""
Returns the 1D ``slim`` indexes of border pixels in the ``Mask2D``, representing all unmasked
sub-pixels (given by ``False``) which neighbor any masked value (give by ``True``) and which are on the
extreme exterior of the mask.
The indexes are the extended below to form the ``sub_border_slim`` which is illustrated above.
This quantity is too complicated to write-out in a docstring, and it is recommended you print it in
Python code to understand it if anything is unclear.
Examples
--------
.. code-block:: python
import autoarray as aa
mask_2d = aa.Mask2D(
mask=[[True, True, True, True, True, True, True, True, True],
[True, False, False, False, False, False, False, False, True],
[True, False, True, True, True, True, True, False, True],
[True, False, True, False, False, False, True, False, True],
[True, False, True, False, True, False, True, False, True],
[True, False, True, False, False, False, True, False, True],
[True, False, True, True, True, True, True, False, True],
[True, False, False, False, False, False, False, False, True],
[True, True, True, True, True, True, True, True, True]]
pixel_scales=1.0,
)
derive_indexes_2d = aa.DeriveIndexes2D(mask=mask_2d)
print(derive_indexes_2d.border_slim)
"""
return self.mask.derive_indexes.border_slim

@cached_property
def sub_border_slim(self) -> np.ndarray:
"""
Expand Down Expand Up @@ -179,15 +217,6 @@ def sub_border_slim(self) -> np.ndarray:
mask_2d=np.array(self.mask), sub_size=self.sub_size
).astype("int")

@property
def sub_grid(self):
return over_sample_util.grid_2d_slim_over_sampled_via_mask_from(
mask_2d=np.array(self.mask),
pixel_scales=self.mask.pixel_scales,
sub_size=np.array(self.sub_size),
origin=self.mask.origin,
)

@cached_property
def border_grid(self) -> np.ndarray:
"""
Expand All @@ -206,9 +235,16 @@ def sub_border_grid(self) -> np.ndarray:
This is NOT all sub-pixels which are in mask pixels at the mask's border, but specifically the sub-pixels
within these border pixels which are at the extreme edge of the border.
"""
return self.sub_grid[self.sub_border_slim]
sub_grid = over_sample_util.grid_2d_slim_over_sampled_via_mask_from(
mask_2d=np.array(self.mask),
pixel_scales=self.mask.pixel_scales,
sub_size=np.array(self.sub_size),
origin=self.mask.origin,
)

def relocated_grid_from(self, grid: Grid2DIrregular) -> Grid2DIrregular:
return sub_grid[self.sub_border_slim]

def relocated_grid_from(self, grid: Grid2D) -> Grid2D:
"""
Relocate the coordinates of a grid to the border of this grid if they are outside the border, where the
border is defined as all pixels at the edge of the grid's mask (see *mask._border_1d_indexes*).
Expand All @@ -235,11 +271,21 @@ def relocated_grid_from(self, grid: Grid2DIrregular) -> Grid2DIrregular:
if len(self.sub_border_grid) == 0:
return grid

return Grid2DIrregular(
values=grid_2d_util.relocated_grid_via_jit_from(
grid=np.array(grid),
border_grid=np.array(grid[self.sub_border_slim]),
),
values = grid_2d_util.relocated_grid_via_jit_from(
grid=np.array(grid),
border_grid=np.array(grid[self.border_slim]),
)

grid_over_sampled = grid_2d_util.relocated_grid_via_jit_from(
grid=np.array(grid.grid_over_sampled),
border_grid=np.array(grid.grid_over_sampled[self.sub_border_slim]),
)

return Grid2D(
values=values,
mask=grid.mask,
over_sampling_size=self.sub_size,
grid_over_sampled=grid_over_sampled,
)

def relocated_mesh_grid_from(
Expand Down
11 changes: 2 additions & 9 deletions autoarray/inversion/pixelization/mesh/rectangular.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,12 @@ def mapper_grids_from(

self.run_time_dict = run_time_dict

relocated_grid_over_sampled = self.relocated_grid_from(
relocated_grid = self.relocated_grid_from(
border_relocator=border_relocator,
source_plane_data_grid=source_plane_data_grid.grid_over_sampled,
source_plane_data_grid=source_plane_data_grid,
preloads=preloads,
)

relocated_grid = Grid2D(
values=source_plane_data_grid,
mask=source_plane_data_grid.mask,
over_sampling_size=source_plane_data_grid.over_sampling_size,
grid_over_sampled=relocated_grid_over_sampled,
)

mesh_grid = self.mesh_grid_from(source_plane_data_grid=relocated_grid)

return MapperGrids(
Expand Down
19 changes: 6 additions & 13 deletions autoarray/inversion/pixelization/mesh/triangulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,29 +70,22 @@ def mapper_grids_from(

self.run_time_dict = run_time_dict

relocated_grid_over_sampled = self.relocated_grid_from(
relocated_grid = self.relocated_grid_from(
border_relocator=border_relocator,
source_plane_data_grid=source_plane_data_grid.grid_over_sampled,
source_plane_data_grid=source_plane_data_grid,
preloads=preloads,
)

relocated_grid = Grid2D(
values=source_plane_data_grid,
mask=source_plane_data_grid.mask,
over_sampling_size=source_plane_data_grid.over_sampling_size,
grid_over_sampled=relocated_grid_over_sampled,
)

relocated_source_plane_mesh_grid = self.relocated_mesh_grid_from(
relocated_mesh_grid = self.relocated_mesh_grid_from(
border_relocator=border_relocator,
source_plane_data_grid=relocated_grid_over_sampled,
source_plane_data_grid=relocated_grid.grid_over_sampled,
source_plane_mesh_grid=source_plane_mesh_grid,
)

try:
source_plane_mesh_grid = self.mesh_grid_from(
source_plane_data_grid=relocated_grid_over_sampled,
source_plane_mesh_grid=relocated_source_plane_mesh_grid,
source_plane_data_grid=relocated_grid.grid_over_sampled,
source_plane_mesh_grid=relocated_mesh_grid,
)
except ValueError as e:
raise e
Expand Down
1 change: 0 additions & 1 deletion autoarray/inversion/pixelization/mesh/voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def mesh_grid_from(
settings
Settings controlling the pixelization for example if a border is used to relocate its exterior coordinates.
"""

return Mesh2DVoronoi(
values=source_plane_mesh_grid,
)
14 changes: 8 additions & 6 deletions test_autoarray/inversion/pixelization/test_border_relocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ def test__relocated_grid_from__inside_border_no_relocations():
mask=mask, sub_size=np.array(mask.pixels_in_mask * [2])
)

relocated_grid = border_relocator.relocated_grid_from(grid=grid.grid_over_sampled)
relocated_grid = border_relocator.relocated_grid_from(grid=grid)

assert (relocated_grid[1] == np.array([0.1, 0.1])).all()
assert (relocated_grid.grid_over_sampled[1] == np.array([0.1, 0.1])).all()


def test__relocated_grid_from__outside_border_includes_relocations():
Expand All @@ -353,9 +353,11 @@ def test__relocated_grid_from__outside_border_includes_relocations():
mask=mask, sub_size=np.array(mask.pixels_in_mask * [2])
)

relocated_grid = border_relocator.relocated_grid_from(grid=grid.grid_over_sampled)
relocated_grid = border_relocator.relocated_grid_from(grid=grid)

assert relocated_grid[1] == pytest.approx([0.97783243, 0.00968151], 1e-4)
assert relocated_grid.grid_over_sampled[1] == pytest.approx(
[0.97783243, 0.00968151], 1e-4
)


def test__relocated_grid_from__positive_origin_included_in_relocate():
Expand All @@ -371,6 +373,6 @@ def test__relocated_grid_from__positive_origin_included_in_relocate():

border_relocator = aa.BorderRelocator(mask=mask, sub_size=grid.over_sampling_size)

relocated_grid = border_relocator.relocated_grid_from(grid=grid.grid_over_sampled)
relocated_grid = border_relocator.relocated_grid_from(grid=grid)

assert relocated_grid[1] == pytest.approx([1.97783243, 1.0], 1e-4)
assert relocated_grid.grid_over_sampled[1] == pytest.approx([1.97783243, 1.0], 1e-4)

0 comments on commit 1fab29e

Please sign in to comment.