@@ -305,31 +305,37 @@ def _run_interface(self, runtime):
305
305
xyz_unit = "mm"
306
306
307
307
# Set a 0.05mm threshold to performing rescaling
308
- atol = {"meter" : 1e-5 , "mm" : 0.01 , "micron" : 10 }[xyz_unit ]
309
-
310
- # Rescale => change zooms
311
- # Resize => update image dimensions
312
- rescale = not np .allclose (zooms , target_zooms , atol = atol )
308
+ atol_gross = {"meter" : 5e-5 , "mm" : 0.05 , "micron" : 50 }[xyz_unit ]
309
+ # if 0.01 > difference > 0.001mm, freesurfer won't be able to merge the images
310
+ atol_fine = {"meter" : 1e-6 , "mm" : 0.001 , "micron" : 1 }[xyz_unit ]
311
+
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 )
313
317
resize = not np .all (shape == target_shape )
314
- if rescale or resize :
315
- 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 :
316
322
scale_factor = target_zooms / zooms
317
- target_affine [:3 , :3 ] = reoriented .affine [:3 , :3 ].dot (
318
- np .diag (scale_factor )
319
- )
323
+ target_affine [:3 , :3 ] = reoriented .affine [:3 , :3 ] @ np .diag (scale_factor )
320
324
321
325
if resize :
322
326
# The shift is applied after scaling.
323
327
# Use a proportional shift to maintain relative position in dataset
324
328
size_factor = target_span / (zooms * shape )
325
329
# Use integer shifts to avoid unnecessary interpolation
326
- offset = (
327
- reoriented .affine [:3 , 3 ] * size_factor - reoriented .affine [:3 , 3 ]
328
- )
330
+ offset = reoriented .affine [:3 , 3 ] * size_factor - reoriented .affine [:3 , 3 ]
329
331
target_affine [:3 , 3 ] = reoriented .affine [:3 , 3 ] + offset .astype (int )
330
332
331
- data = nli .resample_img (reoriented , target_affine , target_shape ).dataobj
332
- 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
333
339
reoriented = reoriented .__class__ (data , target_affine , reoriented .header )
334
340
335
341
# Image may be reoriented, rescaled, and/or resized
0 commit comments