def create_ants_nonlinear_xfm(name='ants_nonlinear_xfm'): import nipype.interfaces.ants as ants from nipype.interfaces.ants.legacy import GenWarpFields ants_nonlinear_xfm = pe.Workflow(name=name) inputspec = pe.Node( util.IdentityInterface(fields=['anatomical_brain', 'reference_brain']), name='inputspec') # use ANTS to warp the masked anatomical image to a template image warp_brain = pe.Node(GenWarpFields(), name='warp_brain') outputspec = pe.Node(util.IdentityInterface(fields=[ 'warp_field', 'affine_transformation', 'inverse_warp', 'output_brain' ]), name='outputspec') ants_nonlinear_xfm.connect(inputspec, 'anatomical_brain', warp_brain, 'input_image') ants_nonlinear_xfm.connect(inputspec, 'reference_brain', warp_brain, 'reference_image') ants_nonlinear_xfm.connect(warp_brain, 'warp_field', outputspec, 'warp_field') ants_nonlinear_xfm.connect(warp_brain, 'affine_transformation', outputspec, 'affine_transformation') ants_nonlinear_xfm.connect(warp_brain, 'inverse_warp_field', outputspec, 'inverse_warp') ants_nonlinear_xfm.connect(warp_brain, 'output_file', outputspec, 'output_brain') return ants_nonlinear_xfm
def test_GenWarpFields_inputs(): input_map = dict( args=dict(argstr='%s', ), bias_field_correction=dict(argstr='-n 1', ), dimension=dict( argstr='-d %d', position=1, usedefault=True, ), environ=dict( nohash=True, usedefault=True, ), force_proceed=dict(argstr='-f 1', ), ignore_exception=dict( nohash=True, usedefault=True, ), input_image=dict( argstr='-i %s', copyfile=False, mandatory=True, ), inverse_warp_template_labels=dict(argstr='-l', ), max_iterations=dict( argstr='-m %s', sep='x', ), num_threads=dict( nohash=True, usedefault=True, ), out_prefix=dict( argstr='-o %s', usedefault=True, ), quality_check=dict(argstr='-q 1', ), reference_image=dict( argstr='-r %s', copyfile=True, mandatory=True, ), similarity_metric=dict(argstr='-s %s', ), terminal_output=dict( mandatory=True, nohash=True, ), transformation_model=dict( argstr='-t %s', usedefault=True, ), ) inputs = GenWarpFields.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value
def test_GenWarpFields_outputs(): output_map = dict(affine_transformation=dict(), input_file=dict(), inverse_warp_field=dict(), output_file=dict(), warp_field=dict(), ) outputs = GenWarpFields.output_spec() for key, metadata in output_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(outputs.traits()[key], metakey), value
def test_GenWarpFields_inputs(): input_map = dict(ignore_exception=dict(nohash=True, usedefault=True, ), transformation_model=dict(usedefault=True, argstr='-t %s', ), quality_check=dict(argstr='-q 1', ), num_threads=dict(nohash=True, usedefault=True, ), force_proceed=dict(argstr='-f 1', ), args=dict(argstr='%s', ), out_prefix=dict(argstr='-o %s', usedefault=True, ), bias_field_correction=dict(argstr='-n 1', ), terminal_output=dict(mandatory=True, nohash=True, ), environ=dict(nohash=True, usedefault=True, ), similarity_metric=dict(argstr='-s %s', ), reference_image=dict(copyfile=True, mandatory=True, argstr='-r %s', ), input_image=dict(copyfile=False, mandatory=True, argstr='-i %s', ), inverse_warp_template_labels=dict(argstr='-l', ), dimension=dict(position=1, usedefault=True, argstr='-d %d', ), max_iterations=dict(sep='x', argstr='-m %s', ), ) inputs = GenWarpFields.input_spec() for key, metadata in input_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(inputs.traits()[key], metakey), value
def test_GenWarpFields_outputs(): output_map = dict( affine_transformation=dict(), input_file=dict(), inverse_warp_field=dict(), output_file=dict(), warp_field=dict(), ) outputs = GenWarpFields.output_spec() for key, metadata in output_map.items(): for metakey, value in metadata.items(): yield assert_equal, getattr(outputs.traits()[key], metakey), value
def create_ants_nonlinear_xfm(name='ants_nonlinear_xfm'): """ Calculates the nonlinear ANTS registration transform. Parameters ---------- name : string, optional Name of the workflow. Returns ------- ants_nonlinear_xfm : nipype.pipeline.engine.Workflow Notes ----- Workflow Inputs:: inputspec.anatomical_brain : string (nifti file) File of brain to be normalized (registered) inputspec.reference_brain : string (nifti file) Target brain file to normalize to Workflow Outputs:: outputspec.warp_field : string (nifti file) Output warp field of registration outputspec.affine_transformation : text file Affine matrix of nonlinear transformation of brain file outputspec.inverse_warp : string (nifti file) Inverse of the warp field of the registration outputspec.output_brain : string (nifti file) Template-registered version of input brain Registration Procedure: 1. Performs a nonlinear anatomical-to-template registration. Workflow Graph: .. image:: :width: 500 Detailed Workflow Graph: .. image:: :width: 500 """ from nipype.interfaces.ants.legacy import GenWarpFields ants_nonlinear_xfm = pe.Workflow(name=name) inputspec = pe.Node( util.IdentityInterface(fields=['anatomical_brain', 'reference_brain']), name='inputspec') # use ANTS to warp the masked anatomical image to a template image warp_brain = pe.Node(GenWarpFields(), name='warp_brain') outputspec = pe.Node(util.IdentityInterface(fields=[ 'warp_field', 'affine_transformation', 'inverse_warp', 'output_brain' ]), name='outputspec') ants_nonlinear_xfm.connect(inputspec, 'anatomical_brain', warp_brain, 'input_image') ants_nonlinear_xfm.connect(inputspec, 'reference_brain', warp_brain, 'reference_image') ants_nonlinear_xfm.connect(warp_brain, 'warp_field', outputspec, 'warp_field') ants_nonlinear_xfm.connect(warp_brain, 'affine_transformation', outputspec, 'affine_transformation') ants_nonlinear_xfm.connect(warp_brain, 'inverse_warp_field', outputspec, 'inverse_warp') ants_nonlinear_xfm.connect(warp_brain, 'output_file', outputspec, 'output_brain') return ants_nonlinear_xfm
def get_struct_norm_workflow(name='normalize_struct'): """ Base structural workflow for normalization Parameters ---------- name : name of workflow. Default = 'normalize_struct' Inputs ------ inputspec.template_file : inputspec.brain : inputspec.segmentation : Outputs ------- outputspec.warp_field : outputspec.affine_transformation : outputspec.inverse_warp : outputspec.unwarped_brain : outputspec.warped_brain : Returns ------- workflow : structural normalization workflow """ #inputs to workflow import nipype.interfaces.freesurfer as fs import nipype.interfaces.ants as ants from nipype.interfaces.ants.legacy import GenWarpFields import nipype.pipeline.engine as pe import nipype.interfaces.utility as util inputspec = pe.Node(util.IdentityInterface( fields=['template_file', 'brain', 'segmentation']), name='inputspec') #converts brain from freesurfer mgz into nii brain_2nii = pe.Node(fs.preprocess.MRIConvert(), name='brain_2nii') brain_2nii.inputs.out_type = 'nii' #converts freesurfer segmentation into nii aparcaseg_2nii = pe.Node(fs.preprocess.MRIConvert(), name='aparcaseg_2nii') aparcaseg_2nii.inputs.out_type = 'nii' #create mask excluding everything outside of cortical surface (from #freesurfer segmentation file) create_mask = pe.Node(fs.model.Binarize(), name='create_mask') create_mask.inputs.min = 1 create_mask.inputs.dilate = 1 create_mask.inputs.out_type = 'nii' #apply mask to anatomical apply_mask = pe.Node(fs.utils.ApplyMask(), name='apply_mask') #use ANTS to warp the masked anatomical image to a template image warp_brain = pe.Node(GenWarpFields(), name='warp_brain') #collects workflow outputs outputspec = pe.Node(util.IdentityInterface(fields=[ 'warp_field', 'affine_transformation', 'inverse_warp', 'unwarped_brain', 'warped_brain' ]), name='outputspec') normalize_struct = pe.Workflow(name=name) normalize_struct.connect([ (inputspec, warp_brain, [('template_file', 'reference_image')]), (inputspec, brain_2nii, [('brain', 'in_file')]), (inputspec, aparcaseg_2nii, [('segmentation', 'in_file')]), (aparcaseg_2nii, create_mask, [('out_file', 'in_file')]), (create_mask, apply_mask, [('binary_file', 'mask_file')]), (brain_2nii, apply_mask, [('out_file', 'in_file')]), (apply_mask, warp_brain, [('out_file', 'input_image')]), (apply_mask, outputspec, [('out_file', 'unwarped_brain')]), (warp_brain, outputspec, [('affine_transformation', 'affine_transformation'), ('warp_field', 'warp_field'), ('inverse_warp_field', 'inverse_warp'), ('output_file', 'warped_brain')]) ]) return normalize_struct