-
Notifications
You must be signed in to change notification settings - Fork 1.4k
SplitDim #3884
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
SplitDim #3884
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
698b5eb
SplitDim
rijobro 452ffc4
fix
rijobro cfe392a
fixes
rijobro 5063e9e
Merge branch 'dev' into SplitDim
rijobro 42aaff2
Merge remote-tracking branch 'MONAI/dev' into SplitDim
rijobro bd8e99f
fix update meta
rijobro 0b0e0d3
Merge remote-tracking branch 'MONAI/dev' into SplitDim
rijobro 1d84d46
update docs
wyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.transforms.utility.array import SplitDim | ||
| from tests.utils import TEST_NDARRAYS | ||
|
|
||
| TESTS = [] | ||
| for p in TEST_NDARRAYS: | ||
| for keepdim in (True, False): | ||
| TESTS.append(((2, 10, 8, 7), keepdim, p)) | ||
|
|
||
|
|
||
| class TestSplitDim(unittest.TestCase): | ||
| @parameterized.expand(TESTS) | ||
| def test_correct_shape(self, shape, keepdim, im_type): | ||
| arr = im_type(np.random.rand(*shape)) | ||
| for dim in range(arr.ndim): | ||
| out = SplitDim(dim, keepdim)(arr) | ||
| self.assertIsInstance(out, (list, tuple)) | ||
| self.assertEqual(len(out), arr.shape[dim]) | ||
| expected_ndim = arr.ndim if keepdim else arr.ndim - 1 | ||
| self.assertEqual(out[0].ndim, expected_ndim) | ||
| # assert is a shallow copy | ||
| arr[0, 0, 0, 0] *= 2 | ||
| self.assertEqual(arr.flatten()[0], out[0].flatten()[0]) | ||
|
|
||
| def test_error(self): | ||
| """Should fail because splitting along singleton dimension""" | ||
| shape = (2, 1, 8, 7) | ||
| for p in TEST_NDARRAYS: | ||
| arr = p(np.random.rand(*shape)) | ||
| with self.assertRaises(RuntimeError): | ||
| _ = SplitDim(dim=1)(arr) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright (c) MONAI Consortium | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import unittest | ||
| from copy import deepcopy | ||
|
|
||
| import numpy as np | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.transforms import LoadImaged | ||
| from monai.transforms.utility.dictionary import SplitDimd | ||
| from tests.utils import TEST_NDARRAYS, assert_allclose, make_nifti_image, make_rand_affine | ||
|
|
||
| TESTS = [] | ||
| for p in TEST_NDARRAYS: | ||
| for keepdim in (True, False): | ||
| for update_meta in (True, False): | ||
| TESTS.append((keepdim, p, update_meta)) | ||
|
|
||
|
|
||
| class TestSplitDimd(unittest.TestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| arr = np.random.rand(2, 10, 8, 7) | ||
| affine = make_rand_affine() | ||
| data = {"i": make_nifti_image(arr, affine)} | ||
|
|
||
| cls.data = LoadImaged("i")(data) | ||
|
|
||
| @parameterized.expand(TESTS) | ||
| def test_correct(self, keepdim, im_type, update_meta): | ||
| data = deepcopy(self.data) | ||
| data["i"] = im_type(data["i"]) | ||
| arr = data["i"] | ||
| for dim in range(arr.ndim): | ||
| out = SplitDimd("i", dim=dim, keepdim=keepdim, update_meta=update_meta)(data) | ||
| self.assertIsInstance(out, dict) | ||
| num_new_keys = 2 if update_meta else 1 | ||
| self.assertEqual(len(out.keys()), len(data.keys()) + num_new_keys * arr.shape[dim]) | ||
| # if updating meta data, pick some random points and | ||
| # check same world coordinates between input and output | ||
| if update_meta: | ||
| for _ in range(10): | ||
| idx = [np.random.choice(i) for i in arr.shape] | ||
| split_im_idx = idx[dim] | ||
| split_idx = deepcopy(idx) | ||
| split_idx[dim] = 0 | ||
| # idx[1:] to remove channel and then add 1 for 4th element | ||
| real_world = data["i_meta_dict"]["affine"] @ (idx[1:] + [1]) | ||
| real_world2 = out[f"i_{split_im_idx}_meta_dict"]["affine"] @ (split_idx[1:] + [1]) | ||
| assert_allclose(real_world, real_world2) | ||
|
|
||
| out = out["i_0"] | ||
| expected_ndim = arr.ndim if keepdim else arr.ndim - 1 | ||
| self.assertEqual(out.ndim, expected_ndim) | ||
| # assert is a shallow copy | ||
| arr[0, 0, 0, 0] *= 2 | ||
| self.assertEqual(arr.flatten()[0], out.flatten()[0]) | ||
|
|
||
| def test_error(self): | ||
| """Should fail because splitting along singleton dimension""" | ||
| shape = (2, 1, 8, 7) | ||
| for p in TEST_NDARRAYS: | ||
| arr = p(np.random.rand(*shape)) | ||
| with self.assertRaises(RuntimeError): | ||
| _ = SplitDimd("i", dim=1)({"i": arr}) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.