Passed
Push — master ( df0967...6e95bb )
by Fernando
01:10
created

TestSubject.test_no_sample()   A

Complexity

Conditions 3

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
"""Tests for Subject."""
4
5
import tempfile
6
import torch
7
from torchio import Subject, ScalarImage, RandomFlip
8
from ..utils import TorchioTestCase
9
10
11
class TestSubject(TorchioTestCase):
12
    """Tests for `Subject`."""
13
    def test_positional_args(self):
14
        with self.assertRaises(ValueError):
15
            with tempfile.NamedTemporaryFile() as f:
16
                Subject(ScalarImage(f.name))
17
18
    def test_input_dict(self):
19
        with tempfile.NamedTemporaryFile() as f:
20
            input_dict = {'image': ScalarImage(f.name)}
21
            Subject(input_dict)
22
            Subject(**input_dict)
23
24
    def test_no_sample(self):
25
        with tempfile.NamedTemporaryFile() as f:
26
            input_dict = {'image': ScalarImage(f.name)}
27
            subject = Subject(input_dict)
28
            with self.assertRaises(RuntimeError):
29
                RandomFlip()(subject)
30
31
    def test_history(self):
32
        transformed = RandomFlip()(self.sample_subject)
33
        self.assertIs(len(transformed.history), 1)
34
35
    def test_inconsistent_shape(self):
36
        subject = Subject(
37
            a=ScalarImage(tensor=torch.rand(1, 2, 3, 4)),
38
            b=ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
39
        )
40
        subject.spatial_shape
41
        with self.assertRaises(RuntimeError):
42
            subject.shape
43
44
    def test_inconsistent_spatial_shape(self):
45
        subject = Subject(
46
            a=ScalarImage(tensor=torch.rand(1, 3, 3, 4)),
47
            b=ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
48
        )
49
        with self.assertRaises(RuntimeError):
50
            subject.spatial_shape
51
52
    def test_plot(self):
53
        self.sample_subject.plot(
54
            show=False,
55
            output_path=self.dir / 'figure.png',
56
            cmap_dict=dict(
57
                t2='viridis',
58
                label={0: 'yellow', 1: 'blue'},
59
            ),
60
        )
61
62
    def test_plot_one_image(self):
63
        subject = Subject(t1=ScalarImage(self.get_image_path('t1_plot')))
64
        subject.plot(show=False)
65