Passed
Pull Request — master (#133)
by Fernando
01:29
created

TestResample.test_missing_affine()   A

Complexity

Conditions 2

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
import numpy as np
0 ignored issues
show
introduced by
Unable to import 'numpy'
Loading history...
2
from numpy.testing import assert_array_equal
0 ignored issues
show
introduced by
Unable to import 'numpy.testing'
Loading history...
3
from torchio import DATA, AFFINE
4
from torchio.transforms import Resample
5
from torchio.utils import nib_to_sitk
6
from ...utils import TorchioTestCase
7
8
9
class TestResample(TorchioTestCase):
10
    """Tests for `Resample`."""
11
    def test_spacing(self):
12
        # Should this raise an error if sizes are different?
13
        spacing = 2
14
        transform = Resample(spacing)
15
        transformed = transform(self.sample)
16
        for image_dict in transformed.values():
17
            image = nib_to_sitk(image_dict[DATA], image_dict[AFFINE])
18
            self.assertEqual(image.GetSpacing(), 3 * (spacing,))
19
20
    def test_reference(self):
21
        sample = self.get_inconsistent_sample()
22
        reference_name = 't1'
23
        transform = Resample(reference_name)
24
        transformed = transform(sample)
25
        ref_image_dict = sample[reference_name]
26
        for image_dict in transformed.values():
27
            self.assertEqual(
28
                ref_image_dict[DATA].shape, image_dict[DATA].shape)
29
            assert_array_equal(ref_image_dict[AFFINE], image_dict[AFFINE])
30
31
    def test_affine(self):
32
        spacing = 1
33
        affine_name = 'pre_affine'
34
        transform = Resample(spacing, pre_affine_name=affine_name)
35
        transformed = transform(self.sample)
36
        for image_dict in transformed.values():
37
            if affine_name in image_dict.keys():
38
                new_affine = np.eye(4)
39
                new_affine[0, 3] = 10
40
                assert_array_equal(image_dict[AFFINE], new_affine)
41
            else:
42
                assert_array_equal(image_dict[AFFINE], np.eye(4))
43
44
    def test_missing_affine(self):
45
        transform = Resample(1, pre_affine_name='missing')
46
        with self.assertRaises(ValueError):
47
            transform(self.sample)
48
49
    def test_wrong_spacing_length(self):
50
        with self.assertRaises(ValueError):
51
            Resample((1, 2))
52
53
    def test_wrong_spacing_value(self):
54
        with self.assertRaises(ValueError):
55
            Resample(0)
56
57
    def test_wrong_target_type(self):
58
        with self.assertRaises(ValueError):
59
            Resample(None)
60
61
    def test_missing_reference(self):
62
        transform = Resample('missing')
63
        with self.assertRaises(ValueError):
64
            transform(self.sample)
65