Skip to content

Commit 5dd70f1

Browse files
committed
RF: Simplify resampling logic during conformation
1 parent 332cea1 commit 5dd70f1

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

niworkflows/interfaces/images.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -305,37 +305,37 @@ def _run_interface(self, runtime):
305305
xyz_unit = "mm"
306306

307307
# Set a 0.05mm threshold to performing rescaling
308-
atol = {"meter": 5e-5, "mm": 0.05, "micron": 50}[xyz_unit]
308+
atol_gross = {"meter": 5e-5, "mm": 0.05, "micron": 50}[xyz_unit]
309309
# if 0.01 > difference > 0.001mm, freesurfer won't be able to merge the images
310310
atol_fine = {"meter": 1e-6, "mm": 0.001, "micron": 1}[xyz_unit]
311311

312-
# Rescale => change zooms
313-
# Fix zooms => set zooms without changing affine
314-
# Resize => update image dimensions
315-
rescale = not np.allclose(zooms, target_zooms, atol=atol)
316-
fix_zooms = not np.allclose(zooms, target_zooms, atol=atol_fine)
312+
# Update zooms => Modify affine
313+
# Rescale => Resample to resized voxels
314+
# Resize => Resample to new image dimensions
315+
update_zooms = not np.allclose(zooms, target_zooms, atol=atol_fine, rtol=0)
316+
rescale = not np.allclose(zooms, target_zooms, atol=atol_gross, rtol=0)
317317
resize = not np.all(shape == target_shape)
318-
if rescale or resize or fix_zooms:
319-
if rescale:
318+
resample = rescale or resize
319+
if resample or update_zooms:
320+
# Use an affine with the corrected zooms, whether or not we resample
321+
if update_zooms:
320322
scale_factor = target_zooms / zooms
321-
target_affine[:3, :3] = reoriented.affine[:3, :3].dot(
322-
np.diag(scale_factor)
323-
)
324-
elif fix_zooms:
325-
reoriented.header.set_zooms(target_zooms)
323+
target_affine[:3, :3] = reoriented.affine[:3, :3] @ np.diag(scale_factor)
326324

327325
if resize:
328326
# The shift is applied after scaling.
329327
# Use a proportional shift to maintain relative position in dataset
330328
size_factor = target_span / (zooms * shape)
331329
# Use integer shifts to avoid unnecessary interpolation
332-
offset = (
333-
reoriented.affine[:3, 3] * size_factor - reoriented.affine[:3, 3]
334-
)
330+
offset = reoriented.affine[:3, 3] * size_factor - reoriented.affine[:3, 3]
335331
target_affine[:3, 3] = reoriented.affine[:3, 3] + offset.astype(int)
336332

337-
data = nli.resample_img(reoriented, target_affine, target_shape).dataobj
338-
conform_xfm = np.linalg.inv(reoriented.affine).dot(target_affine)
333+
conform_xfm = np.linalg.inv(reoriented.affine) @ target_affine
334+
335+
# Create new image
336+
data = reoriented.dataobj
337+
if resample:
338+
data = nli.resample_img(reoriented, target_affine, target_shape).dataobj
339339
reoriented = reoriented.__class__(data, target_affine, reoriented.header)
340340

341341
# Image may be reoriented, rescaled, and/or resized

0 commit comments

Comments
 (0)