From b6c71c851809aa3db50d0e45c16142ed8393890c Mon Sep 17 00:00:00 2001 From: elitap Date: Thu, 17 Aug 2023 12:50:42 +0000 Subject: [PATCH 1/5] catch case if brackets around callables are forgottn by assuring datatyp of second argument Signed-off-by: elitap --- monai/transforms/compose.py | 7 +++++++ tests/test_compose.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py index 0e0093e1bc..158497f75b 100644 --- a/monai/transforms/compose.py +++ b/monai/transforms/compose.py @@ -235,6 +235,13 @@ def __init__( ) -> None: if transforms is None: transforms = [] + + if not isinstance(map_items, bool): + raise ValueError( + f"Argument 'map_items' should be a boolean. Got {type(map_items)}." + "Check brackets when passing a sequence of callables." + ) + self.transforms = ensure_tuple(transforms) self.map_items = map_items self.unpack_items = unpack_items diff --git a/tests/test_compose.py b/tests/test_compose.py index 66a994cbfd..6c97b503d0 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -12,11 +12,14 @@ from __future__ import annotations import logging +import os import sys +import tempfile import unittest from copy import deepcopy from io import StringIO +import nibabel as nib import numpy as np import torch from parameterized import parameterized @@ -641,5 +644,21 @@ def test_compose_execute_equivalence_with_flags(self, flags, data, pipeline): self.assertTrue(expected, actual) +class TestComposeCallableInput(unittest.TestCase): + def test_value_error_when_not_sequence(self): + np_img = np.random.randn(1, 5, 5) + test_image = nib.Nifti1Image(np_img, np.eye(4)) + with tempfile.TemporaryDirectory() as tempdir: + test_image_path = os.path.join(tempdir, "test_image.nii.gz") + nib.save(test_image, test_image_path) + + xform = mt.Compose([mt.LoadImage(image_only=True), mt.Flip(0), mt.Flip(0)]) + data = xform(test_image_path) + np.testing.assert_allclose(np_img, data, atol=1e-3) + + with self.assertRaises(ValueError): + mt.Compose(mt.LoadImage(image_only=True), mt.Flip(0), mt.Flip(0))(test_image_path) + + if __name__ == "__main__": unittest.main() From 381104623ef9c0b7b9a14ede5415e47123e29f7f Mon Sep 17 00:00:00 2001 From: elitap Date: Thu, 17 Aug 2023 12:54:17 +0000 Subject: [PATCH 2/5] rephrase error message Signed-off-by: elitap --- monai/transforms/compose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/transforms/compose.py b/monai/transforms/compose.py index 158497f75b..33182ab8cf 100644 --- a/monai/transforms/compose.py +++ b/monai/transforms/compose.py @@ -238,7 +238,7 @@ def __init__( if not isinstance(map_items, bool): raise ValueError( - f"Argument 'map_items' should be a boolean. Got {type(map_items)}." + f"Argument 'map_items' should be boolean. Got {type(map_items)}." "Check brackets when passing a sequence of callables." ) From ab321d9524385ef014e257e059007d84e24be962 Mon Sep 17 00:00:00 2001 From: elitap Date: Thu, 17 Aug 2023 13:18:46 +0000 Subject: [PATCH 3/5] remove dep in new test case, only use min req --- tests/test_compose.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/test_compose.py b/tests/test_compose.py index 6c97b503d0..9fcc909f2b 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -14,12 +14,10 @@ import logging import os import sys -import tempfile import unittest from copy import deepcopy from io import StringIO -import nibabel as nib import numpy as np import torch from parameterized import parameterized @@ -646,18 +644,14 @@ def test_compose_execute_equivalence_with_flags(self, flags, data, pipeline): class TestComposeCallableInput(unittest.TestCase): def test_value_error_when_not_sequence(self): - np_img = np.random.randn(1, 5, 5) - test_image = nib.Nifti1Image(np_img, np.eye(4)) - with tempfile.TemporaryDirectory() as tempdir: - test_image_path = os.path.join(tempdir, "test_image.nii.gz") - nib.save(test_image, test_image_path) - - xform = mt.Compose([mt.LoadImage(image_only=True), mt.Flip(0), mt.Flip(0)]) - data = xform(test_image_path) - np.testing.assert_allclose(np_img, data, atol=1e-3) - - with self.assertRaises(ValueError): - mt.Compose(mt.LoadImage(image_only=True), mt.Flip(0), mt.Flip(0))(test_image_path) + data = torch.tensor(np.random.randn(1, 5, 5)) + + xform = mt.Compose([mt.Flip(0), mt.Flip(0)]) + res = xform(data) + np.testing.assert_allclose(data, res, atol=1e-3) + + with self.assertRaises(ValueError): + mt.Compose(mt.Flip(0), mt.Flip(0))(data) if __name__ == "__main__": From 48a6750a5a065adba434b2c9c23a362254e121d9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 17 Aug 2023 13:20:08 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_compose.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/test_compose.py b/tests/test_compose.py index 9fcc909f2b..af7202c737 100644 --- a/tests/test_compose.py +++ b/tests/test_compose.py @@ -12,7 +12,6 @@ from __future__ import annotations import logging -import os import sys import unittest from copy import deepcopy From 1f9ca555eb432b7a770802c2d68812f910ebbf52 Mon Sep 17 00:00:00 2001 From: elitap Date: Thu, 17 Aug 2023 13:33:08 +0000 Subject: [PATCH 5/5] I, elitap , hereby add my Signed-off-by to this commit: ab321d9524385ef014e257e059007d84e24be962 Signed-off-by: elitap