Skip to content

Commit 86b3d11

Browse files
committed
wip: initiate implementation
1 parent ed92d4e commit 86b3d11

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

nitransforms/resampling.py

Lines changed: 22 additions & 3 deletions
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,12 @@
1920
_as_homogeneous,
2021
)
2122

23+
SERIALIZE_VOLUME_WINDOW_WIDTH : int = 8
24+
"""Minimum number of volumes to automatically serialize 4D transforms."""
25+
26+
class NotImplementedWarning(UserWarning):
27+
"""A custom class for warnings."""
28+
2229

2330
def apply(
2431
transform,
@@ -29,6 +36,8 @@ def apply(
2936
cval=0.0,
3037
prefilter=True,
3138
output_dtype=None,
39+
serialize_nvols=SERIALIZE_VOLUME_WINDOW_WIDTH,
40+
njobs=None,
3241
):
3342
"""
3443
Apply a transformation to an image, resampling on the reference spatial object.
@@ -89,14 +98,24 @@ def apply(
8998
spatialimage = _nbload(str(spatialimage))
9099

91100
data = np.asanyarray(spatialimage.dataobj)
101+
data_nvols = 1 if data.ndim < 4 else data.shape[-1]
102+
xfm_nvols = len(transform)
92103

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

98-
if data.ndim < transform.ndim:
99-
data = data[..., np.newaxis]
111+
serialize_nvols = serialize_nvols if serialize_nvols and serialize_nvols > 1 else np.inf
112+
serialize_4d = max(data_nvols, xfm_nvols) > serialize_nvols
113+
if serialize_4d:
114+
warn(
115+
"4D transforms serialization into 3D+t not implemented",
116+
NotImplementedWarning,
117+
stacklevel=2,
118+
)
100119

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

0 commit comments

Comments
 (0)