Passed
Push — master ( 29643a...47015e )
by Fernando
01:22
created

TestResample.test_wrong_spacing_value()   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
import numpy as np
2
from numpy.testing import assert_array_equal, assert_array_almost_equal
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 in transformed.get_images(intensity_only=False):
17
            self.assertEqual(image.spacing, 3 * (spacing,))
18
19
    def test_reference_name(self):
20
        sample = self.get_inconsistent_sample()
21
        reference_name = 't1'
22
        transform = Resample(reference_name)
23
        transformed = transform(sample)
24
        reference_image = sample[reference_name]
25
        for image in transformed.get_images(intensity_only=False):
26
            self.assertEqual(
27
                reference_image.shape, image.shape)
28
            assert_array_equal(reference_image[AFFINE], image[AFFINE])
29
30
    def test_affine(self):
31
        spacing = 1
32
        affine_name = 'pre_affine'
33
        transform = Resample(spacing, pre_affine_name=affine_name)
34
        transformed = transform(self.sample)
35
        for image in transformed.values():
36
            if affine_name in image:
37
                target_affine = np.eye(4)
38
                target_affine[:3, 3] = 10, 0, -0.1
39
                assert_array_almost_equal(image[AFFINE], target_affine)
40
            else:
41
                assert_array_equal(image[AFFINE], np.eye(4))
42
43
    def test_missing_affine(self):
44
        transform = Resample(1, pre_affine_name='missing')
45
        with self.assertRaises(ValueError):
46
            transform(self.sample)
47
48
    def test_reference_path(self):
49
        reference_image, reference_path = self.get_reference_image_and_path()
50
        transform = Resample(reference_path)
51
        transformed = transform(self.sample)
52
        for image in transformed.values():
53
            self.assertEqual(reference_image.shape, image.shape)
54
            assert_array_equal(reference_image.affine, image.affine)
55
56
    def test_wrong_spacing_length(self):
57
        with self.assertRaises(ValueError):
58
            Resample((1, 2))
59
60
    def test_wrong_spacing_value(self):
61
        with self.assertRaises(ValueError):
62
            Resample(0)
63
64
    def test_wrong_target_type(self):
65
        with self.assertRaises(ValueError):
66
            Resample(None)
67
68
    def test_missing_reference(self):
69
        transform = Resample('missing')
70
        with self.assertRaises(ValueError):
71
            transform(self.sample)
72
73
    def test_2d(self):
74
        sample = self.make_2d(self.sample)
75
        transform = Resample(2)
76
        transform(sample)
77