-
Notifications
You must be signed in to change notification settings - Fork 233
/
test_subject.py
60 lines (50 loc) · 1.8 KB
/
test_subject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
"""Tests for Subject."""
import tempfile
import torch
from torchio import Subject, ScalarImage, RandomFlip
from ..utils import TorchioTestCase
class TestSubject(TorchioTestCase):
"""Tests for `Subject`."""
def test_positional_args(self):
with self.assertRaises(ValueError):
with tempfile.NamedTemporaryFile() as f:
Subject(ScalarImage(f.name))
def test_input_dict(self):
with tempfile.NamedTemporaryFile() as f:
input_dict = {'image': ScalarImage(f.name)}
Subject(input_dict)
Subject(**input_dict)
def test_no_sample(self):
with tempfile.NamedTemporaryFile() as f:
input_dict = {'image': ScalarImage(f.name)}
subject = Subject(input_dict)
with self.assertRaises(RuntimeError):
RandomFlip()(subject)
def test_history(self):
transformed = RandomFlip()(self.sample)
self.assertIs(len(transformed.history), 1)
def test_inconsistent_shape(self):
subject = Subject(
a=ScalarImage(tensor=torch.rand(1, 2, 3, 4)),
b=ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
)
subject.spatial_shape
with self.assertRaises(RuntimeError):
subject.shape
def test_inconsistent_spatial_shape(self):
subject = Subject(
a=ScalarImage(tensor=torch.rand(1, 3, 3, 4)),
b=ScalarImage(tensor=torch.rand(2, 2, 3, 4)),
)
with self.assertRaises(RuntimeError):
subject.spatial_shape
def test_plot(self):
self.sample.plot(
show=False,
output_path=self.dir / 'figure.png',
cmap_dict=dict(
t2='viridis',
label={0: 'yellow', 1: 'blue'},
),
)