Skip to content

Commit a659355

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

File tree

1 file changed

+61
-28
lines changed

1 file changed

+61
-28
lines changed

nitransforms/resampling.py

+61-28
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,42 @@
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+
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+
"""
3971
return index, ndi.map_coordinates(
4072
data,
4173
targets,
@@ -47,45 +79,46 @@ def _apply_volume(
4779

4880

4981
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:
6193
"""
6294
Apply a transformation to an image, resampling on the reference spatial object.
6395
6496
Parameters
6597
----------
66-
spatialimage : `spatialimage`
98+
spatialimage : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike`
6799
The image object containing the data to be resampled in reference
68100
space
69-
reference : spatial object, optional
101+
reference : :obj:`~nibabel.spatialimages.SpatialImage` or `os.pathlike`
70102
The image, surface, or combination thereof containing the coordinates
71103
of samples that will be sampled.
72-
order : int, optional
104+
order : :obj:`int`, optional
73105
The order of the spline interpolation, default is 3.
74106
The order has to be in the range 0-5.
75-
mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'}, optional
107+
mode : :obj:`str`, optional
76108
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
79112
Constant value for ``mode='constant'``. Default is 0.0.
80-
prefilter: bool, optional
113+
prefilter: :obj:`bool`, optional
81114
Determines if the image's data array is prefiltered with
82115
a spline filter before interpolation. The default is ``True``,
83116
which will create a temporary *float64* array of filtered values
84117
if *order > 1*. If setting this to ``False``, the output will be
85118
slightly blurred if *order > 1*, unless the input is prefiltered,
86119
i.e. it is the result of calling the spline filter on the original
87120
input.
88-
output_dtype: dtype specifier, optional
121+
output_dtype: :obj:`~numpy.dtype`, optional
89122
The dtype of the returned array or image, if specified.
90123
If ``None``, the default behavior is to use the effective dtype of
91124
the input image. If slope and/or intercept are defined, the effective
@@ -97,7 +130,7 @@ def apply(
97130
98131
Returns
99132
-------
100-
resampled : `spatialimage` or ndarray
133+
resampled : :obj:`~nibabel.spatialimages.SpatialImage` or :obj:`~numpy.ndarray`
101134
The data imaged after resampling to reference space.
102135
103136
"""

0 commit comments

Comments
 (0)