Skip to content

Commit 7dcc78d

Browse files
committed
sty: add type annotations
1 parent 026a10a commit 7dcc78d

File tree

1 file changed

+68
-28
lines changed

1 file changed

+68
-28
lines changed

nitransforms/resampling.py

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
from os import cpu_count
1212
from concurrent.futures import ProcessPoolExecutor, as_completed
1313
from pathlib import Path
14+
from typing import Tuple
15+
1416
import numpy as np
1517
from nibabel.loadsave import load as _nbload
1618
from nibabel.arrayproxy import get_obj_dtype
19+
from nibabel.spatialimages import SpatialImage
1720
from scipy import ndimage as ndi
1821

1922
from nitransforms.base import (
2023
ImageGrid,
24+
TransformBase,
2125
TransformError,
2226
SpatialReference,
2327
_as_homogeneous,
@@ -28,14 +32,49 @@
2832

2933

3034
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+
"""
3978
return index, ndi.map_coordinates(
4079
data,
4180
targets,
@@ -47,45 +86,46 @@ def _apply_volume(
4786

4887

4988
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:
61100
"""
62101
Apply a transformation to an image, resampling on the reference spatial object.
63102
64103
Parameters
65104
----------
66-
spatialimage : `spatialimage`
105+
spatialimage : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike`
67106
The image object containing the data to be resampled in reference
68107
space
69-
reference : spatial object, optional
108+
reference : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike`
70109
The image, surface, or combination thereof containing the coordinates
71110
of samples that will be sampled.
72-
order : int, optional
111+
order : :obj:`int`, optional
73112
The order of the spline interpolation, default is 3.
74113
The order has to be in the range 0-5.
75-
mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'}, optional
114+
mode : :obj:`str`, optional
76115
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
79119
Constant value for ``mode='constant'``. Default is 0.0.
80-
prefilter: bool, optional
120+
prefilter: :obj:`bool`, optional
81121
Determines if the image's data array is prefiltered with
82122
a spline filter before interpolation. The default is ``True``,
83123
which will create a temporary *float64* array of filtered values
84124
if *order > 1*. If setting this to ``False``, the output will be
85125
slightly blurred if *order > 1*, unless the input is prefiltered,
86126
i.e. it is the result of calling the spline filter on the original
87127
input.
88-
output_dtype: dtype specifier, optional
128+
output_dtype: :obj:`~numpy.dtype`, optional
89129
The dtype of the returned array or image, if specified.
90130
If ``None``, the default behavior is to use the effective dtype of
91131
the input image. If slope and/or intercept are defined, the effective
@@ -97,7 +137,7 @@ def apply(
97137
98138
Returns
99139
-------
100-
resampled : `spatialimage` or ndarray
140+
resampled : :obj:`~nibabel.spatialimages.SpatialImage` or :obj:`~numpy.ndarray`
101141
The data imaged after resampling to reference space.
102142
103143
"""

0 commit comments

Comments
 (0)