Passed
Push — master ( 59cfb9...460aca )
by Fernando
01:06
created

tests.data.test_subject   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A TestSubject.test_history() 0 3 1
A TestSubject.test_plot() 0 7 1
A TestSubject.test_same_space() 0 20 1
A TestSubject.test_input_dict() 0 5 2
A TestSubject.test_no_sample() 0 6 3
A TestSubject.test_different_space() 0 22 2
A TestSubject.test_positional_args() 0 4 3
A TestSubject.test_plot_one_image() 0 3 1
A TestSubject.test_inconsistent_spatial_shape() 0 7 2
A TestSubject.test_inconsistent_shape() 0 8 2
1
#!/usr/bin/env python
2
3
"""Tests for Subject."""
4
5
import tempfile
6
import pytest
7
import torch
8
import numpy as np
9
import torchio as tio
10
from ..utils import TorchioTestCase
11
12
13
class TestSubject(TorchioTestCase):
14
    """Tests for `Subject`."""
15
    def test_positional_args(self):
16
        with self.assertRaises(ValueError):
17
            with tempfile.NamedTemporaryFile() as f:
18
                tio.Subject(tio.ScalarImage(f.name))
19
20
    def test_input_dict(self):
21
        with tempfile.NamedTemporaryFile() as f:
22
            input_dict = {'image': tio.ScalarImage(f.name)}
23
            tio.Subject(input_dict)
24
            tio.Subject(**input_dict)
25
26
    def test_no_sample(self):
27
        with tempfile.NamedTemporaryFile() as f:
28
            input_dict = {'image': tio.ScalarImage(f.name)}
29
            subject = tio.Subject(input_dict)
30
            with self.assertRaises(RuntimeError):
31
                tio.RandomFlip()(subject)
32
33
    def test_history(self):
34
        transformed = tio.RandomFlip()(self.sample_subject)
35
        self.assertIs(len(transformed.history), 1)
36
37
    def test_inconsistent_shape(self):
38
        subject = tio.Subject(
39
            a=tio.ScalarImage(tensor=torch.rand(1, 2, 3, 4)),
40
            b=tio.ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
41
        )
42
        subject.spatial_shape
43
        with self.assertRaises(RuntimeError):
44
            subject.shape
45
46
    def test_inconsistent_spatial_shape(self):
47
        subject = tio.Subject(
48
            a=tio.ScalarImage(tensor=torch.rand(1, 3, 3, 4)),
49
            b=tio.ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
50
        )
51
        with self.assertRaises(RuntimeError):
52
            subject.spatial_shape
53
54
    def test_plot(self):
55
        self.sample_subject.plot(
56
            show=False,
57
            output_path=self.dir / 'figure.png',
58
            cmap_dict=dict(
59
                t2='viridis',
60
                label={0: 'yellow', 1: 'blue'},
61
            ),
62
        )
63
64
    def test_plot_one_image(self):
65
        subject = tio.Subject(t1=tio.ScalarImage(self.get_image_path('t1_plot')))
66
        subject.plot(show=False)
67
68
    # flake8: noqa: E203, E241
69
    @pytest.mark.skip(reason='waiting for SimpleITK#1276')
70
    def test_different_space(self):
71
        # https://github.com/fepegar/torchio/issues/354
72
        affine1 = np.array([
73
            [ -0.69921875,   0.        ,   0.        , 169.11578369],
74
            [  0.        ,  -0.69921875,   0.        ,  37.26315689],
75
            [  0.        ,   0.        ,   0.69999993,  15.30004883],
76
            [  0.        ,   0.        ,   0.        ,   1.        ],
77
        ])
78
        affine2 = np.array([
79
            [ -0.69921881,   0.        ,   0.        , 169.11578369],
80
            [  0.        ,  -0.69921881,   0.        ,  37.26315689],
81
            [  0.        ,   0.        ,   0.69999993,  15.30003738],
82
            [  0.        ,   0.        ,   0.        ,   1.        ],
83
        ])
84
        t = torch.rand(1, 2, 3, 4)
85
        subject = tio.Subject(
86
            im1=tio.ScalarImage(tensor=t, affine=affine1),
87
            im2=tio.ScalarImage(tensor=t, affine=affine2),
88
        )
89
        with self.assertRaises(RuntimeError):
90
            subject.check_consistent_space()
91
92
    # flake8: noqa: E203, E241
93
    def test_same_space(self):
94
        # https://github.com/fepegar/torchio/issues/381
95
        affine1 = np.array([
96
            [ 4.27109375e-14, -8.71264808e-03,  9.99876633e-01, -3.39850907e+01],
97
            [-5.54687500e-01, -2.71630469e-12,  8.75148028e-17, 1.62282930e+02],
98
            [ 2.71575000e-12, -5.54619070e-01, -1.57073092e-02, 2.28515784e+02],
99
            [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, 1.00000000e+00],
100
        ])
101
        affine2 = np.array([
102
            [ 3.67499773e-08, -8.71257665e-03,  9.99876635e-01, -3.39850922e+01],
103
            [-5.54687500e-01,  3.67499771e-08,  6.73024385e-08, 1.62282928e+02],
104
            [-3.73318194e-08, -5.54619071e-01, -1.57071802e-02, 2.28515778e+02],
105
            [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, 1.00000000e+00],
106
        ])
107
        t = torch.rand(1, 2, 3, 4)
108
        subject = tio.Subject(
109
            im1=tio.ScalarImage(tensor=t, affine=affine1),
110
            im2=tio.ScalarImage(tensor=t, affine=affine2),
111
        )
112
        subject.check_consistent_space()
113