1
|
|
|
import torch |
2
|
|
|
import numpy as np |
3
|
|
|
import torchio as tio |
4
|
|
|
from ...utils import TorchioTestCase |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class TestResample(TorchioTestCase): |
8
|
|
|
"""Tests for `Resample`.""" |
9
|
|
|
def test_spacing(self): |
10
|
|
|
# Should this raise an error if sizes are different? |
11
|
|
|
spacing = 2 |
12
|
|
|
transform = tio.Resample(spacing) |
13
|
|
|
transformed = transform(self.sample_subject) |
14
|
|
|
for image in transformed.get_images(intensity_only=False): |
15
|
|
|
self.assertEqual(image.spacing, 3 * (spacing,)) |
16
|
|
|
|
17
|
|
|
def test_reference_name(self): |
18
|
|
|
subject = self.get_inconsistent_shape_subject() |
19
|
|
|
reference_name = 't1' |
20
|
|
|
transform = tio.Resample(reference_name) |
21
|
|
|
transformed = transform(subject) |
22
|
|
|
reference_image = subject[reference_name] |
23
|
|
|
for image in transformed.get_images(intensity_only=False): |
24
|
|
|
self.assertEqual(reference_image.shape, image.shape) |
25
|
|
|
self.assertTensorAlmostEqual(reference_image.affine, image.affine) |
26
|
|
|
|
27
|
|
|
def test_affine(self): |
28
|
|
|
spacing = 1 |
29
|
|
|
affine_name = 'pre_affine' |
30
|
|
|
transform = tio.Resample(spacing, pre_affine_name=affine_name) |
31
|
|
|
transformed = transform(self.sample_subject) |
32
|
|
|
for image in transformed.values(): |
33
|
|
|
if affine_name in image: |
34
|
|
|
target_affine = np.eye(4) |
35
|
|
|
target_affine[:3, 3] = 10, 0, -0.1 |
36
|
|
|
self.assertTensorAlmostEqual(image.affine, target_affine) |
37
|
|
|
else: |
38
|
|
|
self.assertTensorEqual(image.affine, np.eye(4)) |
39
|
|
|
|
40
|
|
|
def test_missing_affine(self): |
41
|
|
|
transform = tio.Resample(1, pre_affine_name='missing') |
42
|
|
|
with self.assertRaises(ValueError): |
43
|
|
|
transform(self.sample_subject) |
44
|
|
|
|
45
|
|
|
def test_reference_path(self): |
46
|
|
|
reference_image, reference_path = self.get_reference_image_and_path() |
47
|
|
|
transform = tio.Resample(reference_path) |
48
|
|
|
transformed = transform(self.sample_subject) |
49
|
|
|
for image in transformed.values(): |
50
|
|
|
self.assertEqual(reference_image.shape, image.shape) |
51
|
|
|
self.assertTensorAlmostEqual(reference_image.affine, image.affine) |
52
|
|
|
|
53
|
|
|
def test_wrong_spacing_length(self): |
54
|
|
|
with self.assertRaises(RuntimeError): |
55
|
|
|
tio.Resample((1, 2))(self.sample_subject) |
56
|
|
|
|
57
|
|
|
def test_wrong_spacing_value(self): |
58
|
|
|
with self.assertRaises(ValueError): |
59
|
|
|
tio.Resample(0)(self.sample_subject) |
60
|
|
|
|
61
|
|
|
def test_wrong_target_type(self): |
62
|
|
|
with self.assertRaises(RuntimeError): |
63
|
|
|
tio.Resample(None)(self.sample_subject) |
64
|
|
|
|
65
|
|
|
def test_missing_reference(self): |
66
|
|
|
transform = tio.Resample('missing') |
67
|
|
|
with self.assertRaises(ValueError): |
68
|
|
|
transform(self.sample_subject) |
69
|
|
|
|
70
|
|
|
def test_2d(self): |
71
|
|
|
image = tio.ScalarImage(tensor=torch.rand(1, 2, 3, 1)) |
72
|
|
|
transform = tio.Resample(0.5) |
73
|
|
|
shape = transform(image).shape |
74
|
|
|
self.assertEqual(shape, (1, 4, 6, 1)) |
75
|
|
|
|
76
|
|
|
def test_input_list(self): |
77
|
|
|
tio.Resample([1, 2, 3]) |
78
|
|
|
|
79
|
|
|
def test_image_target(self): |
80
|
|
|
tio.Resample(self.sample_subject.t1)(self.sample_subject) |
81
|
|
|
|
82
|
|
|
def test_bad_affine(self): |
83
|
|
|
shape = 1, 2, 3 |
84
|
|
|
affine = np.eye(3) |
85
|
|
|
target = shape, affine |
86
|
|
|
transform = tio.Resample(target) |
87
|
|
|
with self.assertRaises(RuntimeError): |
88
|
|
|
transform(self.sample_subject) |
89
|
|
|
|