-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0027d1b
commit d865ec5
Showing
2 changed files
with
114 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
# | ||
# See COPYING file distributed along with the NiBabel package for the | ||
# copyright and license terms. | ||
# | ||
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ## | ||
"""Resampling utilities.""" | ||
from pathlib import Path | ||
import numpy as np | ||
import h5py | ||
import warnings | ||
from nibabel.loadsave import load as _nbload | ||
from nibabel import funcs as _nbfuncs | ||
from nibabel.nifti1 import intent_codes as INTENT_CODES | ||
from nibabel.cifti2 import Cifti2Image | ||
from scipy import ndimage as ndi | ||
|
||
|
||
def apply( | ||
transform, | ||
spatialimage, | ||
reference=None, | ||
order=3, | ||
mode="constant", | ||
cval=0.0, | ||
prefilter=True, | ||
output_dtype=None, | ||
): | ||
""" | ||
Apply a transformation to an image, resampling on the reference spatial object. | ||
Parameters | ||
---------- | ||
spatialimage : `spatialimage` | ||
The image object containing the data to be resampled in reference | ||
space | ||
reference : spatial object, optional | ||
The image, surface, or combination thereof containing the coordinates | ||
of samples that will be sampled. | ||
order : int, optional | ||
The order of the spline interpolation, default is 3. | ||
The order has to be in the range 0-5. | ||
mode : {'constant', 'reflect', 'nearest', 'mirror', 'wrap'}, optional | ||
Determines how the input image is extended when the resamplings overflows | ||
a border. Default is 'constant'. | ||
cval : float, optional | ||
Constant value for ``mode='constant'``. Default is 0.0. | ||
prefilter: bool, optional | ||
Determines if the image's data array is prefiltered with | ||
a spline filter before interpolation. The default is ``True``, | ||
which will create a temporary *float64* array of filtered values | ||
if *order > 1*. If setting this to ``False``, the output will be | ||
slightly blurred if *order > 1*, unless the input is prefiltered, | ||
i.e. it is the result of calling the spline filter on the original | ||
input. | ||
output_dtype: dtype specifier, optional | ||
The dtype of the returned array or image, if specified. | ||
If ``None``, the default behavior is to use the effective dtype of | ||
the input image. If slope and/or intercept are defined, the effective | ||
dtype is float64, otherwise it is equivalent to the input image's | ||
``get_data_dtype()`` (on-disk type). | ||
If ``reference`` is defined, then the return value is an image, with | ||
a data array of the effective dtype but with the on-disk dtype set to | ||
the input image's on-disk dtype. | ||
Returns | ||
------- | ||
resampled : `spatialimage` or ndarray | ||
The data imaged after resampling to reference space. | ||
""" | ||
if reference is not None and isinstance(reference, (str, Path)): | ||
reference = _nbload(str(reference)) | ||
|
||
_ref = ( | ||
transform.reference if reference is None else SpatialReference.factory(reference) | ||
) | ||
|
||
if _ref is None: | ||
raise TransformError("Cannot apply transform without reference") | ||
|
||
if isinstance(spatialimage, (str, Path)): | ||
spatialimage = _nbload(str(spatialimage)) | ||
|
||
data = np.asanyarray(spatialimage.dataobj) | ||
targets = ImageGrid(spatialimage).index( # data should be an image | ||
_as_homogeneous(transform.map(_ref.ndcoords.T), dim=_ref.ndim) | ||
) | ||
|
||
resampled = ndi.map_coordinates( | ||
data, | ||
targets.T, | ||
output=output_dtype, | ||
order=order, | ||
mode=mode, | ||
cval=cval, | ||
prefilter=prefilter, | ||
) | ||
|
||
if isinstance(_ref, ImageGrid): # If reference is grid, reshape | ||
hdr = None | ||
if _ref.header is not None: | ||
hdr = _ref.header.copy() | ||
hdr.set_data_dtype(output_dtype or spatialimage.get_data_dtype()) | ||
moved = spatialimage.__class__( | ||
resampled.reshape(_ref.shape), | ||
_ref.affine, | ||
hdr, | ||
) | ||
return moved | ||
|
||
return resampled |