From b60bd3f8274d52a88fefa2664af29fb83b7bce5f Mon Sep 17 00:00:00 2001 From: rtmalikian Date: Thu, 18 Jun 2026 00:00:25 -0700 Subject: [PATCH] fix: use P-axis length for ys calculation and clip S-axis bounds Fixes part 2 of #167: - Use mask_RPS.shape[1] (P-axis) instead of shape[2] (S-axis) for ys calculation, since the defacing line iterates over P positions and computes S-axis limits for each. - Clip y values to mask_RPS.shape[2] to prevent IndexError when the defacing line extends beyond the S-axis range. Previously, using shape[2] for the arrange caused the defacing loop to iterate over S-axis positions instead of P-axis positions, which produced incorrect defacing masks for images with narrow S axes. The old code also lacked bounds clipping, causing IndexError when computed S-axis limits exceeded the array dimensions. --- .../defacing/quickshear/nipy_quickshear.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/brainles_preprocessing/defacing/quickshear/nipy_quickshear.py b/brainles_preprocessing/defacing/quickshear/nipy_quickshear.py index 6654eaa..4fd4b13 100644 --- a/brainles_preprocessing/defacing/quickshear/nipy_quickshear.py +++ b/brainles_preprocessing/defacing/quickshear/nipy_quickshear.py @@ -155,11 +155,12 @@ def run_quickshear(bet_img: nb.nifti1.Nifti1Image, buffer: int = 10) -> NDArray: slope = ydiffs[0] / xdiffs[0] yint = low[1][0] - (low[0][0] * slope) - buffer - ys = np.arange(0, mask_RPS.shape[2]) * slope + yint + ys = np.arange(0, mask_RPS.shape[1]) * slope + yint defaced_mask_RPS = np.ones(mask_RPS.shape, dtype="bool") for x, y in zip(np.nonzero(ys > 0)[0], ys.astype(int)): - defaced_mask_RPS[:, x, :y] = 0 + y_clipped = min(y, mask_RPS.shape[2]) + defaced_mask_RPS[:, x, :y_clipped] = 0 defaced_mask = nb.orientations.apply_orientation(defaced_mask_RPS, from_RPS)