Skip to content

Commit 943ff40

Browse files
committed
blurring_mask_2d_from
1 parent ec8ca60 commit 943ff40

File tree

2 files changed

+16
-40
lines changed

2 files changed

+16
-40
lines changed

autoarray/mask/mask_2d_util.py

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ def mask_2d_via_pixel_coordinates_from(
312312
return buffed_mask_2d_from(mask_2d=mask_2d, buffer=buffer)
313313

314314

315-
@numba_util.jit()
315+
from scipy.ndimage import convolve
316+
317+
316318
def blurring_mask_2d_from(
317319
mask_2d: np.ndarray, kernel_shape_native: Tuple[int, int]
318320
) -> np.ndarray:
@@ -348,32 +350,20 @@ def blurring_mask_2d_from(
348350
349351
"""
350352

351-
blurring_mask_2d = np.full(mask_2d.shape, True)
353+
# Create a (3, 3) kernel of ones
354+
kernel = np.ones(kernel_shape_native, dtype=np.uint8)
352355

353-
for y in range(mask_2d.shape[0]):
354-
for x in range(mask_2d.shape[1]):
355-
if not mask_2d[y, x]:
356-
for y1 in range(
357-
(-kernel_shape_native[0] + 1) // 2,
358-
(kernel_shape_native[0] + 1) // 2,
359-
):
360-
for x1 in range(
361-
(-kernel_shape_native[1] + 1) // 2,
362-
(kernel_shape_native[1] + 1) // 2,
363-
):
364-
if (
365-
0 <= x + x1 <= mask_2d.shape[1] - 1
366-
and 0 <= y + y1 <= mask_2d.shape[0] - 1
367-
):
368-
if mask_2d[y + y1, x + x1]:
369-
blurring_mask_2d[y + y1, x + x1] = False
370-
else:
371-
raise exc.MaskException(
372-
"setup_blurring_mask extends beyond the edge "
373-
"of the mask - pad the datas array before masking"
374-
)
375-
376-
return blurring_mask_2d
356+
# Convolve the mask with the kernel, applying logical AND to maintain 'True' regions
357+
convolved_mask = convolve(mask_2d.astype(np.uint8), kernel, mode="reflect", cval=0)
358+
359+
# We want to return the mask where the convolved value is the full kernel size (i.e., 9 for a 3x3 kernel)
360+
result_mask = convolved_mask == np.prod(kernel_shape_native)
361+
362+
blurring_mask = ~mask_2d + result_mask
363+
364+
print(blurring_mask * convolved_mask)
365+
366+
return blurring_mask
377367

378368

379369
@numba_util.jit()

test_autoarray/mask/test_mask_2d_util.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -522,20 +522,6 @@ def test__blurring_mask_2d_from():
522522
)
523523
).all()
524524

525-
mask = np.array(
526-
[
527-
[True, True, True, True, True, True, True],
528-
[True, True, True, True, True, True, True],
529-
[True, True, True, True, True, True, True],
530-
[True, True, True, False, True, True, True],
531-
[True, True, True, True, True, True, True],
532-
[True, True, True, True, True, True, True],
533-
[True, True, True, True, True, True, True],
534-
]
535-
)
536-
537-
blurring_mask = util.mask_2d.blurring_mask_2d_from(mask, kernel_shape_native=(3, 3))
538-
539525
mask = np.array(
540526
[
541527
[True, True, True, True, True, True, True],

0 commit comments

Comments
 (0)