|
14 | 14 | import unittest |
15 | 15 |
|
16 | 16 | import numpy as np |
| 17 | +import torch |
17 | 18 |
|
18 | 19 | from monai.transforms import ShiftIntensity |
19 | 20 | from tests.test_utils import NumpyImageTestCase2D |
20 | 21 |
|
21 | 22 |
|
22 | 23 | class TestShiftIntensity(NumpyImageTestCase2D): |
| 24 | + def test_safe_integer_offset_clamps_unsigned_image(self): |
| 25 | + image = torch.tensor([0, 100, 255], dtype=torch.uint8) |
| 26 | + for offset, expected in ((1, [1, 101, 255]), (-1, [0, 99, 254]), (1.0, [1, 101, 255]), (-1.0, [0, 99, 254])): |
| 27 | + with self.subTest(offset=offset): |
| 28 | + result = ShiftIntensity(offset=offset, safe=True)(image) |
| 29 | + torch.testing.assert_close(result, torch.tensor(expected, dtype=torch.uint8)) |
| 30 | + |
| 31 | + def test_safe_integer_offset_clamps_signed_image(self): |
| 32 | + image = torch.tensor([-128, 0, 127], dtype=torch.int8) |
| 33 | + result = ShiftIntensity(offset=1, safe=True)(image) |
| 34 | + torch.testing.assert_close(result, torch.tensor([-127, 1, 127], dtype=torch.int8)) |
| 35 | + result = ShiftIntensity(offset=-1, safe=True)(image) |
| 36 | + torch.testing.assert_close(result, torch.tensor([-128, -1, 126], dtype=torch.int8)) |
| 37 | + |
| 38 | + def test_safe_int64_preserves_large_integer_precision(self): |
| 39 | + image = torch.tensor([2**60 + 1, torch.iinfo(torch.int64).max], dtype=torch.int64) |
| 40 | + result = ShiftIntensity(offset=1, safe=True)(image) |
| 41 | + torch.testing.assert_close(result, torch.tensor([2**60 + 2, torch.iinfo(torch.int64).max], dtype=torch.int64)) |
| 42 | + negative = torch.tensor([-(2**60 + 1), torch.iinfo(torch.int64).min], dtype=torch.int64) |
| 43 | + result = ShiftIntensity(offset=-1, safe=True)(negative) |
| 44 | + torch.testing.assert_close( |
| 45 | + result, torch.tensor([-(2**60 + 2), torch.iinfo(torch.int64).min], dtype=torch.int64) |
| 46 | + ) |
| 47 | + |
| 48 | + def test_safe_int64_offset_beyond_scalar_range(self): |
| 49 | + image = torch.tensor([2**62], dtype=torch.int64) |
| 50 | + result = ShiftIntensity(offset=float(-3 * 2**62), safe=True)(image) |
| 51 | + torch.testing.assert_close(result, torch.tensor([torch.iinfo(torch.int64).min], dtype=torch.int64)) |
| 52 | + |
| 53 | + def test_safe_bool_input(self): |
| 54 | + image = torch.tensor([False, True], dtype=torch.bool) |
| 55 | + for offset, expected in ((0, [False, True]), (1, [True, True]), (-1, [False, False])): |
| 56 | + with self.subTest(offset=offset): |
| 57 | + result = ShiftIntensity(offset=offset, safe=True)(image) |
| 58 | + torch.testing.assert_close(result, torch.tensor(expected, dtype=torch.bool)) |
| 59 | + |
| 60 | + def test_safe_uint64_rejected_explicitly(self): |
| 61 | + image = torch.tensor([2**63 + 1], dtype=torch.uint64) |
| 62 | + with self.assertRaisesRegex(TypeError, "does not support torch.uint64"): |
| 63 | + ShiftIntensity(offset=1, safe=True)(image) |
| 64 | + |
| 65 | + def test_safe_integer_nonfinite_offsets_rejected(self): |
| 66 | + image = torch.tensor([1, 2], dtype=torch.int64) |
| 67 | + for offset in (float("inf"), float("-inf"), float("nan")): |
| 68 | + with self.subTest(offset=offset), self.assertRaisesRegex(ValueError, "finite offsets"): |
| 69 | + ShiftIntensity(offset=offset, safe=True)(image) |
| 70 | + |
23 | 71 | def test_value(self): |
24 | 72 | shifter = ShiftIntensity(offset=1.0) |
25 | 73 | result = shifter(self.imt) |
|
0 commit comments