-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_rand_grid_distortiond.py
More file actions
105 lines (93 loc) · 4.26 KB
/
test_rand_grid_distortiond.py
File metadata and controls
105 lines (93 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import unittest
import numpy as np
from parameterized import parameterized
from monai.transforms import RandGridDistortiond
from tests.test_utils import TEST_NDARRAYS_ALL, assert_allclose
TESTS = []
num_cells = 2
seed = 0
for p in TEST_NDARRAYS_ALL:
img = np.indices([6, 6]).astype(np.float32)
TESTS.append(
[
dict(
keys=["img", "mask"],
num_cells=num_cells,
prob=1.0,
distort_limit=(-0.1, 0.1),
mode=["bilinear", "nearest"],
padding_mode="zeros",
),
seed,
{"img": p(img), "mask": p(np.ones_like(img[:1]))},
p(
np.array(
[
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[1.5645568, 1.5645568, 1.5645568, 1.5645568, 1.5645568, 0.0],
[3.1291137, 3.1291137, 3.1291137, 3.1291137, 3.1291137, 0.0],
[3.1291137, 3.1291137, 3.1291137, 3.1291137, 3.1291137, 0.0],
[4.6599426, 4.6599426, 4.6599426, 4.6599426, 4.6599426, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
[
[0.0, 1.4770963, 2.9541926, 2.9541926, 4.497961, 0.0],
[0.0, 1.4770963, 2.9541926, 2.9541926, 4.497961, 0.0],
[0.0, 1.4770963, 2.9541926, 2.9541926, 4.497961, 0.0],
[0.0, 1.4770963, 2.9541926, 2.9541926, 4.497961, 0.0],
[0.0, 1.4770963, 2.9541926, 2.9541926, 4.497961, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
]
).astype(np.float32)
),
p(
np.array(
[
[
[1.0, 1.0, 1.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 0.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
]
]
)
),
]
)
class TestRandGridDistortiond(unittest.TestCase):
"""Test cases for RandGridDistortiond dictionary transform."""
@parameterized.expand(TESTS)
def test_rand_grid_distortiond(self, input_param, seed, input_data, expected_val_img, expected_val_mask):
"""Verify distortion produces expected output for image and mask keys."""
g = RandGridDistortiond(**input_param)
g.set_random_state(seed=seed)
result = g(input_data)
assert_allclose(result["img"], expected_val_img, type_test=False, rtol=1e-4, atol=1e-4)
assert_allclose(result["mask"], expected_val_mask, type_test=False, rtol=1e-4, atol=1e-4)
def test_no_transform_with_non_tensor_metadata(self):
"""When _do_transform is False, non-tensor values in the dict should not cause an error."""
img = np.indices([6, 6]).astype(np.float32)
data = {"img": img, "extra_info": 42, "label_name": "tumor"}
g = RandGridDistortiond(keys=["img"], prob=0.0) # prob=0 ensures _do_transform is False
result = g(data)
# non-tensor metadata should pass through unchanged
self.assertEqual(result["extra_info"], 42)
self.assertEqual(result["label_name"], "tumor")
assert_allclose(result["img"], img, type_test=False)
if __name__ == "__main__":
unittest.main()