Skip to content

Commit a0b7fdc

Browse files
committed
fix zooms if difference between zooms and target zooms is between 0.01 and 0.001 mm
1 parent 5432462 commit a0b7fdc

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

niworkflows/interfaces/images.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,23 @@ def _run_interface(self, runtime):
306306

307307
# Set a 0.01mm threshold to performing rescaling
308308
atol = {"meter": 1e-5, "mm": 0.01, "micron": 10}[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]
309311

310312
# Rescale => change zooms
313+
# Fix zooms => set zooms without changing affine
311314
# Resize => update image dimensions
312315
rescale = not np.allclose(zooms, target_zooms, atol=atol)
316+
fix_zooms = not np.allclose(zooms, target_zooms, atol=atol_fine)
313317
resize = not np.all(shape == target_shape)
314-
if rescale or resize:
318+
if rescale or resize or fix_zooms:
315319
if rescale:
316320
scale_factor = target_zooms / zooms
317321
target_affine[:3, :3] = reoriented.affine[:3, :3].dot(
318322
np.diag(scale_factor)
319323
)
324+
elif fix_zooms:
325+
reoriented.header.set_zooms(target_zooms)
320326

321327
if resize:
322328
# The shift is applied after scaling.

niworkflows/interfaces/tests/test_images.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,35 @@ def test_IntraModalMerge(tmpdir, shape, mshape):
180180
merged_data = nb.load(merged).get_fdata(dtype="float32")
181181
new_mshape = (*mshape[:3], 2 if len(mshape) == 3 else mshape[3] * 2)
182182
assert merged_data.shape == new_mshape
183+
184+
185+
def test_conform_resize(tmpdir):
186+
fname = str(tmpdir / "test.nii")
187+
188+
random_data = np.random.random(size=(5, 5, 5))
189+
img = nb.Nifti1Image(random_data, np.eye(4))
190+
img.to_filename(fname)
191+
conform = pe.Node(im.Conform(), name="conform", base_dir=str(tmpdir))
192+
conform.inputs.in_file = fname
193+
conform.inputs.target_zooms = (1, 1, 1.5)
194+
conform.inputs.target_shape = (5, 5, 5)
195+
res = conform.run()
196+
197+
out_img = nb.load(res.outputs.out_file)
198+
assert out_img.header.get_zooms() == conform.inputs.target_zooms
199+
200+
201+
def test_conform_set_zooms(tmpdir):
202+
fname = str(tmpdir / "test.nii")
203+
204+
random_data = np.random.random(size=(5, 5, 5))
205+
img = nb.Nifti1Image(random_data, np.eye(4))
206+
img.to_filename(fname)
207+
conform = pe.Node(im.Conform(), name="conform", base_dir=str(tmpdir))
208+
conform.inputs.in_file = fname
209+
conform.inputs.target_zooms = (1, 1, 1.002)
210+
conform.inputs.target_shape = (5, 5, 5)
211+
res = conform.run()
212+
213+
out_img = nb.load(res.outputs.out_file)
214+
assert np.allclose(out_img.header.get_zooms(), conform.inputs.target_zooms)

0 commit comments

Comments
 (0)