From ac18d997ec003ab845f36c4daa894d95e42cd5f4 Mon Sep 17 00:00:00 2001 From: Behrooz <3968947+drbeh@users.noreply.github.com> Date: Thu, 19 May 2022 14:14:26 +0000 Subject: [PATCH 1/2] Make the order of slices row-major Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> --- monai/data/utils.py | 4 ++-- tests/test_grid_dataset.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/monai/data/utils.py b/monai/data/utils.py index 2bd7b49731..203418f17e 100644 --- a/monai/data/utils.py +++ b/monai/data/utils.py @@ -145,8 +145,8 @@ def iter_patch_slices( ranges = tuple(starmap(range, zip(start_pos, dims, patch_size_))) # choose patches by applying product to the ranges - for position in product(*ranges[::-1]): # reverse ranges order to iterate in index order - yield tuple(slice(s, s + p) for s, p in zip(position[::-1], patch_size_)) + for position in product(*ranges): # reverse ranges order to iterate in index order + yield tuple(slice(s, s + p) for s, p in zip(position, patch_size_)) def dense_patch_slices( diff --git a/tests/test_grid_dataset.py b/tests/test_grid_dataset.py index 4d2d0d6948..529e679142 100644 --- a/tests/test_grid_dataset.py +++ b/tests/test_grid_dataset.py @@ -60,20 +60,20 @@ def test_loading_array(self): np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2)) np.testing.assert_allclose( item[0], - np.array([[[[1.4965, 2.4965], [5.4965, 6.4965]]], [[[11.3584, 12.3584], [15.3584, 16.3584]]]]), + np.array([[[[7.4965, 8.4965], [11.4965, 12.4965]]], [[[11.3584, 12.3584], [15.3584, 16.3584]]]]), rtol=1e-4, ) - np.testing.assert_allclose(item[1], np.array([[[0, 1], [0, 2], [2, 4]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5) + np.testing.assert_allclose(item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5) if sys.platform != "win32": for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=2): np.testing.assert_equal(tuple(item[0].shape), (2, 1, 2, 2)) np.testing.assert_allclose( item[0], - np.array([[[[1.2548, 2.2548], [5.2548, 6.2548]]], [[[9.1106, 10.1106], [13.1106, 14.1106]]]]), + np.array([[[[7.2548, 8.2548], [11.2548, 12.2548]]], [[[9.1106, 10.1106], [13.1106, 14.1106]]]]), rtol=1e-3, ) np.testing.assert_allclose( - item[1], np.array([[[0, 1], [0, 2], [2, 4]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5 + item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5 ) def test_loading_dict(self): @@ -102,20 +102,20 @@ def test_loading_dict(self): self.assertListEqual(item[0]["metadata"], ["test string", "test string"]) np.testing.assert_allclose( item[0]["image"], - np.array([[[[1.4965, 2.4965], [5.4965, 6.4965]]], [[[11.3584, 12.3584], [15.3584, 16.3584]]]]), + np.array([[[[7.4965, 8.4965], [11.4965, 12.4965]]], [[[11.3584, 12.3584], [15.3584, 16.3584]]]]), rtol=1e-4, ) - np.testing.assert_allclose(item[1], np.array([[[0, 1], [0, 2], [2, 4]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5) + np.testing.assert_allclose(item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5) if sys.platform != "win32": for item in DataLoader(ds, batch_size=2, shuffle=False, num_workers=2): np.testing.assert_equal(item[0]["image"].shape, (2, 1, 2, 2)) np.testing.assert_allclose( item[0]["image"], - np.array([[[[1.2548, 2.2548], [5.2548, 6.2548]]], [[[9.1106, 10.1106], [13.1106, 14.1106]]]]), + np.array([[[[7.2548, 8.2548], [11.2548, 12.2548]]], [[[9.1106, 10.1106], [13.1106, 14.1106]]]]), rtol=1e-3, ) np.testing.assert_allclose( - item[1], np.array([[[0, 1], [0, 2], [2, 4]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5 + item[1], np.array([[[0, 1], [2, 4], [0, 2]], [[0, 1], [2, 4], [2, 4]]]), rtol=1e-5 ) From 38f905d261a564867a1e2bbbc31b1559b05b2ce1 Mon Sep 17 00:00:00 2001 From: Behrooz <3968947+drbeh@users.noreply.github.com> Date: Thu, 19 May 2022 17:19:27 +0000 Subject: [PATCH 2/2] Remove a comment Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> --- monai/data/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/data/utils.py b/monai/data/utils.py index 203418f17e..ef71e3cae0 100644 --- a/monai/data/utils.py +++ b/monai/data/utils.py @@ -145,7 +145,7 @@ def iter_patch_slices( ranges = tuple(starmap(range, zip(start_pos, dims, patch_size_))) # choose patches by applying product to the ranges - for position in product(*ranges): # reverse ranges order to iterate in index order + for position in product(*ranges): yield tuple(slice(s, s + p) for s, p in zip(position, patch_size_))