Skip to content

Commit b922fa5

Browse files
committed
wip: initiate implementation
1 parent a366f85 commit b922fa5

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

nitransforms/resampling.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Resampling utilities."""
10+
from warnings import warn
1011
from pathlib import Path
1112
import numpy as np
1213
from nibabel.loadsave import load as _nbload
@@ -19,6 +20,9 @@
1920
_as_homogeneous,
2021
)
2122

23+
SERIALIZE_VOLUME_WINDOW_WIDTH : int = 8
24+
"""Minimum number of volumes to automatically serialize 4D transforms."""
25+
2226

2327
def apply(
2428
transform,
@@ -29,6 +33,8 @@ def apply(
2933
cval=0.0,
3034
prefilter=True,
3135
output_dtype=None,
36+
serialize_nvols=SERIALIZE_VOLUME_WINDOW_WIDTH,
37+
njobs=None,
3238
):
3339
"""
3440
Apply a transformation to an image, resampling on the reference spatial object.
@@ -89,14 +95,20 @@ def apply(
8995
spatialimage = _nbload(str(spatialimage))
9096

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

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

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

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

0 commit comments

Comments
 (0)