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