Skip to content

Commit 3066c77

Browse files
committed
all numba decorators removed
1 parent 39fc173 commit 3066c77

2 files changed

Lines changed: 71 additions & 54 deletions

File tree

autoarray/mask/mask_2d_util.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import warnings
55

66
from autoarray import exc
7-
from autoarray import numba_util
8-
from autoarray import type as ty
9-
from autoarray.numpy_wrapper import use_jax, np as jnp
7+
from autoarray.numpy_wrapper import np as jnp
108

119
def native_index_for_slim_index_2d_from(
1210
mask_2d: np.ndarray,
@@ -402,10 +400,6 @@ def mask_2d_via_pixel_coordinates_from(
402400
return mask_2d
403401
return buffed_mask_2d_from(mask_2d=mask_2d, buffer=buffer) # Apply buf
404402

405-
406-
import numpy as np
407-
408-
409403
def min_false_distance_to_edge(mask: np.ndarray) -> Tuple[int, int]:
410404
"""
411405
Compute the minimum 1D distance in the y and x directions from any `False` value at the mask's extreme positions
@@ -729,38 +723,61 @@ def border_slim_indexes_from(mask_2d: np.ndarray) -> np.ndarray:
729723
return index_array[border_mask]
730724

731725

732-
@numba_util.jit()
733726
def buffed_mask_2d_from(mask_2d: np.ndarray, buffer: int = 1) -> np.ndarray:
734727
"""
735-
Returns a buffed mask from an input mask, where the buffed mask is the input mask but all `False` entries in the
736-
mask are buffed by an integer amount in all 8 surrouning pixels.
728+
Returns a buffed mask from an input mask, where all `False` entries in the mask are "buffed" (set to `False`)
729+
within a specified buffer range in all 8 surrounding directions.
730+
731+
A "buffed" mask is created by marking all the pixels within a square of size `buffer` around each `False`
732+
entry as `False`. This process simulates expanding the masked region around each `False` entry by the specified
733+
buffer distance.
737734
738735
Parameters
739736
----------
740737
mask_2d
741-
The mask whose `False` entries are buffed.
738+
A 2D boolean array where `False` values indicate unmasked pixels.
742739
buffer
743-
The number of pixels around each `False` entry that pixel are buffed in all 8 directions.
740+
The number of pixels around each `False` entry that should be buffed in all 8 surrounding directions.
741+
This controls how far the "buffed" region extends from each `False` value.
744742
745743
Returns
746744
-------
747-
np.ndarray
748-
The buffed mask.
745+
A new 2D boolean array where all `False` entries in the input mask are expanded by the specified buffer
746+
distance, setting all pixels within the buffer range to `False`.
747+
748+
Examples
749+
--------
750+
>>> mask = np.array([
751+
... [True, False, True],
752+
... [False, False, False],
753+
... [True, True, False]
754+
... ])
755+
>>> buffed_mask_2d_from(mask, buffer=1)
756+
array([[False, False, False],
757+
[False, False, False],
758+
[False, False, False]])
749759
"""
760+
# Initialize buffed mask as a copy of the input mask
750761
buffed_mask_2d = mask_2d.copy()
751762

752-
for y in range(mask_2d.shape[0]):
753-
for x in range(mask_2d.shape[1]):
754-
if not mask_2d[y, x]:
755-
for y0 in range(y - buffer, y + 1 + buffer):
756-
for x0 in range(x - buffer, x + 1 + buffer):
757-
if (
758-
y0 >= 0
759-
and x0 >= 0
760-
and y0 <= mask_2d.shape[0] - 1
761-
and x0 <= mask_2d.shape[1] - 1
762-
):
763-
buffed_mask_2d[y0, x0] = False
763+
# Identify the coordinates of all False entries
764+
false_coords = np.nonzero(~mask_2d)
765+
766+
# Create grid of offsets for the neighboring pixels (buffer range)
767+
buffer_range = np.arange(-buffer, buffer + 1)
768+
769+
# Generate all possible neighbors for each False entry
770+
dy, dx = np.meshgrid(buffer_range, buffer_range, indexing='ij')
771+
neighbors = np.stack([dy.ravel(), dx.ravel()], axis=-1)
772+
773+
# Calculate all neighboring positions for all False coordinates
774+
all_neighbors = np.add(np.array(false_coords).T[:, np.newaxis], neighbors)
775+
776+
# Clip the neighbors to stay within the bounds of the mask
777+
valid_neighbors = np.clip(all_neighbors, [0, 0], [mask_2d.shape[0] - 1, mask_2d.shape[1] - 1])
778+
779+
# Update the buffed mask: set all the neighbors to False
780+
buffed_mask_2d[valid_neighbors[:, :, 0], valid_neighbors[:, :, 1]] = False
764781

765782
return buffed_mask_2d
766783

test_autoarray/mask/test_mask_2d_util.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,34 @@
55
import pytest
66

77

8+
def test__native_index_for_slim_index_2d_from():
9+
mask = np.array([[True, True, True], [True, False, True], [True, True, True]])
10+
11+
sub_mask_index_for_sub_mask_1d_index = (
12+
util.mask_2d.native_index_for_slim_index_2d_from(mask_2d=mask)
13+
)
14+
15+
assert (sub_mask_index_for_sub_mask_1d_index == np.array([[1, 1]])).all()
16+
17+
mask = np.array(
18+
[
19+
[True, False, True],
20+
[False, False, False],
21+
[True, False, True],
22+
[True, True, False],
23+
]
24+
)
25+
26+
sub_mask_index_for_sub_mask_1d_index = (
27+
util.mask_2d.native_index_for_slim_index_2d_from(mask_2d=mask)
28+
)
29+
30+
assert (
31+
sub_mask_index_for_sub_mask_1d_index
32+
== np.array([[0, 1], [1, 0], [1, 1], [1, 2], [2, 1], [3, 2]])
33+
).all()
34+
35+
836
def test__mask_2d_circular_from():
937
mask = util.mask_2d.mask_2d_circular_from(
1038
shape_native=(3, 3), pixel_scales=(1.0, 1.0), radius=0.5
@@ -921,34 +949,6 @@ def test__border_slim_indexes_from():
921949
).all()
922950

923951

924-
def test__native_index_for_slim_index_2d_from():
925-
mask = np.array([[True, True, True], [True, False, True], [True, True, True]])
926-
927-
sub_mask_index_for_sub_mask_1d_index = (
928-
util.mask_2d.native_index_for_slim_index_2d_from(mask_2d=mask)
929-
)
930-
931-
assert (sub_mask_index_for_sub_mask_1d_index == np.array([[1, 1]])).all()
932-
933-
mask = np.array(
934-
[
935-
[True, False, True],
936-
[False, False, False],
937-
[True, False, True],
938-
[True, True, False],
939-
]
940-
)
941-
942-
sub_mask_index_for_sub_mask_1d_index = (
943-
util.mask_2d.native_index_for_slim_index_2d_from(mask_2d=mask)
944-
)
945-
946-
assert (
947-
sub_mask_index_for_sub_mask_1d_index
948-
== np.array([[0, 1], [1, 0], [1, 1], [1, 2], [2, 1], [3, 2]])
949-
).all()
950-
951-
952952
def test__rescaled_mask_2d_from():
953953
mask = np.array(
954954
[

0 commit comments

Comments
 (0)