Skip to content

Commit

Permalink
Merge pull request #2146 from oesteban/hotfix/t1w-to-fsnative-lta
Browse files Browse the repository at this point in the history
FIX: Generate proper LTA transform prior BOLD sampling on surfaces
  • Loading branch information
oesteban committed May 27, 2020
2 parents e77d77f + 108ddf4 commit 882685b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
1 change: 0 additions & 1 deletion fmriprep/workflows/bold/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,6 @@ def init_func_preproc_wf(bold_file):
name='bold_surf_wf')
workflow.connect([
(inputnode, bold_surf_wf, [
('t1w_preproc', 'inputnode.t1w_preproc'),
('subjects_dir', 'inputnode.subjects_dir'),
('subject_id', 'inputnode.subject_id'),
('t1w2fsnative_xfm', 'inputnode.t1w2fsnative_xfm')]),
Expand Down
45 changes: 25 additions & 20 deletions fmriprep/workflows/bold/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,8 @@ def init_bold_surf_wf(
BOLD series, resampled to FreeSurfer surfaces
"""
from nipype.interfaces.io import FreeSurferSource
from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.nitransforms import ConcatenateXFMs

# See https://github.com/poldracklab/fmriprep/issues/768
from niworkflows.interfaces.freesurfer import (
PatchedLTAConvert as LTAConvert
)
from niworkflows.interfaces.surf import GiftiSetAnatomicalStructure

workflow = Workflow(name=name)
Expand All @@ -86,12 +81,15 @@ def init_bold_surf_wf(
""".format(out_spaces=', '.join(['*%s*' % s for s in surface_spaces]))

inputnode = pe.Node(
niu.IdentityInterface(fields=['source_file', 't1w_preproc', 'subject_id', 'subjects_dir',
niu.IdentityInterface(fields=['source_file', 'subject_id', 'subjects_dir',
't1w2fsnative_xfm']),
name='inputnode')
itersource = pe.Node(niu.IdentityInterface(fields=['target']), name='itersource')
itersource.iterables = [('target', surface_spaces)]

get_fsnative = pe.Node(FreeSurferSource(), name='get_fsnative',
run_without_submitting=True)

def select_target(subject_id, space):
"""Get the target subject ID, given a source subject ID and a target space."""
return subject_id if space == 'fsnative' else space
Expand All @@ -103,11 +101,8 @@ def select_target(subject_id, space):
rename_src = pe.Node(niu.Rename(format_string='%(subject)s', keep_ext=True),
name='rename_src', run_without_submitting=True,
mem_gb=DEFAULT_MEMORY_MIN_GB)
resampling_xfm = pe.Node(LTAConvert(in_lta='identity.nofile', out_lta=True),
name='resampling_xfm')
merge_xfm = pe.Node(niu.Merge(2), name="merge_xfm", run_without_submitting=True)
concat_xfm = pe.Node(ConcatenateXFMs(out_fmt="fs"), name="concat_xfm")

itk2lta = pe.Node(niu.Function(function=_itk2lta), name="itk2lta",
run_without_submitting=True)
sampler = pe.MapNode(
fs.SampleToSurface(
cortex_mask=True,
Expand All @@ -127,20 +122,18 @@ def select_target(subject_id, space):
joinsource='itersource', name='outputnode')

workflow.connect([
(inputnode, get_fsnative, [('subject_id', 'subject_id'),
('subjects_dir', 'subjects_dir')]),
(inputnode, targets, [('subject_id', 'subject_id')]),
(inputnode, rename_src, [('source_file', 'in_file')]),
(inputnode, resampling_xfm, [('source_file', 'source_file'),
('t1w_preproc', 'target_file')]),
(inputnode, concat_xfm, [('source_file', 'moving'),
('t1w_preproc', 'reference')]),
(inputnode, merge_xfm, [('t1w2fsnative_xfm', 'in2')]),
(inputnode, itk2lta, [('source_file', 'src_file'),
('t1w2fsnative_xfm', 'in_file')]),
(get_fsnative, itk2lta, [('T1', 'dst_file')]),
(inputnode, sampler, [('subjects_dir', 'subjects_dir'),
('subject_id', 'subject_id')]),
(itersource, targets, [('target', 'space')]),
(itersource, rename_src, [('target', 'subject')]),
(resampling_xfm, merge_xfm, [('out_lta', 'in1')]),
(merge_xfm, concat_xfm, [('out', 'in_xfms')]),
(concat_xfm, sampler, [('out_xfm', 'reg_file')]),
(itk2lta, sampler, [('out', 'reg_file')]),
(targets, sampler, [('out', 'target_subject')]),
(rename_src, sampler, [('out_file', 'source_file')]),
(update_metadata, outputnode, [('out_file', 'surfaces')]),
Expand Down Expand Up @@ -774,3 +767,15 @@ def _is_native(in_value):
in_value.get('resolution') == 'native'
or in_value.get('res') == 'native'
)


def _itk2lta(in_file, src_file, dst_file):
import nitransforms as nt
from pathlib import Path
out_file = Path("out.lta").absolute()
nt.linear.load(
in_file,
fmt="fs" if in_file.endswith(".lta") else "itk",
reference=src_file).to_filename(
out_file, moving=dst_file, fmt="fs")
return str(out_file)

0 comments on commit 882685b

Please sign in to comment.