|
1
|
|
|
import torch |
|
2
|
|
|
import torchio as tio |
|
3
|
|
|
from torchio.utils import ( |
|
4
|
|
|
to_tuple, |
|
5
|
|
|
get_stem, |
|
6
|
|
|
guess_type, |
|
7
|
|
|
apply_transform_to_file, |
|
8
|
|
|
) |
|
9
|
|
|
from .utils import TorchioTestCase |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestUtils(TorchioTestCase): |
|
13
|
|
|
"""Tests for `utils` module.""" |
|
14
|
|
|
|
|
15
|
|
|
def test_to_tuple(self): |
|
16
|
|
|
assert to_tuple(1) == (1,) |
|
17
|
|
|
assert to_tuple((1,)) == (1,) |
|
18
|
|
|
assert to_tuple(1, length=3) == (1, 1, 1) |
|
19
|
|
|
assert to_tuple((1, 2)) == (1, 2) |
|
20
|
|
|
assert to_tuple((1, 2), length=3) == (1, 2) |
|
21
|
|
|
assert to_tuple([1, 2], length=3) == (1, 2) |
|
22
|
|
|
|
|
23
|
|
|
def test_get_stem(self): |
|
24
|
|
|
assert get_stem('/home/image.nii.gz') == 'image' |
|
25
|
|
|
assert get_stem('/home/image.nii') == 'image' |
|
26
|
|
|
assert get_stem('/home/image.nrrd') == 'image' |
|
27
|
|
|
|
|
28
|
|
|
def test_guess_type(self): |
|
29
|
|
|
assert guess_type('None') is None |
|
30
|
|
|
assert isinstance(guess_type('1'), int) |
|
31
|
|
|
assert isinstance(guess_type('1.5'), float) |
|
32
|
|
|
assert isinstance(guess_type('(1, 3, 5)'), tuple) |
|
33
|
|
|
assert isinstance(guess_type('(1,3,5)'), tuple) |
|
34
|
|
|
assert isinstance(guess_type('[1,3,5]'), list) |
|
35
|
|
|
assert isinstance(guess_type('test'), str) |
|
36
|
|
|
|
|
37
|
|
|
def test_apply_transform_to_file(self): |
|
38
|
|
|
transform = tio.RandomFlip() |
|
39
|
|
|
apply_transform_to_file( |
|
40
|
|
|
self.get_image_path('input'), |
|
41
|
|
|
transform, |
|
42
|
|
|
self.get_image_path('output'), |
|
43
|
|
|
verbose=True, |
|
44
|
|
|
) |
|
45
|
|
|
|
|
46
|
|
|
def test_subjects_from_batch(self): |
|
47
|
|
|
dataset = tio.SubjectsDataset(4 * [self.sample_subject]) |
|
48
|
|
|
loader = torch.utils.data.DataLoader(dataset, batch_size=4) |
|
49
|
|
|
batch = tio.utils.get_first_item(loader) |
|
50
|
|
|
subjects = tio.utils.get_subjects_from_batch(batch) |
|
51
|
|
|
assert isinstance(subjects[0], tio.Subject) |
|
52
|
|
|
|
|
53
|
|
|
def test_empty_batch(self): |
|
54
|
|
|
with self.assertRaises(RuntimeError): |
|
55
|
|
|
tio.utils.get_batch_images_and_size({}) |
|
56
|
|
|
|