11
11
from os import cpu_count
12
12
from concurrent .futures import ProcessPoolExecutor , as_completed
13
13
from pathlib import Path
14
+ from typing import Tuple
15
+
14
16
import numpy as np
15
17
from nibabel .loadsave import load as _nbload
16
18
from nibabel .arrayproxy import get_obj_dtype
19
+ from nibabel .spatialimages import SpatialImage
17
20
from scipy import ndimage as ndi
18
21
19
22
from nitransforms .base import (
20
23
ImageGrid ,
24
+ TransformBase ,
21
25
TransformError ,
22
26
SpatialReference ,
23
27
_as_homogeneous ,
28
32
29
33
30
34
def _apply_volume (
31
- index ,
32
- data ,
33
- targets ,
34
- order = 3 ,
35
- mode = "constant" ,
36
- cval = 0.0 ,
37
- prefilter = True ,
38
- ):
35
+ index : int ,
36
+ data : np .ndarray ,
37
+ targets : np .ndarray ,
38
+ order : int = 3 ,
39
+ mode : str = "constant" ,
40
+ cval : float = 0.0 ,
41
+ prefilter : bool = True ,
42
+ ) -> Tuple [int , np .ndarray ]:
43
+ """
44
+ Decorate :obj:`~scipy.ndimage.map_coordinates` to return an order index for parallelization.
45
+
46
+ Parameters
47
+ ----------
48
+ index : :obj:`int`
49
+ The index of the volume to apply the interpolation to.
50
+ data : :obj:`~numpy.ndarray`
51
+ The input data array.
52
+ targets : :obj:`~numpy.ndarray`
53
+ The target coordinates for mapping.
54
+ order : :obj:`int`, optional
55
+ The order of the spline interpolation, default is 3.
56
+ The order has to be in the range 0-5.
57
+ mode : :obj:`str`, optional
58
+ Determines how the input image is extended when the resamplings overflows
59
+ a border. One of ``'constant'``, ``'reflect'``, ``'nearest'``, ``'mirror'``,
60
+ or ``'wrap'``. Default is ``'constant'``.
61
+ cval : :obj:`float`, optional
62
+ Constant value for ``mode='constant'``. Default is 0.0.
63
+ prefilter: :obj:`bool`, optional
64
+ Determines if the image's data array is prefiltered with
65
+ a spline filter before interpolation. The default is ``True``,
66
+ which will create a temporary *float64* array of filtered values
67
+ if *order > 1*. If setting this to ``False``, the output will be
68
+ slightly blurred if *order > 1*, unless the input is prefiltered,
69
+ i.e. it is the result of calling the spline filter on the original
70
+ input.
71
+
72
+ Returns
73
+ -------
74
+ (:obj:`int`, :obj:`~numpy.ndarray`)
75
+ The index and the array resulting from the interpolation.
76
+
77
+ """
39
78
return index , ndi .map_coordinates (
40
79
data ,
41
80
targets ,
@@ -47,45 +86,46 @@ def _apply_volume(
47
86
48
87
49
88
def apply (
50
- transform ,
51
- spatialimage ,
52
- reference = None ,
53
- order = 3 ,
54
- mode = "constant" ,
55
- cval = 0.0 ,
56
- prefilter = True ,
57
- output_dtype = None ,
58
- serialize_nvols = SERIALIZE_VOLUME_WINDOW_WIDTH ,
59
- njobs = None ,
60
- ):
89
+ transform : TransformBase ,
90
+ spatialimage : str | Path | SpatialImage ,
91
+ reference : str | Path | SpatialImage = None ,
92
+ order : int = 3 ,
93
+ mode : str = "constant" ,
94
+ cval : float = 0.0 ,
95
+ prefilter : bool = True ,
96
+ output_dtype : np . dtype = None ,
97
+ serialize_nvols : int = SERIALIZE_VOLUME_WINDOW_WIDTH ,
98
+ njobs : int = None ,
99
+ ) -> SpatialImage | np . ndarray :
61
100
"""
62
101
Apply a transformation to an image, resampling on the reference spatial object.
63
102
64
103
Parameters
65
104
----------
66
- spatialimage : `spatialimage `
105
+ spatialimage : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike `
67
106
The image object containing the data to be resampled in reference
68
107
space
69
- reference : spatial object, optional
108
+ reference : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike`
70
109
The image, surface, or combination thereof containing the coordinates
71
110
of samples that will be sampled.
72
- order : int, optional
111
+ order : :obj:` int` , optional
73
112
The order of the spline interpolation, default is 3.
74
113
The order has to be in the range 0-5.
75
- mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'} , optional
114
+ mode : :obj:`str` , optional
76
115
Determines how the input image is extended when the resamplings overflows
77
- a border. Default is 'constant'.
78
- cval : float, optional
116
+ a border. One of ``'constant'``, ``'reflect'``, ``'nearest'``, ``'mirror'``,
117
+ or ``'wrap'``. Default is ``'constant'``.
118
+ cval : :obj:`float`, optional
79
119
Constant value for ``mode='constant'``. Default is 0.0.
80
- prefilter: bool, optional
120
+ prefilter: :obj:` bool` , optional
81
121
Determines if the image's data array is prefiltered with
82
122
a spline filter before interpolation. The default is ``True``,
83
123
which will create a temporary *float64* array of filtered values
84
124
if *order > 1*. If setting this to ``False``, the output will be
85
125
slightly blurred if *order > 1*, unless the input is prefiltered,
86
126
i.e. it is the result of calling the spline filter on the original
87
127
input.
88
- output_dtype: dtype specifier , optional
128
+ output_dtype: :obj:`~numpy. dtype` , optional
89
129
The dtype of the returned array or image, if specified.
90
130
If ``None``, the default behavior is to use the effective dtype of
91
131
the input image. If slope and/or intercept are defined, the effective
@@ -97,7 +137,7 @@ def apply(
97
137
98
138
Returns
99
139
-------
100
- resampled : `spatialimage ` or ndarray
140
+ resampled : :obj:`~nibabel.spatialimages.SpatialImage ` or :obj:`~numpy. ndarray`
101
141
The data imaged after resampling to reference space.
102
142
103
143
"""
0 commit comments