|
4 | 4 | import warnings |
5 | 5 |
|
6 | 6 | 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 |
10 | 8 |
|
11 | 9 | def native_index_for_slim_index_2d_from( |
12 | 10 | mask_2d: np.ndarray, |
@@ -402,10 +400,6 @@ def mask_2d_via_pixel_coordinates_from( |
402 | 400 | return mask_2d |
403 | 401 | return buffed_mask_2d_from(mask_2d=mask_2d, buffer=buffer) # Apply buf |
404 | 402 |
|
405 | | - |
406 | | -import numpy as np |
407 | | - |
408 | | - |
409 | 403 | def min_false_distance_to_edge(mask: np.ndarray) -> Tuple[int, int]: |
410 | 404 | """ |
411 | 405 | 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: |
729 | 723 | return index_array[border_mask] |
730 | 724 |
|
731 | 725 |
|
732 | | -@numba_util.jit() |
733 | 726 | def buffed_mask_2d_from(mask_2d: np.ndarray, buffer: int = 1) -> np.ndarray: |
734 | 727 | """ |
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. |
737 | 734 |
|
738 | 735 | Parameters |
739 | 736 | ---------- |
740 | 737 | mask_2d |
741 | | - The mask whose `False` entries are buffed. |
| 738 | + A 2D boolean array where `False` values indicate unmasked pixels. |
742 | 739 | 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. |
744 | 742 |
|
745 | 743 | Returns |
746 | 744 | ------- |
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]]) |
749 | 759 | """ |
| 760 | + # Initialize buffed mask as a copy of the input mask |
750 | 761 | buffed_mask_2d = mask_2d.copy() |
751 | 762 |
|
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 |
764 | 781 |
|
765 | 782 | return buffed_mask_2d |
766 | 783 |
|
|
0 commit comments