|
1
|
|
|
from torchio.transforms import RandomLabelsToImage |
|
2
|
|
|
from ...utils import TorchioTestCase |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
class TestRandomLabelsToImage(TorchioTestCase): |
|
6
|
|
|
"""Tests for `RandomLabelsToImage`.""" |
|
7
|
|
|
def test_random_simulation(self): |
|
8
|
|
|
"""The transform runs without error and an 'image_from_labels' key is |
|
9
|
|
|
present in the transformed subject.""" |
|
10
|
|
|
transform = RandomLabelsToImage(label_key='label') |
|
11
|
|
|
transformed = transform(self.sample_subject) |
|
12
|
|
|
self.assertIn('image_from_labels', transformed) |
|
13
|
|
|
|
|
14
|
|
|
def test_deterministic_simulation(self): |
|
15
|
|
|
"""The transform creates an image where values are equal to given |
|
16
|
|
|
mean if standard deviation is zero. |
|
17
|
|
|
Using a label map.""" |
|
18
|
|
|
transform = RandomLabelsToImage( |
|
19
|
|
|
label_key='label', |
|
20
|
|
|
mean=[0.5, 2], |
|
21
|
|
|
std=[0, 0] |
|
22
|
|
|
) |
|
23
|
|
|
transformed = transform(self.sample_subject) |
|
24
|
|
|
self.assertTensorEqual( |
|
25
|
|
|
transformed['image_from_labels'].data == 0.5, |
|
26
|
|
|
self.sample_subject['label'].data == 0 |
|
27
|
|
|
) |
|
28
|
|
|
self.assertTensorEqual( |
|
29
|
|
|
transformed['image_from_labels'].data == 2, |
|
30
|
|
|
self.sample_subject['label'].data == 1 |
|
31
|
|
|
) |
|
32
|
|
|
|
|
33
|
|
|
def test_deterministic_simulation_with_discretized_label_map(self): |
|
34
|
|
|
"""The transform creates an image where values are equal to given mean |
|
35
|
|
|
if standard deviation is zero. |
|
36
|
|
|
Using a discretized label map.""" |
|
37
|
|
|
transform = RandomLabelsToImage( |
|
38
|
|
|
label_key='label', |
|
39
|
|
|
mean=[0.5, 2], |
|
40
|
|
|
std=[0, 0], |
|
41
|
|
|
discretize=True |
|
42
|
|
|
) |
|
43
|
|
|
transformed = transform(self.sample_subject) |
|
44
|
|
|
self.assertTensorEqual( |
|
45
|
|
|
transformed['image_from_labels'].data == 0.5, |
|
46
|
|
|
self.sample_subject['label'].data == 0 |
|
47
|
|
|
) |
|
48
|
|
|
self.assertTensorEqual( |
|
49
|
|
|
transformed['image_from_labels'].data == 2, |
|
50
|
|
|
self.sample_subject['label'].data == 1 |
|
51
|
|
|
) |
|
52
|
|
|
|
|
53
|
|
|
def test_deterministic_simulation_with_pv_map(self): |
|
54
|
|
|
"""The transform creates an image where values are equal to given |
|
55
|
|
|
mean weighted by partial-volume if standard deviation is zero.""" |
|
56
|
|
|
subject = self.get_subject_with_partial_volume_label_map(components=2) |
|
57
|
|
|
transform = RandomLabelsToImage( |
|
58
|
|
|
label_key='label', |
|
59
|
|
|
mean=[0.5, 1], |
|
60
|
|
|
std=[0, 0] |
|
61
|
|
|
) |
|
62
|
|
|
transformed = transform(subject) |
|
63
|
|
|
self.assertTensorAlmostEqual( |
|
64
|
|
|
transformed['image_from_labels'].data[0], |
|
65
|
|
|
subject['label'].data[0] * 0.5 + subject['label'].data[1] * 1 |
|
66
|
|
|
) |
|
67
|
|
|
self.assertEqual( |
|
68
|
|
|
transformed['image_from_labels'].data.shape, |
|
69
|
|
|
(1, 10, 20, 30) |
|
70
|
|
|
) |
|
71
|
|
|
|
|
72
|
|
|
def test_deterministic_simulation_with_discretized_pv_map(self): |
|
73
|
|
|
"""The transform creates an image where values are equal to given mean |
|
74
|
|
|
if standard deviation is zero. |
|
75
|
|
|
Using a discretized partial-volume label map.""" |
|
76
|
|
|
subject = self.get_subject_with_partial_volume_label_map() |
|
77
|
|
|
transform = RandomLabelsToImage( |
|
78
|
|
|
label_key='label', |
|
79
|
|
|
mean=[0.5], |
|
80
|
|
|
std=[0], |
|
81
|
|
|
discretize=True |
|
82
|
|
|
) |
|
83
|
|
|
transformed = transform(subject) |
|
84
|
|
|
self.assertTensorAlmostEqual( |
|
85
|
|
|
transformed['image_from_labels'].data, |
|
86
|
|
|
(subject['label'].data > 0) * 0.5 |
|
87
|
|
|
) |
|
88
|
|
|
|
|
89
|
|
|
def test_filling(self): |
|
90
|
|
|
"""The transform can fill in the generated image with an already |
|
91
|
|
|
existing image. |
|
92
|
|
|
Using a label map.""" |
|
93
|
|
|
transform = RandomLabelsToImage( |
|
94
|
|
|
label_key='label', |
|
95
|
|
|
image_key='t1', |
|
96
|
|
|
used_labels=[1] |
|
97
|
|
|
) |
|
98
|
|
|
t1_indices = self.sample_subject['label'].data == 0 |
|
99
|
|
|
transformed = transform(self.sample_subject) |
|
100
|
|
|
self.assertTensorAlmostEqual( |
|
101
|
|
|
transformed['t1'].data[t1_indices], |
|
102
|
|
|
self.sample_subject['t1'].data[t1_indices] |
|
103
|
|
|
) |
|
104
|
|
|
|
|
105
|
|
|
def test_filling_with_discretized_label_map(self): |
|
106
|
|
|
"""The transform can fill in the generated image with an already |
|
107
|
|
|
existing image. |
|
108
|
|
|
Using a discretized label map.""" |
|
109
|
|
|
transform = RandomLabelsToImage( |
|
110
|
|
|
label_key='label', |
|
111
|
|
|
image_key='t1', |
|
112
|
|
|
discretize=True, |
|
113
|
|
|
used_labels=[1] |
|
114
|
|
|
) |
|
115
|
|
|
t1_indices = self.sample_subject['label'].data < 0.5 |
|
116
|
|
|
transformed = transform(self.sample_subject) |
|
117
|
|
|
self.assertTensorAlmostEqual( |
|
118
|
|
|
transformed['t1'].data[t1_indices], |
|
119
|
|
|
self.sample_subject['t1'].data[t1_indices] |
|
120
|
|
|
) |
|
121
|
|
|
|
|
122
|
|
|
def test_filling_with_discretized_pv_label_map(self): |
|
123
|
|
|
"""The transform can fill in the generated image with an already |
|
124
|
|
|
existing image. |
|
125
|
|
|
Using a discretized partial-volume label map.""" |
|
126
|
|
|
subject = self.get_subject_with_partial_volume_label_map(components=2) |
|
127
|
|
|
transform = RandomLabelsToImage( |
|
128
|
|
|
label_key='label', |
|
129
|
|
|
image_key='t1', |
|
130
|
|
|
discretize=True, |
|
131
|
|
|
used_labels=[1] |
|
132
|
|
|
) |
|
133
|
|
|
t1_indices = subject['label'].data.argmax(dim=0) == 0 |
|
134
|
|
|
transformed = transform(subject) |
|
135
|
|
|
self.assertTensorAlmostEqual( |
|
136
|
|
|
transformed['t1'].data[0][t1_indices], |
|
137
|
|
|
subject['t1'].data[0][t1_indices] |
|
138
|
|
|
) |
|
139
|
|
|
|
|
140
|
|
|
def test_filling_without_any_hole(self): |
|
141
|
|
|
"""The transform does not fill anything if there is no hole.""" |
|
142
|
|
|
transform = RandomLabelsToImage( |
|
143
|
|
|
label_key='label', |
|
144
|
|
|
image_key='t1', |
|
145
|
|
|
default_std=0, |
|
146
|
|
|
default_mean=-1, |
|
147
|
|
|
) |
|
148
|
|
|
original_t1 = self.sample_subject.t1.data.clone() |
|
149
|
|
|
transformed = transform(self.sample_subject) |
|
150
|
|
|
self.assertTensorNotEqual(original_t1, transformed.t1.data) |
|
151
|
|
|
|
|
152
|
|
|
def test_with_bad_default_mean_range(self): |
|
153
|
|
|
"""The transform raises an error if default_mean is not a |
|
154
|
|
|
single value nor a tuple of two values.""" |
|
155
|
|
|
with self.assertRaises(ValueError): |
|
156
|
|
|
RandomLabelsToImage(label_key='label', default_mean=(0, 1, 2)) |
|
157
|
|
|
|
|
158
|
|
|
def test_with_bad_default_mean_type(self): |
|
159
|
|
|
"""The transform raises an error if default_mean has the wrong type.""" |
|
160
|
|
|
with self.assertRaises(ValueError): |
|
161
|
|
|
RandomLabelsToImage(label_key='label', default_mean='wrong') |
|
162
|
|
|
|
|
163
|
|
|
def test_with_bad_default_std_range(self): |
|
164
|
|
|
"""The transform raises an error if default_std is not a |
|
165
|
|
|
single value nor a tuple of two values.""" |
|
166
|
|
|
with self.assertRaises(ValueError): |
|
167
|
|
|
RandomLabelsToImage(label_key='label', default_std=(0, 1, 2)) |
|
168
|
|
|
|
|
169
|
|
|
def test_with_bad_default_std_type(self): |
|
170
|
|
|
"""The transform raises an error if default_std has the wrong type.""" |
|
171
|
|
|
with self.assertRaises(ValueError): |
|
172
|
|
|
RandomLabelsToImage(label_key='label', default_std='wrong') |
|
173
|
|
|
|
|
174
|
|
|
def test_with_wrong_label_key_type(self): |
|
175
|
|
|
"""The transform raises an error if a wrong type is given for |
|
176
|
|
|
label_key.""" |
|
177
|
|
|
with self.assertRaises(TypeError): |
|
178
|
|
|
RandomLabelsToImage(label_key=42) |
|
179
|
|
|
|
|
180
|
|
|
def test_with_wrong_used_labels_type(self): |
|
181
|
|
|
"""The transform raises an error if a wrong type is given for |
|
182
|
|
|
used_labels.""" |
|
183
|
|
|
with self.assertRaises(TypeError): |
|
184
|
|
|
RandomLabelsToImage(label_key='label', used_labels=42) |
|
185
|
|
|
|
|
186
|
|
|
def test_with_wrong_used_labels_elements_type(self): |
|
187
|
|
|
"""The transform raises an error if wrong type are given for |
|
188
|
|
|
used_labels elements.""" |
|
189
|
|
|
with self.assertRaises(ValueError): |
|
190
|
|
|
RandomLabelsToImage(label_key='label', used_labels=['wrong']) |
|
191
|
|
|
|
|
192
|
|
|
def test_with_wrong_mean_type(self): |
|
193
|
|
|
"""The transform raises an error if wrong type is given for mean.""" |
|
194
|
|
|
with self.assertRaises(TypeError): |
|
195
|
|
|
RandomLabelsToImage(label_key='label', mean=42) |
|
196
|
|
|
|
|
197
|
|
|
def test_with_wrong_mean_elements_type(self): |
|
198
|
|
|
"""The transform raises an error if wrong type are given for |
|
199
|
|
|
mean elements.""" |
|
200
|
|
|
with self.assertRaises(ValueError): |
|
201
|
|
|
RandomLabelsToImage(label_key='label', mean=['wrong']) |
|
202
|
|
|
|
|
203
|
|
|
def test_with_wrong_std_type(self): |
|
204
|
|
|
"""The transform raises an error if wrong type is given for std.""" |
|
205
|
|
|
with self.assertRaises(TypeError): |
|
206
|
|
|
RandomLabelsToImage(label_key='label', std=42) |
|
207
|
|
|
|
|
208
|
|
|
def test_with_wrong_std_elements_type(self): |
|
209
|
|
|
"""The transform raises an error if wrong type are given for |
|
210
|
|
|
std elements.""" |
|
211
|
|
|
with self.assertRaises(ValueError): |
|
212
|
|
|
RandomLabelsToImage(label_key='label', std=['wrong']) |
|
213
|
|
|
|
|
214
|
|
|
def test_mean_and_std_len_not_matching(self): |
|
215
|
|
|
"""The transform raises an error if mean and std length don't match.""" |
|
216
|
|
|
with self.assertRaises(AssertionError): |
|
217
|
|
|
RandomLabelsToImage(label_key='label', mean=[0], std=[0, 1]) |
|
218
|
|
|
|
|
219
|
|
|
def test_mean_and_used_labels_len_not_matching(self): |
|
220
|
|
|
"""The transform raises an error if mean and used_labels |
|
221
|
|
|
length don't match.""" |
|
222
|
|
|
with self.assertRaises(AssertionError): |
|
223
|
|
|
RandomLabelsToImage( |
|
224
|
|
|
label_key='label', |
|
225
|
|
|
mean=[0], |
|
226
|
|
|
used_labels=[0, 1], |
|
227
|
|
|
) |
|
228
|
|
|
|
|
229
|
|
|
def test_std_and_used_labels_len_not_matching(self): |
|
230
|
|
|
"""The transform raises an error if std and used_labels |
|
231
|
|
|
length don't match.""" |
|
232
|
|
|
with self.assertRaises(AssertionError): |
|
233
|
|
|
RandomLabelsToImage(label_key='label', std=[0], used_labels=[0, 1]) |
|
234
|
|
|
|
|
235
|
|
|
def test_mean_not_matching_number_of_labels(self): |
|
236
|
|
|
"""The transform raises an error at runtime if mean length |
|
237
|
|
|
does not match label numbers.""" |
|
238
|
|
|
transform = RandomLabelsToImage(label_key='label', mean=[0]) |
|
239
|
|
|
with self.assertRaises(RuntimeError): |
|
240
|
|
|
transform(self.sample_subject) |
|
241
|
|
|
|
|
242
|
|
|
def test_std_not_matching_number_of_labels(self): |
|
243
|
|
|
"""The transform raises an error at runtime if std length |
|
244
|
|
|
does not match label numbers.""" |
|
245
|
|
|
transform = RandomLabelsToImage(label_key='label', std=[1, 2, 3]) |
|
246
|
|
|
with self.assertRaises(RuntimeError): |
|
247
|
|
|
transform(self.sample_subject) |
|
248
|
|
|
|
|
249
|
|
|
def test_bad_range(self): |
|
250
|
|
|
with self.assertRaises(ValueError): |
|
251
|
|
|
RandomLabelsToImage(default_mean=(2, 1)) |
|
252
|
|
|
|
|
253
|
|
|
def test_no_labels(self): |
|
254
|
|
|
transform = RandomLabelsToImage() |
|
255
|
|
|
with self.assertRaises(RuntimeError): |
|
256
|
|
|
transform(self.sample_subject.t1) |
|
257
|
|
|
|