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..97da82fcd7a 100644 --- a/tests/transforms/spatial/test_rand_grid_patch.py +++ b/tests/transforms/spatial/test_rand_grid_patch.py @@ -20,7 +20,7 @@ from monai.data import MetaTensor, set_track_meta from monai.transforms.spatial.array import RandGridPatch from monai.utils import set_determinism -from tests.test_utils import TEST_NDARRAYS, assert_allclose +from tests.test_utils import TEST_NDARRAYS, TEST_NDARRAYS_NO_META_TENSOR, assert_allclose A = np.arange(16).repeat(3).reshape(4, 4, 3).transpose(2, 0, 1) A11 = A[:, :2, :2] @@ -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,9 @@ 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]) +for p in TEST_NDARRAYS_NO_META_TENSOR: + 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..680782c340c 100644 --- a/tests/transforms/spatial/test_rand_grid_patchd.py +++ b/tests/transforms/spatial/test_rand_grid_patchd.py @@ -19,7 +19,7 @@ from monai.transforms.spatial.dictionary import RandGridPatchd from monai.utils import set_determinism -from tests.test_utils import TEST_NDARRAYS, assert_allclose +from tests.test_utils import TEST_NDARRAYS, TEST_NDARRAYS_NO_META_TENSOR, assert_allclose A = np.arange(16).repeat(3).reshape(4, 4, 3).transpose(2, 0, 1) A11 = A[:, :2, :2] @@ -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,9 @@ 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]) +for p in TEST_NDARRAYS_NO_META_TENSOR: + TEST_SINGLE.append([p, *TEST_CASE_15]) class TestRandGridPatchd(unittest.TestCase):