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..3f027ee66e1 100644 --- a/tests/transforms/spatial/test_rand_grid_patch.py +++ b/tests/transforms/spatial/test_rand_grid_patch.py @@ -66,6 +66,7 @@ A, [A[:, 1:3, 1:3]], ] +TEST_CASE_14 = [{"patch_size": (0, 2)}, A, [A[:, :, :2], A[:, :, 2:]]] TEST_CASE_META_0 = [ {"patch_size": (2, 2)}, @@ -102,6 +103,7 @@ 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]) 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..2a77d4a45dd 100644 --- a/tests/transforms/spatial/test_rand_grid_patchd.py +++ b/tests/transforms/spatial/test_rand_grid_patchd.py @@ -65,6 +65,7 @@ {"image": A}, [A[:, 1:3, 1:3]], ] +TEST_CASE_14 = [{"patch_size": (0, 2)}, {"image": A}, [A[:, :, :2], A[:, :, 2:]]] TEST_SINGLE = [] for p in TEST_NDARRAYS: @@ -82,6 +83,7 @@ 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]) class TestRandGridPatchd(unittest.TestCase):