-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathtest_normalize_intensityd.py
More file actions
94 lines (82 loc) · 3.53 KB
/
Copy pathtest_normalize_intensityd.py
File metadata and controls
94 lines (82 loc) · 3.53 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
# 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
import torch
from parameterized import parameterized
from monai.data import MetaTensor, set_track_meta
from monai.transforms import NormalizeIntensityd
from tests.test_utils import TEST_NDARRAYS, NumpyImageTestCase2D, assert_allclose
TESTS = []
for p in TEST_NDARRAYS:
for q in TEST_NDARRAYS:
TESTS.append(
[
{"keys": ["img"], "nonzero": True},
{"img": p(np.array([0.0, 3.0, 0.0, 4.0]))},
p(np.array([0.0, -1.0, 0.0, 1.0])),
]
)
TESTS.append(
[
{
"keys": ["img"],
"subtrahend": q(np.array([3.5, 3.5, 3.5, 3.5])),
"divisor": q(np.array([0.5, 0.5, 0.5, 0.5])),
"nonzero": True,
},
{"img": p(np.array([0.0, 3.0, 0.0, 4.0]))},
p(np.array([0.0, -1.0, 0.0, 1.0])),
]
)
TESTS.append(
[
{"keys": ["img"], "nonzero": True},
{"img": p(np.array([0.0, 0.0, 0.0, 0.0]))},
p(np.array([0.0, 0.0, 0.0, 0.0])),
]
)
class TestNormalizeIntensityd(NumpyImageTestCase2D):
@parameterized.expand([[p] for p in TEST_NDARRAYS])
def test_image_normalize_intensityd(self, im_type):
key = "img"
im = im_type(self.imt)
normalizer = NormalizeIntensityd(keys=[key])
normalized = normalizer({key: im})[key]
expected = (self.imt - np.mean(self.imt)) / np.std(self.imt)
assert_allclose(normalized, im_type(expected), rtol=1e-3, type_test="tensor")
@parameterized.expand(TESTS)
def test_nonzero(self, input_param, input_data, expected_data):
key = "img"
normalizer = NormalizeIntensityd(**input_param)
normalized = normalizer(input_data)[key]
assert_allclose(normalized, expected_data, type_test="tensor")
@parameterized.expand([[p] for p in TEST_NDARRAYS])
def test_channel_wise(self, im_type):
key = "img"
normalizer = NormalizeIntensityd(keys=key, nonzero=True, channel_wise=True)
input_data = {key: im_type(np.array([[0.0, 3.0, 0.0, 4.0], [0.0, 4.0, 0.0, 5.0]]))}
normalized = normalizer(input_data)[key]
expected = np.array([[0.0, -1.0, 0.0, 1.0], [0.0, -1.0, 0.0, 1.0]])
assert_allclose(normalized, im_type(expected), type_test="tensor")
@parameterized.expand([["global", {}], ["channelwise", {"channel_wise": True}]])
def test_inverse(self, _, args):
set_track_meta(True)
key = "img"
normalizer = NormalizeIntensityd(keys=key, **args)
data = {key: MetaTensor(torch.randn(3, 6, 6) * 4 + 1)}
original = data[key].clone()
out = normalizer(dict(data))
inv = normalizer.inverse(out)
assert_allclose(inv[key], original, type_test=False, rtol=1e-4, atol=1e-4)
if __name__ == "__main__":
unittest.main()