resampling.py 3.28 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
"""
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

19
20
21
22
from nndet.utils.info import SuppressPrint

with SuppressPrint():
    import nnunet.preprocessing.preprocessing as nn_preprocessing
mibaumgartner's avatar
mibaumgartner committed
23
24


Baumgartner, Michael's avatar
bump  
Baumgartner, Michael committed
25
def resize_segmentation(segmentation, new_shape, order=3):
mibaumgartner's avatar
mibaumgartner committed
26
27
28
29
30
31
    """
    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(
Baumgartner, Michael's avatar
bump  
Baumgartner, Michael committed
32
        segmentation=segmentation, new_shape=new_shape, order=order)
mibaumgartner's avatar
mibaumgartner committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56


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,
                     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,
Baumgartner, Michael's avatar
bump  
Baumgartner, Michael committed
57
                                             order_z_data=order_z_data,
mibaumgartner's avatar
mibaumgartner committed
58
59
60
61
62
                                             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,
Baumgartner, Michael's avatar
bump  
Baumgartner, Michael committed
63
                         do_separate_z=False, order_z=0) -> np.ndarray:
mibaumgartner's avatar
mibaumgartner committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    """
    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
        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,
Baumgartner, Michael's avatar
bump  
Baumgartner, Michael committed
81
        order=order, do_separate_z=do_separate_z, order_z=order_z)