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