Skip to content
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

FIX: Generate proper LTA transform prior BOLD sampling on surfaces #2146

Merged
merged 3 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
47 changes: 27 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,16 @@ 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)
tonii = pe.Node(fs.MRIConvert(out_type='niigz'), name='tonii')
oesteban marked this conversation as resolved.
Show resolved Hide resolved

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 +102,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 +123,19 @@ 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, tonii, [('T1', 'in_file')]),
(tonii, itk2lta, [('out_file', 'dst_file')]),
oesteban marked this conversation as resolved.
Show resolved Hide resolved
(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 +769,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)