Passed
Push — master ( 7b848f...cac223 )
by Fernando
02:39
created

torchio.utils.create_dummy_dataset()   B

Complexity

Conditions 7

Size

Total Lines 59
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 49
nop 6
dl 0
loc 59
rs 7.269
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import ast
2
import shutil
3
import tempfile
4
from pathlib import Path
5
from typing import Union, Iterable, Tuple, Any, Optional, List
6
import torch
7
import numpy as np
8
import nibabel as nib
9
import SimpleITK as sitk
10
from tqdm import trange
11
from .torchio import (
12
    INTENSITY,
13
    LABEL,
14
    TypeData,
15
    TypeNumber,
16
    TypePath,
17
)
18
19
20
FLIP_XY = np.diag((-1, -1, 1))  # used to switch between LPS and RAS
21
22
23
def to_tuple(
24
        value: Union[TypeNumber, Iterable[TypeNumber]],
25
        length: int = 1,
26
        ) -> Tuple[TypeNumber, ...]:
27
    """
28
    to_tuple(1, length=1) -> (1,)
29
    to_tuple(1, length=3) -> (1, 1, 1)
30
31
    If value is an iterable, n is ignored and tuple(value) is returned
32
    to_tuple((1,), length=1) -> (1,)
33
    to_tuple((1, 2), length=1) -> (1, 2)
34
    to_tuple([1, 2], length=3) -> (1, 2)
35
    """
36
    try:
37
        iter(value)
38
        value = tuple(value)
39
    except TypeError:
40
        value = length * (value,)
41
    return value
42
43
44
def get_stem(path: TypePath) -> str:
45
    """
46
    '/home/user/image.nii.gz' -> 'image'
47
    """
48
    path = Path(path)
49
    return path.name.split('.')[0]
50
51
52
def create_dummy_dataset(
53
        num_images: int,
54
        size_range: Tuple[int, int],
55
        directory: Optional[TypePath] = None,
56
        suffix: str = '.nii.gz',
57
        force: bool = False,
58
        verbose: bool = False,
59
        ):
60
    from .data import Image, Subject
61
    output_dir = tempfile.gettempdir() if directory is None else directory
62
    output_dir = Path(output_dir)
63
    images_dir = output_dir / 'dummy_images'
64
    labels_dir = output_dir / 'dummy_labels'
65
66
    if force:
67
        shutil.rmtree(images_dir)
68
        shutil.rmtree(labels_dir)
69
70
    subjects: List[Subject] = []
71
    if images_dir.is_dir():
72
        for i in trange(num_images):
73
            image_path = images_dir / f'image_{i}{suffix}'
74
            label_path = labels_dir / f'label_{i}{suffix}'
75
            subject = Subject(
76
                one_modality=Image(image_path, INTENSITY),
77
                segmentation=Image(label_path, LABEL),
78
            )
79
            subjects.append(subject)
80
    else:
81
        images_dir.mkdir(exist_ok=True, parents=True)
82
        labels_dir.mkdir(exist_ok=True, parents=True)
83
        if verbose:
84
            print('Creating dummy dataset...')
85
            iterable = trange(num_images)
86
        else:
87
            iterable = range(num_images)
88
        for i in iterable:
89
            shape = np.random.randint(*size_range, size=3)
90
            affine = np.eye(4)
91
            image = np.random.rand(*shape)
92
            label = np.ones_like(image)
93
            label[image < 0.33] = 0
94
            label[image > 0.66] = 2
95
            image *= 255
96
97
            image_path = images_dir / f'image_{i}{suffix}'
98
            nii = nib.Nifti1Image(image.astype(np.uint8), affine)
99
            nii.to_filename(str(image_path))
100
101
            label_path = labels_dir / f'label_{i}{suffix}'
102
            nii = nib.Nifti1Image(label.astype(np.uint8), affine)
103
            nii.to_filename(str(label_path))
104
105
            subject = Subject(
106
                one_modality=Image(image_path, INTENSITY),
107
                segmentation=Image(label_path, LABEL),
108
            )
109
            subjects.append(subject)
110
    return subjects
111
112
113
def apply_transform_to_file(
114
        input_path: TypePath,
115
        transform,  # : Transform seems to create a circular import (TODO)
116
        output_path: TypePath,
117
        type: str = INTENSITY,
118
        ):
119
    from . import Image, ImagesDataset, Subject
120
    subject = Subject(image=Image(input_path, type))
121
    dataset = ImagesDataset([subject], transform=transform)
122
    transformed = dataset[0]
123
    dataset.save_sample(transformed, dict(image=output_path))
124
125
126
def guess_type(string: str) -> Any:
127
    # Adapted from
128
    # https://www.reddit.com/r/learnpython/comments/4599hl/module_to_guess_type_from_a_string/czw3f5s
129
    string = string.replace(' ', '')
130
    try:
131
        value = ast.literal_eval(string)
132
    except ValueError:
133
        result_type = str
134
    else:
135
        result_type = type(value)
136
    if result_type in (list, tuple):
137
        string = string[1:-1]  # remove brackets
138
        split = string.split(',')
139
        list_result = [guess_type(n) for n in split]
140
        value = tuple(list_result) if result_type is tuple else list_result
141
        return value
142
    try:
143
        value = result_type(string)
144
    except TypeError:
145
        value = None
146
    return value
147
148
149
def get_rotation_and_spacing_from_affine(
150
        affine: np.ndarray,
151
        ) -> Tuple[np.ndarray, np.ndarray]:
152
    # From https://github.com/nipy/nibabel/blob/master/nibabel/orientations.py
153
    rotation_zoom = affine[:3, :3]
154
    spacing = np.sqrt(np.sum(rotation_zoom * rotation_zoom, axis=0))
155
    rotation = rotation_zoom / spacing
156
    return rotation, spacing
157
158
159
def nib_to_sitk(data: TypeData, affine: TypeData) -> sitk.Image:
160
    array = data.numpy() if isinstance(data, torch.Tensor) else data
161
    affine = affine.numpy() if isinstance(affine, torch.Tensor) else affine
162
    origin = np.dot(FLIP_XY, affine[:3, 3]).astype(np.float64)
163
    rotation, spacing = get_rotation_and_spacing_from_affine(affine)
164
    direction = np.dot(FLIP_XY, rotation)
165
    image = sitk.GetImageFromArray(array.transpose())
166
    if array.ndim == 2:  # ignore first dimension if 2D (1, 1, H, W)
167
        direction = direction[1:3, 1:3]
168
    image.SetOrigin(origin)
169
    image.SetSpacing(spacing)
170
    image.SetDirection(direction.flatten())
171
    return image
172
173
174
def sitk_to_nib(image: sitk.Image) -> Tuple[np.ndarray, np.ndarray]:
175
    data = sitk.GetArrayFromImage(image).transpose()
176
    spacing = np.array(image.GetSpacing())
177
    direction = np.array(image.GetDirection())
178
    origin = image.GetOrigin()
179
    if len(direction) == 9:
180
        rotation = direction.reshape(3, 3)
181
    elif len(direction) == 4:  # ignore first dimension if 2D (1, 1, H, W)
182
        rotation_2d = direction.reshape(2, 2)
183
        rotation = np.eye(3)
184
        rotation[1:3, 1:3] = rotation_2d
185
        spacing = 1, *spacing
186
        origin = 0, *origin
187
    rotation = np.dot(FLIP_XY, rotation)
0 ignored issues
show
introduced by
The variable rotation does not seem to be defined for all execution paths.
Loading history...
188
    rotation_zoom = rotation * spacing
189
    translation = np.dot(FLIP_XY, origin)
190
    affine = np.eye(4)
191
    affine[:3, :3] = rotation_zoom
192
    affine[:3, 3] = translation
193
    return data, affine
194
195
196
def round_up(value: float) -> float:
197
    """Round half towards infinity.
198
199
    Args:
200
        value: The value to round.
201
202
    Example:
203
204
        >>> round(2.5)
205
        2
206
        >>> round(3.5)
207
        4
208
        >>> round_up(2.5)
209
        3
210
        >>> round_up(3.5)
211
        4
212
213
    """
214
    return np.floor(value + 0.5)
215