Skip to content

Commit a5544b4

Browse files
committed
mask_2d_elliptical_From
1 parent c4fe49f commit a5544b4

File tree

1 file changed

+29
-31
lines changed

1 file changed

+29
-31
lines changed

autoarray/mask/mask_2d_util.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,67 +167,65 @@ def elliptical_radius_from(
167167
return np.sqrt(x_scaled_elliptical**2.0 + (y_scaled_elliptical / axis_ratio) ** 2.0)
168168

169169

170-
@numba_util.jit()
170+
171171
def mask_2d_elliptical_from(
172172
shape_native: Tuple[int, int],
173-
pixel_scales: ty.PixelScales,
173+
pixel_scales: Tuple[float, float],
174174
major_axis_radius: float,
175175
axis_ratio: float,
176176
angle: float,
177177
centre: Tuple[float, float] = (0.0, 0.0),
178178
) -> np.ndarray:
179179
"""
180-
Returns an elliptical mask from an input major-axis mask radius, axis-ratio, rotational angle, shape and
181-
centre.
180+
Create an elliptical mask within a 2D array.
182181
183-
This creates a 2D array where all values within the ellipse are unmasked and therefore `False`.
182+
This generates a 2D array where all values within the specified ellipse are unmasked (set to `False`).
184183
185184
Parameters
186185
----------
187-
shape_native: Tuple[int, int]
188-
The (y,x) shape of the mask in units of pixels.
186+
shape_native
187+
The shape of the mask array in pixels.
189188
pixel_scales
190-
The scaled units to pixel units conversion factor of each pixel.
189+
The conversion factors from pixels to scaled units.
191190
major_axis_radius
192-
The major-axis (in scaled units) of the ellipse within which pixels are unmasked.
191+
The major axis radius of the elliptical mask in scaled units.
193192
axis_ratio
194-
The axis-ratio of the ellipse within which pixels are unmasked.
193+
The axis ratio of the ellipse (minor axis / major axis).
195194
angle
196-
The rotation angle of the ellipse within which pixels are unmasked, (counter-clockwise from the positive
197-
x-axis).
195+
The rotation angle of the ellipse in degrees, counter-clockwise from the positive x-axis.
198196
centre
199-
The centre of the ellipse used to mask pixels.
197+
The central coordinate of the ellipse in scaled units.
200198
201199
Returns
202200
-------
203-
ndarray
204-
The 2D mask array whose central pixels are masked as an ellipse.
201+
np.ndarray
202+
The 2D mask array with the elliptical region defined by the major axis radius unmasked (False).
205203
206204
Examples
207205
--------
208-
mask = mask_elliptical_from(
209-
shape=(10, 10), pixel_scales=0.1, major_axis_radius=0.5, ell_comps=(0.333333, 0.0), centre=(0.0, 0.0))
206+
mask = mask_2d_elliptical_from(
207+
shape_native=(10, 10), pixel_scales=(0.1, 0.1), major_axis_radius=0.5, axis_ratio=0.5, angle=45.0, centre=(0.0, 0.0)
208+
)
210209
"""
210+
centres_scaled = mask_2d_centres_from(shape_native, pixel_scales, centre)
211211

212-
mask_2d = np.full(shape_native, True)
212+
y, x = np.ogrid[:shape_native[0], :shape_native[1]]
213+
y_scaled = (y - centres_scaled[0]) * pixel_scales[0]
214+
x_scaled = (x - centres_scaled[1]) * pixel_scales[1]
213215

214-
centres_scaled = mask_2d_centres_from(
215-
shape_native=mask_2d.shape, pixel_scales=pixel_scales, centre=centre
216-
)
216+
# Rotate the coordinates by the angle (counterclockwise)
217217

218-
for y in range(mask_2d.shape[0]):
219-
for x in range(mask_2d.shape[1]):
220-
y_scaled = (y - centres_scaled[0]) * pixel_scales[0]
221-
x_scaled = (x - centres_scaled[1]) * pixel_scales[1]
218+
r_scaled = np.sqrt(x_scaled**2 + y_scaled**2)
222219

223-
r_scaled_elliptical = elliptical_radius_from(
224-
y_scaled, x_scaled, angle, axis_ratio
225-
)
220+
theta_rotated = np.arctan2(y_scaled, x_scaled) + np.radians(angle)
226221

227-
if r_scaled_elliptical <= major_axis_radius:
228-
mask_2d[y, x] = False
222+
y_scaled_elliptical = r_scaled * np.sin(theta_rotated)
223+
x_scaled_elliptical = r_scaled * np.cos(theta_rotated)
229224

230-
return mask_2d
225+
# Compute the elliptical radius
226+
r_scaled_elliptical = np.sqrt(x_scaled_elliptical**2 + (y_scaled_elliptical / axis_ratio)**2)
227+
228+
return ~(r_scaled_elliptical <= major_axis_radius)
231229

232230

233231
@numba_util.jit()

0 commit comments

Comments
 (0)