Passed
Pull Request — master (#222)
by Fernando
02:36
created

tests.transforms.augmentation.test_random_labels_to_image   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 105
dl 0
loc 179
rs 10
c 0
b 0
f 0
wmc 29

18 Methods

Rating   Name   Duplication   Size   Complexity  
A TestRandomLabelsToImage.test_with_bad_default_mean_type() 0 4 2
A TestRandomLabelsToImage.test_with_wrong_label_key_type() 0 5 2
A TestRandomLabelsToImage.test_with_wrong_pv_label_keys_elements_type() 0 5 2
A TestRandomLabelsToImage.test_with_wrong_pv_label_keys_type() 0 5 2
A TestRandomLabelsToImage.test_missing_label_key_and_pv_label_keys() 0 5 2
A TestRandomLabelsToImage.test_with_inconsistent_pv_label_maps_affines() 0 11 2
A TestRandomLabelsToImage.test_with_inconsistent_pv_label_maps_shapes() 0 9 2
A TestRandomLabelsToImage.test_filling() 0 14 1
A TestRandomLabelsToImage.test_filling_with_binary_pv_label_map() 0 14 1
A TestRandomLabelsToImage.test_with_bad_default_mean_range() 0 5 2
A TestRandomLabelsToImage.test_with_bad_default_std_type() 0 4 2
A TestRandomLabelsToImage.test_random_simulation() 0 6 1
A TestRandomLabelsToImage.test_deterministic_simulation_with_pv_label_map() 0 12 1
A TestRandomLabelsToImage.test_with_both_label_key_and_pv_label_keys() 0 5 2
A TestRandomLabelsToImage.test_filling_with_pv_label_map() 0 13 1
A TestRandomLabelsToImage.test_deterministic_simulation() 0 12 1
A TestRandomLabelsToImage.test_deterministic_simulation_with_binary_pv_label_map() 0 13 1
A TestRandomLabelsToImage.test_with_bad_default_std_range() 0 5 2
1
from torchio.transforms import RandomLabelsToImage
2
from torchio import DATA, AFFINE
3
from ...utils import TorchioTestCase
4
from numpy.testing import assert_array_equal
5
6
7
class TestRandomLabelsToImage(TorchioTestCase):
8
    """Tests for `RandomLabelsToImage`."""
9
    def test_random_simulation(self):
10
        """The transform runs without error and an 'image' key is
11
        present in the transformed sample."""
12
        transform = RandomLabelsToImage(label_key='label')
13
        transformed = transform(self.sample)
14
        self.assertIn('image', transformed)
15
16
    def test_deterministic_simulation(self):
17
        """The transform creates an image where values are equal to given
18
        mean if standard deviation is zero.
19
        Using a label map."""
20
        transform = RandomLabelsToImage(
21
            label_key='label',
22
            gaussian_parameters={1: {'mean': 0.5, 'std': 0}}
23
        )
24
        transformed = transform(self.sample)
25
        assert_array_equal(
26
            transformed['image'][DATA] == 0.5,
27
            self.sample['label'][DATA] == 1
28
        )
29
30
    def test_deterministic_simulation_with_pv_label_map(self):
31
        """The transform creates an image where values are equal to given mean
32
        if standard deviation is zero.
33
        Using a PV label map."""
34
        transform = RandomLabelsToImage(
35
            pv_label_keys=['label'],
36
            gaussian_parameters={'label': {'mean': 0.5, 'std': 0}}
37
        )
38
        transformed = transform(self.sample)
39
        assert_array_equal(
40
            transformed['image'][DATA] == 0.5,
41
            self.sample['label'][DATA] == 1
42
        )
43
44
    def test_deterministic_simulation_with_binary_pv_label_map(self):
45
        """The transform creates an image where values are equal to given mean
46
        if standard deviation is zero.
47
        Using a binarized PV label map."""
48
        transform = RandomLabelsToImage(
49
            pv_label_keys=['label'],
50
            gaussian_parameters={'label': {'mean': 0.5, 'std': 0}},
51
            binarize=True
52
        )
53
        transformed = transform(self.sample)
54
        assert_array_equal(
55
            transformed['image'][DATA] == 0.5,
56
            self.sample['label'][DATA] == 1
57
        )
58
59
    def test_filling(self):
60
        """The transform can fill in the generated image with an already
61
        existing image.
62
        Using a label map."""
63
        transform = RandomLabelsToImage(
64
            label_key='label',
65
            image_key='t1',
66
            gaussian_parameters={0: {'mean': 0.0, 'std': 0}}
67
        )
68
        t1_indices = self.sample['label'][DATA] == 0
69
        transformed = transform(self.sample)
70
        assert_array_equal(
71
            transformed['t1'][DATA][t1_indices],
72
            self.sample['t1'][DATA][t1_indices]
73
        )
74
75
    def test_filling_with_pv_label_map(self):
76
        """The transform can fill in the generated image with an already
77
        existing image.
78
        Using a PV label map."""
79
        transform = RandomLabelsToImage(
80
            pv_label_keys=['label'],
81
            image_key='t1'
82
        )
83
        t1_indices = self.sample['label'][DATA] == 0
84
        transformed = transform(self.sample)
85
        assert_array_equal(
86
            transformed['t1'][DATA][t1_indices],
87
            self.sample['t1'][DATA][t1_indices]
88
        )
89
90
    def test_filling_with_binary_pv_label_map(self):
91
        """The transform can fill in the generated image with an already
92
        existing image.
93
        Using a binarized PV label map."""
94
        transform = RandomLabelsToImage(
95
            pv_label_keys=['label'],
96
            image_key='t1',
97
            binarize=True
98
        )
99
        t1_indices = self.sample['label'][DATA] == 0
100
        transformed = transform(self.sample)
101
        assert_array_equal(
102
            transformed['t1'][DATA][t1_indices],
103
            self.sample['t1'][DATA][t1_indices]
104
        )
105
106
    def test_missing_label_key_and_pv_label_keys(self):
107
        """The transform raises an error if both label_key and pv_label_keys
108
         are None."""
109
        with self.assertRaises(ValueError):
110
            RandomLabelsToImage()
111
112
    def test_with_both_label_key_and_pv_label_keys(self):
113
        """The transform raises an error if both label_key and pv_label_keys
114
        are set."""
115
        with self.assertRaises(ValueError):
116
            RandomLabelsToImage(label_key='label', pv_label_keys=['label'])
117
118
    def test_with_bad_default_mean_range(self):
119
        """The transform raises an error if default_mean is not a
120
        single value nor a tuple of two values."""
121
        with self.assertRaises(ValueError):
122
            RandomLabelsToImage(label_key='label', default_mean=(0, 1, 2))
123
124
    def test_with_bad_default_mean_type(self):
125
        """The transform raises an error if default_mean has the wrong type."""
126
        with self.assertRaises(ValueError):
127
            RandomLabelsToImage(label_key='label', default_mean='wrong')
128
129
    def test_with_bad_default_std_range(self):
130
        """The transform raises an error if default_std is not a
131
        single value nor a tuple of two values."""
132
        with self.assertRaises(ValueError):
133
            RandomLabelsToImage(label_key='label', default_std=(0, 1, 2))
134
135
    def test_with_bad_default_std_type(self):
136
        """The transform raises an error if default_std has the wrong type."""
137
        with self.assertRaises(ValueError):
138
            RandomLabelsToImage(label_key='label', default_std='wrong')
139
140
    def test_with_wrong_label_key_type(self):
141
        """The transform raises an error if a wrong type is given for
142
        label_key."""
143
        with self.assertRaises(TypeError):
144
            RandomLabelsToImage(label_key=42)
145
146
    def test_with_wrong_pv_label_keys_type(self):
147
        """The transform raises an error if a wrong type is given for
148
        pv_label_keys."""
149
        with self.assertRaises(TypeError):
150
            RandomLabelsToImage(pv_label_keys=42)
151
152
    def test_with_wrong_pv_label_keys_elements_type(self):
153
        """The transform raises an error if wrong type are given for
154
        pv_label_keys elements."""
155
        with self.assertRaises(TypeError):
156
            RandomLabelsToImage(pv_label_keys=[42, 27])
157
158
    def test_with_inconsistent_pv_label_maps_shapes(self):
159
        """The transform raises an error if PV label maps have
160
        inconsistent shapes."""
161
        transform = RandomLabelsToImage(
162
            pv_label_keys=['label', 'label2'],
163
        )
164
        sample = self.get_inconsistent_sample()
165
        with self.assertRaises(RuntimeError):
166
            transform(sample)
167
168
    def test_with_inconsistent_pv_label_maps_affines(self):
169
        """The transform raises a warning if PV label maps have
170
        inconsistent affines."""
171
        transform = RandomLabelsToImage(
172
            pv_label_keys=['label', 'label2'],
173
        )
174
        sample = self.get_inconsistent_sample()
175
        sample['label2'][DATA] = sample['label'][DATA].clone()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DATA does not seem to be defined.
Loading history...
176
        sample['label2'][AFFINE][0, 0] = -1
177
        with self.assertRaises(RuntimeWarning):
178
            transform(sample)
179