Skip to content

Commit ff27742

Browse files
Jammy2211Jammy2211
authored andcommitted
move zoom array to Zoom2D
1 parent 6b363da commit ff27742

7 files changed

Lines changed: 159 additions & 143 deletions

File tree

autoarray/mask/derive/zoom_2d.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22
import numpy as np
3-
from typing import List, Tuple, Union
3+
from typing import TYPE_CHECKING, List, Tuple, Union
44

5+
if TYPE_CHECKING:
6+
from autoarray.structures.arrays.uniform_2d import Array2D
7+
8+
from autoarray.structures.arrays import array_2d_util
59
from autoarray.structures.grids import grid_2d_util
610

711

@@ -164,4 +168,38 @@ def shape_native(self) -> Tuple[int, int]:
164168
The shape of the zoomed in region in pixels.
165169
"""
166170
region = self.region
167-
return (region[1] - region[0], region[3] - region[2])
171+
return (region[1] - region[0], region[3] - region[2])
172+
173+
def array_2d_from(self, array : Array2D, buffer: int = 1) -> Array2D:
174+
"""
175+
Extract the 2D region of an array corresponding to the rectangle encompassing all unmasked values.
176+
177+
This is used to extract and visualize only the region of an image that is used in an analysis.
178+
179+
Parameters
180+
----------
181+
buffer
182+
The number pixels around the extracted array used as a buffer.
183+
"""
184+
from autoarray.structures.arrays.uniform_2d import Array2D
185+
from autoarray.mask.mask_2d import Mask2D
186+
187+
extracted_array_2d = array_2d_util.extracted_array_2d_from(
188+
array_2d=np.array(array.native),
189+
y0=self.region[0] - buffer,
190+
y1=self.region[1] + buffer,
191+
x0=self.region[2] - buffer,
192+
x1=self.region[3] + buffer,
193+
)
194+
195+
mask = Mask2D.all_false(
196+
shape_native=extracted_array_2d.shape,
197+
pixel_scales=array.pixel_scales,
198+
origin=array.mask.mask_centre,
199+
)
200+
201+
arr = array_2d_util.convert_array_2d(
202+
array_2d=extracted_array_2d, mask_2d=mask
203+
)
204+
205+
return Array2D(values=arr, mask=mask, header=array.header)

autoarray/plot/mat_plot/two_d.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from autoarray.plot.mat_plot.abstract import AbstractMatPlot
1313
from autoarray.plot.auto_labels import AutoLabels
1414
from autoarray.plot.visuals.two_d import Visuals2D
15+
from autoarray.mask.derive.zoom_2d import Zoom2D
1516
from autoarray.structures.arrays.uniform_2d import Array2D
1617

1718
from autoarray.structures.arrays import array_2d_util
@@ -259,9 +260,11 @@ def plot_array(
259260
"zoom_around_mask"
260261
]:
261262

263+
zoom = Zoom2D(mask=array.mask)
264+
262265
buffer = 0 if array.mask.is_all_false else 1
263266

264-
array = array.zoomed_around_mask(buffer=buffer)
267+
array = zoom.array_2d_from(array=array, buffer=buffer)
265268

266269
extent = array.geometry.extent
267270

autoarray/plot/wrap/two_d/array_overlay.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ArrayOverlay(AbstractMatWrap2D):
1919
def overlay_array(self, array, figure):
2020
aspect = figure.aspect_from(shape_native=array.shape_native)
2121

22-
array_zoom = array.zoomed_around_mask(buffer=0)
22+
zoom = Zoom2D(mask=array.mask)
23+
array_zoom = zoom.array_2d_from(array=array, buffer=0)
2324
extent = array_zoom.geometry.extent
2425

2526
plt.imshow(X=array.native, aspect=aspect, extent=extent, **self.config_dict)

autoarray/structures/arrays/uniform_2d.py

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,13 @@ def native_for_fits(self) -> "Array2D":
306306
If it is already stored in its `native` representation it is return as it is. If not, it is mapped from
307307
`slim` to `native` and returned as a new `Array2D`.
308308
"""
309+
if conf.instance["visualize"]["plots"]["fits_are_zoomed"]:
309310

311+
zoom = Zoom2D(mask=self.mask)
312+
313+
buffer = 0 if self.mask.is_all_false else 1
314+
315+
return zoom.array_2d_from(array=self, buffer=buffer)
310316

311317
return Array2D(
312318
values=self, mask=self.mask, header=self.header, store_native=True
@@ -471,40 +477,6 @@ def brightest_sub_pixel_coordinate_in_region_from(
471477
pixel_coordinates_2d=(subpixel_y, subpixel_x)
472478
)
473479

474-
def zoomed_around_mask(self, buffer: int = 1) -> "Array2D":
475-
"""
476-
Extract the 2D region of an array corresponding to the rectangle encompassing all unmasked values.
477-
478-
This is used to extract and visualize only the region of an image that is used in an analysis.
479-
480-
Parameters
481-
----------
482-
buffer
483-
The number pixels around the extracted array used as a buffer.
484-
"""
485-
486-
zoom = Zoom2D(mask=self.mask)
487-
488-
extracted_array_2d = array_2d_util.extracted_array_2d_from(
489-
array_2d=np.array(self.native),
490-
y0=zoom.region[0] - buffer,
491-
y1=zoom.region[1] + buffer,
492-
x0=zoom.region[2] - buffer,
493-
x1=zoom.region[3] + buffer,
494-
)
495-
496-
mask = Mask2D.all_false(
497-
shape_native=extracted_array_2d.shape,
498-
pixel_scales=self.pixel_scales,
499-
origin=self.mask.mask_centre,
500-
)
501-
502-
array = array_2d_util.convert_array_2d(
503-
array_2d=extracted_array_2d, mask_2d=mask
504-
)
505-
506-
return Array2D(values=array, mask=mask, header=self.header)
507-
508480
def resized_from(
509481
self, new_shape: Tuple[int, int], mask_pad_value: int = 0.0
510482
) -> "Array2D":

test_autoarray/mask/derive/test_zoom_2d.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,106 @@ def test__quantities():
109109

110110
assert zoom.centre == (6, 2)
111111
assert zoom.offset_pixels == (3, 1)
112+
113+
114+
def test__array_2d_from():
115+
array_2d = [
116+
[1.0, 2.0, 3.0, 4.0],
117+
[5.0, 6.0, 7.0, 8.0],
118+
[9.0, 10.0, 11.0, 12.0],
119+
[13.0, 14.0, 15.0, 16.0],
120+
]
121+
122+
mask = aa.Mask2D(
123+
mask=[
124+
[True, True, True, True],
125+
[True, False, False, True],
126+
[True, False, False, True],
127+
[True, True, True, True],
128+
],
129+
pixel_scales=(1.0, 1.0),
130+
)
131+
132+
arr = aa.Array2D(values=array_2d, mask=mask)
133+
zoom = aa.Zoom2D(mask=mask)
134+
arr_zoomed = zoom.array_2d_from(array=arr, buffer=0)
135+
136+
assert (arr_zoomed.native == np.array([[6.0, 7.0], [10.0, 11.0]])).all()
137+
138+
mask = aa.Mask2D(
139+
mask=np.array(
140+
[
141+
[True, True, True, True],
142+
[True, False, False, True],
143+
[False, False, False, True],
144+
[True, True, True, True],
145+
]
146+
),
147+
pixel_scales=(1.0, 1.0),
148+
)
149+
150+
arr = aa.Array2D(values=array_2d, mask=mask)
151+
zoom = aa.Zoom2D(mask=mask)
152+
arr_zoomed = zoom.array_2d_from(array=arr, buffer=0)
153+
154+
assert (arr_zoomed.native == np.array([[0.0, 6.0, 7.0], [9.0, 10.0, 11.0]])).all()
155+
156+
mask = aa.Mask2D(
157+
mask=np.array(
158+
[
159+
[True, False, True, True],
160+
[True, False, False, True],
161+
[True, False, False, True],
162+
[True, True, True, True],
163+
]
164+
),
165+
pixel_scales=(1.0, 1.0),
166+
)
167+
168+
arr = aa.Array2D(values=array_2d, mask=mask)
169+
zoom = aa.Zoom2D(mask=mask)
170+
arr_zoomed = zoom.array_2d_from(array=arr, buffer=0)
171+
172+
assert (arr_zoomed.native == np.array([[2.0, 0.0], [6.0, 7.0], [10.0, 11.0]])).all()
173+
174+
array_2d = np.ones(shape=(4, 4))
175+
176+
mask = aa.Mask2D(
177+
mask=np.array(
178+
[
179+
[True, True, True, True],
180+
[True, False, False, True],
181+
[True, False, False, True],
182+
[True, True, True, True],
183+
]
184+
),
185+
pixel_scales=(1.0, 1.0),
186+
)
187+
188+
arr = aa.Array2D(values=array_2d, mask=mask)
189+
zoom = aa.Zoom2D(mask=mask)
190+
arr_zoomed = zoom.array_2d_from(array=arr, buffer=0)
191+
192+
assert arr_zoomed.mask.origin == (0.0, 0.0)
193+
194+
array_2d = np.ones(shape=(6, 6))
195+
196+
mask = aa.Mask2D(
197+
mask=np.array(
198+
[
199+
[True, True, True, True, True, True],
200+
[True, True, True, True, True, True],
201+
[True, True, True, False, False, True],
202+
[True, True, True, False, False, True],
203+
[True, True, True, True, True, True],
204+
[True, True, True, True, True, True],
205+
]
206+
),
207+
pixel_scales=(1.0, 1.0),
208+
)
209+
210+
arr = aa.Array2D(values=array_2d, mask=mask)
211+
zoom = aa.Zoom2D(mask=mask)
212+
arr_zoomed = zoom.array_2d_from(array=arr, buffer=0)
213+
214+
assert arr_zoomed.mask.origin == (0.0, 1.0)

test_autoarray/plot/wrap/base/test_ticks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ def test__yticks__set():
6060
units = aplt.Units(use_scaled=True, ticks_convert_factor=None)
6161

6262
yticks = aplt.YTicks(fontsize=34)
63-
array_zoom = array.zoomed_around_mask(buffer=1)
63+
zoom = aa.Zoom2D(mask=array.mask)
64+
array_zoom = zoom.array_2d_from(array=array, buffer=0)
6465
extent = array_zoom.geometry.extent
6566
yticks.set(min_value=extent[2], max_value=extent[3], units=units)
6667

@@ -106,7 +107,8 @@ def test__xticks__set():
106107
array = aa.Array2D.ones(shape_native=(2, 2), pixel_scales=1.0)
107108
units = aplt.Units(use_scaled=True, ticks_convert_factor=None)
108109
xticks = aplt.XTicks(fontsize=34)
109-
array_zoom = array.zoomed_around_mask(buffer=1)
110+
zoom = aa.Zoom2D(mask=array.mask)
111+
array_zoom = zoom.array_2d_from(array=array, buffer=0)
110112
extent = array_zoom.geometry.extent
111113
xticks.set(min_value=extent[0], max_value=extent[1], units=units)
112114

test_autoarray/structures/arrays/test_uniform_2d.py

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -354,109 +354,6 @@ def test__trimmed_after_convolution_from():
354354
).all()
355355
assert new_arr.mask.pixel_scales == (1.0, 1.0)
356356

357-
358-
def test__zoomed_around_mask():
359-
array_2d = [
360-
[1.0, 2.0, 3.0, 4.0],
361-
[5.0, 6.0, 7.0, 8.0],
362-
[9.0, 10.0, 11.0, 12.0],
363-
[13.0, 14.0, 15.0, 16.0],
364-
]
365-
366-
mask = aa.Mask2D(
367-
mask=[
368-
[True, True, True, True],
369-
[True, False, False, True],
370-
[True, False, False, True],
371-
[True, True, True, True],
372-
],
373-
pixel_scales=(1.0, 1.0),
374-
)
375-
376-
arr_masked = aa.Array2D(values=array_2d, mask=mask)
377-
378-
arr_zoomed = arr_masked.zoomed_around_mask(buffer=0)
379-
380-
assert (arr_zoomed.native == np.array([[6.0, 7.0], [10.0, 11.0]])).all()
381-
382-
mask = aa.Mask2D(
383-
mask=np.array(
384-
[
385-
[True, True, True, True],
386-
[True, False, False, True],
387-
[False, False, False, True],
388-
[True, True, True, True],
389-
]
390-
),
391-
pixel_scales=(1.0, 1.0),
392-
)
393-
394-
arr_masked = aa.Array2D(values=array_2d, mask=mask)
395-
arr_zoomed = arr_masked.zoomed_around_mask(buffer=0)
396-
397-
assert (arr_zoomed.native == np.array([[0.0, 6.0, 7.0], [9.0, 10.0, 11.0]])).all()
398-
399-
mask = aa.Mask2D(
400-
mask=np.array(
401-
[
402-
[True, False, True, True],
403-
[True, False, False, True],
404-
[True, False, False, True],
405-
[True, True, True, True],
406-
]
407-
),
408-
pixel_scales=(1.0, 1.0),
409-
)
410-
411-
arr_masked = aa.Array2D(values=array_2d, mask=mask)
412-
arr_zoomed = arr_masked.zoomed_around_mask(buffer=0)
413-
assert (arr_zoomed.native == np.array([[2.0, 0.0], [6.0, 7.0], [10.0, 11.0]])).all()
414-
415-
416-
def test__zoomed_around_mask__origin_updated():
417-
array_2d = np.ones(shape=(4, 4))
418-
419-
mask = aa.Mask2D(
420-
mask=np.array(
421-
[
422-
[True, True, True, True],
423-
[True, False, False, True],
424-
[True, False, False, True],
425-
[True, True, True, True],
426-
]
427-
),
428-
pixel_scales=(1.0, 1.0),
429-
)
430-
431-
arr_masked = aa.Array2D(values=array_2d, mask=mask)
432-
433-
arr_zoomed = arr_masked.zoomed_around_mask(buffer=0)
434-
435-
assert arr_zoomed.mask.origin == (0.0, 0.0)
436-
437-
array_2d = np.ones(shape=(6, 6))
438-
439-
mask = aa.Mask2D(
440-
mask=np.array(
441-
[
442-
[True, True, True, True, True, True],
443-
[True, True, True, True, True, True],
444-
[True, True, True, False, False, True],
445-
[True, True, True, False, False, True],
446-
[True, True, True, True, True, True],
447-
[True, True, True, True, True, True],
448-
]
449-
),
450-
pixel_scales=(1.0, 1.0),
451-
)
452-
453-
arr_masked = aa.Array2D(values=array_2d, mask=mask)
454-
455-
arr_zoomed = arr_masked.zoomed_around_mask(buffer=0)
456-
457-
assert arr_zoomed.mask.origin == (0.0, 1.0)
458-
459-
460357
def test__binned_across_rows():
461358
array = aa.Array2D.no_mask(values=np.ones((4, 3)), pixel_scales=1.0)
462359

0 commit comments

Comments
 (0)