-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Implement distance_transform_edt and the DistanceTransformEDT transform #6981
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
Changes from 10 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
48a2873
Implement distance_transform_edt and the DistanceTransformEDT transform
matt3o cbac352
Fix code style
matt3o 150d59b
Add test_distance_transform_edt to min_tests.py
matt3o 2349f89
Add DistanceTransformEDTd
matt3o e3b3846
Update docs
matt3o 62cbdce
Fix test
matt3o f32841d
Add typing
matt3o 9a88bae
Fix typing for sampling argument
matt3o c4ed6c5
Fix typing return value
matt3o f76957e
Merge branch 'dev' into gpu_edt_transform
wyli 6eccc4a
Update docs to match the code
matt3o 4e26689
CuPy now allows 4D channel-wise input
matt3o 2e97953
Merge branch 'dev' into gpu_edt_transform
matt3o 7943ffd
fixes format
wyli 101cc62
Update monai/transforms/post/array.py
matt3o 24814c4
Apply suggestions from code review
matt3o e98b10a
Add test for 4D input
matt3o 6d19ae4
Remove force_scipy flag
matt3o 1f60c62
Remove force_scipy flag
matt3o 3cccb54
Rework distance_transform_edt to include more parameters
matt3o 582efb4
Code styling
matt3o 2bb17e1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] aa31e31
DCO Remediation Commit for Matthias Hadlich <matthiashadlich@posteo.de>
matt3o 94db8ea
Final fixes
matt3o d0075c5
Fix typing
matt3o d4da2af
Merge branch 'dev' into gpu_edt_transform
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
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,121 @@ | ||
| # 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import unittest | ||
|
|
||
| import numpy as np | ||
| from parameterized import parameterized | ||
|
|
||
| from monai.transforms import DistanceTransformEDT, DistanceTransformEDTd | ||
| from tests.utils import HAS_CUPY, assert_allclose, optional_import, skip_if_no_cuda | ||
|
|
||
| momorphology, has_cucim = optional_import("cucim.core.operations.morphology") | ||
| ndimage, has_ndimage = optional_import("scipy.ndimage") | ||
| cp, _ = optional_import("cupy") | ||
|
|
||
| TEST_CASES = [ | ||
| [ | ||
|
wyli marked this conversation as resolved.
|
||
| np.array( | ||
| ([0, 1, 1, 1, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 0]), dtype=np.float32 | ||
| ), | ||
| np.array( | ||
| [ | ||
| [0.0, 1.0, 1.4142, 2.2361, 3.0], | ||
| [0.0, 0.0, 1.0, 2.0, 2.0], | ||
| [0.0, 1.0, 1.4142, 1.4142, 1.0], | ||
| [0.0, 1.0, 1.4142, 1.0, 0.0], | ||
| [0.0, 1.0, 1.0, 0.0, 0.0], | ||
| ] | ||
| ), | ||
| ] | ||
| ] | ||
|
|
||
| SAMPLING_TEST_CASES = [ | ||
| [ | ||
| 2, | ||
| np.array( | ||
| ([0, 1, 1, 1, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 0]), dtype=np.float32 | ||
| ), | ||
| np.array( | ||
| [ | ||
| [0.0, 2.0, 2.828427, 4.472136, 6.0], | ||
| [0.0, 0.0, 2.0, 4.0, 4.0], | ||
| [0.0, 2.0, 2.828427, 2.828427, 2.0], | ||
| [0.0, 2.0, 2.828427, 2.0, 0.0], | ||
| [0.0, 2.0, 2.0, 0.0, 0.0], | ||
| ] | ||
| ), | ||
| ] | ||
| ] | ||
|
|
||
| RAISES_TEST_CASES = ( | ||
| [ | ||
| np.array( | ||
| [[[[0, 1, 1, 1, 1], [0, 0, 1, 1, 1], [0, 1, 1, 1, 1], [0, 1, 1, 1, 0], [0, 1, 1, 0, 0]]]], dtype=np.float32 | ||
| ) | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| class TestDistanceTransformEDT(unittest.TestCase): | ||
| @parameterized.expand(TEST_CASES) | ||
| def test_scipy_transform(self, input, expected_output): | ||
| transform = DistanceTransformEDT(force_scipy=True) | ||
| output = transform(input) | ||
| assert_allclose(output, expected_output, atol=1e-4, rtol=1e-4, type_test=False) | ||
|
|
||
| @parameterized.expand(TEST_CASES) | ||
| def test_scipy_transformd(self, input, expected_output): | ||
| transform = DistanceTransformEDTd(keys=("to_transform",), force_scipy=True) | ||
| data = {"to_transform": input} | ||
| data_ = transform(data) | ||
| output = data_["to_transform"] | ||
| assert_allclose(output, expected_output, atol=1e-4, rtol=1e-4, type_test=False) | ||
|
|
||
| @parameterized.expand(TEST_CASES) | ||
| @skip_if_no_cuda | ||
| @unittest.skipUnless(HAS_CUPY, "CuPy is required.") | ||
| @unittest.skipUnless(momorphology, "cuCIM transforms are required.") | ||
| def test_cucim_transform(self, input, expected_output): | ||
| transform = DistanceTransformEDT() | ||
| output = transform(input) | ||
| assert_allclose(cp.asnumpy(output), cp.asnumpy(expected_output), atol=1e-4, rtol=1e-4, type_test=False) | ||
|
|
||
| @parameterized.expand(SAMPLING_TEST_CASES) | ||
| def test_sampling(self, sampling, input, expected_output): | ||
| transform = DistanceTransformEDT(force_scipy=True, sampling=sampling) | ||
| output = transform(input) | ||
| assert_allclose(output, expected_output, atol=1e-4, rtol=1e-4, type_test=False) | ||
|
|
||
| @parameterized.expand(SAMPLING_TEST_CASES) | ||
| @skip_if_no_cuda | ||
| @unittest.skipUnless(HAS_CUPY, "CuPy is required.") | ||
| @unittest.skipUnless(momorphology, "cuCIM transforms are required.") | ||
| def test_cucim_sampling(self, sampling, input, expected_output): | ||
| transform = DistanceTransformEDT(sampling=sampling) | ||
| output = transform(input) | ||
| assert_allclose(cp.asnumpy(output), cp.asnumpy(expected_output), atol=1e-4, rtol=1e-4, type_test=False) | ||
|
|
||
| # @skip_if_no_cuda | ||
| # @unittest.skipUnless(HAS_CUPY, "CuPy is required.") | ||
| # @unittest.skipUnless(momorphology, "cuCIM transforms are required.") | ||
| # @parameterized.expand(RAISES_TEST_CASES) | ||
| # def test_cucim_raises(self, raises): | ||
| # """Currently only 2D and 3D images are supported by CuPy. This test checks for the according error message""" | ||
| # transform = DistanceTransformEDT() | ||
| # with self.assertRaises(NotImplementedError): | ||
| # output = transform(raises) | ||
|
|
||
|
|
||
| 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.