|
8 | 8 | ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
|
9 | 9 | """Resampling utilities."""
|
10 | 10 |
|
| 11 | +from functools import partial |
11 | 12 | from pathlib import Path
|
12 | 13 | import numpy as np
|
13 | 14 | from nibabel.loadsave import load as _nbload
|
@@ -135,33 +136,37 @@ def apply(
|
135 | 136 | else None
|
136 | 137 | )
|
137 | 138 |
|
138 |
| - # Order F ensures individual volumes are contiguous in memory |
139 |
| - # Also matches NIfTI, making final save more efficient |
140 |
| - resampled = np.zeros( |
141 |
| - (len(ref_ndcoords), len(transform)), dtype=input_dtype, order="F" |
| 139 | + map_coordinates = partial( |
| 140 | + ndi.map_coordinates, |
| 141 | + order=order, |
| 142 | + mode=mode, |
| 143 | + cval=cval, |
| 144 | + prefilter=prefilter, |
142 | 145 | )
|
143 | 146 |
|
144 |
| - for t in range(n_resamplings): |
145 |
| - xfm_t = transform if n_resamplings == 1 else transform[t] |
| 147 | + def _apply_volume(index, data, transform, targets=None): |
| 148 | + xfm_t = transform if n_resamplings == 1 else transform[index] |
146 | 149 |
|
147 | 150 | if targets is None:
|
148 | 151 | targets = ImageGrid(spatialimage).index( # data should be an image
|
149 | 152 | _as_homogeneous(xfm_t.map(ref_ndcoords), dim=_ref.ndim)
|
150 | 153 | )
|
151 | 154 |
|
152 |
| - # Interpolate |
153 |
| - resampled[..., t] = ndi.map_coordinates( |
154 |
| - ( |
155 |
| - data |
156 |
| - if data is not None |
157 |
| - else spatialimage.dataobj[..., t].astype(input_dtype, copy=False) |
158 |
| - ), |
159 |
| - targets, |
160 |
| - order=order, |
161 |
| - mode=mode, |
162 |
| - cval=cval, |
163 |
| - prefilter=prefilter, |
| 155 | + data_t = ( |
| 156 | + data |
| 157 | + if data is not None |
| 158 | + else spatialimage.dataobj[..., index].astype(input_dtype, copy=False) |
164 | 159 | )
|
| 160 | + return map_coordinates(data_t, targets) |
| 161 | + |
| 162 | + # Order F ensures individual volumes are contiguous in memory |
| 163 | + # Also matches NIfTI, making final save more efficient |
| 164 | + resampled = np.zeros( |
| 165 | + (len(ref_ndcoords), len(transform)), dtype=input_dtype, order="F" |
| 166 | + ) |
| 167 | + for t in range(n_resamplings): |
| 168 | + # Interpolate |
| 169 | + resampled[..., t] = _apply_volume(t, data, transform, targets=targets) |
165 | 170 |
|
166 | 171 | else:
|
167 | 172 | data = np.asanyarray(spatialimage.dataobj, dtype=input_dtype)
|
|
0 commit comments