Skip to content

Commit

Permalink
ENH: Merge T2w images and coregister to T1w template
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jan 21, 2023
1 parent 270762f commit 3b63103
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
53 changes: 53 additions & 0 deletions smriprep/workflows/anatomical.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from nipype.interfaces.ants import N4BiasFieldCorrection

from niworkflows.engine.workflows import LiterateWorkflow as Workflow
from niworkflows.interfaces.fixes import FixHeaderApplyTransforms as ApplyTransforms
from niworkflows.interfaces.freesurfer import (
StructuralReference,
PatchedLTAConvert as LTAConvert,
Expand All @@ -62,6 +63,7 @@ def init_anat_preproc_wf(
hires,
longitudinal,
t1w,
t2w,
omp_nthreads,
output_dir,
skull_strip_mode,
Expand Down Expand Up @@ -98,6 +100,7 @@ def init_anat_preproc_wf(
hires=True,
longitudinal=False,
t1w=['t1w.nii.gz'],
t2w=[],
omp_nthreads=1,
output_dir='.',
skull_strip_mode='force',
Expand Down Expand Up @@ -454,6 +457,7 @@ def _check_img(img):
bids_root=bids_root,
freesurfer=freesurfer,
num_t1w=num_t1w,
t2w=t2w,
output_dir=output_dir,
spaces=spaces,
)
Expand Down Expand Up @@ -522,6 +526,55 @@ def _check_img(img):
name="surface_recon_wf", omp_nthreads=omp_nthreads, hires=hires
)
applyrefined = pe.Node(fsl.ApplyMask(), name="applyrefined")

if t2w:
t2w_template_wf = init_anat_template_wf(
longitudinal=longitudinal,
omp_nthreads=omp_nthreads,
num_t1w=len(t2w),
name="t2w_template_wf",
)
bbreg = pe.Node(
fs.BBRegister(
contrast_type="t2",
init="coreg",
dof=6,
out_lta_file=True,
args="--gm-proj-abs 2 --wm-proj-abs 1",
),
name="bbreg",
)
coreg_xfms = pe.Node(niu.Merge(2), name="merge_xfms", run_without_submitting=True)
t2tot1_xfm = pe.Node(ConcatenateXFMs(), name="t2tot1_xfm", run_without_submitting=True)
t2w_resample = pe.Node(
ApplyTransforms(
dimension=3,
default_value=0,
float=True,
interpolation="LanczosWindowedSinc",
),
name="t2w_resample",
)
# fmt:off
workflow.connect([
(inputnode, t2w_template_wf, [('t2w', 'inputnode.t1w')]),
(t2w_template_wf, bbreg, [('outputnode.t1w_ref', 'source_file')]),
(surface_recon_wf, bbreg, [
('outputnode.subject_id', 'inputnode.subject_id'),
('outputnode.subjects_dir', 'inputnode.subjects_dir'),
]),
(bbreg, coreg_xfms, [('out_lta_file', 'in1')]),
(surface_recon_wf, coreg_xfms, [('outputnode.fsnative2t1w_xfm', 'in2')]),
(coreg_xfms, t2tot1_xfm, [('out', 'in_xfms')]),
(t2w_template_wf, t2w_resample, [('outputnode.t1w_ref', 'input_image')]),
(brain_extraction_wf, t2w_resample, [
(('outputnode.bias_corrected', _pop), 'reference_image'),
]),
(t2tot1_xfm, t2w_resample, [('out_xfm', 'transforms')]),
(t2w_resample, outputnode, [('output_image', 't2w_preproc')]),
])
# fmt:on

# fmt:off
workflow.connect([
(inputnode, fs_isrunning, [
Expand Down
2 changes: 2 additions & 0 deletions smriprep/workflows/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def init_single_subject_wf(
# for documentation purposes
subject_data = {
"t1w": ["/completely/made/up/path/sub-01_T1w.nii.gz"],
"t2w": [],
}
else:
subject_data = collect_data(layout, subject_id, bids_filters=bids_filters)[0]
Expand Down Expand Up @@ -406,6 +407,7 @@ def init_single_subject_wf(
longitudinal=longitudinal,
name="anat_preproc_wf",
t1w=subject_data["t1w"],
t2w=subject_data["t2w"],
omp_nthreads=omp_nthreads,
output_dir=output_dir,
skull_strip_fixed_seed=skull_strip_fixed_seed,
Expand Down
24 changes: 24 additions & 0 deletions smriprep/workflows/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def init_anat_derivatives_wf(
bids_root,
freesurfer,
num_t1w,
t2w,
output_dir,
spaces,
name="anat_derivatives_wf",
Expand Down Expand Up @@ -241,6 +242,8 @@ def init_anat_derivatives_wf(
Segmentation in T1w space
t1w_tpms
Tissue probability maps in T1w space
t2w_preproc
The preprocessed T2w image, bias-corrected and resampled into anatomical space.
anat2std_xfm
Nonlinear spatial transform to resample imaging data given in anatomical space
into standard space.
Expand Down Expand Up @@ -284,6 +287,7 @@ def init_anat_derivatives_wf(
"t1w_mask",
"t1w_dseg",
"t1w_tpms",
"t2w_preproc",
"anat2std_xfm",
"std2anat_xfm",
"t1w2fsnative_xfm",
Expand Down Expand Up @@ -344,6 +348,26 @@ def init_anat_derivatives_wf(
])
# fmt:on

if t2w:
ds_t2w_preproc = pe.Node(
DerivativesDataSink(
base_directory=output_dir,
desc="preproc",
suffix="T2w",
compress=True,
),
name="ds_t1w_preproc",
run_without_submitting=True,
)
ds_t2w_preproc.inputs.SkullStripped = False
ds_t2w_preproc.inputs.source_files = t2w

# fmt:off
workflow.connect([
(inputnode, ds_t2w_preproc, [('t2w_preproc', 'in_file')]),
])
# fmt:on

# Transforms
if spaces.get_spaces(nonstandard=False, dim=(3,)):
ds_std2t1w_xfm = pe.MapNode(
Expand Down

0 comments on commit 3b63103

Please sign in to comment.