7
7
#
8
8
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9
9
"""Resampling utilities."""
10
+ from warnings import warn
10
11
from pathlib import Path
11
12
import numpy as np
12
13
from nibabel .loadsave import load as _nbload
19
20
_as_homogeneous ,
20
21
)
21
22
23
+ SERIALIZE_VOLUME_WINDOW_WIDTH : int = 8
24
+ """Minimum number of volumes to automatically serialize 4D transforms."""
25
+
22
26
23
27
def apply (
24
28
transform ,
@@ -29,6 +33,8 @@ def apply(
29
33
cval = 0.0 ,
30
34
prefilter = True ,
31
35
output_dtype = None ,
36
+ serialize_nvols = SERIALIZE_VOLUME_WINDOW_WIDTH ,
37
+ njobs = None ,
32
38
):
33
39
"""
34
40
Apply a transformation to an image, resampling on the reference spatial object.
@@ -89,14 +95,20 @@ def apply(
89
95
spatialimage = _nbload (str (spatialimage ))
90
96
91
97
data = np .asanyarray (spatialimage .dataobj )
98
+ data_nvols = 1 if data .ndim < 4 else data .shape [- 1 ]
99
+ xfm_nvols = len (transforms )
92
100
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 :
94
104
raise ValueError (
95
105
"The fourth dimension of the data does not match the tranform's shape."
96
106
)
97
107
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" )
100
112
101
113
# For model-based nonlinear transforms, generate the corresponding dense field
102
114
if hasattr (transform , "to_field" ) and callable (transform .to_field ):
0 commit comments