1
|
|
|
import torchio as tio |
2
|
|
|
from torchio.utils import ( |
3
|
|
|
to_tuple, |
4
|
|
|
get_stem, |
5
|
|
|
guess_type, |
6
|
|
|
apply_transform_to_file, |
7
|
|
|
) |
8
|
|
|
from .utils import TorchioTestCase |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class TestUtils(TorchioTestCase): |
12
|
|
|
"""Tests for `utils` module.""" |
13
|
|
|
|
14
|
|
|
def test_to_tuple(self): |
15
|
|
|
assert to_tuple(1) == (1,) |
16
|
|
|
assert to_tuple((1,)) == (1,) |
17
|
|
|
assert to_tuple(1, length=3) == (1, 1, 1) |
18
|
|
|
assert to_tuple((1, 2)) == (1, 2) |
19
|
|
|
assert to_tuple((1, 2), length=3) == (1, 2) |
20
|
|
|
assert to_tuple([1, 2], length=3) == (1, 2) |
21
|
|
|
|
22
|
|
|
def test_get_stem(self): |
23
|
|
|
assert get_stem('/home/image.nii.gz') == 'image' |
24
|
|
|
assert get_stem('/home/image.nii') == 'image' |
25
|
|
|
assert get_stem('/home/image.nrrd') == 'image' |
26
|
|
|
|
27
|
|
|
def test_guess_type(self): |
28
|
|
|
assert guess_type('None') is None |
29
|
|
|
assert isinstance(guess_type('1'), int) |
30
|
|
|
assert isinstance(guess_type('1.5'), float) |
31
|
|
|
assert isinstance(guess_type('(1, 3, 5)'), tuple) |
32
|
|
|
assert isinstance(guess_type('(1,3,5)'), tuple) |
33
|
|
|
assert isinstance(guess_type('[1,3,5]'), list) |
34
|
|
|
assert isinstance(guess_type('test'), str) |
35
|
|
|
|
36
|
|
|
def test_apply_transform_to_file(self): |
37
|
|
|
transform = tio.RandomFlip() |
38
|
|
|
apply_transform_to_file( |
39
|
|
|
self.get_image_path('input'), |
40
|
|
|
transform, |
41
|
|
|
self.get_image_path('output'), |
42
|
|
|
verbose=True, |
43
|
|
|
) |
44
|
|
|
|