Skip to content

Commit

Permalink
Merge pull request #188 from effigies/fix/nitransforms_convert
Browse files Browse the repository at this point in the history
FIX: Convert LTA to ITK with nitransforms
  • Loading branch information
mgxd authored May 22, 2020
2 parents 45978a0 + e73a9f7 commit 8535ce9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
20 changes: 9 additions & 11 deletions smriprep/workflows/anatomical.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.freesurfer import (
StructuralReference,
PatchedConcatenateLTA as ConcatenateLTA,
PatchedLTAConvert as LTAConvert,
)
from niworkflows.interfaces.images import TemplateDimensions, Conform, ValidateImage
from niworkflows.interfaces.nitransforms import ConcatenateXFMs
from niworkflows.interfaces.utility import KeySelect
from niworkflows.utils.misc import fix_multi_T1w_source_name, add_suffix
from niworkflows.anat.ants import init_brain_extraction_wf, init_n4_only_wf
Expand Down Expand Up @@ -594,12 +594,10 @@ def init_anat_template_wf(longitudinal, omp_nthreads, num_t1w, name='anat_templa
# 2. Reorient template to RAS, if needed (mri_robust_template may set to LIA)
t1w_reorient = pe.Node(image.Reorient(), name='t1w_reorient')

concat_affines = pe.MapNode(
ConcatenateLTA(out_type='RAS2RAS', invert_out=True),
iterfield=['in_lta1', 'in_lta2'],
name='concat_affines')

lta_to_itk = pe.MapNode(LTAConvert(out_itk=True), iterfield=['in_lta'], name='lta_to_itk')
merge_xfm = pe.MapNode(niu.Merge(2), name='merge_xfm',
iterfield=['in1', 'in2'], run_without_submitting=True)
concat_xfms = pe.MapNode(ConcatenateXFMs(inverse=True), name="concat_xfms",
iterfield=['in_xfms'], run_without_submitting=True)

def _set_threads(in_list, maximum):
return min(len(in_list), maximum)
Expand All @@ -614,12 +612,12 @@ def _set_threads(in_list, maximum):
(n4_correct, t1w_merge, [('output_image', 'in_files')]),
(t1w_merge, t1w_reorient, [('out_file', 'in_file')]),
# Combine orientation and template transforms
(t1w_conform_xfm, concat_affines, [('out_lta', 'in_lta1')]),
(t1w_merge, concat_affines, [('transform_outputs', 'in_lta2')]),
(concat_affines, lta_to_itk, [('out_file', 'in_lta')]),
(t1w_conform_xfm, merge_xfm, [('out_lta', 'in1')]),
(t1w_merge, merge_xfm, [('transform_outputs', 'in2')]),
(merge_xfm, concat_xfms, [('out', 'in_xfms')]),
# Output
(t1w_reorient, outputnode, [('out_file', 't1w_ref')]),
(lta_to_itk, outputnode, [('out_itk', 't1w_realign_xfm')]),
(concat_xfms, outputnode, [('out_xfm', 't1w_realign_xfm')]),
])

return workflow
Expand Down
19 changes: 10 additions & 9 deletions smriprep/workflows/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from nipype.interfaces import utility as niu
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.utils.misc import fix_multi_T1w_source_name
from niworkflows.interfaces.freesurfer import PatchedLTAConvert as LTAConvert

from ..interfaces import DerivativesDataSink

Expand Down Expand Up @@ -232,7 +231,7 @@ def init_anat_derivatives_wf(bids_root, freesurfer, num_t1w, output_dir,
# It is necessary because from is a protected keyword (not allowed as argument name).
ds_t1w_ref_xfms = pe.MapNode(
DerivativesDataSink(base_directory=output_dir, to='T1w', mode='image', suffix='xfm',
**{'from': 'orig'}),
extension='txt', **{'from': 'orig'}),
iterfield=['source_file', 'in_file'],
name='ds_t1w_ref_xfms', run_without_submitting=True)
workflow.connect([
Expand All @@ -243,17 +242,19 @@ def init_anat_derivatives_wf(bids_root, freesurfer, num_t1w, output_dir,
if not freesurfer:
return workflow

from niworkflows.interfaces.nitransforms import ConcatenateXFMs
from niworkflows.interfaces.surf import Path2BIDS

# FS native space transforms
lta2itk_fwd = pe.Node(LTAConvert(out_itk=True), name='lta2itk_fwd')
lta2itk_inv = pe.Node(LTAConvert(out_itk=True), name='lta2itk_inv')
lta2itk_fwd = pe.Node(ConcatenateXFMs(), name='lta2itk_fwd', run_without_submitting=True)
lta2itk_inv = pe.Node(ConcatenateXFMs(), name='lta2itk_inv', run_without_submitting=True)
ds_t1w_fsnative = pe.Node(
DerivativesDataSink(base_directory=output_dir, mode='image', to='fsnative', suffix='xfm',
dismiss_entities=("session",), **{'from': 'T1w'}),
extension="txt", dismiss_entities=("session",), **{'from': 'T1w'}),
name='ds_t1w_fsnative', run_without_submitting=True)
ds_fsnative_t1w = pe.Node(
DerivativesDataSink(base_directory=output_dir, mode='image', to='T1w', suffix='xfm',
extension="txt",
dismiss_entities=("session",), **{'from': 'fsnative'}),
name='ds_fsnative_t1w', run_without_submitting=True)
# Surfaces
Expand All @@ -274,12 +275,12 @@ def init_anat_derivatives_wf(bids_root, freesurfer, num_t1w, output_dir,
name='ds_t1w_fsparc', run_without_submitting=True)

workflow.connect([
(inputnode, lta2itk_fwd, [('t1w2fsnative_xfm', 'in_lta')]),
(inputnode, lta2itk_inv, [('fsnative2t1w_xfm', 'in_lta')]),
(inputnode, lta2itk_fwd, [('t1w2fsnative_xfm', 'in_xfms')]),
(inputnode, lta2itk_inv, [('fsnative2t1w_xfm', 'in_xfms')]),
(t1w_name, ds_t1w_fsnative, [('out', 'source_file')]),
(lta2itk_fwd, ds_t1w_fsnative, [('out_itk', 'in_file')]),
(lta2itk_fwd, ds_t1w_fsnative, [('out_xfm', 'in_file')]),
(t1w_name, ds_fsnative_t1w, [('out', 'source_file')]),
(lta2itk_inv, ds_fsnative_t1w, [('out_itk', 'in_file')]),
(lta2itk_inv, ds_fsnative_t1w, [('out_xfm', 'in_file')]),
(inputnode, name_surfs, [('surfaces', 'in_file')]),
(inputnode, ds_surfs, [('surfaces', 'in_file')]),
(t1w_name, ds_surfs, [('out', 'source_file')]),
Expand Down

0 comments on commit 8535ce9

Please sign in to comment.