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
+ mode : :obj:`str`, optional
57
+ Points outside the boundaries of the input are filled according to the
58
+ given mode ('constant', 'nearest', 'reflect', 'mirror', 'wrap') (default is 'constant').
59
+ cval : :obj:`float`, optional
60
+ Value used for points outside the boundaries of the input if mode='constant' (default is 0.0).
61
+ prefilter : :obj:`bool`, optional
62
+ Determines if the input array is prefiltered with a spline filter before interpolation
63
+ (default is True).
64
+
65
+ Returns
66
+ -------
67
+ (:obj:`int`, :obj:`~numpy.ndarray`)
68
+ The index and the array resulting from the interpolation.
69
+
70
+ """
39
71
return index , ndi .map_coordinates (
40
72
data ,
41
73
targets ,
@@ -47,45 +79,46 @@ def _apply_volume(
47
79
48
80
49
81
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
- ):
82
+ transform : TransformBase ,
83
+ spatialimage : str | Path | SpatialImage ,
84
+ reference : str | Path | SpatialImage = None ,
85
+ order : int = 3 ,
86
+ mode : str = "constant" ,
87
+ cval : float = 0.0 ,
88
+ prefilter : bool = True ,
89
+ output_dtype : np . dtype = None ,
90
+ serialize_nvols : int = SERIALIZE_VOLUME_WINDOW_WIDTH ,
91
+ njobs : int = None ,
92
+ ) -> SpatialImage | np . ndarray :
61
93
"""
62
94
Apply a transformation to an image, resampling on the reference spatial object.
63
95
64
96
Parameters
65
97
----------
66
- spatialimage : `spatialimage `
98
+ spatialimage : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike `
67
99
The image object containing the data to be resampled in reference
68
100
space
69
- reference : spatial object, optional
101
+ reference : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike`
70
102
The image, surface, or combination thereof containing the coordinates
71
103
of samples that will be sampled.
72
- order : int, optional
104
+ order : :obj:` int` , optional
73
105
The order of the spline interpolation, default is 3.
74
106
The order has to be in the range 0-5.
75
- mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'} , optional
107
+ mode : :obj:`str` , optional
76
108
Determines how the input image is extended when the resamplings overflows
77
- a border. Default is 'constant'.
78
- cval : float, optional
109
+ a border. One of ``'constant'``, ``'reflect'``, ``'nearest'``, ``'mirror'``,
110
+ or ``'wrap'``. Default is ``'constant'``.
111
+ cval : :obj:`float`, optional
79
112
Constant value for ``mode='constant'``. Default is 0.0.
80
- prefilter: bool, optional
113
+ prefilter: :obj:` bool` , optional
81
114
Determines if the image's data array is prefiltered with
82
115
a spline filter before interpolation. The default is ``True``,
83
116
which will create a temporary *float64* array of filtered values
84
117
if *order > 1*. If setting this to ``False``, the output will be
85
118
slightly blurred if *order > 1*, unless the input is prefiltered,
86
119
i.e. it is the result of calling the spline filter on the original
87
120
input.
88
- output_dtype: dtype specifier , optional
121
+ output_dtype: :obj:`~numpy. dtype` , optional
89
122
The dtype of the returned array or image, if specified.
90
123
If ``None``, the default behavior is to use the effective dtype of
91
124
the input image. If slope and/or intercept are defined, the effective
@@ -97,7 +130,7 @@ def apply(
97
130
98
131
Returns
99
132
-------
100
- resampled : `spatialimage ` or ndarray
133
+ resampled : :obj:`~nibabel.spatialimages.SpatialImage ` or :obj:`~numpy. ndarray`
101
134
The data imaged after resampling to reference space.
102
135
103
136
"""
0 commit comments