Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion monai/transforms/spatial/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
7 changes: 6 additions & 1 deletion tests/transforms/spatial/test_rand_grid_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)},
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion tests/transforms/spatial/test_rand_grid_patchd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down