Skip to content

Commit

Permalink
wip: initiate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Jul 23, 2024
1 parent a366f85 commit 0b2302f
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions nitransforms/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
"""Resampling utilities."""
from warnings import warn
from pathlib import Path
import numpy as np
from nibabel.loadsave import load as _nbload
Expand All @@ -19,6 +20,9 @@
_as_homogeneous,
)

SERIALIZE_MIN_NUM_VOLUMES : int = 8
"""Minimum number of volumes to automatically serialize 4D transforms."""


def apply(
transform,
Expand All @@ -29,6 +33,8 @@ def apply(
cval=0.0,
prefilter=True,
output_dtype=None,
serialize_nvols=SERIALIZE_MIN_NUM_VOLUMES,
njobs=None,
):
"""
Apply a transformation to an image, resampling on the reference spatial object.
Expand Down Expand Up @@ -89,14 +95,20 @@ def apply(
spatialimage = _nbload(str(spatialimage))

data = np.asanyarray(spatialimage.dataobj)
data_nvols = 1 if data.ndim < 4 else data.shape[-1]
xfm_nvols = len(transforms)

if data.ndim == 4 and data.shape[-1] != len(transform):
if data_nvols == 1 and xfm_nvols > 1:
data = data[..., np.newaxis]
elif data_nvols != xfm_nvols:
raise ValueError(
"The fourth dimension of the data does not match the tranform's shape."
)

if data.ndim < transform.ndim:
data = data[..., np.newaxis]
serialize_nvols = serialize_nvols if serialize_nvols and serialize_nvols > 1 else np.inf
serialize_4d = max(data_nvols, xfm_nvols) > serialize_nvols
if serialize_4d:
warn("4D transforms serialization into 3D+t not implemented")

# For model-based nonlinear transforms, generate the corresponding dense field
if hasattr(transform, "to_field") and callable(transform.to_field):
Expand Down

0 comments on commit 0b2302f

Please sign in to comment.