From f6cb06a0dbd4ae69f8ef3ea983baf955bd0ead5b Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Thu, 19 Jan 2023 23:57:40 +0800 Subject: [PATCH 1/3] add warn in `RandHistogramShift` Signed-off-by: KumoLiu --- monai/transforms/intensity/array.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 67afd7f3c3..2286c43b4d 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -1468,9 +1468,15 @@ def __call__(self, img: NdarrayOrTensor, randomize: bool = True) -> NdarrayOrTen if self.reference_control_points is None or self.floating_control_points is None: raise RuntimeError("please call the `randomize()` function first.") img_t = convert_to_tensor(img, track_meta=False) + img_min, img_max = img_t.min(), img_t.max() + if img_min == img_max: + warn( + f"The intensity of the image has only one value {img_min}. " + "No histogram shift is done, just return the original image." + ) + return img xp, *_ = convert_to_dst_type(self.reference_control_points, dst=img_t) yp, *_ = convert_to_dst_type(self.floating_control_points, dst=img_t) - img_min, img_max = img_t.min(), img_t.max() reference_control_points_scaled = xp * (img_max - img_min) + img_min floating_control_points_scaled = yp * (img_max - img_min) + img_min img_t = self.interp(img_t, reference_control_points_scaled, floating_control_points_scaled) From 0a6d45487babd252ca4ff2ab94cc74c1ee3df5ee Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Fri, 20 Jan 2023 00:02:13 +0800 Subject: [PATCH 2/3] minor fix Signed-off-by: KumoLiu --- monai/transforms/intensity/array.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/transforms/intensity/array.py b/monai/transforms/intensity/array.py index 2286c43b4d..774acf1e31 100644 --- a/monai/transforms/intensity/array.py +++ b/monai/transforms/intensity/array.py @@ -1471,8 +1471,8 @@ def __call__(self, img: NdarrayOrTensor, randomize: bool = True) -> NdarrayOrTen img_min, img_max = img_t.min(), img_t.max() if img_min == img_max: warn( - f"The intensity of the image has only one value {img_min}. " - "No histogram shift is done, just return the original image." + f"The image's intensity is a single value {img_min}. " + "The original image is simply returned, no histogram shift is done." ) return img xp, *_ = convert_to_dst_type(self.reference_control_points, dst=img_t) From fecf8f8c07f280b4ba58d4f9b11a6ed334e2049a Mon Sep 17 00:00:00 2001 From: KumoLiu Date: Fri, 20 Jan 2023 00:20:16 +0800 Subject: [PATCH 3/3] add unit test Signed-off-by: KumoLiu --- tests/test_rand_histogram_shift.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_rand_histogram_shift.py b/tests/test_rand_histogram_shift.py index 64dd6a926b..318dad9dfa 100644 --- a/tests/test_rand_histogram_shift.py +++ b/tests/test_rand_histogram_shift.py @@ -44,6 +44,16 @@ ] ) +WARN_TESTS = [] +for p in TEST_NDARRAYS: + WARN_TESTS.append( + [ + {"num_control_points": 5, "prob": 1.0}, + {"img": p(np.zeros(8).reshape((1, 2, 2, 2)))}, + np.zeros(8).reshape((1, 2, 2, 2)), + ] + ) + class TestRandHistogramShift(unittest.TestCase): @parameterized.expand(TESTS) @@ -71,6 +81,12 @@ def test_interp(self): self.assertEqual(yi.shape, (3, 2)) assert_allclose(yi, array_type([[1.0, 5.0], [0.5, -0.5], [4.0, 5.0]])) + @parameterized.expand(WARN_TESTS) + def test_warn(self, input_param, input_data, expected_val): + with self.assertWarns(Warning): + result = RandHistogramShift(**input_param)(**input_data) + assert_allclose(result, expected_val, type_test="tensor") + if __name__ == "__main__": unittest.main()