44
55if TYPE_CHECKING :
66 from autoarray .structures .arrays .uniform_2d import Array2D
7+ from autoarray .mask .mask_2d import Mask2D
78
89from autoarray .structures .arrays import array_2d_util
910from autoarray .structures .grids import grid_2d_util
@@ -18,7 +19,7 @@ def __init__(self, mask: Union[np.ndarray, List]):
1819
1920 A `Mask2D` masks values which are associated with a uniform 2D rectangular grid of pixels, where unmasked
2021 entries (which are `False`) are used in subsequent calculations and masked values (which are `True`) are
21- omitted (for a full description see the :meth:`Mask2D` class API
22+ omitted (for a full description see the :meth:`Mask2D` class API
2223 documentation <autoarray.mask.mask_2d.Mask2D.__new__>`).
2324
2425 The `Zoom2D` object calculations many different zoomed in qu
@@ -170,7 +171,66 @@ def shape_native(self) -> Tuple[int, int]:
170171 region = self .region
171172 return (region [1 ] - region [0 ], region [3 ] - region [2 ])
172173
173- def array_2d_from (self , array : Array2D , buffer : int = 1 ) -> Array2D :
174+ def extent_from (self , buffer : int = 1 ) -> np .ndarray :
175+ """
176+ For an extracted zoomed array computed from the method *zoomed_around_mask* compute its extent in scaled
177+ coordinates.
178+
179+ The extent of the grid in scaled units returned as an ``ndarray`` of the form [x_min, x_max, y_min, y_max].
180+
181+ This is used visualize zoomed and extracted arrays via the imshow() method.
182+
183+ Parameters
184+ ----------
185+ buffer
186+ The number pixels around the extracted array used as a buffer.
187+ """
188+ from autoarray .mask .mask_2d import Mask2D
189+
190+ extracted_array_2d = array_2d_util .extracted_array_2d_from (
191+ array_2d = np .array (self .mask ),
192+ y0 = self .region [0 ] - buffer ,
193+ y1 = self .region [1 ] + buffer ,
194+ x0 = self .region [2 ] - buffer ,
195+ x1 = self .region [3 ] + buffer ,
196+ )
197+
198+ mask = Mask2D .all_false (
199+ shape_native = extracted_array_2d .shape ,
200+ pixel_scales = self .mask .pixel_scales ,
201+ origin = self .centre ,
202+ )
203+
204+ return mask .geometry .extent
205+
206+ def mask_2d_from (self , buffer : int = 1 ) -> "Mask2D" :
207+ """
208+ Extract the 2D region of a mask corresponding to the rectangle encompassing all unmasked values.
209+
210+ This is used to extract and visualize only the region of an image that is used in an analysis.
211+
212+ Parameters
213+ ----------
214+ buffer
215+ The number pixels around the extracted array used as a buffer.
216+ """
217+ from autoarray .mask .mask_2d import Mask2D
218+
219+ extracted_mask_2d = array_2d_util .extracted_array_2d_from (
220+ array_2d = np .array (self .mask ),
221+ y0 = self .region [0 ] - buffer ,
222+ y1 = self .region [1 ] + buffer ,
223+ x0 = self .region [2 ] - buffer ,
224+ x1 = self .region [3 ] + buffer ,
225+ )
226+
227+ return Mask2D (
228+ mask = extracted_mask_2d ,
229+ pixel_scales = self .mask .pixel_scales ,
230+ origin = self .mask .origin ,
231+ )
232+
233+ def array_2d_from (self , array : Array2D , buffer : int = 1 ) -> Array2D :
174234 """
175235 Extract the 2D region of an array corresponding to the rectangle encompassing all unmasked values.
176236
@@ -192,14 +252,20 @@ def array_2d_from(self, array : Array2D, buffer: int = 1) -> Array2D:
192252 x1 = self .region [3 ] + buffer ,
193253 )
194254
195- mask = Mask2D .all_false (
196- shape_native = extracted_array_2d .shape ,
255+ extracted_mask_2d = array_2d_util .extracted_array_2d_from (
256+ array_2d = np .array (self .mask ),
257+ y0 = self .region [0 ] - buffer ,
258+ y1 = self .region [1 ] + buffer ,
259+ x0 = self .region [2 ] - buffer ,
260+ x1 = self .region [3 ] + buffer ,
261+ )
262+
263+ mask = Mask2D (
264+ mask = extracted_mask_2d ,
197265 pixel_scales = array .pixel_scales ,
198266 origin = array .mask .mask_centre ,
199267 )
200268
201- arr = array_2d_util .convert_array_2d (
202- array_2d = extracted_array_2d , mask_2d = mask
203- )
269+ arr = array_2d_util .convert_array_2d (array_2d = extracted_array_2d , mask_2d = mask )
204270
205- return Array2D (values = arr , mask = mask , header = array .header )
271+ return Array2D (values = arr , mask = mask , header = array .header )
0 commit comments