fepegar / torchio
1 3
import copy
2 3
import torch
3 3
import torchio as tio
4 3
from .utils import TorchioTestCase
5

6

7 3
class TestUtils(TorchioTestCase):
8
    """Tests for `utils` module."""
9

10 3
    def test_to_tuple(self):
11 3
        assert tio.utils.to_tuple(1) == (1,)
12 3
        assert tio.utils.to_tuple((1,)) == (1,)
13 3
        assert tio.utils.to_tuple(1, length=3) == (1, 1, 1)
14 3
        assert tio.utils.to_tuple((1, 2)) == (1, 2)
15 3
        assert tio.utils.to_tuple((1, 2), length=3) == (1, 2)
16 3
        assert tio.utils.to_tuple([1, 2], length=3) == (1, 2)
17

18 3
    def test_get_stem(self):
19 3
        assert tio.utils.get_stem('/home/image.nii.gz') == 'image'
20 3
        assert tio.utils.get_stem('/home/image.nii') == 'image'
21 3
        assert tio.utils.get_stem('/home/image.nrrd') == 'image'
22

23 3
    def test_guess_type(self):
24 3
        assert tio.utils.guess_type('None') is None
25 3
        assert isinstance(tio.utils.guess_type('1'), int)
26 3
        assert isinstance(tio.utils.guess_type('1.5'), float)
27 3
        assert isinstance(tio.utils.guess_type('(1, 3, 5)'), tuple)
28 3
        assert isinstance(tio.utils.guess_type('(1,3,5)'), tuple)
29 3
        assert isinstance(tio.utils.guess_type('[1,3,5]'), list)
30 3
        assert isinstance(tio.utils.guess_type('test'), str)
31

32 3
    def test_apply_transform_to_file(self):
33 3
        transform = tio.RandomFlip()
34 3
        tio.utils.apply_transform_to_file(
35
            self.get_image_path('input'),
36
            transform,
37
            self.get_image_path('output'),
38
            verbose=True,
39
        )
40

41 3
    def test_subjects_from_batch(self):
42 3
        dataset = tio.SubjectsDataset(4 * [self.sample_subject])
43 3
        loader = torch.utils.data.DataLoader(dataset, batch_size=4)
44 3
        batch = tio.utils.get_first_item(loader)
45 3
        subjects = tio.utils.get_subjects_from_batch(batch)
46 3
        assert isinstance(subjects[0], tio.Subject)
47

48 3
    def test_add_images_from_batch(self):
49 3
        subject = copy.deepcopy(self.sample_subject)
50 3
        subjects = 4 * [subject]
51 3
        preds = torch.rand(4, *subject.shape)
52 3
        tio.utils.add_images_from_batch(subjects, preds)
53

54 3
    def test_empty_batch(self):
55 3
        with self.assertRaises(RuntimeError):
56 3
            tio.utils.get_batch_images_and_size({})

Read our documentation on viewing source code .

Loading