From 7e9303eb65579e61e6d1d5509d7d836eb460b407 Mon Sep 17 00:00:00 2001 From: Rajioba1 Date: Sun, 2 Aug 2026 20:35:51 -0500 Subject: [PATCH 1/2] Fix RandGridPatch whole-dimension patch sizes Signed-off-by: Rajioba1 --- monai/transforms/spatial/array.py | 2 +- tests/transforms/spatial/test_rand_grid_patch.py | 4 ++++ tests/transforms/spatial/test_rand_grid_patchd.py | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/monai/transforms/spatial/array.py b/monai/transforms/spatial/array.py index 2d3edaeed57..0804c067e09 100644 --- a/monai/transforms/spatial/array.py +++ b/monai/transforms/spatial/array.py @@ -3567,7 +3567,7 @@ def randomize(self, array): else: min_offset = ensure_tuple_rep(self.min_offset, len(self.patch_size)) if self.max_offset is None: - max_offset = tuple(s % p for s, p in zip(array.shape[1:], self.patch_size)) + max_offset = tuple(s % p if p else 0 for s, p in zip(array.shape[1:], self.patch_size)) else: max_offset = ensure_tuple_rep(self.max_offset, len(self.patch_size)) diff --git a/tests/transforms/spatial/test_rand_grid_patch.py b/tests/transforms/spatial/test_rand_grid_patch.py index 884c7269f10..e33ee96bf09 100644 --- a/tests/transforms/spatial/test_rand_grid_patch.py +++ b/tests/transforms/spatial/test_rand_grid_patch.py @@ -66,6 +66,8 @@ A, [A[:, 1:3, 1:3]], ] +TEST_CASE_14 = [{"patch_size": (0, 2)}, A, [A[:, :, :2], A[:, :, 2:]]] +TEST_CASE_15 = [{"patch_size": (None, 2)}, A, [A[:, :, :2], A[:, :, 2:]]] TEST_CASE_META_0 = [ {"patch_size": (2, 2)}, @@ -102,6 +104,8 @@ TEST_SINGLE.append([p, *TEST_CASE_11]) TEST_SINGLE.append([p, *TEST_CASE_12]) TEST_SINGLE.append([p, *TEST_CASE_13]) + TEST_SINGLE.append([p, *TEST_CASE_14]) + TEST_SINGLE.append([p, *TEST_CASE_15]) class TestRandGridPatch(unittest.TestCase): diff --git a/tests/transforms/spatial/test_rand_grid_patchd.py b/tests/transforms/spatial/test_rand_grid_patchd.py index 6b0f9d6eb36..1db4707133e 100644 --- a/tests/transforms/spatial/test_rand_grid_patchd.py +++ b/tests/transforms/spatial/test_rand_grid_patchd.py @@ -65,6 +65,8 @@ {"image": A}, [A[:, 1:3, 1:3]], ] +TEST_CASE_14 = [{"patch_size": (0, 2)}, {"image": A}, [A[:, :, :2], A[:, :, 2:]]] +TEST_CASE_15 = [{"patch_size": (None, 2)}, {"image": A}, [A[:, :, :2], A[:, :, 2:]]] TEST_SINGLE = [] for p in TEST_NDARRAYS: @@ -82,6 +84,8 @@ TEST_SINGLE.append([p, *TEST_CASE_11]) TEST_SINGLE.append([p, *TEST_CASE_12]) TEST_SINGLE.append([p, *TEST_CASE_13]) + TEST_SINGLE.append([p, *TEST_CASE_14]) + TEST_SINGLE.append([p, *TEST_CASE_15]) class TestRandGridPatchd(unittest.TestCase): From 2ce211620cd3f2a6ca7574c3c7ee53a046a45084 Mon Sep 17 00:00:00 2001 From: Rajioba1 Date: Sun, 2 Aug 2026 21:09:10 -0500 Subject: [PATCH 2/2] Avoid object metadata in RandGridPatch tests Signed-off-by: Rajioba1 --- tests/transforms/spatial/test_rand_grid_patch.py | 2 -- tests/transforms/spatial/test_rand_grid_patchd.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/tests/transforms/spatial/test_rand_grid_patch.py b/tests/transforms/spatial/test_rand_grid_patch.py index e33ee96bf09..3f027ee66e1 100644 --- a/tests/transforms/spatial/test_rand_grid_patch.py +++ b/tests/transforms/spatial/test_rand_grid_patch.py @@ -67,7 +67,6 @@ [A[:, 1:3, 1:3]], ] TEST_CASE_14 = [{"patch_size": (0, 2)}, A, [A[:, :, :2], A[:, :, 2:]]] -TEST_CASE_15 = [{"patch_size": (None, 2)}, A, [A[:, :, :2], A[:, :, 2:]]] TEST_CASE_META_0 = [ {"patch_size": (2, 2)}, @@ -105,7 +104,6 @@ TEST_SINGLE.append([p, *TEST_CASE_12]) TEST_SINGLE.append([p, *TEST_CASE_13]) TEST_SINGLE.append([p, *TEST_CASE_14]) - TEST_SINGLE.append([p, *TEST_CASE_15]) class TestRandGridPatch(unittest.TestCase): diff --git a/tests/transforms/spatial/test_rand_grid_patchd.py b/tests/transforms/spatial/test_rand_grid_patchd.py index 1db4707133e..2a77d4a45dd 100644 --- a/tests/transforms/spatial/test_rand_grid_patchd.py +++ b/tests/transforms/spatial/test_rand_grid_patchd.py @@ -66,7 +66,6 @@ [A[:, 1:3, 1:3]], ] TEST_CASE_14 = [{"patch_size": (0, 2)}, {"image": A}, [A[:, :, :2], A[:, :, 2:]]] -TEST_CASE_15 = [{"patch_size": (None, 2)}, {"image": A}, [A[:, :, :2], A[:, :, 2:]]] TEST_SINGLE = [] for p in TEST_NDARRAYS: @@ -85,7 +84,6 @@ TEST_SINGLE.append([p, *TEST_CASE_12]) TEST_SINGLE.append([p, *TEST_CASE_13]) TEST_SINGLE.append([p, *TEST_CASE_14]) - TEST_SINGLE.append([p, *TEST_CASE_15]) class TestRandGridPatchd(unittest.TestCase):