def hmc_afni(name='fMRI_HMC_afni', st_correct=False): """A head motion correction (HMC) workflow for functional scans""" workflow = pe.Workflow(name=name) inputnode = pe.Node(niu.IdentityInterface( fields=['in_file', 'fd_radius', 'start_idx', 'stop_idx']), name='inputnode') outputnode = pe.Node( niu.IdentityInterface(fields=['out_file', 'out_movpar']), name='outputnode') drop_trs = pe.Node(afp.Calc(expr='a', outputtype='NIFTI_GZ'), name='drop_trs') deoblique = pe.Node(afp.Refit(deoblique=True), name='deoblique') reorient = pe.Node(afp.Resample(orientation='RPI', outputtype='NIFTI_GZ'), name='reorient') get_mean_RPI = pe.Node(afp.TStat(options='-mean', outputtype='NIFTI_GZ'), name='get_mean_RPI') # calculate hmc parameters hmc = pe.Node(afp.Volreg(args='-Fourier -twopass', zpad=4, outputtype='NIFTI_GZ'), name='motion_correct') get_mean_motion = get_mean_RPI.clone('get_mean_motion') hmc_A = hmc.clone('motion_correct_A') hmc_A.inputs.md1d_file = 'max_displacement.1D' movpar = pe.Node(niu.Function(function=fd_jenkinson, input_names=['in_file', 'rmax'], output_names=['out_file']), name='Mat2Movpar') workflow.connect([(inputnode, drop_trs, [('in_file', 'in_file_a'), ('start_idx', 'start_idx'), ('stop_idx', 'stop_idx')]), (inputnode, movpar, [('fd_radius', 'rmax')]), (deoblique, reorient, [('out_file', 'in_file')]), (reorient, get_mean_RPI, [('out_file', 'in_file')]), (reorient, hmc, [('out_file', 'in_file')]), (get_mean_RPI, hmc, [('out_file', 'basefile')]), (hmc, get_mean_motion, [('out_file', 'in_file')]), (reorient, hmc_A, [('out_file', 'in_file')]), (get_mean_motion, hmc_A, [('out_file', 'basefile')]), (hmc_A, outputnode, [('out_file', 'out_file')]), (hmc_A, movpar, [('oned_matrix_save', 'in_file')]), (movpar, outputnode, [('out_file', 'out_movpar')])]) if st_correct: st_corr = pe.Node(afp.TShift(outputtype='NIFTI_GZ'), name='TimeShifts') workflow.connect([(drop_trs, st_corr, [('out_file', 'in_file')]), (st_corr, deoblique, [('out_file', 'in_file')])]) else: workflow.connect([(drop_trs, deoblique, [('out_file', 'in_file')])]) return workflow
def slice_timing_wf(name='slice_timing'): # allocate a workflow object wf = pe.Workflow(name=name) # configure the workflow's input spec inputNode = pe.Node( util.IdentityInterface(fields=['func_ts', 'tr', 'tpattern']), name='inputspec') # configure the workflow's output spec outputNode = pe.Node( util.IdentityInterface(fields=['slice_time_corrected']), name='outputspec') # create TShift AFNI node func_slice_timing_correction = pe.Node(interface=preprocess.TShift(), name='slice_timing') func_slice_timing_correction.inputs.outputtype = 'NIFTI_GZ' wf.connect([ ( inputNode, func_slice_timing_correction, [ ('func_ts', 'in_file'), ( # add the @ prefix to the tpattern file going into # AFNI 3dTshift - needed this so the tpattern file # output from get_scan_params would be tied downstream # via a connection (to avoid poofing) ('tpattern', nullify, add_afni_prefix), 'tpattern'), (('tr', nullify), 'tr'), ]), ]) wf.connect(func_slice_timing_correction, 'out_file', outputNode, 'slice_time_corrected') return wf
def create_bo_func_preproc(slice_timing_correction = False, wf_name = 'bo_func_preproc'): """ The main purpose of this workflow is to process functional data. Raw rest file is deobliqued and reoriented into RPI. Then take the mean intensity values over all time points for each voxel and use this image to calculate motion parameters. The image is then skullstripped, normalized and a processed mask is obtained to use it further in Image analysis. Parameters ---------- slice_timing_correction : boolean Slice timing Correction option wf_name : string Workflow name Returns ------- func_preproc : workflow object Functional Preprocessing workflow object Notes ----- `Source <https://github.com/FCP-INDI/C-PAC/blob/master/CPAC/func_preproc/func_preproc.py>`_ Workflow Inputs:: inputspec.rest : func/rest file or a list of func/rest nifti file User input functional(T2) Image, in any of the 8 orientations inputspec.start_idx : string Starting volume/slice of the functional image (optional) inputspec.stop_idx : string Last volume/slice of the functional image (optional) scan_params.tr : string Subject TR scan_params.acquistion : string Acquisition pattern (interleaved/sequential, ascending/descending) scan_params.ref_slice : integer Reference slice for slice timing correction Workflow Outputs:: outputspec.drop_tr : string (nifti file) Path to Output image with the initial few slices dropped outputspec.slice_time_corrected : string (nifti file) Path to Slice time corrected image outputspec.refit : string (nifti file) Path to deobliqued anatomical data outputspec.reorient : string (nifti file) Path to RPI oriented anatomical data outputspec.motion_correct_ref : string (nifti file) Path to Mean intensity Motion corrected image (base reference image for the second motion correction run) outputspec.motion_correct : string (nifti file) Path to motion corrected output file outputspec.max_displacement : string (Mat file) Path to maximum displacement (in mm) for brain voxels in each volume outputspec.movement_parameters : string (Mat file) Path to 1D file containing six movement/motion parameters(3 Translation, 3 Rotations) in different columns (roll pitch yaw dS dL dP) outputspec.skullstrip : string (nifti file) Path to skull stripped Motion Corrected Image outputspec.mask : string (nifti file) Path to brain-only mask outputspec.example_func : string (nifti file) Mean, Skull Stripped, Motion Corrected output T2 Image path (Image with mean intensity values across voxels) outputpsec.preprocessed : string (nifti file) output skull stripped, motion corrected T2 image with normalized intensity values outputspec.preprocessed_mask : string (nifti file) Mask obtained from normalized preprocessed image Order of commands: - Get the start and the end volume index of the functional run. If not defined by the user, return the first and last volume. get_idx(in_files, stop_idx, start_idx) - Dropping the initial TRs. For details see `3dcalc <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dcalc.html>`_:: 3dcalc -a rest.nii.gz[4..299] -expr 'a' -prefix rest_3dc.nii.gz - Slice timing correction. For details see `3dshift <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dTshift.html>`_:: 3dTshift -TR 2.1s -slice 18 -tpattern alt+z -prefix rest_3dc_shift.nii.gz rest_3dc.nii.gz - Deobliqing the scans. For details see `3drefit <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3drefit.html>`_:: 3drefit -deoblique rest_3dc.nii.gz - Re-orienting the Image into Right-to-Left Posterior-to-Anterior Inferior-to-Superior (RPI) orientation. For details see `3dresample <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dresample.html>`_:: 3dresample -orient RPI -prefix rest_3dc_RPI.nii.gz -inset rest_3dc.nii.gz - Calculate voxel wise statistics. Get the RPI Image with mean intensity values over all timepoints for each voxel. For details see `3dTstat <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dTstat.html>`_:: 3dTstat -mean -prefix rest_3dc_RPI_3dT.nii.gz rest_3dc_RPI.nii.gz - Motion Correction. For details see `3dvolreg <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dvolreg.html>`_:: 3dvolreg -Fourier -twopass -base rest_3dc_RPI_3dT.nii.gz/ -zpad 4 -maxdisp1D rest_3dc_RPI_3dvmd1D.1D -1Dfile rest_3dc_RPI_3dv1D.1D -prefix rest_3dc_RPI_3dv.nii.gz rest_3dc_RPI.nii.gz The base image or the reference image is the mean intensity RPI image obtained in the above the step.For each volume in RPI-oriented T2 image, the command, aligns the image with the base mean image and calculates the motion, displacement and movement parameters. It also outputs the aligned 4D volume and movement and displacement parameters for each volume. - Calculate voxel wise statistics. Get the motion corrected output Image from the above step, with mean intensity values over all timepoints for each voxel. For details see `3dTstat <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dTstat.html>`_:: 3dTstat -mean -prefix rest_3dc_RPI_3dv_3dT.nii.gz rest_3dc_RPI_3dv.nii.gz - Motion Correction and get motion, movement and displacement parameters. For details see `3dvolreg <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dvolreg.html>`_:: 3dvolreg -Fourier -twopass -base rest_3dc_RPI_3dv_3dT.nii.gz -zpad 4 -maxdisp1D rest_3dc_RPI_3dvmd1D.1D -1Dfile rest_3dc_RPI_3dv1D.1D -prefix rest_3dc_RPI_3dv.nii.gz rest_3dc_RPI.nii.gz The base image or the reference image is the mean intensity motion corrected image obtained from the above the step (first 3dvolreg run). For each volume in RPI-oriented T2 image, the command, aligns the image with the base mean image and calculates the motion, displacement and movement parameters. It also outputs the aligned 4D volume and movement and displacement parameters for each volume. - Unwarp the motion corrected using a regularized B0 field map that is in RPI space, one of the outputs from the calib_preproc using FSL FUGUE fugue --nocheck=on -i rest_3dc_RPI_3dv.nii.gz --loadfmap=cal_reg_bo_RPI --unwarpdir=x --dwell=1 -u rest_3dc_RPI_3dv_unwarped.nii.gz - Create a brain-only mask. For details see `3dautomask <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dAutomask.html>`_:: 3dAutomask -prefix rest_3dc_RPI_3dv_unwarped_automask.nii.gz rest_3dc_RPI_3dv_unwarped.nii.gz - Edge Detect(remove skull) and get the brain only. For details see `3dcalc <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dcalc.html>`_:: 3dcalc -a rest_3dc_RPI_3dv_unwarped.nii.gz -b rest_3dc_RPI_3dv_unwarped_automask.nii.gz -expr 'a*b' -prefix rest_3dc_RPI_3dv_unwarped_3dc.nii.gz - Normalizing the image intensity values. For details see `fslmaths <http://www.fmrib.ox.ac.uk/fsl/avwutils/index.html>`_:: fslmaths rest_3dc_RPI_3dv_unwarped_3dc.nii.gz -ing 10000 rest_3dc_RPI_3dv_unwarped_3dc_maths.nii.gz -odt float Normalized intensity = (TrueValue*10000)/global4Dmean - Calculate mean of skull stripped image. For details see `3dTstat <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dTstat.html>`_:: 3dTstat -mean -prefix rest_3dc_RPI_3dv_unwarped_3dc_3dT.nii.gz rest_3dc_RPI_3dv_unwarped_3dc.nii.gz - Create Mask (Generate mask from Normalized data). For details see `fslmaths <http://www.fmrib.ox.ac.uk/fsl/avwutils/index.html>`_:: fslmaths rest_3dc_RPI_3dv_unwarped_3dc_maths.nii.gz -Tmin -bin rest_3dc_RPI_3dv_unwarped_3dc_maths_maths.nii.gz -odt char High Level Workflow Graph: .. image:: ../images/func_preproc.dot.png :width: 1000 Detailed Workflow Graph: .. image:: ../images/func_preproc_detailed.dot.png :width: 1000 Examples -------- >>> from func_preproc import * >>> preproc = create_func_preproc(slice_timing_correction=True) >>> preproc.inputs.inputspec.func='sub1/func/rest.nii.gz' >>> preproc.inputs.scan_params.TR = '2.0' >>> preproc.inputs.scan_params.ref_slice = 19 >>> preproc.inputs.scan_params.acquisition = 'alt+z2' >>> preproc.run() #doctest: +SKIP >>> from func_preproc import * >>> preproc = create_func_preproc(slice_timing_correction=False) >>> preproc.inputs.inputspec.func='sub1/func/rest.nii.gz' >>> preproc.inputs.inputspec.start_idx = 4 >>> preproc.inputs.inputspec.stop_idx = 250 >>> preproc.run() #doctest: +SKIP """ preproc = pe.Workflow(name=wf_name) inputNode = pe.Node(util.IdentityInterface(fields=['rest', 'calib_reg_bo_RPI', 'start_idx', 'stop_idx']), name='inputspec') scan_params = pe.Node(util.IdentityInterface(fields=['tr', 'acquisition', 'ref_slice']), name = 'scan_params') outputNode = pe.Node(util.IdentityInterface(fields=['drop_tr', 'refit', 'reorient', 'reorient_mean', 'motion_correct', 'motion_correct_ref', 'movement_parameters', 'max_displacement', 'bo_unwarped', 'mask', 'mask_preunwarp', 'skullstrip', 'example_func', 'preprocessed', 'preprocessed_mask', 'slice_time_corrected']), name='outputspec') func_get_idx = pe.Node(util.Function(input_names=['in_files', 'stop_idx', 'start_idx'], output_names=['stopidx', 'startidx'], function=get_idx), name='func_get_idx') preproc.connect(inputNode, 'rest', func_get_idx, 'in_files') preproc.connect(inputNode, 'start_idx', func_get_idx, 'start_idx') preproc.connect(inputNode, 'stop_idx', func_get_idx, 'stop_idx') func_drop_trs = pe.Node(interface=preprocess.Calc(), name='func_drop_trs') func_drop_trs.inputs.expr = 'a' func_drop_trs.inputs.outputtype = 'NIFTI_GZ' preproc.connect(inputNode, 'rest', func_drop_trs, 'in_file_a') preproc.connect(func_get_idx, 'startidx', func_drop_trs, 'start_idx') preproc.connect(func_get_idx, 'stopidx', func_drop_trs, 'stop_idx') preproc.connect(func_drop_trs, 'out_file', outputNode, 'drop_tr') func_slice_timing_correction = pe.Node(interface=preprocess.TShift(), name = 'func_slice_timing_correction') func_slice_timing_correction.inputs.outputtype = 'NIFTI_GZ' func_deoblique = pe.Node(interface=preprocess.Refit(), name='func_deoblique') func_deoblique.inputs.deoblique = True if slice_timing_correction: preproc.connect(func_drop_trs, 'out_file', func_slice_timing_correction,'in_file') preproc.connect(scan_params, 'tr', func_slice_timing_correction, 'tr') preproc.connect(scan_params, 'acquisition', func_slice_timing_correction, 'tpattern') preproc.connect(scan_params, 'ref_slice', func_slice_timing_correction, 'tslice') preproc.connect(func_slice_timing_correction, 'out_file', func_deoblique, 'in_file') preproc.connect(func_slice_timing_correction, 'out_file', outputNode, 'slice_time_corrected') else: preproc.connect(func_drop_trs, 'out_file', func_deoblique, 'in_file') preproc.connect(func_deoblique, 'out_file', outputNode, 'refit') func_reorient = pe.Node(interface=preprocess.Resample(), name='func_reorient') func_reorient.inputs.orientation = 'RPI' func_reorient.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_deoblique, 'out_file', func_reorient, 'in_file') preproc.connect(func_reorient, 'out_file', outputNode, 'reorient') func_get_mean_RPI = pe.Node(interface=preprocess.TStat(), name='func_get_mean_RPI') func_get_mean_RPI.inputs.options = '-mean' func_get_mean_RPI.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_reorient, 'out_file', func_get_mean_RPI, 'in_file') #calculate motion parameters func_motion_correct = pe.Node(interface=preprocess.Volreg(), name='func_motion_correct') func_motion_correct.inputs.args = '-Fourier -twopass' func_motion_correct.inputs.zpad = 4 func_motion_correct.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_reorient, 'out_file', func_motion_correct, 'in_file') preproc.connect(func_get_mean_RPI, 'out_file', func_motion_correct, 'basefile') func_get_mean_motion = func_get_mean_RPI.clone('func_get_mean_motion') preproc.connect(func_motion_correct, 'out_file', func_get_mean_motion, 'in_file') preproc.connect(func_get_mean_motion, 'out_file', outputNode, 'motion_correct_ref') func_motion_correct_A = func_motion_correct.clone('func_motion_correct_A') func_motion_correct_A.inputs.md1d_file = 'max_displacement.1D' preproc.connect(func_reorient, 'out_file', func_motion_correct_A, 'in_file') preproc.connect(func_get_mean_motion, 'out_file', func_motion_correct_A, 'basefile') preproc.connect(func_motion_correct_A, 'out_file', outputNode, 'motion_correct') preproc.connect(func_motion_correct_A, 'md1d_file', outputNode, 'max_displacement') preproc.connect(func_motion_correct_A, 'oned_file', outputNode, 'movement_parameters') func_get_brain_mask = pe.Node(interface=preprocess.Automask(), name='func_get_brain_mask') # func_get_brain_mask.inputs.dilate = 1 func_get_brain_mask.inputs.outputtype = 'NIFTI_GZ' #------------------------- func_bo_unwarp = pe.Node(interface=fsl.FUGUE(),name='bo_unwarp') #func_bo_unwarp.inputs.in_file='lfo_mc' func_bo_unwarp.inputs.args='--nocheck=on' #func_bo_unwarp.inputs.fmap_in_file='calib' func_bo_unwarp.inputs.dwell_time=1.0 func_bo_unwarp.inputs.unwarp_direction='x' preproc.connect(inputNode, 'calib_reg_bo_RPI', func_bo_unwarp, 'fmap_in_file') preproc.connect(func_motion_correct_A, 'out_file', func_bo_unwarp, 'in_file') preproc.connect(func_bo_unwarp, 'unwarped_file', outputNode, 'bo_unwarped') preproc.connect(func_bo_unwarp, 'unwarped_file', func_get_brain_mask, 'in_file') #-------------------------- preproc.connect(func_get_brain_mask, 'out_file', outputNode, 'mask') #------------ ALSO give the example_func_preunwarp func_get_brain_mask_A = func_get_brain_mask.clone('func_get_brain_mask_A') preproc.connect(func_motion_correct_A, 'out_file', func_get_brain_mask_A, 'in_file') preproc.connect(func_get_brain_mask_A, 'out_file', outputNode, 'mask_preunwarp') func_edge_detect = pe.Node(interface=preprocess.Calc(), name='func_edge_detect') func_edge_detect.inputs.expr = 'a*b' func_edge_detect.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_bo_unwarp, 'unwarped_file', func_edge_detect, 'in_file_a') preproc.connect(func_get_brain_mask, 'out_file', func_edge_detect, 'in_file_b') preproc.connect(func_edge_detect, 'out_file', outputNode, 'skullstrip') func_mean_skullstrip = pe.Node(interface=preprocess.TStat(), name='func_mean_skullstrip') func_mean_skullstrip.inputs.options = '-mean' func_mean_skullstrip.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_edge_detect, 'out_file', func_mean_skullstrip, 'in_file') preproc.connect(func_mean_skullstrip, 'out_file', outputNode, 'example_func') func_normalize = pe.Node(interface=fsl.ImageMaths(), name='func_normalize') func_normalize.inputs.op_string = '-ing 10000' func_normalize.inputs.out_data_type = 'float' preproc.connect(func_edge_detect, 'out_file', func_normalize, 'in_file') preproc.connect(func_normalize, 'out_file', outputNode, 'preprocessed') func_mask_normalize = pe.Node(interface=fsl.ImageMaths(), name='func_mask_normalize') func_mask_normalize.inputs.op_string = '-Tmin -bin' func_mask_normalize.inputs.out_data_type = 'char' preproc.connect(func_normalize, 'out_file', func_mask_normalize, 'in_file') preproc.connect(func_mask_normalize, 'out_file', outputNode, 'preprocessed_mask') return preproc
def func_motion_correct_workflow(workflow, resource_pool, config): # resource pool should have: # functional_scan import os import sys import nipype.interfaces.io as nio import nipype.pipeline.engine as pe import nipype.interfaces.utility as util import nipype.interfaces.fsl.maths as fsl from nipype.interfaces.afni import preprocess from workflow_utils import check_input_resources, \ check_config_settings check_input_resources(resource_pool, "functional_scan") check_config_settings(config, "start_idx") check_config_settings(config, "stop_idx") check_config_settings(config, "slice_timing_correction") func_get_idx = pe.Node(util.Function( input_names=['in_files', 'stop_idx', 'start_idx'], output_names=['stopidx', 'startidx'], function=get_idx), name='func_get_idx') func_get_idx.inputs.in_files = resource_pool["functional_scan"] func_get_idx.inputs.start_idx = config["start_idx"] func_get_idx.inputs.stop_idx = config["stop_idx"] func_drop_trs = pe.Node(interface=preprocess.Calc(), name='func_drop_trs') func_drop_trs.inputs.in_file_a = resource_pool["functional_scan"] func_drop_trs.inputs.expr = 'a' func_drop_trs.inputs.outputtype = 'NIFTI_GZ' workflow.connect(func_get_idx, 'startidx', func_drop_trs, 'start_idx') workflow.connect(func_get_idx, 'stopidx', func_drop_trs, 'stop_idx') #workflow.connect(func_drop_trs, 'out_file', # outputNode, 'drop_tr') func_slice_timing_correction = pe.Node(interface=preprocess.TShift(), name='func_slice_time_correction') func_slice_timing_correction.inputs.outputtype = 'NIFTI_GZ' func_deoblique = pe.Node(interface=preprocess.Refit(), name='func_deoblique') func_deoblique.inputs.deoblique = True if config["slice_timing_correction"] == True: workflow.connect(func_drop_trs, 'out_file', func_slice_timing_correction, 'in_file') workflow.connect(func_slice_timing_correction, 'out_file', func_deoblique, 'in_file') else: workflow.connect(func_drop_trs, 'out_file', func_deoblique, 'in_file') func_reorient = pe.Node(interface=preprocess.Resample(), name='func_reorient') func_reorient.inputs.orientation = 'RPI' func_reorient.inputs.outputtype = 'NIFTI_GZ' workflow.connect(func_deoblique, 'out_file', func_reorient, 'in_file') func_get_mean_RPI = pe.Node(interface=preprocess.TStat(), name='func_get_mean_RPI') func_get_mean_RPI.inputs.options = '-mean' func_get_mean_RPI.inputs.outputtype = 'NIFTI_GZ' workflow.connect(func_reorient, 'out_file', func_get_mean_RPI, 'in_file') # calculate motion parameters func_motion_correct = pe.Node(interface=preprocess.Volreg(), name='func_motion_correct') func_motion_correct.inputs.args = '-Fourier -twopass' func_motion_correct.inputs.zpad = 4 func_motion_correct.inputs.outputtype = 'NIFTI_GZ' workflow.connect(func_reorient, 'out_file', func_motion_correct, 'in_file') workflow.connect(func_get_mean_RPI, 'out_file', func_motion_correct, 'basefile') func_get_mean_motion = func_get_mean_RPI.clone('func_get_mean_motion') workflow.connect(func_motion_correct, 'out_file', func_get_mean_motion, 'in_file') func_motion_correct_A = func_motion_correct.clone('func_motion_correct_A') func_motion_correct_A.inputs.md1d_file = 'max_displacement.1D' workflow.connect(func_reorient, 'out_file', func_motion_correct_A, 'in_file') workflow.connect(func_get_mean_motion, 'out_file', func_motion_correct_A, 'basefile') resource_pool["func_motion_correct"] = (func_motion_correct_A, 'out_file') resource_pool["coordinate_transformation"] = \ (func_motion_correct_A, 'oned_matrix_save') return workflow, resource_pool