resampling.py 3.37 KB
Newer Older
mibaumgartner's avatar
mibaumgartner committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Copyright 2020 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import numpy as np

import nnunet.preprocessing.preprocessing as nn_preprocessing


def resize_segmentation(segmentation, new_shape, order=3, cval=0):
    """
    Resizes a segmentation map. Supports all orders (see skimage documentation). Will transform segmentation map to one
    hot encoding which is resized and transformed back to a segmentation map.
    This prevents interpolation artifacts ([0, 0, 2] -> [0, 1, 2])
    """
    return nn_preprocessing.resize_segmentation(
        segmentation=segmentation, new_shape=new_shape, order=order, cval=cval)


def get_do_separate_z(spacing, anisotropy_threshold: float = 3):
    return nn_preprocessing.get_do_separate_z(spacing=spacing, anisotropy_threshold=anisotropy_threshold)


def get_lowres_axis(new_spacing):
    return nn_preprocessing.get_lowres_axis(new_spacing=new_spacing)


def resample_patient(data,
                     seg,
                     original_spacing,
                     target_spacing,
                     order_data=3,
                     order_seg=0,
                     force_separate_z=False,
                     cval_data=0,
                     cval_seg=-1,
                     order_z_data=0, 
                     order_z_seg=0,
                     separate_z_anisotropy_threshold: float = 3,
                     ):
    return nn_preprocessing.resample_patient(data=data, seg=seg, original_spacing=original_spacing,
                                             target_spacing=target_spacing, order_data=order_data,
                                             order_seg=order_seg, force_separate_z=force_separate_z,
                                             cval_data=cval_data, cval_seg=cval_seg, order_z_data=order_z_data,
                                             order_z_seg=order_z_seg,
                                             separate_z_anisotropy_threshold=separate_z_anisotropy_threshold)


def resample_data_or_seg(data, new_shape, is_seg, axis=None, order=3,
                         do_separate_z=False, cval=0, order_z=0) -> np.ndarray:
    """
    Resample data or segmentation

    Args:
        data: array to resample [C, dims]
        new_shape: define new dims (without channels)
        is_seg: changes the resampling strategy
        axis: anisotropic axis, different resampling order used here
        order: order of resampling along the isotropic axis
        do_separate_z: Different resampling along z dimensions
        cval: //
        order_z: if separate z resampling is done then this is the order for resampling in z

    Returns:
        np.ndarray: resampled array
    """
    return nn_preprocessing.resample_data_or_seg(
        data=data, new_shape=new_shape, is_seg=is_seg, axis=axis,
        order=order, do_separate_z=do_separate_z, cval=cval, order_z=order_z)