Passed
Pull Request — master (#121)
by Fernando
01:28
created

TestResample.test_reference()   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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