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
+
26
+ class NotImplementedWarning (UserWarning ):
27
+ """A custom class for warnings."""
28
+
22
29
23
30
def apply (
24
31
transform ,
@@ -29,6 +36,8 @@ def apply(
29
36
cval = 0.0 ,
30
37
prefilter = True ,
31
38
output_dtype = None ,
39
+ serialize_nvols = SERIALIZE_VOLUME_WINDOW_WIDTH ,
40
+ njobs = None ,
32
41
):
33
42
"""
34
43
Apply a transformation to an image, resampling on the reference spatial object.
@@ -89,14 +98,24 @@ def apply(
89
98
spatialimage = _nbload (str (spatialimage ))
90
99
91
100
data = np .asanyarray (spatialimage .dataobj )
101
+ data_nvols = 1 if data .ndim < 4 else data .shape [- 1 ]
102
+ xfm_nvols = len (transform )
92
103
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 :
94
107
raise ValueError (
95
108
"The fourth dimension of the data does not match the tranform's shape."
96
109
)
97
110
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
+ )
100
119
101
120
# For model-based nonlinear transforms, generate the corresponding dense field
102
121
if hasattr (transform , "to_field" ) and callable (transform .to_field ):
0 commit comments