|
12 | 12 | import unittest
|
13 | 13 |
|
14 | 14 | import numpy as np
|
| 15 | +import torch |
15 | 16 | from parameterized import parameterized
|
16 | 17 |
|
17 | 18 | from monai.transforms import NormalizeIntensity
|
18 |
| -from tests.utils import NumpyImageTestCase2D |
| 19 | +from tests.utils import TEST_NDARRAYS, NumpyImageTestCase2D, assert_allclose |
19 | 20 |
|
20 |
| -TEST_CASES = [ |
21 |
| - [{"nonzero": True}, np.array([0.0, 3.0, 0.0, 4.0]), np.array([0.0, -1.0, 0.0, 1.0])], |
22 |
| - [ |
23 |
| - {"subtrahend": np.array([3.5, 3.5, 3.5, 3.5]), "divisor": np.array([0.5, 0.5, 0.5, 0.5]), "nonzero": True}, |
24 |
| - np.array([0.0, 3.0, 0.0, 4.0]), |
25 |
| - np.array([0.0, -1.0, 0.0, 1.0]), |
26 |
| - ], |
27 |
| - [{"nonzero": True}, np.array([0.0, 0.0, 0.0, 0.0]), np.array([0.0, 0.0, 0.0, 0.0])], |
28 |
| - [{"nonzero": False}, np.array([0.0, 0.0, 0.0, 0.0]), np.array([0.0, 0.0, 0.0, 0.0])], |
29 |
| - [{"nonzero": False}, np.array([1, 1, 1, 1]), np.array([0.0, 0.0, 0.0, 0.0])], |
30 |
| - [ |
31 |
| - {"nonzero": False, "channel_wise": True, "subtrahend": [1, 2, 3]}, |
32 |
| - np.ones((3, 2, 2)), |
33 |
| - np.array([[[0.0, 0.0], [0.0, 0.0]], [[-1.0, -1.0], [-1.0, -1.0]], [[-2.0, -2.0], [-2.0, -2.0]]]), |
34 |
| - ], |
35 |
| - [ |
36 |
| - {"nonzero": True, "channel_wise": True, "subtrahend": [1, 2, 3], "divisor": [0, 0, 2]}, |
37 |
| - np.ones((3, 2, 2)), |
38 |
| - np.array([[[0.0, 0.0], [0.0, 0.0]], [[-1.0, -1.0], [-1.0, -1.0]], [[-1.0, -1.0], [-1.0, -1.0]]]), |
39 |
| - ], |
40 |
| - [ |
41 |
| - {"nonzero": True, "channel_wise": False, "subtrahend": 2, "divisor": 0}, |
42 |
| - np.ones((3, 2, 2)), |
43 |
| - np.ones((3, 2, 2)) * -1.0, |
44 |
| - ], |
45 |
| - [ |
46 |
| - {"nonzero": True, "channel_wise": False, "subtrahend": np.ones((3, 2, 2)) * 0.5, "divisor": 0}, |
47 |
| - np.ones((3, 2, 2)), |
48 |
| - np.ones((3, 2, 2)) * 0.5, |
49 |
| - ], |
50 |
| - [ |
51 |
| - {"nonzero": True, "channel_wise": True, "subtrahend": np.ones((3, 2, 2)) * 0.5, "divisor": [0, 1, 0]}, |
52 |
| - np.ones((3, 2, 2)), |
53 |
| - np.ones((3, 2, 2)) * 0.5, |
54 |
| - ], |
55 |
| -] |
| 21 | +TESTS = [] |
| 22 | +for p in TEST_NDARRAYS: |
| 23 | + TESTS.append([p, {"nonzero": True}, np.array([0.0, 3.0, 0.0, 4.0]), np.array([0.0, -1.0, 0.0, 1.0])]) |
| 24 | + for q in TEST_NDARRAYS: |
| 25 | + for u in TEST_NDARRAYS: |
| 26 | + TESTS.append( |
| 27 | + [ |
| 28 | + p, |
| 29 | + { |
| 30 | + "subtrahend": q(np.array([3.5, 3.5, 3.5, 3.5])), |
| 31 | + "divisor": u(np.array([0.5, 0.5, 0.5, 0.5])), |
| 32 | + "nonzero": True, |
| 33 | + }, |
| 34 | + np.array([0.0, 3.0, 0.0, 4.0]), |
| 35 | + np.array([0.0, -1.0, 0.0, 1.0]), |
| 36 | + ] |
| 37 | + ) |
| 38 | + TESTS.append([p, {"nonzero": True}, np.array([0.0, 0.0, 0.0, 0.0]), np.array([0.0, 0.0, 0.0, 0.0])]) |
| 39 | + TESTS.append([p, {"nonzero": False}, np.array([0.0, 0.0, 0.0, 0.0]), np.array([0.0, 0.0, 0.0, 0.0])]) |
| 40 | + TESTS.append([p, {"nonzero": False}, np.array([1, 1, 1, 1]), np.array([0.0, 0.0, 0.0, 0.0])]) |
| 41 | + TESTS.append( |
| 42 | + [ |
| 43 | + p, |
| 44 | + {"nonzero": False, "channel_wise": True, "subtrahend": [1, 2, 3]}, |
| 45 | + np.ones((3, 2, 2)), |
| 46 | + np.array([[[0.0, 0.0], [0.0, 0.0]], [[-1.0, -1.0], [-1.0, -1.0]], [[-2.0, -2.0], [-2.0, -2.0]]]), |
| 47 | + ] |
| 48 | + ) |
| 49 | + TESTS.append( |
| 50 | + [ |
| 51 | + p, |
| 52 | + {"nonzero": True, "channel_wise": True, "subtrahend": [1, 2, 3], "divisor": [0, 0, 2]}, |
| 53 | + np.ones((3, 2, 2)), |
| 54 | + np.array([[[0.0, 0.0], [0.0, 0.0]], [[-1.0, -1.0], [-1.0, -1.0]], [[-1.0, -1.0], [-1.0, -1.0]]]), |
| 55 | + ] |
| 56 | + ) |
| 57 | + TESTS.append( |
| 58 | + [ |
| 59 | + p, |
| 60 | + {"nonzero": True, "channel_wise": False, "subtrahend": 2, "divisor": 0}, |
| 61 | + np.ones((3, 2, 2)), |
| 62 | + np.ones((3, 2, 2)) * -1.0, |
| 63 | + ] |
| 64 | + ) |
| 65 | + TESTS.append( |
| 66 | + [ |
| 67 | + p, |
| 68 | + {"nonzero": True, "channel_wise": False, "subtrahend": np.ones((3, 2, 2)) * 0.5, "divisor": 0}, |
| 69 | + np.ones((3, 2, 2)), |
| 70 | + np.ones((3, 2, 2)) * 0.5, |
| 71 | + ] |
| 72 | + ) |
| 73 | + TESTS.append( |
| 74 | + [ |
| 75 | + p, |
| 76 | + {"nonzero": True, "channel_wise": True, "subtrahend": np.ones((3, 2, 2)) * 0.5, "divisor": [0, 1, 0]}, |
| 77 | + np.ones((3, 2, 2)), |
| 78 | + np.ones((3, 2, 2)) * 0.5, |
| 79 | + ] |
| 80 | + ) |
56 | 81 |
|
57 | 82 |
|
58 | 83 | class TestNormalizeIntensity(NumpyImageTestCase2D):
|
59 |
| - def test_default(self): |
| 84 | + @parameterized.expand([[p] for p in TEST_NDARRAYS]) |
| 85 | + def test_default(self, im_type): |
| 86 | + im = im_type(self.imt.copy()) |
60 | 87 | normalizer = NormalizeIntensity()
|
61 |
| - normalized = normalizer(self.imt.copy()) |
62 |
| - self.assertTrue(normalized.dtype == np.float32) |
| 88 | + normalized = normalizer(im) |
| 89 | + self.assertEqual(type(im), type(normalized)) |
| 90 | + if isinstance(normalized, torch.Tensor): |
| 91 | + self.assertEqual(im.device, normalized.device) |
| 92 | + self.assertTrue(normalized.dtype in (np.float32, torch.float32)) |
63 | 93 | expected = (self.imt - np.mean(self.imt)) / np.std(self.imt)
|
64 |
| - np.testing.assert_allclose(normalized, expected, rtol=1e-3) |
| 94 | + assert_allclose(expected, normalized, rtol=1e-3) |
65 | 95 |
|
66 |
| - @parameterized.expand(TEST_CASES) |
67 |
| - def test_nonzero(self, input_param, input_data, expected_data): |
| 96 | + @parameterized.expand(TESTS) |
| 97 | + def test_nonzero(self, in_type, input_param, input_data, expected_data): |
68 | 98 | normalizer = NormalizeIntensity(**input_param)
|
69 |
| - np.testing.assert_allclose(expected_data, normalizer(input_data)) |
| 99 | + im = in_type(input_data) |
| 100 | + normalized = normalizer(im) |
| 101 | + self.assertEqual(type(im), type(normalized)) |
| 102 | + if isinstance(normalized, torch.Tensor): |
| 103 | + self.assertEqual(im.device, normalized.device) |
| 104 | + assert_allclose(expected_data, normalized) |
70 | 105 |
|
71 |
| - def test_channel_wise(self): |
| 106 | + @parameterized.expand([[p] for p in TEST_NDARRAYS]) |
| 107 | + def test_channel_wise(self, im_type): |
72 | 108 | normalizer = NormalizeIntensity(nonzero=True, channel_wise=True)
|
73 |
| - input_data = np.array([[0.0, 3.0, 0.0, 4.0], [0.0, 4.0, 0.0, 5.0]]) |
| 109 | + input_data = im_type(np.array([[0.0, 3.0, 0.0, 4.0], [0.0, 4.0, 0.0, 5.0]])) |
74 | 110 | expected = np.array([[0.0, -1.0, 0.0, 1.0], [0.0, -1.0, 0.0, 1.0]])
|
75 |
| - np.testing.assert_allclose(expected, normalizer(input_data)) |
| 111 | + normalized = normalizer(input_data) |
| 112 | + self.assertEqual(type(input_data), type(normalized)) |
| 113 | + if isinstance(normalized, torch.Tensor): |
| 114 | + self.assertEqual(input_data.device, normalized.device) |
| 115 | + assert_allclose(expected, normalized) |
76 | 116 |
|
77 |
| - def test_value_errors(self): |
78 |
| - input_data = np.array([[0.0, 3.0, 0.0, 4.0], [0.0, 4.0, 0.0, 5.0]]) |
| 117 | + @parameterized.expand([[p] for p in TEST_NDARRAYS]) |
| 118 | + def test_value_errors(self, im_type): |
| 119 | + input_data = im_type(np.array([[0.0, 3.0, 0.0, 4.0], [0.0, 4.0, 0.0, 5.0]])) |
79 | 120 | normalizer = NormalizeIntensity(nonzero=True, channel_wise=True, subtrahend=[1])
|
80 | 121 | with self.assertRaises(ValueError):
|
81 | 122 | normalizer(input_data)
|
|
0 commit comments