|
6 | 6 | from autoarray import exc |
7 | 7 | from autoarray.numpy_wrapper import np as jnp |
8 | 8 |
|
| 9 | + |
9 | 10 | def native_index_for_slim_index_2d_from( |
10 | 11 | mask_2d: np.ndarray, |
11 | 12 | ) -> np.ndarray: |
@@ -400,6 +401,7 @@ def mask_2d_via_pixel_coordinates_from( |
400 | 401 | return mask_2d |
401 | 402 | return buffed_mask_2d_from(mask_2d=mask_2d, buffer=buffer) # Apply buf |
402 | 403 |
|
| 404 | + |
403 | 405 | def min_false_distance_to_edge(mask: np.ndarray) -> Tuple[int, int]: |
404 | 406 | """ |
405 | 407 | Compute the minimum 1D distance in the y and x directions from any `False` value at the mask's extreme positions |
@@ -618,14 +620,18 @@ def edge_1d_indexes_from(mask_2d: np.ndarray) -> np.ndarray: |
618 | 620 | array([0, 1, 2, 3, 5, 6, 7, 8]) |
619 | 621 | """ |
620 | 622 | # Pad the mask to handle edge cases without index errors |
621 | | - padded_mask = np.pad(mask_2d, pad_width=1, mode='constant', constant_values=True) |
| 623 | + padded_mask = np.pad(mask_2d, pad_width=1, mode="constant", constant_values=True) |
622 | 624 |
|
623 | 625 | # Identify neighbors in 3x3 regions around each pixel |
624 | 626 | neighbors = ( |
625 | | - padded_mask[:-2, 1:-1] | padded_mask[2:, 1:-1] | # Up, Down |
626 | | - padded_mask[1:-1, :-2] | padded_mask[1:-1, 2:] | # Left, Right |
627 | | - padded_mask[:-2, :-2] | padded_mask[:-2, 2:] | # Top-left, Top-right |
628 | | - padded_mask[2:, :-2] | padded_mask[2:, 2:] # Bottom-left, Bottom-right |
| 627 | + padded_mask[:-2, 1:-1] |
| 628 | + | padded_mask[2:, 1:-1] # Up, Down |
| 629 | + | padded_mask[1:-1, :-2] |
| 630 | + | padded_mask[1:-1, 2:] # Left, Right |
| 631 | + | padded_mask[:-2, :-2] |
| 632 | + | padded_mask[:-2, 2:] # Top-left, Top-right |
| 633 | + | padded_mask[2:, :-2] |
| 634 | + | padded_mask[2:, 2:] # Bottom-left, Bottom-right |
629 | 635 | ) |
630 | 636 |
|
631 | 637 | # Identify edge pixels: False values with at least one True neighbor |
@@ -708,10 +714,10 @@ def border_slim_indexes_from(mask_2d: np.ndarray) -> np.ndarray: |
708 | 714 |
|
709 | 715 | # Identify border pixels: where the full length in any direction is True |
710 | 716 | border_mask = ( |
711 | | - (up_sums == np.arange(height)[:, None]) | |
712 | | - (down_sums == np.arange(height - 1, -1, -1)[:, None]) | |
713 | | - (left_sums == np.arange(width)[None, :]) | |
714 | | - (right_sums == np.arange(width - 1, -1, -1)[None, :]) |
| 717 | + (up_sums == np.arange(height)[:, None]) |
| 718 | + | (down_sums == np.arange(height - 1, -1, -1)[:, None]) |
| 719 | + | (left_sums == np.arange(width)[None, :]) |
| 720 | + | (right_sums == np.arange(width - 1, -1, -1)[None, :]) |
715 | 721 | ) & ~mask_2d |
716 | 722 |
|
717 | 723 | # Create an index array where False entries get sequential 1D indices |
@@ -767,14 +773,16 @@ def buffed_mask_2d_from(mask_2d: np.ndarray, buffer: int = 1) -> np.ndarray: |
767 | 773 | buffer_range = np.arange(-buffer, buffer + 1) |
768 | 774 |
|
769 | 775 | # Generate all possible neighbors for each False entry |
770 | | - dy, dx = np.meshgrid(buffer_range, buffer_range, indexing='ij') |
| 776 | + dy, dx = np.meshgrid(buffer_range, buffer_range, indexing="ij") |
771 | 777 | neighbors = np.stack([dy.ravel(), dx.ravel()], axis=-1) |
772 | 778 |
|
773 | 779 | # Calculate all neighboring positions for all False coordinates |
774 | 780 | all_neighbors = np.add(np.array(false_coords).T[:, np.newaxis], neighbors) |
775 | 781 |
|
776 | 782 | # 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]) |
| 783 | + valid_neighbors = np.clip( |
| 784 | + all_neighbors, [0, 0], [mask_2d.shape[0] - 1, mask_2d.shape[1] - 1] |
| 785 | + ) |
778 | 786 |
|
779 | 787 | # Update the buffed mask: set all the neighbors to False |
780 | 788 | buffed_mask_2d[valid_neighbors[:, :, 0], valid_neighbors[:, :, 1]] = False |
@@ -833,6 +841,3 @@ def rescaled_mask_2d_from(mask_2d: np.ndarray, rescale_factor: float) -> np.ndar |
833 | 841 | rescaled_mask_2d[:, 0] = 1 |
834 | 842 | rescaled_mask_2d[:, rescaled_mask_2d.shape[1] - 1] = 1 |
835 | 843 | return np.isclose(rescaled_mask_2d, 1) |
836 | | - |
837 | | - |
838 | | - |
|
0 commit comments