@@ -349,31 +349,37 @@ def _run_interface(self, runtime):
349349 xyz_unit = "mm"
350350
351351 # Set a 0.05mm threshold to performing rescaling
352- atol = {"meter" : 1e-5 , "mm" : 0.01 , "micron" : 10 }[xyz_unit ]
353-
354- # Rescale => change zooms
355- # Resize => update image dimensions
356- rescale = not np .allclose (zooms , target_zooms , atol = atol )
352+ atol_gross = {"meter" : 5e-5 , "mm" : 0.05 , "micron" : 50 }[xyz_unit ]
353+ # if 0.01 > difference > 0.001mm, freesurfer won't be able to merge the images
354+ atol_fine = {"meter" : 1e-6 , "mm" : 0.001 , "micron" : 1 }[xyz_unit ]
355+
356+ # Update zooms => Modify affine
357+ # Rescale => Resample to resized voxels
358+ # Resize => Resample to new image dimensions
359+ update_zooms = not np .allclose (zooms , target_zooms , atol = atol_fine , rtol = 0 )
360+ rescale = not np .allclose (zooms , target_zooms , atol = atol_gross , rtol = 0 )
357361 resize = not np .all (shape == target_shape )
358- if rescale or resize :
359- if rescale :
362+ resample = rescale or resize
363+ if resample or update_zooms :
364+ # Use an affine with the corrected zooms, whether or not we resample
365+ if update_zooms :
360366 scale_factor = target_zooms / zooms
361- target_affine [:3 , :3 ] = reoriented .affine [:3 , :3 ].dot (
362- np .diag (scale_factor )
363- )
367+ target_affine [:3 , :3 ] = reoriented .affine [:3 , :3 ] @ np .diag (scale_factor )
364368
365369 if resize :
366370 # The shift is applied after scaling.
367371 # Use a proportional shift to maintain relative position in dataset
368372 size_factor = target_span / (zooms * shape )
369373 # Use integer shifts to avoid unnecessary interpolation
370- offset = (
371- reoriented .affine [:3 , 3 ] * size_factor - reoriented .affine [:3 , 3 ]
372- )
374+ offset = reoriented .affine [:3 , 3 ] * size_factor - reoriented .affine [:3 , 3 ]
373375 target_affine [:3 , 3 ] = reoriented .affine [:3 , 3 ] + offset .astype (int )
374376
375- data = nli .resample_img (reoriented , target_affine , target_shape ).dataobj
376- conform_xfm = np .linalg .inv (reoriented .affine ).dot (target_affine )
377+ conform_xfm = np .linalg .inv (reoriented .affine ) @ target_affine
378+
379+ # Create new image
380+ data = reoriented .dataobj
381+ if resample :
382+ data = nli .resample_img (reoriented , target_affine , target_shape ).dataobj
377383 reoriented = reoriented .__class__ (data , target_affine , reoriented .header )
378384
379385 # Image may be reoriented, rescaled, and/or resized
0 commit comments