Passed
Pull Request — master (#270)
by Fernando
01:10
created

TestNibabelToSimpleITK.test_wrong_num_dims()   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
"""Tests for `utils` package."""
4
5
import unittest
6
import torch
7
import numpy as np
8
import SimpleITK as sitk
9
from torchio import RandomFlip
10
from torchio.utils import (
11
    to_tuple,
12
    get_stem,
13
    guess_type,
14
    nib_to_sitk,
15
    sitk_to_nib,
16
    apply_transform_to_file,
17
)
18
from .utils import TorchioTestCase
19
20
21
class TestUtils(TorchioTestCase):
22
    """Tests for `utils` module."""
23
24
    def test_to_tuple(self):
25
        assert to_tuple(1) == (1,)
26
        assert to_tuple((1,)) == (1,)
27
        assert to_tuple(1, length=3) == (1, 1, 1)
28
        assert to_tuple((1, 2)) == (1, 2)
29
        assert to_tuple((1, 2), length=3) == (1, 2)
30
        assert to_tuple([1, 2], length=3) == (1, 2)
31
32
    def test_get_stem(self):
33
        assert get_stem('/home/image.nii.gz') == 'image'
34
        assert get_stem('/home/image.nii') == 'image'
35
        assert get_stem('/home/image.nrrd') == 'image'
36
37
    def test_guess_type(self):
38
        assert guess_type('None') is None
39
        assert isinstance(guess_type('1'), int)
40
        assert isinstance(guess_type('1.5'), float)
41
        assert isinstance(guess_type('(1, 3, 5)'), tuple)
42
        assert isinstance(guess_type('(1,3,5)'), tuple)
43
        assert isinstance(guess_type('[1,3,5]'), list)
44
        assert isinstance(guess_type('test'), str)
45
46
    def test_apply_transform_to_file(self):
47
        transform = RandomFlip()
48
        apply_transform_to_file(
49
            self.get_image_path('input'),
50
            transform,
51
            self.get_image_path('output'),
52
            verbose=True,
53
        )
54
55
    def test_sitk_to_nib(self):
56
        data = np.random.rand(10, 12)
57
        image = sitk.GetImageFromArray(data)
58
        tensor, affine = sitk_to_nib(image)
59
        self.assertAlmostEqual(data.sum(), tensor.sum())
60
61
62
class TestNibabelToSimpleITK(TorchioTestCase):
63
    def setUp(self):
64
        super().setUp()
65
        self.affine = np.eye(4)
66
67
    def test_wrong_num_dims(self):
68
        with self.assertRaises(ValueError):
69
            nib_to_sitk(np.random.rand(10, 10), self.affine)
70
71
    def test_2d_single(self):
72
        data = np.random.rand(1, 10, 12, 1)
73
        image = nib_to_sitk(data, self.affine)
74
        assert image.GetDimension() == 2
75
        assert image.GetSize() == (10, 12)
76
        assert image.GetNumberOfComponentsPerPixel() == 1
77
78
    def test_2d_multi(self):
79
        data = np.random.rand(5, 10, 12, 1)
80
        image = nib_to_sitk(data, self.affine)
81
        assert image.GetDimension() == 2
82
        assert image.GetSize() == (10, 12)
83
        assert image.GetNumberOfComponentsPerPixel() == 5
84
85
    def test_2d_3d_single(self):
86
        data = np.random.rand(1, 10, 12, 1)
87
        image = nib_to_sitk(data, self.affine, force_3d=True)
88
        assert image.GetDimension() == 3
89
        assert image.GetSize() == (10, 12, 1)
90
        assert image.GetNumberOfComponentsPerPixel() == 1
91
92
    def test_2d_3d_multi(self):
93
        data = np.random.rand(5, 10, 12, 1)
94
        image = nib_to_sitk(data, self.affine, force_3d=True)
95
        assert image.GetDimension() == 3
96
        assert image.GetSize() == (10, 12, 1)
97
        assert image.GetNumberOfComponentsPerPixel() == 5
98
99
    def test_3d_single(self):
100
        data = np.random.rand(1, 8, 10, 12)
101
        image = nib_to_sitk(data, self.affine)
102
        assert image.GetDimension() == 3
103
        assert image.GetSize() == (8, 10, 12)
104
        assert image.GetNumberOfComponentsPerPixel() == 1
105
106
    def test_3d_multi(self):
107
        data = np.random.rand(5, 8, 10, 12)
108
        image = nib_to_sitk(data, self.affine)
109
        assert image.GetDimension() == 3
110
        assert image.GetSize() == (8, 10, 12)
111
        assert image.GetNumberOfComponentsPerPixel() == 5
112