Skip to content

Commit

Permalink
simplify grid over sampled API
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Dec 16, 2024
1 parent 1fab29e commit 305f50e
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 52 deletions.
2 changes: 1 addition & 1 deletion autoarray/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def make_regularization_matern_kernel():

def make_rectangular_mesh_grid_3x3():
return aa.Mesh2DRectangular.overlay_grid(
grid=make_grid_2d_sub_2_7x7().grid_over_sampled, shape_native=(3, 3)
grid=make_grid_2d_sub_2_7x7().over_sampled, shape_native=(3, 3)
)


Expand Down
8 changes: 4 additions & 4 deletions autoarray/inversion/pixelization/border_relocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,16 @@ def relocated_grid_from(self, grid: Grid2D) -> Grid2D:
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]),
over_sampled = grid_2d_util.relocated_grid_via_jit_from(
grid=np.array(grid.over_sampled),
border_grid=np.array(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,
over_sampled=over_sampled,
)

def relocated_mesh_grid_from(
Expand Down
6 changes: 3 additions & 3 deletions autoarray/inversion/pixelization/mappers/delaunay.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def pix_sub_weights(self) -> PixSubWeights:
delaunay = self.delaunay

simplex_index_for_sub_slim_index = delaunay.find_simplex(
self.source_plane_data_grid.grid_over_sampled
self.source_plane_data_grid.over_sampled
)
pix_indexes_for_simplex_index = delaunay.simplices

mappings, sizes = mapper_util.pix_indexes_for_sub_slim_index_delaunay_from(
source_plane_data_grid=np.array(
self.source_plane_data_grid.grid_over_sampled
self.source_plane_data_grid.over_sampled
),
simplex_index_for_sub_slim_index=simplex_index_for_sub_slim_index,
pix_indexes_for_simplex_index=pix_indexes_for_simplex_index,
Expand All @@ -130,7 +130,7 @@ def pix_sub_weights(self) -> PixSubWeights:

weights = mapper_util.pixel_weights_delaunay_from(
source_plane_data_grid=np.array(
self.source_plane_data_grid.grid_over_sampled
self.source_plane_data_grid.over_sampled
),
source_plane_mesh_grid=np.array(self.source_plane_mesh_grid),
slim_index_for_sub_slim_index=self.slim_index_for_sub_slim_index,
Expand Down
4 changes: 2 additions & 2 deletions autoarray/inversion/pixelization/mappers/rectangular.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def pix_sub_weights(self) -> PixSubWeights:
are equal to 1.0.
"""
mappings = geometry_util.grid_pixel_indexes_2d_slim_from(
grid_scaled_2d_slim=np.array(self.source_plane_data_grid.grid_over_sampled),
grid_scaled_2d_slim=np.array(self.source_plane_data_grid.over_sampled),
shape_native=self.source_plane_mesh_grid.shape_native,
pixel_scales=self.source_plane_mesh_grid.pixel_scales,
origin=self.source_plane_mesh_grid.origin,
Expand All @@ -112,6 +112,6 @@ def pix_sub_weights(self) -> PixSubWeights:
mappings=mappings,
sizes=np.ones(len(mappings), dtype="int"),
weights=np.ones(
(len(self.source_plane_data_grid.grid_over_sampled), 1), dtype="int"
(len(self.source_plane_data_grid.over_sampled), 1), dtype="int"
),
)
2 changes: 1 addition & 1 deletion autoarray/inversion/pixelization/mappers/voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def pix_sub_weights(self) -> PixSubWeights:
"""

mappings, sizes, weights = mapper_util.pix_size_weights_voronoi_nn_from(
grid=self.source_plane_data_grid.grid_over_sampled,
grid=self.source_plane_data_grid.over_sampled,
mesh_grid=self.source_plane_mesh_grid,
)

Expand Down
2 changes: 1 addition & 1 deletion autoarray/inversion/pixelization/mesh/rectangular.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def mesh_grid_from(
by overlaying the `source_plane_data_grid` with the rectangular pixelization.
"""
return Mesh2DRectangular.overlay_grid(
shape_native=self.shape, grid=source_plane_data_grid.grid_over_sampled
shape_native=self.shape, grid=source_plane_data_grid.over_sampled
)

@property
Expand Down
4 changes: 2 additions & 2 deletions autoarray/inversion/pixelization/mesh/triangulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ def mapper_grids_from(

relocated_mesh_grid = self.relocated_mesh_grid_from(
border_relocator=border_relocator,
source_plane_data_grid=relocated_grid.grid_over_sampled,
source_plane_data_grid=relocated_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.grid_over_sampled,
source_plane_data_grid=relocated_grid.over_sampled,
source_plane_mesh_grid=relocated_mesh_grid,
)
except ValueError as e:
Expand Down
4 changes: 2 additions & 2 deletions autoarray/operators/over_sampling/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def wrapper(
return func(obj=obj, grid=grid, *args, **kwargs)

if obj is not None:
values = func(obj, grid.grid_over_sampled, *args, **kwargs)
values = func(obj, grid.over_sampled, *args, **kwargs)
else:
values = func(grid.grid_over_sampled, *args, **kwargs)
values = func(grid.over_sampled, *args, **kwargs)

return grid.over_sampler.binned_array_2d_from(array=values)

Expand Down
4 changes: 2 additions & 2 deletions autoarray/plot/get_visuals/two_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ def via_mapper_for_source_from(self, mapper: MapperRectangular) -> Visuals2D:

grid = self.get(
"grid",
mapper.source_plane_data_grid.grid_over_sampled,
mapper.source_plane_data_grid.over_sampled,
"mapper_source_plane_data_grid",
)

try:
border_grid = mapper.mapper_grids.source_plane_data_grid.grid_over_sampled[
border_grid = mapper.mapper_grids.source_plane_data_grid.over_sampled[
mapper.border_relocator.sub_border_slim
]
border = self.get("border", border_grid)
Expand Down
6 changes: 3 additions & 3 deletions autoarray/plot/mat_plot/two_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ def _plot_rectangular_mapper(

visuals_2d.plot_via_plotter(
plotter=self,
grid_indexes=mapper.source_plane_data_grid.grid_over_sampled,
grid_indexes=mapper.source_plane_data_grid.over_sampled,
mapper=mapper,
geometry=mapper.mapper_grids.mask.geometry,
)
Expand Down Expand Up @@ -694,7 +694,7 @@ def _plot_delaunay_mapper(

visuals_2d.plot_via_plotter(
plotter=self,
grid_indexes=mapper.source_plane_data_grid.grid_over_sampled,
grid_indexes=mapper.source_plane_data_grid.over_sampled,
mapper=mapper,
geometry=mapper.mapper_grids.mask.geometry,
)
Expand Down Expand Up @@ -777,7 +777,7 @@ def _plot_voronoi_mapper(

visuals_2d.plot_via_plotter(
plotter=self,
grid_indexes=mapper.source_plane_data_grid.grid_over_sampled,
grid_indexes=mapper.source_plane_data_grid.over_sampled,
mapper=mapper,
geometry=mapper.mapper_grids.mask.geometry,
)
Expand Down
2 changes: 1 addition & 1 deletion autoarray/plot/visuals/two_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ def plot_via_plotter(self, plotter, grid_indexes=None, mapper=None, geometry=Non

else:
plotter.index_scatter.scatter_grid_indexes(
grid=mapper.source_plane_data_grid.grid_over_sampled,
grid=mapper.source_plane_data_grid.over_sampled,
indexes=indexes,
)
12 changes: 6 additions & 6 deletions autoarray/structures/decorators/to_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ def via_grid_2d(self, result) -> Union[Grid2D, List[Grid2D]]:
"""
if not isinstance(result, list):
try:
grid_over_sampled = result.grid_over_sampled
over_sampled = result.over_sampled
except AttributeError:
grid_over_sampled = None
over_sampled = None

return Grid2D(
values=result,
mask=self.mask,
over_sampling_size=self.over_sampling_size,
grid_over_sampled=grid_over_sampled,
over_sampled=over_sampled,
)

try:
grid_over_sampled_list = [res.grid_over_sampled for res in result]
grid_over_sampled_list = [res.over_sampled for res in result]
except AttributeError:
grid_over_sampled_list = [None] * len(result)

Expand All @@ -44,9 +44,9 @@ def via_grid_2d(self, result) -> Union[Grid2D, List[Grid2D]]:
values=res,
mask=self.mask,
over_sampling_size=self.over_sampling_size,
grid_over_sampled=grid_over_sampled,
over_sampled=over_sampled,
)
for res, grid_over_sampled in zip(result, grid_over_sampled_list)
for res, over_sampled in zip(result, grid_over_sampled_list)
]

def via_grid_2d_irr(self, result) -> Union[Grid2DIrregular, List[Grid2DIrregular]]:
Expand Down
8 changes: 4 additions & 4 deletions autoarray/structures/grids/uniform_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(
mask: Mask2D,
store_native: bool = False,
over_sampling_size: Union[int, Array2D] = 4,
grid_over_sampled: Optional[Grid2D] = None,
over_sampled: Optional[Grid2D] = None,
*args,
**kwargs,
):
Expand Down Expand Up @@ -177,8 +177,8 @@ def __init__(

self.over_sampler = OverSampler(sub_size=over_sampling_size, mask=mask)

if grid_over_sampled is None:
self.grid_over_sampled = (
if over_sampled is None:
self.over_sampled = (
over_sample_util.grid_2d_slim_over_sampled_via_mask_from(
mask_2d=np.array(self.mask),
pixel_scales=self.mask.pixel_scales,
Expand All @@ -188,7 +188,7 @@ def __init__(
)

else:
self.grid_over_sampled = grid_over_sampled
self.over_sampled = over_sampled

@classmethod
def no_mask(
Expand Down
12 changes: 6 additions & 6 deletions test_autoarray/inversion/pixelization/mappers/test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test__rectangular_mapper():

# Slightly manipulate input grid so sub gridding is evidence in first source pixel.
grid = aa.Grid2D.from_mask(mask=mask, over_sampling_size=2)
grid.grid_over_sampled[0, 0] = -2.0
grid.grid_over_sampled[0, 1] = 2.0
grid.over_sampled[0, 0] = -2.0
grid.over_sampled[0, 1] = 2.0

mesh = aa.mesh.Rectangular(shape=(3, 3))

Expand Down Expand Up @@ -72,8 +72,8 @@ def test__delaunay_mapper():
# Slightly manipulate input grid so sub gridding is evidence in first source pixel.
grid = aa.Grid2D.from_mask(mask=mask, over_sampling_size=2)

grid.grid_over_sampled[0, 0] = -2.0
grid.grid_over_sampled[0, 1] = 2.0
grid.over_sampled[0, 0] = -2.0
grid.over_sampled[0, 1] = 2.0

mesh = aa.mesh.Delaunay()
image_mesh = aa.image_mesh.Overlay(shape=(3, 3))
Expand Down Expand Up @@ -128,8 +128,8 @@ def test__voronoi_mapper():
# Slightly manipulate input grid so sub gridding is evidence in first source pixel.
grid = aa.Grid2D.from_mask(mask=mask, over_sampling_size=2)

grid.grid_over_sampled[0, 0] = -2.0
grid.grid_over_sampled[0, 1] = 2.0
grid.over_sampled[0, 0] = -2.0
grid.over_sampled[0, 1] = 2.0

mesh = aa.mesh.Voronoi()
image_mesh = aa.image_mesh.Overlay(shape=(3, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util():
)

mesh_grid = aa.Mesh2DRectangular.overlay_grid(
shape_native=(3, 3), grid=grid.grid_over_sampled
shape_native=(3, 3), grid=grid.over_sampled
)

mapper_grids = aa.MapperGrids(
Expand All @@ -34,7 +34,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util():
pix_indexes_for_sub_slim_index_util = np.array(
[
aa.util.geometry.grid_pixel_indexes_2d_slim_from(
grid_scaled_2d_slim=np.array(grid.grid_over_sampled),
grid_scaled_2d_slim=np.array(grid.over_sampled),
shape_native=mesh_grid.shape_native,
pixel_scales=mesh_grid.pixel_scales,
origin=mesh_grid.origin,
Expand All @@ -49,7 +49,7 @@ def test__pix_indexes_for_sub_slim_index__matches_util():

def test__pixel_signals_from__matches_util(grid_2d_sub_1_7x7, image_7x7):
mesh_grid = aa.Mesh2DRectangular.overlay_grid(
shape_native=(3, 3), grid=grid_2d_sub_1_7x7.grid_over_sampled
shape_native=(3, 3), grid=grid_2d_sub_1_7x7.over_sampled
)

mapper_grids = aa.MapperGrids(
Expand Down
12 changes: 6 additions & 6 deletions test_autoarray/inversion/pixelization/test_border_relocator.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,15 @@ def test__relocated_grid_from__inside_border_no_relocations():
grid = aa.Grid2D.from_mask(
mask=mask, over_sampling_size=np.array(mask.pixels_in_mask * [2])
)
grid.grid_over_sampled[1, :] = [0.1, 0.1]
grid.over_sampled[1, :] = [0.1, 0.1]

border_relocator = aa.BorderRelocator(
mask=mask, sub_size=np.array(mask.pixels_in_mask * [2])
)

relocated_grid = border_relocator.relocated_grid_from(grid=grid)

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


def test__relocated_grid_from__outside_border_includes_relocations():
Expand All @@ -347,15 +347,15 @@ def test__relocated_grid_from__outside_border_includes_relocations():
grid = aa.Grid2D.from_mask(
mask=mask, over_sampling_size=np.array(mask.pixels_in_mask * [2])
)
grid.grid_over_sampled[1, :] = [10.1, 0.1]
grid.over_sampled[1, :] = [10.1, 0.1]

border_relocator = aa.BorderRelocator(
mask=mask, sub_size=np.array(mask.pixels_in_mask * [2])
)

relocated_grid = border_relocator.relocated_grid_from(grid=grid)

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

Expand All @@ -369,10 +369,10 @@ def test__relocated_grid_from__positive_origin_included_in_relocate():
)

grid = aa.Grid2D.from_mask(mask=mask, over_sampling_size=2)
grid.grid_over_sampled[1, :] = [11.1, 1.0]
grid.over_sampled[1, :] = [11.1, 1.0]

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

relocated_grid = border_relocator.relocated_grid_from(grid=grid)

assert relocated_grid.grid_over_sampled[1] == pytest.approx([1.97783243, 1.0], 1e-4)
assert relocated_grid.over_sampled[1] == pytest.approx([1.97783243, 1.0], 1e-4)
4 changes: 2 additions & 2 deletions test_autoarray/inversion/plot/test_mapper_plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ def test__get_2d__via_mapper_for_source_from(rectangular_mapper_7x7_3x3):
assert get_2d.origin.in_list == [(0.0, 0.0)]
assert (
get_2d.grid
== rectangular_mapper_7x7_3x3.source_plane_data_grid.grid_over_sampled
== rectangular_mapper_7x7_3x3.source_plane_data_grid.over_sampled
).all()
assert (get_2d.mesh_grid == rectangular_mapper_7x7_3x3.source_plane_mesh_grid).all()
border_grid = rectangular_mapper_7x7_3x3.mapper_grids.source_plane_data_grid.grid_over_sampled[
border_grid = rectangular_mapper_7x7_3x3.mapper_grids.source_plane_data_grid.over_sampled[
rectangular_mapper_7x7_3x3.border_relocator.sub_border_slim
]
assert (get_2d.border == border_grid).all()
Expand Down
2 changes: 1 addition & 1 deletion test_autoarray/operators/over_sample/test_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test__in_grid_2d__over_sample_uniform__out_ndarray_1d():

mask_sub_2 = aa.Mask2D(mask=mask_sub_2, pixel_scales=(0.5, 0.5))

grid = aa.Grid2D(values=grid_2d.grid_over_sampled, mask=mask_sub_2)
grid = aa.Grid2D(values=grid_2d.over_sampled, mask=mask_sub_2)

ndarray_1d_via_grid = obj.ndarray_1d_from(grid=grid)

Expand Down
4 changes: 2 additions & 2 deletions test_autoarray/plot/get_visuals/test_two_d.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ def test__via_mapper_for_source_from(rectangular_mapper_7x7_3x3):
assert visuals_2d.origin == (1.0, 1.0)
assert (
visuals_2d_via.grid
== rectangular_mapper_7x7_3x3.source_plane_data_grid.grid_over_sampled
== rectangular_mapper_7x7_3x3.source_plane_data_grid.over_sampled
).all()
border_grid = rectangular_mapper_7x7_3x3.mapper_grids.source_plane_data_grid.grid_over_sampled[
border_grid = rectangular_mapper_7x7_3x3.mapper_grids.source_plane_data_grid.over_sampled[
rectangular_mapper_7x7_3x3.border_relocator.sub_border_slim
]
assert (visuals_2d_via.border == border_grid).all()
Expand Down

0 comments on commit 305f50e

Please sign in to comment.