Skip to content

Commit 4cd2971

Browse files
committed
mask_slim_indexes_from
1 parent affea87 commit 4cd2971

1 file changed

Lines changed: 28 additions & 51 deletions

File tree

autoarray/mask/mask_2d_util.py

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,11 @@ def blurring_mask_2d_from(
457457
"""
458458

459459
# Get the distance from False values to edges
460-
y_distance, x_distance = min_false_distance_to_edge(
461-
mask_2d
462-
)
460+
y_distance, x_distance = min_false_distance_to_edge(mask_2d)
463461

464462
# Compute kernel half-size in y and x direction
465-
y_kernel_distance = (
466-
kernel_shape_native[0]
467-
) // 2
468-
x_kernel_distance = (
469-
kernel_shape_native[1]
470-
) // 2
463+
y_kernel_distance = (kernel_shape_native[0]) // 2
464+
x_kernel_distance = (kernel_shape_native[1]) // 2
471465

472466
# Check if mask is too small for the kernel size
473467
if (y_distance < y_kernel_distance) or (x_distance < x_kernel_distance):
@@ -477,29 +471,18 @@ def blurring_mask_2d_from(
477471
)
478472

479473
# Create a kernel with the given PSF shape
480-
kernel = np.ones(
481-
kernel_shape_native, dtype=np.uint8
482-
)
474+
kernel = np.ones(kernel_shape_native, dtype=np.uint8)
483475

484476
# Convolve mask with kernel producing non-zero values around mask False values
485-
convolved_mask = convolve(
486-
mask_2d.astype(np.uint8), kernel, mode="reflect", cval=0
487-
)
477+
convolved_mask = convolve(mask_2d.astype(np.uint8), kernel, mode="reflect", cval=0)
488478

489479
# Identify pixels that are non-zero and fully covered by kernel
490-
result_mask = convolved_mask == np.prod(
491-
kernel_shape_native
492-
)
480+
result_mask = convolved_mask == np.prod(kernel_shape_native)
493481

494482
# Create the blurring mask by removing False values in original mask
495-
blurring_mask = (
496-
~mask_2d + result_mask
497-
)
483+
return ~mask_2d + result_mask
498484

499-
return blurring_mask
500485

501-
502-
@numba_util.jit()
503486
def mask_slim_indexes_from(
504487
mask_2d: np.ndarray, return_masked_indexes: bool = True
505488
) -> np.ndarray:
@@ -509,49 +492,43 @@ def mask_slim_indexes_from(
509492
For example, for the following ``Mask2D``:
510493
511494
::
512-
[[True, True, True, True]
495+
[[True, True, True, True],
513496
[True, False, False, True],
514497
[True, False, True, True],
515498
[True, True, True, True]]
516499
517-
This has three unmasked (``False`` values) which have the ``slim`` indexes, there ``unmasked_slim`` is:
500+
This has three unmasked (``False`` values) which have the ``slim`` indexes, their ``unmasked_slim`` is:
518501
519502
::
520503
[0, 1, 2]
521504
522505
Parameters
523506
----------
524507
mask_2d
525-
The mask for which the 1D unmasked pixel indexes are computed.
508+
A 2D array representing the mask, where `True` indicates a masked pixel and `False` indicates an unmasked pixel.
526509
return_masked_indexes
527-
Whether to return the masked index values (`value=True`) or the unmasked index values (`value=False`).
510+
A boolean flag that determines whether to return indexes of masked (`True`) or unmasked (`False`) pixels.
528511
529512
Returns
530513
-------
531-
np.ndarray
532-
The 1D indexes of all unmasked pixels on the mask.
533-
"""
534-
535-
mask_pixel_total = 0
536-
537-
for y in range(0, mask_2d.shape[0]):
538-
for x in range(0, mask_2d.shape[1]):
539-
if mask_2d[y, x] == return_masked_indexes:
540-
mask_pixel_total += 1
541-
542-
mask_pixels = np.zeros(mask_pixel_total)
543-
mask_index = 0
544-
regular_index = 0
514+
A 1D array of indexes corresponding to either the masked or unmasked pixels in the mask.
545515
546-
for y in range(0, mask_2d.shape[0]):
547-
for x in range(0, mask_2d.shape[1]):
548-
if mask_2d[y, x] == return_masked_indexes:
549-
mask_pixels[mask_index] = regular_index
550-
mask_index += 1
551-
552-
regular_index += 1
553-
554-
return mask_pixels
516+
Examples
517+
--------
518+
>>> mask = np.array([[True, True, True, True],
519+
... [True, False, False, True],
520+
... [True, False, True, True],
521+
... [True, True, True, True]])
522+
>>> mask_slim_indexes_from(mask, return_masked_indexes=True)
523+
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
524+
>>> mask_slim_indexes_from(mask, return_masked_indexes=False)
525+
array([10, 11])
526+
"""
527+
# Flatten the mask and use np.where to get indexes of either True or False
528+
mask_flat = mask_2d.flatten()
529+
530+
# Get the indexes where the mask is equal to return_masked_indexes (True or False)
531+
return np.where(mask_flat == return_masked_indexes)[0]
555532

556533

557534
@numba_util.jit()

0 commit comments

Comments
 (0)