Skip to content

[RTM] RF: Miscellaneous registration refactorings #741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions fmriprep/workflows/anatomical.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def init_anat_preproc_wf(skull_strip_ants, output_spaces, template, debug, frees
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
fs_2_t1_transform
t1_2_fsnative_reverse_transform
Affine transform from FreeSurfer subject space to T1w space
surfaces
GIFTI surfaces (gray/white boundary, midthickness, pial, inflated)
Expand All @@ -174,7 +174,7 @@ def init_anat_preproc_wf(skull_strip_ants, output_spaces, template, debug, frees
fields=['t1_preproc', 't1_brain', 't1_mask', 't1_seg', 't1_tpms',
't1_2_mni', 't1_2_mni_forward_transform', 't1_2_mni_reverse_transform',
'mni_mask', 'mni_seg', 'mni_tpms',
'subjects_dir', 'subject_id', 'fs_2_t1_transform', 'surfaces']),
'subjects_dir', 'subject_id', 't1_2_fsnative_reverse_transform', 'surfaces']),
name='outputnode')

# 0. Reorient T1w image(s) to RAS and resample to common voxel space
Expand Down Expand Up @@ -314,7 +314,7 @@ def len_above_thresh(in_list, threshold, longitudinal):
(surface_recon_wf, outputnode, [
('outputnode.subjects_dir', 'subjects_dir'),
('outputnode.subject_id', 'subject_id'),
('outputnode.fs_2_t1_transform', 'fs_2_t1_transform'),
('outputnode.t1_2_fsnative_reverse_transform', 't1_2_fsnative_reverse_transform'),
('outputnode.surfaces', 'surfaces')]),
])

Expand Down Expand Up @@ -499,7 +499,7 @@ def init_surface_recon_wf(omp_nthreads, hires, name='surface_recon_wf'):
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
fs_2_t1_transform
t1_2_fsnative_reverse_transform
FSL-style affine matrix translating from FreeSurfer T1.mgz to T1w
surfaces
GIFTI surfaces for gray/white matter boundary, pial surface,
Expand All @@ -521,7 +521,8 @@ def init_surface_recon_wf(omp_nthreads, hires, name='surface_recon_wf'):
name='inputnode')
outputnode = pe.Node(
niu.IdentityInterface(
fields=['subjects_dir', 'subject_id', 'fs_2_t1_transform', 'surfaces', 'out_report']),
fields=['subjects_dir', 'subject_id', 't1_2_fsnative_reverse_transform', 'surfaces',
'out_report']),
name='outputnode')

recon_config = pe.Node(FSDetectInputs(hires_enabled=hires), name='recon_config',
Expand All @@ -542,6 +543,7 @@ def init_surface_recon_wf(omp_nthreads, hires, name='surface_recon_wf'):
fs_transform = pe.Node(
fs.Tkregister2(fsl_out='freesurfer2subT1.mat', reg_header=True),
name='fs_transform')
fsl2lta = pe.Node(fs.utils.LTAConvert(out_lta=True), name='fsl2lta')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would imagine that we don't need FSL transforms because we are not chaining them? (we resample in T1 space, I assume). Is this correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered below, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doing this right now because nipype doesn't have LTA outputs from tkregister enabled. See nipy/nipype#2217. (Though it also doesn't matter in the long run, since we drop tkregister in #733.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh. Actually just realized I'm missing two inputs to fsl2lta. Hold off on merging.


autorecon_resume_wf = init_autorecon_resume_wf(omp_nthreads=omp_nthreads)
gifti_surface_wf = init_gifti_surface_wf()
Expand Down Expand Up @@ -572,12 +574,15 @@ def init_surface_recon_wf(omp_nthreads, hires, name='surface_recon_wf'):
# reoriented image
(inputnode, fs_transform, [('t1w', 'target_image')]),
(autorecon1, fs_transform, [('T1', 'moving_image')]),
(inputnode, fsl2lta, [('t1w', 'target_file')]),
(autorecon1, fsl2lta, [('T1', 'source_file')]),
(fs_transform, fsl2lta, [('fsl_file', 'in_fsl')]),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oesteban These are the new lines. lta_convert needs to know the source and target to create a new LTA affine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

# Output
(autorecon_resume_wf, outputnode, [('outputnode.subjects_dir', 'subjects_dir'),
('outputnode.subject_id', 'subject_id'),
('outputnode.out_report', 'out_report')]),
(gifti_surface_wf, outputnode, [('outputnode.surfaces', 'surfaces')]),
(fs_transform, outputnode, [('fsl_file', 'fs_2_t1_transform')]),
(fsl2lta, outputnode, [('out_lta', 't1_2_fsnative_reverse_transform')]),
])

return workflow
Expand Down
3 changes: 2 additions & 1 deletion fmriprep/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ def init_single_subject_wf(subject_id, task_id, name,
(anat_preproc_wf, func_preproc_wf,
[('outputnode.subjects_dir', 'inputnode.subjects_dir'),
('outputnode.subject_id', 'inputnode.subject_id'),
('outputnode.fs_2_t1_transform', 'inputnode.fs_2_t1_transform')]),
('outputnode.t1_2_fsnative_reverse_transform',
'inputnode.t1_2_fsnative_reverse_transform')]),
])

return workflow
92 changes: 33 additions & 59 deletions fmriprep/workflows/bold.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from niworkflows.nipype import logging
from niworkflows.nipype.utils.filemanip import split_filename
from niworkflows.nipype.pipeline import engine as pe
from niworkflows.nipype.interfaces import ants, afni, c3, fsl
from niworkflows.nipype.interfaces import ants, afni, fsl
from niworkflows.nipype.interfaces import utility as niu
from niworkflows.nipype.interfaces import freesurfer as fs

Expand Down Expand Up @@ -173,7 +173,7 @@ def init_func_preproc_wf(bold_file, ignore, freesurfer,
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
fs_2_t1_transform
t1_2_fsnative_reverse_transform
Affine transform from FreeSurfer subject space to T1w space


Expand Down Expand Up @@ -245,7 +245,7 @@ def init_func_preproc_wf(bold_file, ignore, freesurfer,
inputnode = pe.Node(niu.IdentityInterface(
fields=['bold_file', 't1_preproc', 't1_brain', 't1_mask', 't1_seg', 't1_tpms',
't1_2_mni_forward_transform', 't1_2_mni_reverse_transform',
'subjects_dir', 'subject_id', 'fs_2_t1_transform']),
'subjects_dir', 'subject_id', 't1_2_fsnative_reverse_transform']),
name='inputnode')
inputnode.inputs.bold_file = bold_file

Expand Down Expand Up @@ -320,16 +320,17 @@ def init_func_preproc_wf(bold_file, ignore, freesurfer,
(inputnode, bold_reference_wf, [('bold_file', 'inputnode.bold_file')]),
(bold_reference_wf, bold_hmc_wf, [
('outputnode.raw_ref_image', 'inputnode.raw_ref_image')]),
(inputnode, bold_reg_wf, [('bold_file', 'inputnode.name_source'),
('t1_preproc', 'inputnode.t1_preproc'),
('t1_brain', 'inputnode.t1_brain'),
('t1_mask', 'inputnode.t1_mask'),
('t1_seg', 'inputnode.t1_seg'),
# Undefined if --no-freesurfer, but this is safe
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id'),
('fs_2_t1_transform', 'inputnode.fs_2_t1_transform')
]),
(inputnode, bold_reg_wf, [
('bold_file', 'inputnode.name_source'),
('t1_preproc', 'inputnode.t1_preproc'),
('t1_brain', 'inputnode.t1_brain'),
('t1_mask', 'inputnode.t1_mask'),
('t1_seg', 'inputnode.t1_seg'),
# Undefined if --no-freesurfer, but this is safe
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id'),
('t1_2_fsnative_reverse_transform', 'inputnode.t1_2_fsnative_reverse_transform')
]),
(inputnode, bold_confounds_wf, [('t1_tpms', 'inputnode.t1_tpms'),
('t1_mask', 'inputnode.t1_mask')]),
(bold_hmc_wf, bold_reg_wf, [('outputnode.bold_split', 'inputnode.bold_split'),
Expand Down Expand Up @@ -859,17 +860,13 @@ def init_bold_reg_wf(freesurfer, bold2t1w_dof, bold_file_size_gb, omp_nthreads,
FreeSurfer SUBJECTS_DIR
subject_id
FreeSurfer subject ID
fs_2_t1_transform
t1_2_fsnative_reverse_transform
Affine transform from FreeSurfer subject space to T1w space
fieldwarp
a :abbr:`DFM (displacements field map)` in ITK format

Outputs

mat_bold_to_t1
Affine transform from ``ref_bold_brain`` to T1 space (FSL format)
mat_t1_to_bold
Affine transform from T1 space to BOLD space (FSL format)
itk_bold_to_t1
Affine transform from ``ref_bold_brain`` to T1 space (ITK format)
itk_t1_to_bold
Expand All @@ -878,25 +875,22 @@ def init_bold_reg_wf(freesurfer, bold2t1w_dof, bold_file_size_gb, omp_nthreads,
Motion-corrected BOLD series in T1 space
bold_mask_t1
BOLD mask in T1 space
fs_reg_file
Affine transform from ``ref_bold_brain`` to T1 space (FreeSurfer ``reg`` format)
out_report
Reportlet visualizing quality of registration

"""
workflow = pe.Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(fields=['name_source', 'ref_bold_brain', 'ref_bold_mask',
't1_preproc', 't1_brain', 't1_mask',
't1_seg', 'bold_split', 'hmc_xforms',
'subjects_dir', 'subject_id', 'fs_2_t1_transform',
'fieldwarp']),
niu.IdentityInterface(
fields=['name_source', 'ref_bold_brain', 'ref_bold_mask',
't1_preproc', 't1_brain', 't1_mask',
't1_seg', 'bold_split', 'hmc_xforms',
'subjects_dir', 'subject_id', 't1_2_fsnative_reverse_transform', 'fieldwarp']),
name='inputnode'
)
outputnode = pe.Node(
niu.IdentityInterface(fields=['mat_bold_to_t1', 'mat_t1_to_bold',
'itk_bold_to_t1', 'itk_t1_to_bold',
'bold_t1', 'bold_mask_t1', 'fs_reg_file',
niu.IdentityInterface(fields=['itk_bold_to_t1', 'itk_t1_to_bold',
'bold_t1', 'bold_mask_t1',
'out_report']),
name='outputnode'
)
Expand All @@ -906,37 +900,17 @@ def init_bold_reg_wf(freesurfer, bold2t1w_dof, bold_file_size_gb, omp_nthreads,
else:
bbr_wf = init_fsl_bbr_wf(bold2t1w_dof, report=True)

# make equivalent warp fields
invt_bbr = pe.Node(fsl.ConvertXFM(invert_xfm=True), name='invt_bbr',
mem_gb=DEFAULT_MEMORY_MIN_GB)

# BOLD to T1 transform matrix is from fsl, using c3 tools to convert to
# something ANTs will like.
fsl2itk_fwd = pe.Node(c3.C3dAffineTool(fsl2ras=True, itk_transform=True),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I was about to propose this change. This is to move c3d into the actual (bbregister or BBR) registration algorithm pipeline, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c3d is being used in the FLIRT-BBR workflow to translate from FSL to ITK.

lta_convert is being used in the bbregister workflow to translate from LTA to ITK. LTA is FreeSurfer's format, and there's no real need to spend time in FSL's space.

name='fsl2itk_fwd', mem_gb=DEFAULT_MEMORY_MIN_GB)
fsl2itk_inv = pe.Node(c3.C3dAffineTool(fsl2ras=True, itk_transform=True),
name='fsl2itk_inv', mem_gb=DEFAULT_MEMORY_MIN_GB)

workflow.connect([
(inputnode, bbr_wf, [('ref_bold_brain', 'inputnode.in_file'),
('fs_2_t1_transform', 'inputnode.fs_2_t1_transform'),
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id'),
('t1_seg', 'inputnode.t1_seg'),
('t1_brain', 'inputnode.t1_brain')]),
(inputnode, fsl2itk_fwd, [('t1_preproc', 'reference_file'),
('ref_bold_brain', 'source_file')]),
(inputnode, fsl2itk_inv, [('ref_bold_brain', 'reference_file'),
('t1_preproc', 'source_file')]),
(bbr_wf, invt_bbr, [('outputnode.out_matrix_file', 'in_file')]),
(bbr_wf, fsl2itk_fwd, [('outputnode.out_matrix_file', 'transform_file')]),
(invt_bbr, fsl2itk_inv, [('out_file', 'transform_file')]),
(bbr_wf, outputnode, [('outputnode.out_matrix_file', 'mat_bold_to_t1'),
('outputnode.out_reg_file', 'fs_reg_file'),
(inputnode, bbr_wf, [
('ref_bold_brain', 'inputnode.in_file'),
('t1_2_fsnative_reverse_transform', 'inputnode.t1_2_fsnative_reverse_transform'),
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id'),
('t1_seg', 'inputnode.t1_seg'),
('t1_brain', 'inputnode.t1_brain')]),
(bbr_wf, outputnode, [('outputnode.itk_bold_to_t1', 'itk_bold_to_t1'),
('outputnode.itk_t1_to_bold', 'itk_t1_to_bold'),
('outputnode.out_report', 'out_report')]),
(invt_bbr, outputnode, [('out_file', 'mat_t1_to_bold')]),
(fsl2itk_fwd, outputnode, [('itk_transform', 'itk_bold_to_t1')]),
(fsl2itk_inv, outputnode, [('itk_transform', 'itk_t1_to_bold')]),
])

gen_ref = pe.Node(GenerateSamplingReference(), name='gen_ref',
Expand All @@ -952,7 +926,7 @@ def init_bold_reg_wf(freesurfer, bold2t1w_dof, bold_file_size_gb, omp_nthreads,
(inputnode, gen_ref, [('ref_bold_brain', 'moving_image'),
('t1_brain', 'fixed_image')]),
(gen_ref, mask_t1w_tfm, [('out_file', 'reference_image')]),
(fsl2itk_fwd, mask_t1w_tfm, [('itk_transform', 'transforms')]),
(bbr_wf, mask_t1w_tfm, [('outputnode.itk_bold_to_t1', 'transforms')]),
(inputnode, mask_t1w_tfm, [('ref_bold_mask', 'input_image')]),
(mask_t1w_tfm, outputnode, [('output_image', 'bold_mask_t1')])
])
Expand All @@ -977,7 +951,7 @@ def init_bold_reg_wf(freesurfer, bold2t1w_dof, bold_file_size_gb, omp_nthreads,
merge = pe.Node(Merge(compress=use_compression), name='merge', mem_gb=bold_file_size_gb * 3)

workflow.connect([
(fsl2itk_fwd, merge_xforms, [('itk_transform', 'in1')]),
(bbr_wf, merge_xforms, [('outputnode.itk_bold_to_t1', 'in1')]),
(merge_xforms, bold_to_t1w_transform, [('out', 'transforms')]),
(inputnode, merge, [('name_source', 'header_source')]),
(merge, outputnode, [('out_file', 'bold_t1')]),
Expand Down
Loading