Passed
Push — master ( 68316f...4d5c4f )
by Fernando
01:16
created

TestSubject.test_different_space()   A

Complexity

Conditions 2

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nop 1
dl 0
loc 20
rs 9.55
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
"""Tests for Subject."""
4
5
import tempfile
6
import torch
7
import numpy as np
8
import torchio as tio
9
from ..utils import TorchioTestCase
10
11
12
class TestSubject(TorchioTestCase):
13
    """Tests for `Subject`."""
14
    def test_positional_args(self):
15
        with self.assertRaises(ValueError):
16
            with tempfile.NamedTemporaryFile() as f:
17
                tio.Subject(tio.ScalarImage(f.name))
18
19
    def test_input_dict(self):
20
        with tempfile.NamedTemporaryFile() as f:
21
            input_dict = {'image': tio.ScalarImage(f.name)}
22
            tio.Subject(input_dict)
23
            tio.Subject(**input_dict)
24
25
    def test_no_sample(self):
26
        with tempfile.NamedTemporaryFile() as f:
27
            input_dict = {'image': tio.ScalarImage(f.name)}
28
            subject = tio.Subject(input_dict)
29
            with self.assertRaises(RuntimeError):
30
                tio.RandomFlip()(subject)
31
32
    def test_history(self):
33
        transformed = tio.RandomFlip()(self.sample_subject)
34
        self.assertIs(len(transformed.history), 1)
35
36
    def test_inconsistent_shape(self):
37
        subject = tio.Subject(
38
            a=tio.ScalarImage(tensor=torch.rand(1, 2, 3, 4)),
39
            b=tio.ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
40
        )
41
        subject.spatial_shape
42
        with self.assertRaises(RuntimeError):
43
            subject.shape
44
45
    def test_inconsistent_spatial_shape(self):
46
        subject = tio.Subject(
47
            a=tio.ScalarImage(tensor=torch.rand(1, 3, 3, 4)),
48
            b=tio.ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
49
        )
50
        with self.assertRaises(RuntimeError):
51
            subject.spatial_shape
52
53
    def test_plot(self):
54
        self.sample_subject.plot(
55
            show=False,
56
            output_path=self.dir / 'figure.png',
57
            cmap_dict=dict(
58
                t2='viridis',
59
                label={0: 'yellow', 1: 'blue'},
60
            ),
61
        )
62
63
    def test_plot_one_image(self):
64
        subject = tio.Subject(t1=tio.ScalarImage(self.get_image_path('t1_plot')))
65
        subject.plot(show=False)
66
67
    # flake8: noqa: E203, E241
68
    def test_different_space(self):
69
        affine1 = np.array([
70
            [ -0.69921875,   0.        ,   0.        , 169.11578369],
71
            [  0.        ,  -0.69921875,   0.        ,  37.26315689],
72
            [  0.        ,   0.        ,   0.69999993,  15.30004883],
73
            [  0.        ,   0.        ,   0.        ,   1.        ],
74
        ])
75
        affine2 = np.array([
76
            [ -0.69921881,   0.        ,   0.        , 169.11578369],
77
            [  0.        ,  -0.69921881,   0.        ,  37.26315689],
78
            [  0.        ,   0.        ,   0.69999993,  15.30003738],
79
            [  0.        ,   0.        ,   0.        ,   1.        ],
80
        ])
81
        t = torch.rand(1, 2, 3, 4)
82
        subject = tio.Subject(
83
            im1=tio.ScalarImage(tensor=t, affine=affine1),
84
            im2=tio.ScalarImage(tensor=t, affine=affine2),
85
        )
86
        with self.assertRaises(RuntimeError):
87
            subject.check_consistent_space()
88