|
17 | 17 | from monai.transforms import RandSpatialCropSamplesd
|
18 | 18 |
|
19 | 19 | TEST_CASE_1 = [
|
20 |
| - {"keys": ["img", "seg"], "num_samples": 4, "roi_size": [3, 3, 3], "random_center": True}, |
21 |
| - {"img": np.random.randint(0, 2, size=[3, 3, 3, 3]), "seg": np.random.randint(0, 2, size=[3, 3, 3, 3])}, |
22 |
| - (3, 3, 3, 3), |
| 20 | + {"keys": ["img", "seg"], "num_samples": 4, "roi_size": [2, 2, 2], "random_center": True}, |
| 21 | + {"img": np.arange(81).reshape(3, 3, 3, 3), "seg": np.arange(81, 0, -1).reshape(3, 3, 3, 3)}, |
| 22 | + [(3, 3, 3, 2), (3, 2, 2, 2), (3, 3, 3, 2), (3, 3, 2, 2)], |
| 23 | + { |
| 24 | + "img": np.array( |
| 25 | + [ |
| 26 | + [[[0, 1], [3, 4]], [[9, 10], [12, 13]], [[18, 19], [21, 22]]], |
| 27 | + [[[27, 28], [30, 31]], [[36, 37], [39, 40]], [[45, 46], [48, 49]]], |
| 28 | + [[[54, 55], [57, 58]], [[63, 64], [66, 67]], [[72, 73], [75, 76]]], |
| 29 | + ] |
| 30 | + ), |
| 31 | + "seg": np.array( |
| 32 | + [ |
| 33 | + [[[81, 80], [78, 77]], [[72, 71], [69, 68]], [[63, 62], [60, 59]]], |
| 34 | + [[[54, 53], [51, 50]], [[45, 44], [42, 41]], [[36, 35], [33, 32]]], |
| 35 | + [[[27, 26], [24, 23]], [[18, 17], [15, 14]], [[9, 8], [6, 5]]], |
| 36 | + ] |
| 37 | + ), |
| 38 | + }, |
23 | 39 | ]
|
24 | 40 |
|
25 | 41 | TEST_CASE_2 = [
|
26 |
| - {"keys": ["img", "seg"], "num_samples": 8, "roi_size": [3, 3, 3], "random_center": False}, |
27 |
| - {"img": np.random.randint(0, 2, size=[3, 3, 3, 3]), "seg": np.random.randint(0, 2, size=[3, 3, 3, 3])}, |
28 |
| - (3, 3, 3, 3), |
| 42 | + {"keys": ["img", "seg"], "num_samples": 8, "roi_size": [2, 2, 3], "random_center": False}, |
| 43 | + {"img": np.arange(81).reshape(3, 3, 3, 3), "seg": np.arange(81, 0, -1).reshape(3, 3, 3, 3)}, |
| 44 | + [(3, 3, 3, 3), (3, 2, 3, 3), (3, 2, 2, 3), (3, 2, 3, 3), (3, 3, 3, 3), (3, 3, 3, 3), (3, 2, 2, 3), (3, 3, 2, 3)], |
| 45 | + { |
| 46 | + "img": np.array( |
| 47 | + [ |
| 48 | + [[[0, 1, 2], [3, 4, 5]], [[9, 10, 11], [12, 13, 14]], [[18, 19, 20], [21, 22, 23]]], |
| 49 | + [[[27, 28, 29], [30, 31, 32]], [[36, 37, 38], [39, 40, 41]], [[45, 46, 47], [48, 49, 50]]], |
| 50 | + [[[54, 55, 56], [57, 58, 59]], [[63, 64, 65], [66, 67, 68]], [[72, 73, 74], [75, 76, 77]]], |
| 51 | + ] |
| 52 | + ), |
| 53 | + "seg": np.array( |
| 54 | + [ |
| 55 | + [[[81, 80, 79], [78, 77, 76]], [[72, 71, 70], [69, 68, 67]], [[63, 62, 61], [60, 59, 58]]], |
| 56 | + [[[54, 53, 52], [51, 50, 49]], [[45, 44, 43], [42, 41, 40]], [[36, 35, 34], [33, 32, 31]]], |
| 57 | + [[[27, 26, 25], [24, 23, 22]], [[18, 17, 16], [15, 14, 13]], [[9, 8, 7], [6, 5, 4]]], |
| 58 | + ] |
| 59 | + ), |
| 60 | + }, |
29 | 61 | ]
|
30 | 62 |
|
31 | 63 |
|
32 | 64 | class TestRandSpatialCropSamplesd(unittest.TestCase):
|
33 | 65 | @parameterized.expand([TEST_CASE_1, TEST_CASE_2])
|
34 |
| - def test_shape(self, input_param, input_data, expected_shape): |
35 |
| - result = RandSpatialCropSamplesd(**input_param)(input_data) |
36 |
| - for item in result: |
37 |
| - self.assertTupleEqual(item["img"].shape, expected_shape) |
38 |
| - self.assertTupleEqual(item["seg"].shape, expected_shape) |
| 66 | + def test_shape(self, input_param, input_data, expected_shape, expected_last): |
| 67 | + xform = RandSpatialCropSamplesd(**input_param) |
| 68 | + xform.set_random_state(1234) |
| 69 | + result = xform(input_data) |
| 70 | + for item, expected in zip(result, expected_shape): |
| 71 | + self.assertTupleEqual(item["img"].shape, expected) |
| 72 | + self.assertTupleEqual(item["seg"].shape, expected) |
| 73 | + np.testing.assert_allclose(item["img"], expected_last["img"]) |
| 74 | + np.testing.assert_allclose(item["seg"], expected_last["seg"]) |
39 | 75 |
|
40 | 76 |
|
41 | 77 | if __name__ == "__main__":
|
|
0 commit comments