Skip to content
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

Add warning in RandHistogramShift #5877

Merged
merged 3 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion monai/transforms/intensity/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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)
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)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_rand_histogram_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()