From 137892a85e71fc5dab14bc5a97ed6b19a9780691 Mon Sep 17 00:00:00 2001 From: Eloi Date: Mon, 4 Nov 2024 20:05:13 +0100 Subject: [PATCH 1/5] Fix ImageFilter to allow Gaussian filter without filter_size Fixes #8127 Update `ImageFilter` to handle Gaussian filter without requiring `filter_size`. * Modify `monai/transforms/utility/array.py` to allow Gaussian filter without `filter_size`. - Adjust `_check_filter_format` method to skip `filter_size` check for Gaussian filter. Indeed Gauss filter is the only one in the list that doesn't require a filter_size. * Add unit test in `tests/test_image_filter.py` for Gaussian filter without `filter_size`. - Verify output shape matches input shape. Note that this method is compliant with the dictionnary version since this one load the fixed version. Signed-off-by: Eloi Navet --- monai/transforms/utility/array.py | 4 ++-- tests/test_image_filter.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index 72dd189009..a2bc9d548f 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -1609,9 +1609,9 @@ def _check_all_values_uneven(self, x: tuple) -> None: def _check_filter_format(self, filter: str | NdarrayOrTensor | nn.Module, filter_size: int | None = None) -> None: if isinstance(filter, str): - if not filter_size: + if filter != "gauss" and not filter_size: # Gauss is the only filter that does not require `filter_size` raise ValueError("`filter_size` must be specified when specifying filters by string.") - if filter_size % 2 == 0: + if filter_size and filter_size % 2 == 0: raise ValueError("`filter_size` should be a single uneven integer.") if filter not in self.supported_filters: raise NotImplementedError(f"{filter}. Supported filters are {self.supported_filters}.") diff --git a/tests/test_image_filter.py b/tests/test_image_filter.py index 76e38d94f4..fb08b2295d 100644 --- a/tests/test_image_filter.py +++ b/tests/test_image_filter.py @@ -134,6 +134,12 @@ def test_pass_empty_metadata_dict(self): out_tensor = filter(image) self.assertTrue(isinstance(out_tensor, MetaTensor)) + def test_gaussian_filter_without_filter_size(self): + "Test Gaussian filter without specifying filter_size" + filter = ImageFilter("gauss", sigma=2) + out_tensor = filter(SAMPLE_IMAGE_2D) + self.assertEqual(out_tensor.shape[1:], SAMPLE_IMAGE_2D.shape[1:]) + class TestImageFilterDict(unittest.TestCase): From e4dc6a87cecc4b8b4ac0396096500bdb803d63e2 Mon Sep 17 00:00:00 2001 From: Eloi Date: Mon, 4 Nov 2024 21:07:12 +0100 Subject: [PATCH 2/5] Update array.py To satisfy the linter Signed-off-by: Eloi Signed-off-by: Eloi Navet --- monai/transforms/utility/array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/utility/array.py b/monai/transforms/utility/array.py index a2bc9d548f..1b3c59afdb 100644 --- a/monai/transforms/utility/array.py +++ b/monai/transforms/utility/array.py @@ -1609,7 +1609,7 @@ def _check_all_values_uneven(self, x: tuple) -> None: def _check_filter_format(self, filter: str | NdarrayOrTensor | nn.Module, filter_size: int | None = None) -> None: if isinstance(filter, str): - if filter != "gauss" and not filter_size: # Gauss is the only filter that does not require `filter_size` + if filter != "gauss" and not filter_size: # Gauss is the only filter that does not require `filter_size` raise ValueError("`filter_size` must be specified when specifying filters by string.") if filter_size and filter_size % 2 == 0: raise ValueError("`filter_size` should be a single uneven integer.") From 7c5b8a00a3a647cd9d93ee55898a76e6a81ce271 Mon Sep 17 00:00:00 2001 From: Eloi Navet Date: Wed, 6 Nov 2024 10:10:29 +0000 Subject: [PATCH 3/5] DCO Remediation Commit for Eloi I, Eloi , hereby add my Signed-off-by to this commit: 79e8aa0584a809c0d3c038bdf4d96c2de8efac18 Signed-off-by: Eloi Signed-off-by: Eloi Navet From df0a97c4d20db4156a412747f4fcdaf0e7dd4c13 Mon Sep 17 00:00:00 2001 From: Eloi Navet Date: Wed, 6 Nov 2024 10:42:31 +0000 Subject: [PATCH 4/5] DCO Remediation Commit for Eloi I, Eloi eloi.navet@gmail.com, hereby add my Signed-off-by to this commit: 79e8aa0584a809c0d3c038bdf4d96c2de8efac18 Signed-off-by: Eloi eloi.navet@gmail.com Signed-off-by: Eloi Navet From fb5be1cfb53a47d1436fbfa1d5560ed9ae014605 Mon Sep 17 00:00:00 2001 From: Eloi Navet Date: Wed, 6 Nov 2024 10:42:38 +0000 Subject: [PATCH 5/5] DCO Remediation Commit for Eloi Navet I, Eloi Navet , hereby add my Signed-off-by to this commit: 82715cba068c764842398fb6a10bc2242520f1ad Signed-off-by: Eloi Navet