def anatomical_preprocessing(): ''' Inputs: MP2RAGE Skull stripped image using Spectre-2010 Workflow: 1. reorient to RPI 2. create a brain mask Returns: brain brain_mask ''' # define workflow flow = Workflow('anat_preprocess') inputnode = Node(util.IdentityInterface( fields=['anat', 'anat_gm', 'anat_wm', 'anat_csf', 'anat_first']), name='inputnode') outputnode = Node(util.IdentityInterface(fields=[ 'brain', 'brain_gm', 'brain_wm', 'brain_csf', 'brain_first', 'brain_mask', ]), name='outputnode') reorient = Node(interface=preprocess.Resample(), name='anat_reorient') reorient.inputs.orientation = 'RPI' reorient.inputs.outputtype = 'NIFTI' erode = Node(interface=fsl.ErodeImage(), name='anat_preproc') reorient_gm = reorient.clone('anat_preproc_gm') reorient_wm = reorient.clone('anat_preproc_wm') reorient_cm = reorient.clone('anat_preproc_csf') reorient_first = reorient.clone('anat_preproc_first') make_mask = Node(interface=fsl.UnaryMaths(), name='anat_preproc_mask') make_mask.inputs.operation = 'bin' # connect workflow nodes flow.connect(inputnode, 'anat', reorient, 'in_file') flow.connect(inputnode, 'anat_gm', reorient_gm, 'in_file') flow.connect(inputnode, 'anat_wm', reorient_wm, 'in_file') flow.connect(inputnode, 'anat_csf', reorient_cm, 'in_file') flow.connect(inputnode, 'anat_first', reorient_first, 'in_file') flow.connect(reorient, 'out_file', erode, 'in_file') flow.connect(erode, 'out_file', make_mask, 'in_file') flow.connect(make_mask, 'out_file', outputnode, 'brain_mask') flow.connect(erode, 'out_file', outputnode, 'brain') flow.connect(reorient_gm, 'out_file', outputnode, 'brain_gm') flow.connect(reorient_wm, 'out_file', outputnode, 'brain_wm') flow.connect(reorient_cm, 'out_file', outputnode, 'brain_csf') flow.connect(reorient_first, 'out_file', outputnode, 'brain_first') return flow
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 mri_reorient_wf(name='ReorientWorkflow'): """A workflow to reorient images to 'RPI' orientation""" workflow = pe.Workflow(name=name) inputnode = pe.Node(niu.IdentityInterface(fields=['in_file']), name='inputnode') outputnode = pe.Node(niu.IdentityInterface(fields=['out_file']), name='outputnode') deoblique = pe.Node(afp.Refit(deoblique=True), name='deoblique') reorient = pe.Node(afp.Resample(orientation='RPI', outputtype='NIFTI_GZ'), name='reorient') workflow.connect([(inputnode, deoblique, [('in_file', 'in_file')]), (deoblique, reorient, [('out_file', 'in_file')]), (reorient, outputnode, [('out_file', 'out_file')])]) return workflow
def anatomical_reorient_workflow(workflow, resource_pool, config): # resource pool should have: # anatomical_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_input_resources(resource_pool, "anatomical_scan") anat_deoblique = pe.Node(interface=preprocess.Refit(), name='anat_deoblique') anat_deoblique.inputs.in_file = resource_pool["anatomical_scan"] anat_deoblique.inputs.deoblique = True anat_reorient = pe.Node(interface=preprocess.Resample(), name='anat_reorient') anat_reorient.inputs.orientation = 'RPI' anat_reorient.inputs.outputtype = 'NIFTI_GZ' workflow.connect(anat_deoblique, 'out_file', anat_reorient, 'in_file') resource_pool["anatomical_reorient"] = (anat_reorient, 'out_file') return workflow, resource_pool
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 anatomical_reorient_workflow(workflow, resource_pool, config, name="_"): """Build a Nipype workflow to deoblique and reorient an anatomical scan from a NIFTI file. - This is a seminal workflow that can only take an input directly from disk (i.e. no Nipype workflow connections/pointers, and this is where the pipeline will actually begin). For the sake of building the pipeine in reverse, if this workflow is called when there is no input file available, this function will return the unmodified workflow and resource pool directly back. - In conjunction with the other workflow-building functions, if this function returns the workflow and resource pool unmodified, each function up will do the same until it reaches the top level, allowing the pipeline builder to continue "searching" for a base-level input without crashing at this one. Expected Resources in Resource Pool - anatomical_scan: The raw anatomical scan in a NIFTI image. New Resources Added to Resource Pool - anatomical_reorient: The deobliqued, reoriented anatomical scan. Workflow Steps 1. AFNI's 3drefit to deoblique the anatomical scan. 2. AFNI's 3dresample to reorient the deobliqued anatomical scan to RPI. :type workflow: Nipype workflow object :param workflow: A Nipype workflow object which can already contain other connected nodes; this function will insert the following workflow into this one provided. :type resource_pool: dict :param resource_pool: A dictionary defining input files and pointers to Nipype node outputs / workflow connections; the keys are the resource names. :type config: dict :param config: A dictionary defining the configuration settings for the workflow, such as directory paths or toggled options. :type name: str :param name: (default: "_") A string to append to the end of each node name. :rtype: Nipype workflow object :return: The Nipype workflow originally provided, but with this function's sub-workflow connected into it. :rtype: dict :return: The resource pool originally provided, but updated (if applicable) with the newest outputs and connections. """ import nipype.pipeline.engine as pe from nipype.interfaces.afni import preprocess if "anatomical_scan" not in resource_pool.keys(): return workflow, resource_pool anat_deoblique = pe.Node(interface=preprocess.Refit(), name='anat_deoblique%s' % name) anat_deoblique.inputs.in_file = resource_pool["anatomical_scan"] anat_deoblique.inputs.deoblique = True anat_reorient = pe.Node(interface=preprocess.Resample(), name='anat_reorient%s' % name) anat_reorient.inputs.orientation = 'RPI' anat_reorient.inputs.outputtype = 'NIFTI_GZ' workflow.connect(anat_deoblique, 'out_file', anat_reorient, 'in_file') resource_pool["anatomical_reorient"] = (anat_reorient, 'out_file') return workflow, resource_pool
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
def create_calib_preproc(wf_name='calib_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 ---------- wf_name : string Workflow name Returns ------- calib_preproc : workflow object Functional Preprocessing workflow object Notes ----- Workflow Inputs:: inputspec.bo_name : cal_bo nifti filepath B0 field map from CBI calibration scan inputspec.rho_name : cal_rho nifti filepath spin density image CBI calibration scan inputspec.rs_name : cal_rs nifti filepath R2* image CBI calibration scan inputspec.reg_bo_name : regularized cal_bo nifti filepath smoothed B0 field map from CBI calibration scan for unwarping inputspec.echo_spacing : string - default 30 Echo Time in units of echo spacing inputspec.readout_dir : string - default 1 Last volume/slice of the functional image (optional) inputspec.synth_name : cal_synth nifti NAME only MUST end in '.nii.gz' - default cal_synth.nii.gz synthetic image simulation the conditions of B0 dropout and other epi distortions Workflow Outputs:: outputspec.cal_bo_RPI : string (nifti file) deobliqued reoriented B0 field map from CBI calibration scan outputspec.cal_rho_RPI : string (nifti file) deobliqued reoriented spin density image CBI calibration scan outputspec.cal_rs_RPI : string (nifti file) deobliqued reoriented R2* image CBI calibration scan outputspec.cal_reg_bo_RPI : regularized cal_bo nifti filepath deobliqued reoriented smoothed B0 field map from CBI calibration scan for unwarping outputspec.cal_synth_RPI : string (nifti file) deobliqued reoriented synthetic image Order of commands: 1. Deoblique bo image 2. Deoblique rho Image 3. Deoblique rs image 4. Use 1,2,3 in cbiCalibSynthEPI program with TE=30, readout_dir=1 and cal_synth as defaults 5. Reorient to RPI, 1,2,3 and output 6. Reorient to RPI 4 and output 7. Deoblique cal_reg_bo image 8. Reorient 7 and output High Level Workflow Graph: .. image:: ../images/calib_preproc.dot.png :width: 1000 Detailed Workflow Graph: .. image:: ../images/calib_preproc_detailed.dot.png :width: 1000 Examples -------- >>> from calib_preproc import * >>> preproc = create_calib_preproc() >>> preproc.inputs.inputspec.bo_name='/sam/wave1/sub4974/calib_1/cal_bo.nii.gz' >>> preproc.inputs.inputspec.rho_name='/sam/wave1/sub4974/calib_1/cal_rho.nii.gz' >>> preproc.inputs.inputspec.rs_name='/sam/wave1/sub4974/calib_1/cal_rs.nii.gz' >>> preproc.inputs.inputspec.reg_bo_name='/sam/wave1/sub4974/calib_1/cal_reg_bo.nii.gz' >>> preproc.base_dir='./' >>> preproc.run() >>> from calib_preproc import * >>> preproc = create_calib_preproc() >>> preproc.inputs.inputspec.bo_name='/sam/wave1/sub4974/calib_1/cal_bo.nii.gz' >>> preproc.inputs.inputspec.rho_name='/sam/wave1/sub4974/calib_1/cal_rho.nii.gz' >>> preproc.inputs.inputspec.rs_name='/sam/wave1/sub4974/calib_1/cal_rs.nii.gz' >>> preproc.inputs.inputspec.reg_bo_name='/sam/wave1/sub4974/calib_1/cal_reg_bo.nii.gz' >>> preproc.inputs.inputspec.reg_bo_name='/sam/wave1/sub4974/calib_1/cal_reg_bo.nii.gz' >>> preproc.inputs.inputspec.synth_name='cal_epi_synth.nii.gz' >>> preproc.inputs.inputspec.echo_spacing='30' >>> preproc.inputs.inputspec.readout_dir='1' >>> preproc.base_dir='./' >>> preproc.run() """ preproc = pe.Workflow(name=wf_name) inputNode = pe.Node(util.IdentityInterface(fields=[ 'bo_name', 'rho_name', 'rs_name', 'reg_bo_name', 'echo_spacing', 'readout_dir', 'synth_name' ]), name='inputspec') outputNode = pe.Node(util.IdentityInterface(fields=[ 'cal_bo_RPI', 'cal_rho_RPI', 'cal_rs_RPI', 'cal_reg_bo_RPI', 'cal_synth_RPI' ]), name='outputspec') func_create_synth = pe.Node(util.Function(input_names=[ 'boName', 'rhoName', 'rsName', 'TE', 'kyDir', 'synthName' ], output_names=['epi_synth_path'], function=cbiCalibSynthEPI), name='func_create_synth') bo_deoblique = pe.Node(interface=preprocess.Refit(), name='bo_deoblique') bo_deoblique.inputs.deoblique = True rho_deoblique = bo_deoblique.clone('rho_deoblique') rs_deoblique = bo_deoblique.clone('rs_deoblique') reg_bo_deoblique = bo_deoblique.clone('reg_bo_deoblique') preproc.connect(inputNode, 'echo_spacing', func_create_synth, 'TE') preproc.connect(inputNode, 'readout_dir', func_create_synth, 'kyDir') preproc.connect(inputNode, 'synth_name', func_create_synth, 'synthName') preproc.connect(inputNode, 'bo_name', bo_deoblique, 'in_file') preproc.connect(bo_deoblique, 'out_file', func_create_synth, 'boName') preproc.connect(inputNode, 'rho_name', rho_deoblique, 'in_file') preproc.connect(rho_deoblique, 'out_file', func_create_synth, 'rhoName') preproc.connect(inputNode, 'rs_name', rs_deoblique, 'in_file') preproc.connect(rs_deoblique, 'out_file', func_create_synth, 'rsName') synth_reorient = pe.Node(interface=preprocess.Resample(), name='synth_reorient') synth_reorient.inputs.orientation = 'RPI' synth_reorient.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_create_synth, 'epi_synth_path', synth_reorient, 'in_file') preproc.connect(synth_reorient, 'out_file', outputNode, 'cal_synth_RPI') bo_reorient = synth_reorient.clone('bo_reorient') rho_reorient = synth_reorient.clone('rho_reorient') rs_reorient = synth_reorient.clone('rs_reorient') reg_bo_reorient = synth_reorient.clone('reg_bo_reorient') preproc.connect(bo_deoblique, 'out_file', bo_reorient, 'in_file') preproc.connect(bo_reorient, 'out_file', outputNode, 'cal_bo_RPI') preproc.connect(rho_deoblique, 'out_file', rho_reorient, 'in_file') preproc.connect(rho_reorient, 'out_file', outputNode, 'cal_rho_RPI') preproc.connect(rs_deoblique, 'out_file', rs_reorient, 'in_file') preproc.connect(rs_reorient, 'out_file', outputNode, 'cal_rs_RPI') preproc.connect(inputNode, 'reg_bo_name', reg_bo_deoblique, 'in_file') preproc.connect(reg_bo_deoblique, 'out_file', reg_bo_reorient, 'in_file') preproc.connect(reg_bo_reorient, 'out_file', outputNode, 'cal_reg_bo_RPI') return preproc
def create_anat_preproc(already_skullstripped=False): """ The main purpose of this workflow is to process T1 scans. Raw mprage file is deobliqued, reoriented into RPI and skullstripped. Also, a whole brain only mask is generated from the skull stripped image for later use in registration. Returns ------- anat_preproc : workflow Anatomical Preprocessing Workflow Notes ----- `Source <https://github.com/FCP-INDI/C-PAC/blob/master/CPAC/anat_preproc/anat_preproc.py>`_ Workflow Inputs:: inputspec.anat : mprage file or a list of mprage nifti file User input anatomical(T1) Image, in any of the 8 orientations Workflow Outputs:: outputspec.refit : nifti file Deobliqued anatomical data outputspec.reorient : nifti file RPI oriented anatomical data outputspec.skullstrip : nifti file Skull Stripped RPI oriented mprage file with normalized intensities. outputspec.brain : nifti file Skull Stripped RPI Brain Image with original intensity values and not normalized or scaled. Order of commands: - Deobliqing the scans. For details see `3drefit <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3drefit.html>`_:: 3drefit -deoblique mprage.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 mprage_RPI.nii.gz -inset mprage.nii.gz - SkullStripping the image. For details see `3dSkullStrip <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dSkullStrip.html>`_:: 3dSkullStrip -input mprage_RPI.nii.gz -o_ply mprage_RPI_3dT.nii.gz - The skull stripping step modifies the intensity values. To get back the original intensity values, we do an element wise product of RPI data with step function of skull Stripped data. For details see `3dcalc <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dcalc.html>`_:: 3dcalc -a mprage_RPI.nii.gz -b mprage_RPI_3dT.nii.gz -expr 'a*step(b)' -prefix mprage_RPI_3dc.nii.gz High Level Workflow Graph: .. image:: ../images/anatpreproc_graph.dot.png :width: 500 Detailed Workflow Graph: .. image:: ../images/anatpreproc_graph_detailed.dot.png :width: 500 Examples -------- >>> import anat >>> preproc = create_anat_preproc() >>> preproc.inputs.inputspec.anat='sub1/anat/mprage.nii.gz' >>> preproc.run() #doctest: +SKIP """ preproc = pe.Workflow(name='anat_preproc') inputNode = pe.Node(util.IdentityInterface(fields=['anat']), name='inputspec') outputNode = pe.Node(util.IdentityInterface( fields=['refit', 'reorient', 'skullstrip', 'brain']), name='outputspec') anat_deoblique = pe.Node(interface=preprocess.Refit(), name='anat_deoblique') anat_deoblique.inputs.deoblique = True anat_reorient = pe.Node(interface=preprocess.Resample(), name='anat_reorient') anat_reorient.inputs.orientation = 'RPI' anat_reorient.inputs.outputtype = 'NIFTI_GZ' if not already_skullstripped: anat_skullstrip = pe.Node(interface=preprocess.SkullStrip(), name='anat_skullstrip') #anat_skullstrip.inputs.options = '-o_ply' anat_skullstrip.inputs.outputtype = 'NIFTI_GZ' anat_skullstrip_orig_vol = pe.Node(interface=preprocess.Calc(), name='anat_skullstrip_orig_vol') anat_skullstrip_orig_vol.inputs.expr = 'a*step(b)' anat_skullstrip_orig_vol.inputs.outputtype = 'NIFTI_GZ' preproc.connect(inputNode, 'anat', anat_deoblique, 'in_file') preproc.connect(anat_deoblique, 'out_file', anat_reorient, 'in_file') if not already_skullstripped: preproc.connect(anat_reorient, 'out_file', anat_skullstrip, 'in_file') preproc.connect(anat_skullstrip, 'out_file', anat_skullstrip_orig_vol, 'in_file_b') else: preproc.connect(anat_reorient, 'out_file', anat_skullstrip_orig_vol, 'in_file_b') preproc.connect(anat_reorient, 'out_file', anat_skullstrip_orig_vol, 'in_file_a') preproc.connect(anat_deoblique, 'out_file', outputNode, 'refit') preproc.connect(anat_reorient, 'out_file', outputNode, 'reorient') if not already_skullstripped: preproc.connect(anat_skullstrip, 'out_file', outputNode, 'skullstrip') preproc.connect(anat_skullstrip_orig_vol, 'out_file', outputNode, 'brain') return preproc
def create_func_preproc(use_bet=False, wf_name='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 ---------- 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 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.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: - 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. - 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_automask.nii.gz rest_3dc_RPI_3dv.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.nii.gz -b rest_3dc_RPI_3dv_automask.nii.gz -expr 'a*b' -prefix rest_3dc_RPI_3dv_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_3dc.nii.gz -ing 10000 rest_3dc_RPI_3dv_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_3dc_3dT.nii.gz rest_3dc_RPI_3dv_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_3dc_maths.nii.gz -Tmin -bin rest_3dc_RPI_3dv_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 -------- >>> import func_preproc >>> preproc = create_func_preproc(bet=True) >>> preproc.inputs.inputspec.func='sub1/func/rest.nii.gz' >>> preproc.run() #doctest: +SKIP >>> import func_preproc >>> preproc = create_func_preproc(bet=False) >>> preproc.inputs.inputspec.func='sub1/func/rest.nii.gz' >>> preproc.run() #doctest: +SKIP """ preproc = pe.Workflow(name=wf_name) inputNode = pe.Node(util.IdentityInterface(fields=['func']), name='inputspec') outputNode = pe.Node( util.IdentityInterface(fields=[ 'refit', 'reorient', 'reorient_mean', 'motion_correct', 'motion_correct_ref', 'movement_parameters', 'max_displacement', # 'xform_matrix', 'mask', 'skullstrip', 'example_func', 'preprocessed', 'preprocessed_mask', 'slice_time_corrected', 'oned_matrix_save' ]), name='outputspec') try: from nipype.interfaces.afni import utils as afni_utils func_deoblique = pe.Node(interface=afni_utils.Refit(), name='func_deoblique') except ImportError: func_deoblique = pe.Node(interface=preprocess.Refit(), name='func_deoblique') func_deoblique.inputs.deoblique = True preproc.connect(inputNode, 'func', func_deoblique, 'in_file') try: func_reorient = pe.Node(interface=afni_utils.Resample(), name='func_reorient') except UnboundLocalError: 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') try: func_get_mean_RPI = pe.Node(interface=afni_utils.TStat(), name='func_get_mean_RPI') except UnboundLocalError: 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') preproc.connect(func_motion_correct_A, 'oned_matrix_save', outputNode, 'oned_matrix_save') if not use_bet: func_get_brain_mask = pe.Node(interface=preprocess.Automask(), name='func_get_brain_mask') func_get_brain_mask.inputs.outputtype = 'NIFTI_GZ' preproc.connect(func_motion_correct_A, 'out_file', func_get_brain_mask, 'in_file') preproc.connect(func_get_brain_mask, 'out_file', outputNode, 'mask') else: func_get_brain_mask = pe.Node(interface=fsl.BET(), name='func_get_brain_mask_BET') func_get_brain_mask.inputs.mask = True func_get_brain_mask.inputs.functional = True erode_one_voxel = pe.Node(interface=fsl.ErodeImage(), name='erode_one_voxel') erode_one_voxel.inputs.kernel_shape = 'box' erode_one_voxel.inputs.kernel_size = 1.0 preproc.connect(func_motion_correct_A, 'out_file', func_get_brain_mask, 'in_file') preproc.connect(func_get_brain_mask, 'mask_file', erode_one_voxel, 'in_file') preproc.connect(erode_one_voxel, 'out_file', outputNode, 'mask') try: func_edge_detect = pe.Node(interface=afni_utils.Calc(), name='func_edge_detect') except UnboundLocalError: 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_motion_correct_A, 'out_file', func_edge_detect, 'in_file_a') if not use_bet: preproc.connect(func_get_brain_mask, 'out_file', func_edge_detect, 'in_file_b') else: preproc.connect(erode_one_voxel, 'out_file', func_edge_detect, 'in_file_b') preproc.connect(func_edge_detect, 'out_file', outputNode, 'skullstrip') try: func_mean_skullstrip = pe.Node(interface=afni_utils.TStat(), name='func_mean_skullstrip') except UnboundLocalError: 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_equilibrate(): ''' Workflow to get the scanner data ready. Anatomical and functional images are deobliqued. 5 TRs are removed from func data. inputs inputnode.verio_anat inputnode.verio_func inputnode.verio_func_se inputnode.verio_func_se_inv outputs outputnode.analyze_anat outputnode.analyze_func outputnode.analyze_func_se outputnode.analyze_func_se_inv ''' flow = Workflow('func_equilibrate') inputnode = Node(util.IdentityInterface( fields=['verio_func', 'verio_func_se', 'verio_func_seinv']), name='inputnode') outputnode = Node(util.IdentityInterface(fields=[ 'analyze_func', 'func_mask', 'analyze_func_se', 'analyze_func_seinv' ]), name='outputnode') ## functional image # 1. remove TRS remove_trs = Node(interface=preprocess.Calc(), name='func_drop_trs') remove_trs.inputs.start_idx = 5 remove_trs.inputs.stop_idx = 421 remove_trs.inputs.expr = 'a' remove_trs.inputs.outputtype = 'NIFTI_GZ' # 2. to RPI func_rpi = Node(interface=preprocess.Resample(), name='func_rpi') func_rpi.inputs.orientation = 'RPI' func_rpi.inputs.outputtype = 'NIFTI_GZ' # 3. func deoblique func_deoblique = Node(interface=preprocess.Refit(), name='func_deoblique') func_deoblique.inputs.deoblique = True flow.connect(inputnode, 'verio_func', remove_trs, 'in_file_a') flow.connect(remove_trs, 'out_file', func_rpi, 'in_file') flow.connect(func_rpi, 'out_file', func_deoblique, 'in_file') flow.connect(func_deoblique, 'out_file', outputnode, 'analyze_func') ########################################################################################################### ########################################################################################################### # se to RPI se_rpi = Node(interface=preprocess.Resample(), name='se_rpi') se_rpi.inputs.orientation = 'RPI' se_rpi.inputs.outputtype = 'NIFTI_GZ' # 3. func deoblique se_deoblique = Node(interface=preprocess.Refit(), name='se_deoblique') se_deoblique.inputs.deoblique = True flow.connect(inputnode, 'verio_func_se', se_rpi, 'in_file') flow.connect(se_rpi, 'out_file', se_deoblique, 'in_file') flow.connect(se_deoblique, 'out_file', outputnode, 'analyze_func_se') ########################################################################################################### ########################################################################################################### ########################################################################################################### # se_inv to RPI se_inv_rpi = Node(interface=preprocess.Resample(), name='seinv_rpi') se_inv_rpi.inputs.orientation = 'RPI' se_inv_rpi.inputs.outputtype = 'NIFTI_GZ' # 3. func deoblique se_inv_deoblique = Node(interface=preprocess.Refit(), name='seinv_deoblique') se_inv_deoblique.inputs.deoblique = True flow.connect(inputnode, 'verio_func_seinv', se_inv_rpi, 'in_file') flow.connect(se_inv_rpi, 'out_file', se_inv_deoblique, 'in_file') flow.connect(se_inv_deoblique, 'out_file', outputnode, 'analyze_func_seinv') return flow
def create_asl_preproc(c, strat, wf_name='asl_preproc'): # resource_pool = strat? # print('resource pool asl preproc: ', str(strat.get_resource_pool())) # allocate a workflow object asl_workflow = pe.Workflow(name=wf_name) asl_workflow.base_dir = c.workingDirectory # configure the workflow's input spec inputNode = pe.Node(util.IdentityInterface(fields=[ 'asl_file', 'anatomical_skull', 'anatomical_brain', 'seg_wm_pve' ]), name='inputspec') # configure the workflow's output spec outputNode = pe.Node(util.IdentityInterface( fields=['meanasl', 'perfusion_image', 'diffdata', 'diffdata_mean']), name='outputspec') # get segmentation output dir and file stub # create nodes for de-obliquing and reorienting try: from nipype.interfaces.afni import utils as afni_utils func_deoblique = pe.Node(interface=afni_utils.Refit(), name='func_deoblique') except ImportError: func_deoblique = pe.Node(interface=preprocess.Refit(), name='func_deoblique') func_deoblique.inputs.deoblique = True asl_workflow.connect(inputNode, 'asl_file', func_deoblique, 'in_file') try: func_reorient = pe.Node(interface=afni_utils.Resample(), name='func_reorient') except UnboundLocalError: func_reorient = pe.Node(interface=preprocess.Resample(), name='func_reorient') func_reorient.inputs.orientation = 'RPI' func_reorient.inputs.outputtype = 'NIFTI_GZ' # connect deoblique to reorient asl_workflow.connect(func_deoblique, 'out_file', func_reorient, 'in_file') # create node for splitting control and label pairs (unused currently) split_pairs_imports = ['import os', 'import subprocess'] split_ASL_pairs = pe.Node(interface=util.Function( input_names=['asl_file'], output_names=['control_image', 'label_image'], function=split_pairs, imports=split_pairs_imports), name='split_pairs') # create node for calculating subtracted images diffdata_imports = ['import os', 'import subprocess'] run_diffdata = pe.Node(interface=util.Function( input_names=['asl_file'], output_names=['diffdata_image', 'diffdata_mean'], function=diffdata, imports=diffdata_imports), name='diffdata') asl_workflow.connect(func_reorient, 'out_file', run_diffdata, 'asl_file') asl_workflow.connect(run_diffdata, 'diffdata_image', outputNode, 'diffdata') asl_workflow.connect(run_diffdata, 'diffdata_mean', outputNode, 'diffdata_mean') # create node for oxford_asl (perfusion image) asl_imports = ['import os', 'import subprocess'] run_oxford_asl = pe.Node(interface=util.Function( input_names=[ 'asl_file', 'anatomical_skull', 'anatomical_brain', 'seg' ], output_names=['perfusion_image', 'asl2anat_linear_xfm', 'asl2anat'], function=oxford_asl, imports=asl_imports), name='run_oxford_asl') # wire inputs from resource pool to ASL preprocessing FSL script # connect output of reorient to run_oxford_asl asl_workflow.connect(func_reorient, 'out_file', run_oxford_asl, 'asl_file') asl_workflow.connect(inputNode, 'seg_wm_pve', run_oxford_asl, 'seg') # pass the anatomical to the workflow asl_workflow.connect(inputNode, 'anatomical_skull', run_oxford_asl, 'anatomical_skull') # pass the anatomical to the workflow asl_workflow.connect(inputNode, 'anatomical_brain', run_oxford_asl, 'anatomical_brain') # connect oxford_asl outputs to outputNode asl_workflow.connect(run_oxford_asl, 'asl2anat_linear_xfm', outputNode, 'asl2anat_linear_xfm') asl_workflow.connect(run_oxford_asl, 'asl2anat', outputNode, 'asl2anat') asl_workflow.connect(run_oxford_asl, 'perfusion_image', outputNode, 'perfusion_image') strat.update_resource_pool({ 'mean_asl_in_anat': (run_oxford_asl, 'anat_asl'), 'asl_to_anat_linear_xfm': (run_oxford_asl, 'asl2anat_linear_xfm') }) # Take mean of the asl data for registration try: get_mean_asl = pe.Node(interface=afni_utils.TStat(), name='get_mean_asl') except UnboundLocalError: get_mean_asl = pe.Node(interface=preprocess.TStat(), name='get_mean_asl') get_mean_asl.inputs.options = '-mean' get_mean_asl.inputs.outputtype = 'NIFTI_GZ' asl_workflow.connect(func_reorient, 'out_file', get_mean_asl, 'in_file') asl_workflow.connect(get_mean_asl, 'out_file', outputNode, 'meanasl') return asl_workflow
def create_anat_preproc(already_skullstripped=False): """ Generate a workflow to do basic anatomical preprocessing (deoblique, reorient, skull-strip). Parameters ---------- already_skullstripped : bool, optional True/False depending on if the anatomical files used as input have already been skull stripped. Returns ------- anat_preproc : workflow Notes ----- Source code for the latest version can be found `on github <https://github.com/FCP-INDI/C-PAC/blob/master/CPAC/anat_preproc/anat_preproc.py>`_ Workflow Inputs:: inputspec.anat : nifti file or list of nifti files User specified anatomical (T1) image, in any of the 8 orientations Workflow Outputs:: outputspec.deoblique : nifti file Deobliqued anatomical image. outputspec.reorient : nifti file RPI oriented anatomical image. outputspec.brain : nifti file Skull-stripped anatomical image. Order of preprocessing steps and command-line equivalents: - Deoblique. For details see `3drefit <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3drefit.html>`_:: 3drefit -deoblique mprage.nii.gz - Re-orient to RPI. For details see `3dresample <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dresample.html>`_:: 3dresample -orient RPI -prefix mprage_RPI.nii.gz -inset mprage.nii.gz - Skull-strip. For details see `3dSkullStrip <http://afni.nimh.nih.gov/pub/dist/doc/program_help/3dSkullStrip.html>`_:: 3dSkullStrip -input mprage_RPI.nii.gz -orig_vol mprage_RPI_3dT.nii.gz High Level Workflow Graph: .. image:: ../images/anatpreproc_graph.dot.png :width: 500 Detailed Workflow Graph: .. image:: ../images/anatpreproc_graph_detailed.dot.png :width: 500 Examples -------- >>> import anat >>> preproc = create_anat_preproc() >>> preproc.inputs.inputspec.anat='sub1/anat/mprage.nii.gz' >>> preproc.run() #doctest: +SKIP """ preproc = pe.Workflow(name='anat_preproc') """ Configure Workflow Nodes """ # The input to this workflow is usually the anatomical image specified in the CPAC subject list. inputNode = pe.Node(util.IdentityInterface(fields=['anat']), name='inputspec') # This workflow outputs deobliqued, reoriented, and skull-stripped versions of the input image. outputNode = pe.Node(util.IdentityInterface( fields=['deoblique', 'reorient', 'skullstrip', 'brain']), name='outputspec') # Set up deoblique function. anat_deoblique = pe.Node(interface=preprocess.Refit(), name='anat_deoblique') anat_deoblique.inputs.deoblique = True # Set up reorient function. anat_reorient = pe.Node(interface=preprocess.Resample(), name='anat_reorient') anat_reorient.inputs.orientation = 'RPI' anat_reorient.inputs.outputtype = 'NIFTI_GZ' if not already_skullstripped: # Set up Skull Stripping anat_skullstrip = pe.Node(interface=preprocess.SkullStrip(), name='anat_skullstrip') # Keep intensity values the same in the output as in the input. anat_skullstrip.inputs.options = '-orig_vol' anat_skullstrip.inputs.outputtype = 'NIFTI_GZ' """ Connect Workflow Nodes """ # Deoblique takes the 'raw' anatomical image as input. preproc.connect(inputNode, 'anat', anat_deoblique, 'in_file') # The deobliqued image is used as the input for Reorient. preproc.connect(anat_deoblique, 'out_file', anat_reorient, 'in_file') if not already_skullstripped: # The reoriented image is used as the input for Skullstrip. preproc.connect(anat_reorient, 'out_file', anat_skullstrip, 'in_file') # Output deobliqued and reoriented images. preproc.connect(anat_deoblique, 'out_file', outputNode, 'deoblique') preproc.connect(anat_reorient, 'out_file', outputNode, 'reorient') # Output skull-stripped image. if not already_skullstripped: preproc.connect(anat_skullstrip, 'out_file', outputNode, 'brain') else: preproc.connect(anat_reorient, 'out_file', outputNode, 'brain') return preproc
def func_preproc_workflow(workflow, resource_pool, config, name="_"): """Build and run a Nipype workflow to deoblique and reorient a functional scan from a NIFTI file. - This is a seminal workflow that can only take an input directly from disk (i.e. no Nipype workflow connections/pointers, and this is where the pipeline will actually begin). For the sake of building the pipeine in reverse, if this workflow is called when there is no input file available, this function will return the unmodified workflow and resource pool directly back. - In conjunction with the other workflow-building functions, if this function returns the workflow and resource pool unmodified, each function up will do the same until it reaches the top level, allowing the pipeline builder to continue "searching" for a base-level input without crashing at this one. Expected Resources in Resource Pool - functional_scan: The raw functional 4D timeseries in a NIFTI file. New Resources Added to Resource Pool - func_reorient: The deobliqued, reoriented functional timeseries. Workflow Steps 1. get_idx function node (if a start_idx and/or stop_idx is set in the configuration) to generate the volume range to keep in the timeseries 2. AFNI 3dcalc to drop volumes not included in the range (if a start_idx and/or stop_idx has been set in the configuration only) 3. AFNI 3drefit to deoblique the file 4. AFNI 3dresample to reorient the file to RPI :type workflow: Nipype workflow object :param workflow: A Nipype workflow object which can already contain other connected nodes; this function will insert the following workflow into this one provided. :type resource_pool: dict :param resource_pool: A dictionary defining input files and pointers to Nipype node outputs / workflow connections; the keys are the resource names. :type config: dict :param config: A dictionary defining the configuration settings for the workflow, such as directory paths or toggled options. :type name: str :param name: (default: "_") A string to append to the end of each node name. :rtype: Nipype workflow object :return: The Nipype workflow originally provided, but with this function's sub-workflow connected into it. :rtype: dict :return: The resource pool originally provided, but updated (if applicable) with the newest outputs and connections. """ import nipype.pipeline.engine as pe import nipype.interfaces.utility as util from nipype.interfaces.afni import preprocess if "functional_scan" not in resource_pool.keys(): return workflow, resource_pool if "start_idx" not in config.keys(): config["start_idx"] = 0 if "stop_idx" not in config.keys(): config["stop_idx"] = None drop_trs = False if (config["start_idx"] != 0) and (config["stop_idx"] != None): drop_trs = True 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%s' % name) 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"] if drop_trs: func_drop_trs = pe.Node(interface=preprocess.Calc(), name='func_drop_trs%s' % name) 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') func_deoblique = pe.Node(interface=preprocess.Refit(), name='func_deoblique%s' % name) func_deoblique.inputs.deoblique = True if drop_trs: workflow.connect(func_drop_trs, 'out_file', func_deoblique, 'in_file') else: func_deoblique.inputs.in_file = resource_pool["functional_scan"] func_reorient = pe.Node(interface=preprocess.Resample(), name='func_reorient%s' % name) func_reorient.inputs.orientation = 'RPI' func_reorient.inputs.outputtype = 'NIFTI_GZ' workflow.connect(func_deoblique, 'out_file', func_reorient, 'in_file') resource_pool["func_reorient"] = (func_reorient, 'out_file') return workflow, resource_pool