def create_smoothing_pipeline(name='smoothing'): # set fsl output type fsl.FSLCommand.set_default_output_type('NIFTI') # initiate workflow smoothing = Workflow(name='smoothing') # inputnode inputnode=Node(util.IdentityInterface(fields=['ts_transformed', 'fwhm' ]), name='inputnode') # outputnode outputnode=Node(util.IdentityInterface(fields=['ts_smoothed' ]), name='outputnode') #apply smoothing smooth = MapNode(fsl.Smooth(),name = 'smooth', iterfield='in_file') smoothing.connect([ (inputnode, smooth, [ ('ts_transformed', 'in_file'), ('fwhm', 'fwhm')] ), (smooth, outputnode, [('smoothed_file', 'ts_smoothed')] ) ]) return smoothing
def preprocess(aroma_config, fwhm_config, input_img, mask_file, taskdir, task): # Resample to mask space # This is for AROMA images. Does it hurt non-AROMA? resample_path = os.path.join(taskdir, task + '_resample.nii.gz') resample = afni.Resample(master=mask_file, out_file=resample_path, in_file=input_img) resample_run = resample.run() # Apply fmriprep-calculated brain mask to the functional image masked_file_path = os.path.join(taskdir, task + "_input_functional_masked.nii.gz") applymask = fsl.ApplyMask(mask_file=mask_file, out_file=masked_file_path) applymask.inputs.in_file = resample_run.outputs.out_file mask_run = applymask.run() masked_input = masked_file_path if aroma_config: print("No smoothing required.") processed_image = masked_input else: # smoothing print("Smoothing the skull-stripped BOLD.") smooth = fsl.Smooth(in_file=masked_input, fwhm=fwhm_config) smooth_run = smooth.run() processed_image = smooth_run.outputs.smoothed_file return processed_image
def smooth_data(name='func_smoothed'): from nipype.pipeline.engine import Node, Workflow import nipype.interfaces.utility as util import nipype.interfaces.fsl as fsl flow = Workflow(name) inputnode = Node(util.IdentityInterface(fields=['func_data']), name='inputnode') outputnode = Node(util.IdentityInterface(fields=['func_smoothed']), name='outputnode') smooth = Node(interface=fsl.Smooth(), name='func_smooth_fwhm_4') smooth.inputs.fwhm = 4.0 smooth.terminal_output = 'file' flow.connect(inputnode, 'func_data', smooth, 'in_file') flow.connect(smooth, 'smoothed_file', outputnode, 'func_smoothed') return flow
preproc_wf.connect(nipype_bandpass, 'out_files', outputspec, 'nipype_bp_susan_sm{0}_files'.format(kernel)) tsnr = pe.MapNode(conf.TSNR(), iterfield=['in_file'], name='nipype_bp_susan_sm{0}_tsnr_'.format(kernel)) preproc_wf.connect(nipype_bandpass, 'out_files', tsnr, 'in_file') preproc_wf.connect(tsnr, 'tsnr_file', outputspec, 'nipype_bp_susan_sm{0}_tsnr_files'.format(kernel)) # # ### FSL Smoothing # # fsl_smooth[i] = pe.MapNode(fsl.Smooth(), iterfield=['in_file'], name='fsl_smooth_{0}'.format(kernel)) fsl_smooth[i].inputs.fwhm = float(kernel) preproc_wf.connect(maskfunc, 'out_file', fsl_smooth[i], 'in_file') # Mask the smoothed data with the dilated mask maskfunc3 = pe.MapNode(fsl.ImageMaths(suffix='_mask', op_string='-mas'), iterfield=['in_file'], name='fsl_mask_{0}_'.format(kernel)) preproc_wf.connect(fsl_smooth[i], 'smoothed_file', maskfunc3, 'in_file') preproc_wf.connect(fs_threshold2, ('binary_file', pickfirst), maskfunc3, 'in_file2') preproc_wf.connect(maskfunc3, 'out_file', outputspec, 'fsl_sm{0}_files'.format(kernel))
from __future__ import print_function from builtins import str import nipype.interfaces.fsl as fsl result = fsl.BET(in_file='data/s1/struct.nii').run() print(result) """ Running a single program is not much of a breakthrough. Lets run motion correction followed by smoothing (isotropic - in other words not using SUSAN). Notice that in the first line we are setting the output data type for all FSL interfaces. """ fsl.FSLCommand.set_default_output_type('NIFTI_GZ') result1 = fsl.MCFLIRT(in_file='data/s1/f3.nii').run() result2 = fsl.Smooth(in_file='f3_mcf.nii.gz', fwhm=6).run() """ Simple workflow --------------- In the previous example we knew that fsl.MCFLIRT will produce a file called f3_mcf.nii.gz and we have hard coded this as an input to fsl.Smooth. This is quite limited, but luckily nipype supports joining interfaces in pipelines. This way output of one interface will be used as an input of another without having to hard code anything. Before connecting Interfaces we need to put them into (separate) Nodes and give them unique names. This way every interface will process data in a separate folder. """ import nipype.pipeline.engine as pe import os motion_correct = pe.Node(
#Make a mask of the warped image, to use it with atropos binarize_warped_image = Node(fsl.UnaryMaths(), name='Binarize_Warped_Image') binarize_warped_image.inputs.operation = 'bin' binarize_warped_image.output_datatype = 'char' #----------------------------------------------------------------------------------------------------- # In[1]: #Multiply by Jacobian determinant to ge the modulate image modulate_GM = Node(ants.MultiplyImages(), name='Modulate_GM') modulate_GM.inputs.dimension = 3 modulate_GM.inputs.output_product_image = 'Modulated_GM.nii.gz' #----------------------------------------------------------------------------------------------------- # In[1]: #Smooth the modulated images smoothing = Node(fsl.Smooth(), name='Smoothing') smoothing.iterables = ('fwhm', [1.5, 2, 2.3, 2.7, 3]) #----------------------------------------------------------------------------------------------------- # In[1]: VBM_workflow.connect([ (infosource, selectfiles, [('subject_id', 'subject_id')]), (selectfiles, bias_corr, [('3D', 'input_image')]), (bias_corr, brain_ext, [('output_image', 'anatomical_image')]), (brain_ext, reg_sub_to_temp, [('BrainExtractionBrain', 'moving_image')]), (reg_sub_to_temp, get_warp_field, [('forward_transforms', 'inlist')]), (get_warp_field, jacobian, [('out', 'deformationField')]), #------------------------------------------------------------------------------------ (reg_sub_to_temp, binarize_warped_image, [('warped_image', 'in_file')]), (reg_sub_to_temp, atropos, [('warped_image', 'intensity_images')]), (binarize_warped_image, atropos, [('out_file', 'mask_image')]),
# Replace nan values after realignment with 0 nan_to_0 = Node(interface=fsl.ImageMaths(op_string=' -nan', suffix='_nanto0'), name='nan_to_0') pet4D_masked = Node(interface=fsl.ImageMaths(op_string=' -mas', suffix='_masked'), name='pet4D_masked') dynamic_mean = Node(interface=DynamicMean(startTime=args.t_start_SUVR, endTime=args.t_end_SUVR, weights=dyn_mean_wts), name="dynamic_mean") # Gaussian smoothing smooth = Node(interface=fsl.Smooth(fwhm=args.smooth_fwhm), name="smooth") realign_qc = Node(interface=realign_snapshots(splitTime=0), name="realign_qc") datasink = Node(interface=nio.DataSink(), name="datasink") datasink.inputs.base_directory = output_dir datasink.inputs.container = os.path.join('output', 'realign_wf') datasink.inputs.substitutions = [('_roi', ''), ('_merged', ''), ('_reoriented', ''), ('_unpadded', ''), ('_nanto0', ''), ('_masked', '')] datasink.inputs.regexp_substitutions = [(r'_\d+\.\d+to\d+\.\d+min', r'')] realign_workflow = Workflow(name="realign_workflow") realign_workflow.base_dir = os.path.join(output_dir, 'realign_workingdir') realign_workflow.config = { "execution": {