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
|
|
|
def test_same_space(self): |
70
|
|
|
# https://github.com/fepegar/torchio/issues/381 |
71
|
|
|
affine1 = np.array([ |
72
|
|
|
[ 4.27109375e-14, -8.71264808e-03, 9.99876633e-01, -3.39850907e+01], |
73
|
|
|
[-5.54687500e-01, -2.71630469e-12, 8.75148028e-17, 1.62282930e+02], |
74
|
|
|
[ 2.71575000e-12, -5.54619070e-01, -1.57073092e-02, 2.28515784e+02], |
75
|
|
|
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00], |
76
|
|
|
]) |
77
|
|
|
affine2 = np.array([ |
78
|
|
|
[ 3.67499773e-08, -8.71257665e-03, 9.99876635e-01, -3.39850922e+01], |
79
|
|
|
[-5.54687500e-01, 3.67499771e-08, 6.73024385e-08, 1.62282928e+02], |
80
|
|
|
[-3.73318194e-08, -5.54619071e-01, -1.57071802e-02, 2.28515778e+02], |
81
|
|
|
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00], |
82
|
|
|
]) |
83
|
|
|
t = torch.rand(1, 2, 3, 4) |
84
|
|
|
subject = tio.Subject( |
85
|
|
|
im1=tio.ScalarImage(tensor=t, affine=affine1), |
86
|
|
|
im2=tio.ScalarImage(tensor=t, affine=affine2), |
87
|
|
|
) |
88
|
|
|
subject.check_consistent_space() |
89
|
|
|
|