Skip to content

Commit 72c99b5

Browse files
committed
TransformedGrid class in geometry proiles now uses Grid structure
1 parent cd3f0aa commit 72c99b5

7 files changed

Lines changed: 79 additions & 31 deletions

File tree

autoarray/mask/mapping.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from autoarray import exc
44
from autoarray.mask import mask as msk
55
from autoarray.structures import grids, arrays
6-
from autoarray.util import binning_util, array_util, grid_util
6+
from autoarray.util import binning_util, array_util, grid_util, mask_util
77

88

99
class Mapping(object):
@@ -349,6 +349,19 @@ def unmasked_blurred_array_2d_from_padded_array_psf_and_image_shape(
349349
padded_array=blurred_image, image_shape=image_shape
350350
)
351351

352+
def rescaled_mask_from_rescale_factor(self, rescale_factor):
353+
rescaled_mask = mask_util.rescaled_mask_2d_from_mask_2d_and_rescale_factor(
354+
mask_2d=self.mask, rescale_factor=rescale_factor
355+
)
356+
return msk.Mask(mask_2d=rescaled_mask, pixel_scales=self.mask.pixel_scales, sub_size=self.mask.sub_size, origin=self.mask.origin)
357+
358+
@property
359+
def edge_buffed_mask(self):
360+
edge_buffed_mask = mask_util.edge_buffed_mask_2d_from_mask_2d(mask_2d=self.mask).astype(
361+
"bool"
362+
)
363+
return msk.Mask(mask_2d=edge_buffed_mask, pixel_scales=self.mask.pixel_scales, sub_size=self.mask.sub_size, origin=self.mask.origin)
364+
352365
@property
353366
def mask_sub_1(self):
354367
return msk.Mask(mask_2d=self.mask, sub_size=1, pixel_scales=self.mask.pixel_scales, origin=self.mask.origin)

autoarray/mask/mask.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def __array_finalize__(self, obj):
6262
@classmethod
6363
def manual(cls, mask_2d, pixel_scales=None, sub_size=1, origin=(0.0, 0.0), invert=False):
6464

65-
mask_2d = np.asarray(mask_2d).astype('bool')
65+
if type(mask_2d) is list:
66+
mask_2d = np.asarray(mask_2d).astype('bool')
6667

6768
if invert:
6869
mask_2d = np.invert(mask_2d)
@@ -358,7 +359,7 @@ def shape_2d(self):
358359

359360
@property
360361
def sub_shape_1d(self):
361-
return self.pixels_in_mask * self.sub_size ** 2.0
362+
return int(self.pixels_in_mask * self.sub_size ** 2.0)
362363

363364
@property
364365
def sub_shape_2d(self):

autoarray/structures/abstract_structure.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ def __new__(cls, structure_1d, mask, *args, **kwargs):
119119
obj.mask = mask
120120
return obj
121121

122+
def __array_finalize__(self, obj):
123+
124+
if isinstance(obj, AbstractStructure):
125+
126+
self.mask = obj.mask
127+
122128
@property
123129
def shape_1d(self):
124130
return self.mask.shape_1d
@@ -127,16 +133,6 @@ def shape_1d(self):
127133
def sub_shape_1d(self):
128134
return self.mask.sub_shape_1d
129135

130-
@property
131-
def in_1d(self):
132-
return self
133-
134-
def __array_finalize__(self, obj):
135-
136-
if isinstance(obj, AbstractStructure):
137-
138-
self.mask = obj.mask
139-
140136
@property
141137
def shape_2d(self):
142138
return self.mask.shape
@@ -145,6 +141,10 @@ def shape_2d(self):
145141
def sub_shape_2d(self):
146142
return self.mask.sub_shape_2d
147143

144+
@property
145+
def in_1d(self):
146+
return self
147+
148148
@property
149149
def pixel_scales(self):
150150
return self.mask.pixel_scales

autoarray/structures/arrays.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ def from_sub_array_2d_pixel_scales_and_sub_size(cls, sub_array_2d, pixel_scales,
227227
@classmethod
228228
def manual_1d(cls, array, shape_2d, pixel_scales=None, sub_size=1, origin=(0.0, 0.0)):
229229

230-
array = np.asarray(array)
230+
if type(array) is list:
231+
array = np.asarray(array)
231232

232233
if type(pixel_scales) is float:
233234
pixel_scales = (pixel_scales, pixel_scales)
@@ -242,7 +243,8 @@ def manual_1d(cls, array, shape_2d, pixel_scales=None, sub_size=1, origin=(0.0,
242243
@classmethod
243244
def manual_2d(cls, array, pixel_scales=None, sub_size=1, origin=(0.0, 0.0)):
244245

245-
array = np.asarray(array)
246+
if type(array) is list:
247+
array = np.asarray(array)
246248

247249
if type(pixel_scales) is float:
248250
pixel_scales = (pixel_scales, pixel_scales)
@@ -281,7 +283,8 @@ class MaskedArray(AbstractArray):
281283
@classmethod
282284
def manual_1d(cls, array, mask):
283285

284-
array = np.asarray(array)
286+
if type(array) is list:
287+
array = np.asarray(array)
285288

286289
if array.shape[0] != mask.sub_pixels_in_mask:
287290
raise exc.ArrayException('The input 1D array does not have the same number of entries as sub-pixels in'
@@ -292,7 +295,8 @@ def manual_1d(cls, array, mask):
292295
@classmethod
293296
def manual_2d(cls, array, mask):
294297

295-
array = np.asarray(array)
298+
if type(array) is list:
299+
array = np.asarray(array)
296300

297301
if array.shape != mask.sub_shape_2d:
298302
raise exc.ArrayException('The input array is 2D but not the same dimensions as the sub-mask '

autoarray/structures/grids.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ def from_sub_grid_2d_pixel_scales_and_sub_size(cls, sub_grid_2d, pixel_scales, s
382382
@classmethod
383383
def manual_1d(cls, grid, shape_2d, pixel_scales=None, sub_size=1, origin=(0.0, 0.0)):
384384

385-
grid = np.asarray(grid)
385+
if type(grid) is list:
386+
grid = np.asarray(grid)
386387

387388
if type(pixel_scales) is float:
388389
pixel_scales = (pixel_scales, pixel_scales)
@@ -400,7 +401,8 @@ def manual_1d(cls, grid, shape_2d, pixel_scales=None, sub_size=1, origin=(0.0, 0
400401
@classmethod
401402
def manual_2d(cls, grid, pixel_scales=None, sub_size=1, origin=(0.0, 0.0)):
402403

403-
grid = np.asarray(grid)
404+
if type(grid) is list:
405+
grid = np.asarray(grid)
404406

405407
if type(pixel_scales) is float:
406408
pixel_scales = (pixel_scales, pixel_scales)
@@ -499,8 +501,9 @@ class MaskedGrid(AbstractGrid):
499501

500502
@classmethod
501503
def manual_1d(cls, grid, mask):
502-
503-
grid = np.asarray(grid)
504+
505+
if type(grid) is list:
506+
grid = np.asarray(grid)
504507

505508
if grid.shape[0] != mask.sub_pixels_in_mask:
506509
raise exc.GridException('The input 1D grid does not have the same number of entries as sub-pixels in'
@@ -511,7 +514,8 @@ def manual_1d(cls, grid, mask):
511514
@classmethod
512515
def manual_2d(cls, grid, mask):
513516

514-
grid = np.asarray(grid)
517+
if type(grid) is list:
518+
grid = np.asarray(grid)
515519

516520
if (grid.shape[0], grid.shape[1]) != mask.sub_shape_2d:
517521
raise exc.GridException('The input grid is 2D but not the same dimensions as the sub-mask '
@@ -562,7 +566,8 @@ def __new__(
562566
nearest_irregular_1d_index_for_mask_1d_index : ndarray
563567
A 1D array that maps every grid pixel to its nearest pixelization-grid pixel.
564568
"""
565-
grid = np.asarray(grid)
569+
if type(grid) is list:
570+
grid = np.asarray(grid)
566571
obj = grid.view(cls)
567572
obj.nearest_irregular_1d_index_for_mask_1d_index = (
568573
nearest_irregular_1d_index_for_mask_1d_index
@@ -793,13 +798,11 @@ def from_mask_grid_and_pixel_scale_interpolation_grids(
793798

794799
rescale_factor = mask.pixel_scale / pixel_scale_interpolation_grid
795800

796-
rescaled_mask = mask_util.rescaled_mask_2d_from_mask_2d_and_rescale_factor(
797-
mask_2d=mask, rescale_factor=rescale_factor
798-
)
801+
mask = mask.mapping.mask_sub_1
799802

800-
interp_mask = mask_util.edge_buffed_mask_2d_from_mask_2d(mask_2d=rescaled_mask).astype(
801-
"bool"
802-
)
803+
rescaled_mask = mask.mapping.rescaled_mask_from_rescale_factor(rescale_factor=rescale_factor)
804+
805+
interp_mask = rescaled_mask.mapping.edge_buffed_mask
803806

804807
interp_grid = grid_util.grid_1d_via_mask_2d(
805808
mask_2d=interp_mask,
@@ -813,7 +816,7 @@ def from_mask_grid_and_pixel_scale_interpolation_grids(
813816

814817
return Interpolator(
815818
grid=grid,
816-
interp_grid=interp_grid,
819+
interp_grid=MaskedGrid.manual_1d(grid=interp_grid, mask=interp_mask),
817820
pixel_scale_interpolation_grid=pixel_scale_interpolation_grid,
818821
)
819822

autoarray/structures/visibilities.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class Visibilities(AbstractVisibilities):
8686
@classmethod
8787
def manual_1d(cls, visibilities):
8888

89-
visibilities = np.asarray(visibilities)
89+
if type(visibilities) is list:
90+
visibilities = np.asarray(visibilities)
9091

9192
return Visibilities(visibilities_1d=visibilities)
9293

test_autoarray/unit/mask/test_mapping.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,32 @@ def test__resized_mask__trim__compare_to_manual_mask(self):
659659

660660
assert (mask_resized == mask_resized_manual).all()
661661

662+
def test__rescaled_mask_from_rescale_factor__compare_to_manual_mask(self):
663+
664+
mask = aa.mask.unmasked(shape_2d=(5, 5))
665+
mask[2, 2] = True
666+
667+
mask_rescaled = mask.mapping.rescaled_mask_from_rescale_factor(rescale_factor=2.0)
668+
669+
mask_rescaled_manual = np.full(fill_value=False, shape=(3, 3))
670+
mask_rescaled_manual[1, 1] = True
671+
672+
mask_rescaled_manual = aa.util.mask.rescaled_mask_2d_from_mask_2d_and_rescale_factor(
673+
mask_2d=mask, rescale_factor=2.0
674+
)
675+
676+
assert (mask_rescaled == mask_rescaled_manual).all()
677+
678+
def test__edged_buffed_mask__compare_to_manual_mask(self):
679+
680+
mask = aa.mask.unmasked(shape_2d=(5, 5))
681+
mask[2, 2] = True
682+
683+
edge_buffed_mask_manual = aa.util.mask.edge_buffed_mask_2d_from_mask_2d(mask_2d=mask).astype(
684+
"bool"
685+
)
686+
687+
assert (mask.mapping.edge_buffed_mask == edge_buffed_mask_manual).all()
662688

663689
class TestBinnedMask:
664690

0 commit comments

Comments
 (0)