Passed
Pull Request — main (#1310)
by Fernando
01:24
created

TestResample.test_bad_affine()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
import numpy as np
2
import pytest
3
import torch
4
5
import torchio as tio
6
7
from ...utils import TorchioTestCase
8
9
10
class TestResample(TorchioTestCase):
11
    """Tests for `Resample`."""
12
13
    def test_spacing(self):
14
        # Should this raise an error if sizes are different?
15
        spacing = 2
16
        transform = tio.Resample(spacing)
17
        transformed = transform(self.sample_subject)
18
        for image in transformed.get_images(intensity_only=False):
19
            assert image.spacing == 3 * (spacing,)
20
21
    def test_reference_name(self):
22
        subject = self.get_inconsistent_shape_subject()
23
        reference_name = 't1'
24
        transform = tio.Resample(reference_name)
25
        transformed = transform(subject)
26
        reference_image = subject[reference_name]
27
        for image in transformed.get_images(intensity_only=False):
28
            assert reference_image.shape == image.shape
29
            self.assert_tensor_almost_equal(
30
                reference_image.affine,
31
                image.affine,
32
            )
33
34
    def test_affine(self):
35
        spacing = 1
36
        affine_name = 'pre_affine'
37
        transform = tio.Resample(spacing, pre_affine_name=affine_name)
38
        transformed = transform(self.sample_subject)
39
        for image in transformed.values():
40
            if affine_name in image:
41
                target_affine = np.eye(4)
42
                target_affine[:3, 3] = 10, 0, -0.1
43
                self.assert_tensor_almost_equal(image.affine, target_affine)
44
            else:
45
                self.assert_tensor_equal(image.affine, np.eye(4))
46
47
    def test_missing_affine(self):
48
        transform = tio.Resample(1, pre_affine_name='missing')
49
        with pytest.raises(ValueError):
50
            transform(self.sample_subject)
51
52
    def test_reference_path(self):
53
        reference_image, reference_path = self.get_reference_image_and_path()
54
        transform = tio.Resample(reference_path)
55
        transformed = transform(self.sample_subject)
56
        for image in transformed.values():
57
            assert reference_image.shape == image.shape
58
            self.assert_tensor_almost_equal(
59
                reference_image.affine,
60
                image.affine,
61
            )
62
63
    def test_wrong_spacing_length(self):
64
        with pytest.raises(RuntimeError):
65
            tio.Resample((1, 2))(self.sample_subject)
66
67
    def test_wrong_spacing_value(self):
68
        with pytest.raises(ValueError):
69
            tio.Resample(0)(self.sample_subject)
70
71
    def test_wrong_target_type(self):
72
        with pytest.raises(RuntimeError):
73
            tio.Resample(None)(self.sample_subject)
74
75
    def test_missing_reference(self):
76
        transform = tio.Resample('missing')
77
        with pytest.raises(ValueError):
78
            transform(self.sample_subject)
79
80
    def test_2d(self):
81
        """Check that image is still 2D after resampling."""
82
        image = tio.ScalarImage(tensor=torch.rand(1, 2, 3, 1))
83
        transform = tio.Resample(0.5)
84
        shape = transform(image).shape
85
        assert shape == (1, 4, 6, 1)
86
87
    def test_input_list(self):
88
        tio.Resample([1, 2, 3])(self.sample_subject)
89
90
    def test_input_array(self):
91
        resample = tio.Resample(np.asarray([1, 2, 3]))
92
        resample(self.sample_subject)
93
94
    def test_image_target(self):
95
        tio.Resample(self.sample_subject.t1)(self.sample_subject)
96
97
    def test_bad_affine(self):
98
        shape = 1, 2, 3
99
        affine = np.eye(3)
100
        target = shape, affine
101
        transform = tio.Resample(target)
102
        with pytest.raises(RuntimeError):
103
            transform(self.sample_subject)
104
105
    def test_resample_flip(self):
106
        image = torch.rand(1, 10, 10, 10)
107
        resample = tio.Resample(1.35)
108
        flip = tio.Flip(0)
109
        flipped_and_resampled = resample(flip(image))
110
        resampled_and_flipped = flip(resample(image))
111
        self.assert_tensor_almost_equal(
112
            flipped_and_resampled.data,
113
            resampled_and_flipped.data,
114
        )
115