Skip to content

Commit

Permalink
Merge pull request #415 from mgxd/enh/mni-reg
Browse files Browse the repository at this point in the history
ENH: Add flag for multi-step registration to adult templates
  • Loading branch information
mgxd authored Dec 13, 2024
2 parents b9efa6f + 63afae1 commit b7e3fcc
Show file tree
Hide file tree
Showing 11 changed files with 652 additions and 58 deletions.
6 changes: 6 additions & 0 deletions nibabies/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,12 @@ def _str_none(val):
action='store_true',
help='Replace low intensity voxels in CSF mask with average',
)
g_baby.add_argument(
'--multi-step-reg',
action='store_true',
help='For certain adult templates (MNI152NLin6Asym), perform two step '
'registrations (native -> MNIInfant -> template) and concatenate into a single xfm',
)
return parser


Expand Down
3 changes: 3 additions & 0 deletions nibabies/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ class workflow(_Config):
"""Run FreeSurfer ``recon-all`` with the ``-logitudinal`` flag."""
medial_surface_nan = None
"""Fill medial surface with :abbr:`NaNs (not-a-number)` when sampling."""
multi_step_reg = False
"""Perform multiple registrations (native -> MNIInfant -> template) and concatenate into a
single transform"""
norm_csf = False
"""Replace low intensity voxels in CSF mask with average."""
project_goodvoxels = False
Expand Down
90 changes: 90 additions & 0 deletions nibabies/data/xfm_manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"from-MNI152NLin6Asym_to-MNIInfant+10_xfm.h5": {
"url": "https://osf.io/download/jf6vz/",
"hash": "md5:df6d40e5bbdca85f083866ad26507796"
},
"from-MNI152NLin6Asym_to-MNIInfant+11_xfm.h5": {
"url": "https://osf.io/download/zmjyn/",
"hash": "md5:ff8435f06c0a44be88e050492b90ffea"
},
"from-MNI152NLin6Asym_to-MNIInfant+1_xfm.h5": {
"url": "https://osf.io/download/kx7ny/",
"hash": "md5:c27d35dff75d59d605c8d786c985594e"
},
"from-MNI152NLin6Asym_to-MNIInfant+2_xfm.h5": {
"url": "https://osf.io/download/6758aa0c67a7782b00f73c77/",
"hash": "md5:81fabdc70c200bf099893bd1377ef0f7"
},
"from-MNI152NLin6Asym_to-MNIInfant+3_xfm.h5": {
"url": "https://osf.io/download/6758aa1c76bfbc22cbf73b0a/",
"hash": "md5:c8f5b79b95f9aa65add5524e88601cc6"
},
"from-MNI152NLin6Asym_to-MNIInfant+4_xfm.h5": {
"url": "https://osf.io/download/6758aa1bb96fd819c41e25a4/",
"hash": "md5:1e4b927115a76b031c46e6180fc76a30"
},
"from-MNI152NLin6Asym_to-MNIInfant+5_xfm.h5": {
"url": "https://osf.io/download/6758aa146e0cd8ca5f563b2b/",
"hash": "md5:25bfd0837a88db267762974c0a530535"
},
"from-MNI152NLin6Asym_to-MNIInfant+6_xfm.h5": {
"url": "https://osf.io/download/6758ab50b95a2e75b11e23f0/",
"hash": "md5:7ed4732832ed6dd45dd2259d8b4454e7"
},
"from-MNI152NLin6Asym_to-MNIInfant+7_xfm.h5": {
"url": "https://osf.io/download/6758ab5667a7782b00f73cfa/",
"hash": "md5:a1244c38b7b4825abefc5834d0398b08"
},
"from-MNI152NLin6Asym_to-MNIInfant+8_xfm.h5": {
"url": "https://osf.io/download/rq2an/",
"hash": "md5:50d11fdac22c6589af8a7f61e4b3e41a"
},
"from-MNI152NLin6Asym_to-MNIInfant+9_xfm.h5": {
"url": "https://osf.io/download/6758ab67eacdd8b34803d991/",
"hash": "md5:3184d91f8b3a386ca3ec913c365651d8"
},
"from-MNIInfant+10_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/4xh9q/",
"hash": "md5:a1f3dd3c0ac8b05efbaf893cca6f9641"
},
"from-MNIInfant+11_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a5026e0cd8ca5f56380d/",
"hash": "md5:0d1aadef884574e54065d4e2cdb8e398"
},
"from-MNIInfant+1_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/7ge2b/",
"hash": "md5:d5e4272140c6f582f64b7f39b31ca837"
},
"from-MNIInfant+2_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a3c1a678894ad71e2422/",
"hash": "md5:b03651dae4d378410c44f1c6c63dbea0"
},
"from-MNIInfant+3_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a3c3bc61ce5912662055/",
"hash": "md5:7cc099e26647e670c8e75ead2cfe39a6"
},
"from-MNIInfant+4_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a3bc040c053b58f73b47/",
"hash": "md5:e92e9150f2ad4d2730f005aa9750438d"
},
"from-MNIInfant+5_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a3bdea7294dbdd66161a/",
"hash": "md5:9cf6cf3fb500c229da15490c9080201a"
},
"from-MNIInfant+6_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a3bf040c053b58f73b4b/",
"hash": "md5:2212fdb57b85e8a0f7fa9feea5b0dd1b"
},
"from-MNIInfant+7_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a4f78af26d0a97661ca9/",
"hash": "md5:6913f8191201350311ff61525fae8a21"
},
"from-MNIInfant+8_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a500f82c189df71e256f/",
"hash": "md5:809455af8416cd61c1693b5c7eafbd13"
},
"from-MNIInfant+9_to-MNI152NLin6Asym_xfm.h5": {
"url": "https://osf.io/download/6758a4ff040c053b58f73bd1/",
"hash": "md5:49317cbb038c399d4df7428f07d36983"
}
}
2 changes: 2 additions & 0 deletions nibabies/interfaces/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def _chdir(path):
'sub-01_run-01_echo-1_bold.nii.gz',
'sub-01_run-01_echo-2_bold.nii.gz',
'sub-01_run-01_echo-3_bold.nii.gz',
'xfm0.h5',
'xfm1.h5',
)


Expand Down
86 changes: 84 additions & 2 deletions nibabies/interfaces/patches.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import nipype.interfaces.freesurfer as fs
from nipype.interfaces.base import File, traits
from pathlib import Path

from nipype.interfaces import (
freesurfer as fs,
)
from nipype.interfaces.ants.base import ANTSCommand, ANTSCommandInputSpec
from nipype.interfaces.base import File, InputMultiObject, TraitedSpec, traits


class _MRICoregInputSpec(fs.registration.MRICoregInputSpec):
Expand All @@ -23,3 +28,80 @@ class MRICoreg(fs.MRICoreg):
"""

input_spec = _MRICoregInputSpec


class ConcatXFMInputSpec(ANTSCommandInputSpec):
transforms = InputMultiObject(
traits.Either(File(exists=True), 'identity'),
argstr='%s',
mandatory=True,
desc='transform files: will be applied in reverse order. For '
'example, the last specified transform will be applied first.',
)
out_xfm = traits.File(
'concat_xfm.h5',
usedefault=True,
argstr='--output [ %s, 1 ]',
desc='output file name',
)
reference_image = File(
argstr='--reference-image %s',
mandatory=True,
desc='reference image space that you wish to warp INTO',
exists=True,
)
invert_transform_flags = InputMultiObject(traits.Bool())


class ConcatXFMOutputSpec(TraitedSpec):
out_xfm = File(desc='Combined transform')


class ConcatXFM(ANTSCommand):
"""
Streamed use of antsApplyTransforms to combine multiple xfms into a single file
Examples
--------
>>> from nibabies.interfaces.patches import ConcatXFM
>>> cxfm = ConcatXFM()
>>> cxfm.inputs.transforms = [testdir / 'xfm0.h5', testdir / 'xfm1.h5']
>>> cxfm.inputs.reference_image = testdir / 'anatomical.nii'
>>> cxfm.cmdline # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
'antsApplyTransforms --output [ concat_xfm.h5, 1 ] --reference-image .../anatomical.nii \
--transform .../xfm0.h5 --transform .../xfm1.h5'
"""

_cmd = 'antsApplyTransforms'
input_spec = ConcatXFMInputSpec
output_spec = ConcatXFMOutputSpec

def _get_transform_filenames(self):
retval = []
invert_flags = self.inputs.invert_transform_flags
if not invert_flags:
invert_flags = [False] * len(self.inputs.transforms)
elif len(self.inputs.transforms) != len(invert_flags):
raise ValueError(
'ERROR: The invert_transform_flags list must have the same number '
'of entries as the transforms list.'
)

for transform, invert in zip(self.inputs.transforms, invert_flags, strict=False):
if invert:
retval.append(f'--transform [ {transform}, 1 ]')
else:
retval.append(f'--transform {transform}')
return ' '.join(retval)

def _format_arg(self, opt, spec, val):
if opt == 'transforms':
return self._get_transform_filenames()
return super()._format_arg(opt, spec, val)

def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_xfm'] = Path(self.inputs.out_xfm).absolute()
return outputs
Loading

0 comments on commit b7e3fcc

Please sign in to comment.