Passed
Pull Request — master (#353)
by Fernando
01:17
created

torchio.utils   D

Complexity

Total Complexity 59

Size/Duplication

Total Lines 353
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 243
dl 0
loc 353
rs 4.08
c 0
b 0
f 0
wmc 59

16 Functions

Rating   Name   Duplication   Size   Complexity  
A to_tuple() 0 19 2
A get_major_sitk_version() 0 6 2
A get_random_seed() 0 7 1
A get_torchio_cache_dir() 0 2 1
A compress() 0 4 3
B guess_type() 0 21 6
B create_dummy_dataset() 0 59 7
A round_up() 0 19 1
B sitk_to_nib() 0 35 6
A check_sequence() 0 6 2
A get_stem() 0 11 2
C ensure_4d() 0 32 10
A history_collate() 0 14 4
A apply_transform_to_file() 0 13 3
B nib_to_sitk() 0 40 8
A get_rotation_and_spacing_from_affine() 0 8 1

How to fix   Complexity   

Complexity

Complex classes like torchio.utils often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import ast
2
import gzip
3
import shutil
4
import tempfile
5
from pathlib import Path
6
from typing import Union, Iterable, Tuple, Any, Optional, List, Sequence
7
8
import torch
9
from torch.utils.data._utils.collate import default_collate
10
import numpy as np
11
import nibabel as nib
12
import SimpleITK as sitk
13
from tqdm import trange
14
15
from .torchio import (
16
    INTENSITY,
17
    TypeData,
18
    TypeNumber,
19
    TypePath,
20
    REPO_URL,
21
)
22
23
24
FLIP_XY = np.diag((-1, -1, 1))  # used to switch between LPS and RAS
25
26
27
def to_tuple(
28
        value: Union[TypeNumber, Iterable[TypeNumber]],
29
        length: int = 1,
30
        ) -> Tuple[TypeNumber, ...]:
31
    """
32
    to_tuple(1, length=1) -> (1,)
33
    to_tuple(1, length=3) -> (1, 1, 1)
34
35
    If value is an iterable, n is ignored and tuple(value) is returned
36
    to_tuple((1,), length=1) -> (1,)
37
    to_tuple((1, 2), length=1) -> (1, 2)
38
    to_tuple([1, 2], length=3) -> (1, 2)
39
    """
40
    try:
41
        iter(value)
42
        value = tuple(value)
43
    except TypeError:
44
        value = length * (value,)
45
    return value
46
47
48
def get_stem(
49
        path: Union[TypePath, List[TypePath]]
50
        ) -> Union[str, List[str]]:
51
    """
52
    '/home/user/image.nii.gz' -> 'image'
53
    """
54
    def _get_stem(path_string):
55
        return Path(path_string).name.split('.')[0]
56
    if isinstance(path, (str, Path)):
57
        return _get_stem(path)
58
    return [_get_stem(p) for p in path]
59
60
61
def create_dummy_dataset(
62
        num_images: int,
63
        size_range: Tuple[int, int],
64
        directory: Optional[TypePath] = None,
65
        suffix: str = '.nii.gz',
66
        force: bool = False,
67
        verbose: bool = False,
68
        ):
69
    from .data import ScalarImage, LabelMap, Subject
70
    output_dir = tempfile.gettempdir() if directory is None else directory
71
    output_dir = Path(output_dir)
72
    images_dir = output_dir / 'dummy_images'
73
    labels_dir = output_dir / 'dummy_labels'
74
75
    if force:
76
        shutil.rmtree(images_dir)
77
        shutil.rmtree(labels_dir)
78
79
    subjects: List[Subject] = []
80
    if images_dir.is_dir():
81
        for i in trange(num_images):
82
            image_path = images_dir / f'image_{i}{suffix}'
83
            label_path = labels_dir / f'label_{i}{suffix}'
84
            subject = Subject(
85
                one_modality=ScalarImage(image_path),
86
                segmentation=LabelMap(label_path),
87
            )
88
            subjects.append(subject)
89
    else:
90
        images_dir.mkdir(exist_ok=True, parents=True)
91
        labels_dir.mkdir(exist_ok=True, parents=True)
92
        if verbose:
93
            print('Creating dummy dataset...')  # noqa: T001
94
            iterable = trange(num_images)
95
        else:
96
            iterable = range(num_images)
97
        for i in iterable:
98
            shape = np.random.randint(*size_range, size=3)
99
            affine = np.eye(4)
100
            image = np.random.rand(*shape)
101
            label = np.ones_like(image)
102
            label[image < 0.33] = 0
103
            label[image > 0.66] = 2
104
            image *= 255
105
106
            image_path = images_dir / f'image_{i}{suffix}'
107
            nii = nib.Nifti1Image(image.astype(np.uint8), affine)
108
            nii.to_filename(str(image_path))
109
110
            label_path = labels_dir / f'label_{i}{suffix}'
111
            nii = nib.Nifti1Image(label.astype(np.uint8), affine)
112
            nii.to_filename(str(label_path))
113
114
            subject = Subject(
115
                one_modality=ScalarImage(image_path),
116
                segmentation=LabelMap(label_path),
117
            )
118
            subjects.append(subject)
119
    return subjects
120
121
122
def apply_transform_to_file(
123
        input_path: TypePath,
124
        transform,  # : Transform seems to create a circular import
125
        output_path: TypePath,
126
        type: str = INTENSITY,  # noqa: A002
127
        verbose: bool = False,
128
        ):
129
    from . import Image, Subject
130
    subject = Subject(image=Image(input_path, type=type))
131
    transformed = transform(subject)
132
    transformed.image.save(output_path)
133
    if verbose and transformed.history:
134
        print(transformed.history[0])  # noqa: T001
135
136
137
def guess_type(string: str) -> Any:
138
    # Adapted from
139
    # https://www.reddit.com/r/learnpython/comments/4599hl/module_to_guess_type_from_a_string/czw3f5s
140
    string = string.replace(' ', '')
141
    try:
142
        value = ast.literal_eval(string)
143
    except ValueError:
144
        result_type = str
145
    else:
146
        result_type = type(value)
147
    if result_type in (list, tuple):
148
        string = string[1:-1]  # remove brackets
149
        split = string.split(',')
150
        list_result = [guess_type(n) for n in split]
151
        value = tuple(list_result) if result_type is tuple else list_result
152
        return value
153
    try:
154
        value = result_type(string)
155
    except TypeError:
156
        value = None
157
    return value
158
159
160
def get_rotation_and_spacing_from_affine(
161
        affine: np.ndarray,
162
        ) -> Tuple[np.ndarray, np.ndarray]:
163
    # From https://github.com/nipy/nibabel/blob/master/nibabel/orientations.py
164
    rotation_zoom = affine[:3, :3]
165
    spacing = np.sqrt(np.sum(rotation_zoom * rotation_zoom, axis=0))
166
    rotation = rotation_zoom / spacing
167
    return rotation, spacing
168
169
170
def nib_to_sitk(
171
        data: TypeData,
172
        affine: TypeData,
173
        squeeze: bool = False,
174
        force_3d: bool = False,
175
        force_4d: bool = False,
176
        ) -> sitk.Image:
177
    """Create a SimpleITK image from a tensor and a 4x4 affine matrix."""
178
    if data.ndim != 4:
179
        raise ValueError(f'Input must be 4D, but has shape {tuple(data.shape)}')
180
    # Possibilities
181
    # (1, w, h, 1)
182
    # (c, w, h, 1)
183
    # (1, w, h, 1)
184
    # (c, w, h, d)
185
    array = np.asarray(data)
186
    affine = np.asarray(affine).astype(np.float64)
187
188
    is_multichannel = array.shape[0] > 1 and not force_4d
189
    is_2d = array.shape[3] == 1 and not force_3d
190
    if is_2d:
191
        array = array[..., 0]
192
    if not is_multichannel and not force_4d:
193
        array = array[0]
194
    array = array.transpose()  # (W, H, D, C) or (W, H, D)
195
    image = sitk.GetImageFromArray(array, isVector=is_multichannel)
196
197
    rotation, spacing = get_rotation_and_spacing_from_affine(affine)
198
    origin = np.dot(FLIP_XY, affine[:3, 3])
199
    direction = np.dot(FLIP_XY, rotation)
200
    if is_2d:  # ignore first dimension if 2D (1, W, H, 1)
201
        direction = direction[:2, :2]
202
    image.SetOrigin(origin)  # should I add a 4th value if force_4d?
203
    image.SetSpacing(spacing)
204
    image.SetDirection(direction.flatten())
205
    if data.ndim == 4:
206
        assert image.GetNumberOfComponentsPerPixel() == data.shape[0]
207
    num_spatial_dims = 2 if is_2d else 3
208
    assert image.GetSize() == data.shape[1: 1 + num_spatial_dims]
209
    return image
210
211
212
def sitk_to_nib(
213
        image: sitk.Image,
214
        keepdim: bool = False,
215
        ) -> Tuple[np.ndarray, np.ndarray]:
216
    data = sitk.GetArrayFromImage(image).transpose()
217
    num_components = image.GetNumberOfComponentsPerPixel()
218
    if num_components == 1:
219
        data = data[np.newaxis]  # add channels dimension
220
    input_spatial_dims = image.GetDimension()
221
    if input_spatial_dims == 2:
222
        data = data[..., np.newaxis]
223
    if not keepdim:
224
        data = ensure_4d(data, num_spatial_dims=input_spatial_dims)
225
    assert data.shape[0] == num_components
226
    assert data.shape[1: 1 + input_spatial_dims] == image.GetSize()
227
    spacing = np.array(image.GetSpacing())
228
    direction = np.array(image.GetDirection())
229
    origin = image.GetOrigin()
230
    if len(direction) == 9:
231
        rotation = direction.reshape(3, 3)
232
    elif len(direction) == 4:  # ignore first dimension if 2D (1, W, H, 1)
233
        rotation_2d = direction.reshape(2, 2)
234
        rotation = np.eye(3)
235
        rotation[:2, :2] = rotation_2d
236
        spacing = *spacing, 1
237
        origin = *origin, 0
238
    else:
239
        raise RuntimeError(f'Direction not understood: {direction}')
240
    rotation = np.dot(FLIP_XY, rotation)
241
    rotation_zoom = rotation * spacing
242
    translation = np.dot(FLIP_XY, origin)
243
    affine = np.eye(4)
244
    affine[:3, :3] = rotation_zoom
245
    affine[:3, 3] = translation
246
    return data, affine
247
248
249
def ensure_4d(tensor: TypeData, num_spatial_dims=None) -> TypeData:
250
    # I wish named tensors were properly supported in PyTorch
251
    num_dimensions = tensor.ndim
252
    if num_dimensions == 4:
253
        pass
254
    elif num_dimensions == 5:  # hope (X, X, X, 1, X)
255
        if tensor.shape[-1] == 1:
256
            tensor = tensor[..., 0, :]
257
    elif num_dimensions == 2:  # assume 2D monochannel (W, H)
258
        tensor = tensor[np.newaxis, ..., np.newaxis]  # (1, W, H, 1)
259
    elif num_dimensions == 3:  # 2D multichannel or 3D monochannel?
260
        if num_spatial_dims == 2:
261
            tensor = tensor[..., np.newaxis]  # (C, W, H, 1)
262
        elif num_spatial_dims == 3:  # (W, H, D)
263
            tensor = tensor[np.newaxis]  # (1, W, H, D)
264
        else:  # try to guess
265
            shape = tensor.shape
266
            maybe_rgb = 3 in (shape[0], shape[-1])
267
            if maybe_rgb:
268
                if shape[-1] == 3:  # (W, H, 3)
269
                    tensor = tensor.permute(2, 0, 1)  # (3, W, H)
270
                tensor = tensor[..., np.newaxis]  # (3, W, H, 1)
271
            else:  # (W, H, D)
272
                tensor = tensor[np.newaxis]  # (1, W, H, D)
273
    else:
274
        message = (
275
            f'{num_dimensions}D images not supported yet. Please create an'
276
            f' issue in {REPO_URL} if you would like support for them'
277
        )
278
        raise ValueError(message)
279
    assert tensor.ndim == 4
280
    return tensor
281
282
283
def get_torchio_cache_dir():
284
    return Path('~/.cache/torchio').expanduser()
285
286
287
def round_up(value: float) -> int:
288
    """Round half towards infinity.
289
290
    Args:
291
        value: The value to round.
292
293
    Example:
294
295
        >>> round(2.5)
296
        2
297
        >>> round(3.5)
298
        4
299
        >>> round_up(2.5)
300
        3
301
        >>> round_up(3.5)
302
        4
303
304
    """
305
    return int(np.floor(value + 0.5))
306
307
308
def compress(input_path, output_path):
309
    with open(input_path, 'rb') as f_in:
310
        with gzip.open(output_path, 'wb') as f_out:
311
            shutil.copyfileobj(f_in, f_out)
312
313
314
def check_sequence(sequence: Sequence, name: str):
315
    try:
316
        iter(sequence)
317
    except TypeError:
318
        message = f'"{name}" must be a sequence, not {type(name)}'
319
        raise TypeError(message)
320
321
322
def get_random_seed():
323
    """Generate a random seed.
324
325
    Returns:
326
        A random seed as an int
327
    """
328
    return torch.randint(0, 2**31, (1,)).item()
329
330
331
def get_major_sitk_version() -> int:
332
    # This attribute was added in version 2
333
    # https://github.com/SimpleITK/SimpleITK/pull/1171
334
    version = getattr(sitk, '__version__', None)
335
    major_version = 1 if version is None else 2
336
    return major_version
337
338
339
def history_collate(batch: Sequence, collate_transforms=True):
340
    attr = 'history' if collate_transforms else 'applied_transforms'
341
    # Adapted from
342
    # https://github.com/romainVala/torchQC/blob/master/segmentation/collate_functions.py
343
    from .data import Subject
344
    first_element = batch[0]
345
    if isinstance(first_element, Subject):
346
        dictionary = {
347
            key: default_collate([d[key] for d in batch])
348
            for key in first_element
349
        }
350
        if hasattr(first_element, attr):
351
            dictionary.update({attr: [getattr(d, attr) for d in batch]})
352
        return dictionary
353