Passed
Pull Request — master (#134)
by Fernando
03:18
created

TorchioTestCase.get_reference_image_and_path()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
import shutil
2
import random
3
import tempfile
4
import unittest
5
from pathlib import Path
6
import numpy as np
0 ignored issues
show
introduced by
Unable to import 'numpy'
Loading history...
7
import nibabel as nib
0 ignored issues
show
introduced by
Unable to import 'nibabel'
Loading history...
8
from torchio.datasets import IXITiny
9
from torchio import INTENSITY, LABEL, Image, ImagesDataset, Subject
10
11
12
class TorchioTestCase(unittest.TestCase):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
13
14
    def setUp(self):
15
        """Set up test fixtures, if any."""
16
        self.dir = Path(tempfile.gettempdir()) / '.torchio_tests'
17
        self.dir.mkdir(exist_ok=True)
18
        random.seed(42)
19
        np.random.seed(42)
20
21
        registration_matrix = np.array([
22
            [1, 0, 0, 10],
23
            [0, 1, 0, 0],
24
            [0, 0, 1.2, 0],
25
            [0, 0, 0, 1]
26
        ])
27
28
        subject_a = Subject(
29
            t1=Image(self.get_image_path('t1_a'), INTENSITY),
30
        )
31
        subject_b = Subject(
32
            t1=Image(self.get_image_path('t1_b'), INTENSITY),
33
            label=Image(self.get_image_path('label_b', binary=True), LABEL),
34
        )
35
        subject_c = Subject(
36
            label=Image(self.get_image_path('label_c', binary=True), LABEL),
37
        )
38
        subject_d = Subject(
39
            t1=Image(
40
                self.get_image_path('t1_d'),
41
                INTENSITY,
42
                pre_affine=registration_matrix,
43
            ),
44
            t2=Image(self.get_image_path('t2_d'), INTENSITY),
45
            label=Image(self.get_image_path('label_d', binary=True), LABEL),
46
        )
47
        self.subjects_list = [
48
            subject_a,
49
            subject_b,
50
            subject_c,
51
            subject_d,
52
        ]
53
        self.dataset = ImagesDataset(self.subjects_list)
54
        self.sample = self.dataset[-1]
55
56
    def get_inconsistent_sample(self):
57
        """Return a sample containing images of different shape."""
58
        subject = Subject(
59
            t1=Image(self.get_image_path('t1_d'), INTENSITY),
60
            t2=Image(
61
                self.get_image_path('t2_d', shape=(10, 20, 31)), INTENSITY),
62
            label=Image(
63
                self.get_image_path(
64
                    'label_d',
65
                    shape=(8, 17, 25),
66
                    binary=True,
67
                ),
68
                LABEL,
69
            ),
70
        )
71
        subjects_list = [subject]
72
        dataset = ImagesDataset(subjects_list)
73
        return dataset[0]
74
75
    def get_reference_image_and_path(self):
76
        """Return a reference image and its path"""
77
        path = self.get_image_path('ref', shape=(10, 20, 31))
78
        image = Image(path, INTENSITY)
79
        return image, path
80
81
    def tearDown(self):
82
        """Tear down test fixtures, if any."""
83
        print('Deleting', self.dir)
84
        shutil.rmtree(self.dir)
85
86
    def get_ixi_tiny(self):
0 ignored issues
show
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
87
        root_dir = Path(tempfile.gettempdir()) / 'torchio' / 'ixi_tiny'
88
        return IXITiny(root_dir, download=True)
89
90
    def get_image_path(
91
            self,
92
            stem,
93
            binary=False,
94
            shape=(10, 20, 30),
95
            spacing=(1, 1, 1),
96
            ):
97
        data = np.random.rand(*shape)
98
        if binary:
99
            data = (data > 0.5).astype(np.uint8)
100
        affine = np.diag((*spacing, 1))
101
        suffix = random.choice(('.nii.gz', '.nii'))
102
        path = self.dir / f'{stem}{suffix}'
103
        nib.Nifti1Image(data, affine).to_filename(str(path))
104
        if np.random.rand() > 0.5:
105
            path = str(path)
106
        return path
107