Skip to content

Commit 6bbae23

Browse files
committed
feat: Function to convert transform parameters from mm to vox
1 parent a8fb727 commit 6bbae23

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

sdcflows/workflows/fit/syn.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,21 @@ def _warp_dir(fixed_image, pe_dir, nlevels=3):
604604
return nlevels * [retval.tolist()]
605605

606606

607+
def _mm2vox(moving_image, pe_dir, registration_config):
608+
import nibabel as nb
609+
610+
params = registration_config['transform_parameters']
611+
612+
img = nb.load(moving_image)
613+
zooms = nb.affines.voxel_sizes(img.affine)
614+
pe_res = zooms["ijk".index(pe_dir[0])]
615+
616+
return [
617+
[*level_params[:2], level_params[2] / pe_res]
618+
for level_params in params
619+
]
620+
621+
607622
def _merge_meta(epi_ref, meta_list):
608623
"""Prepare a tuple of EPI reference and metadata."""
609624
return (epi_ref, meta_list[0])

sdcflows/workflows/fit/tests/test_syn.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
"""Test fieldmap-less SDC-SyN."""
2424

2525
import json
26+
27+
import numpy as np
28+
import nibabel as nb
2629
import pytest
2730
from nipype.pipeline import engine as pe
2831

29-
from ..syn import init_syn_sdc_wf, init_syn_preprocessing_wf, _adjust_zooms, _set_dtype
32+
from .... import data
33+
from ..syn import init_syn_sdc_wf, init_syn_preprocessing_wf, _adjust_zooms, _set_dtype, _mm2vox
3034

3135

3236
@pytest.mark.veryslow
@@ -254,3 +258,27 @@ def test_ensure_dtype(in_dtype, out_dtype, tmpdir):
254258
assert out_file == f"{in_dtype}.nii.gz"
255259
else:
256260
assert out_file == f"{in_dtype}_{out_dtype}.nii.gz"
261+
262+
263+
def test_mm2vox(tmp_path):
264+
img = nb.Nifti1Image(np.zeros((10, 10, 10)), np.diag((2, 3, 4, 1)))
265+
img_file = tmp_path / "test.nii.gz"
266+
img.to_filename(img_file)
267+
268+
config = json.loads(data.load.readable("sd_syn.json").read_text())
269+
270+
params = config['transform_parameters']
271+
mm_values = np.array([level[2] for level in params])
272+
273+
vox_params_i = _mm2vox(str(img_file), 'i', config)
274+
vox_values_i = [level[2] for level in vox_params_i]
275+
assert [mm_level[:2] == vox_level[:2] for mm_level, vox_level in zip(params, vox_params_i)]
276+
assert np.array_equal(vox_values_i, mm_values / 2)
277+
278+
vox_params_j = _mm2vox(str(img_file), 'j', config)
279+
vox_values_j = [level[2] for level in vox_params_j]
280+
assert np.array_equal(vox_values_j, mm_values / 3)
281+
282+
vox_params_k = _mm2vox(str(img_file), 'k', config)
283+
vox_values_k = [level[2] for level in vox_params_k]
284+
assert np.array_equal(vox_values_k, mm_values / 4)

0 commit comments

Comments
 (0)