Reusable workflows

Nipype doesn't just allow you to create your own workflows. It also already comes with predefined workflows, developed by the community, for the community. For a full list of all workflows, look under the Workflows section of the main homepage.

But to give you a short overview, there are workflows about:

Functional MRI workflows:

  • from fsl about resting state, fixed_effects, modelfit, featreg, susan_smooth and many more
  • from spm about DARTEL and VBM

Structural MRI workflows

  • from ants about ANTSBuildTemplate and antsRegistrationBuildTemplate
  • from freesurfer about bem, recon and tessellation

Diffusion workflows:

  • from camino about connectivity_mapping, diffusion and group_connectivity
  • from dipy about denoise
  • from fsl about artifacts, dti, epi, tbss and many more
  • from mrtrix about connectivity_mapping, diffusion and group_connectivity

How to load a workflow from Nipype

Let's consider the example of a functional MRI workflow, that uses FSL's Susan algorithm to smooth some data. To load such a workflow, we only need the following command:


In [ ]:
from nipype.workflows.fmri.fsl.preprocess import create_susan_smooth
smoothwf = create_susan_smooth()

Once a workflow is created, we need to make sure that the mandatory inputs are specified. To see which inputs we have to define, we can use the command:

create_susan_smooth?

Which gives us the output:

Create a SUSAN smoothing workflow

Parameters
----------
Inputs:
    inputnode.in_files : functional runs (filename or list of filenames)
    inputnode.fwhm : fwhm for smoothing with SUSAN
    inputnode.mask_file : mask used for estimating SUSAN thresholds (but not for smoothing)

Outputs:
    outputnode.smoothed_files : functional runs (filename or list of filenames)

As we can see, we also need a mask file. For the sake of convenience, let's take the mean image of a functional image and threshold it at the 50% percentil:


In [ ]:
!fslmaths /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz \
    -Tmean -thrP 50 /output/sub-01_ses-test_task-fingerfootlips_mask.nii.gz

Now, we're ready to finish up our smooth workflow.


In [ ]:
smoothwf.inputs.inputnode.in_files = '/data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz'
smoothwf.inputs.inputnode.mask_file = '/output/sub-01_ses-test_task-fingerfootlips_mask.nii.gz'
smoothwf.inputs.inputnode.fwhm = 4
smoothwf.base_dir = '/output'

Before we run it, let's visualize the graph:


In [ ]:
%pylab inline
from IPython.display import Image
smoothwf.write_graph(graph2use='colored', format='png', simple_form=True)
Image(filename='/output/susan_smooth/graph.dot.png')


Populating the interactive namespace from numpy and matplotlib
170904-05:44:26,906 workflow INFO:
	 Generated workflow graph: /output/susan_smooth/graph.dot.png (graph2use=colored, simple_form=True).
Out[ ]:

And we're ready to go:


In [ ]:
smoothwf.run('MultiProc', plugin_args={'n_procs': 4})


170904-05:44:26,954 workflow INFO:
	 Workflow susan_smooth settings: ['check', 'execution', 'logging']
170904-05:44:26,961 workflow INFO:
	 Running in parallel.
170904-05:44:26,965 workflow INFO:
	 Executing: mask ID: 0
170904-05:44:26,969 workflow INFO:
	 Executing: median ID: 2
170904-05:44:26,972 workflow INFO:
	 Executing node mask in dir: /output/susan_smooth/mask
170904-05:44:26,976 workflow INFO:
	 Executing node median in dir: /output/susan_smooth/median
170904-05:44:26,981 workflow INFO:
	 Executing node _mask0 in dir: /output/susan_smooth/mask/mapflow/_mask0
170904-05:44:26,984 workflow INFO:
	 Executing node _median0 in dir: /output/susan_smooth/median/mapflow/_median0
170904-05:44:26,989 workflow INFO:
	 Running: fslmaths /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz -mas /output/sub-01_ses-test_task-fingerfootlips_mask.nii.gz /output/susan_smooth/mask/mapflow/_mask0/sub-01_ses-test_task-fingerfootlips_bold_mask.nii.gz
170904-05:44:26,993 workflow INFO:
	 Running: fslstats /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz -k /output/sub-01_ses-test_task-fingerfootlips_mask.nii.gz -p 50 
170904-05:44:28,38 interface INFO:
	 stdout 2017-09-04T05:44:28.038347:1312.000000 
170904-05:44:28,546 workflow INFO:
	 [Job finished] jobname: median jobid: 2
170904-05:44:29,561 workflow INFO:
	 [Job finished] jobname: mask jobid: 0
170904-05:44:29,564 workflow INFO:
	 Executing: meanfunc2 ID: 1
170904-05:44:29,569 workflow INFO:
	 Executing node meanfunc2 in dir: /output/susan_smooth/meanfunc2
170904-05:44:29,577 workflow INFO:
	 Executing node _meanfunc20 in dir: /output/susan_smooth/meanfunc2/mapflow/_meanfunc20
170904-05:44:29,581 workflow INFO:
	 Running: fslmaths /output/susan_smooth/mask/mapflow/_mask0/sub-01_ses-test_task-fingerfootlips_bold_mask.nii.gz -Tmean /output/susan_smooth/meanfunc2/mapflow/_meanfunc20/sub-01_ses-test_task-fingerfootlips_bold_mask_mean.nii.gz
170904-05:44:31,128 workflow INFO:
	 [Job finished] jobname: meanfunc2 jobid: 1
170904-05:44:31,131 workflow INFO:
	 Executing: merge ID: 3
170904-05:44:31,135 workflow INFO:
	 Executing node merge in dir: /output/susan_smooth/merge
170904-05:44:31,145 workflow INFO:
	 [Job finished] jobname: merge jobid: 3
170904-05:44:31,148 workflow INFO:
	 Executing: smooth ID: 4
170904-05:44:31,155 workflow INFO:
	 Executing node smooth in dir: /output/susan_smooth/smooth
170904-05:44:31,160 workflow INFO:
	 Executing node _smooth0 in dir: /output/susan_smooth/smooth/mapflow/_smooth0
170904-05:44:31,165 workflow INFO:
	 Running: susan /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz 984.0000000000 1.6986436006 3 1 1 /output/susan_smooth/meanfunc2/mapflow/_meanfunc20/sub-01_ses-test_task-fingerfootlips_bold_mask_mean.nii.gz 984.0000000000 /output/susan_smooth/smooth/mapflow/_smooth0/sub-01_ses-test_task-fingerfootlips_bold_smooth.nii.gz
170904-05:45:07,951 workflow INFO:
	 [Job finished] jobname: smooth jobid: 4
Out[ ]:
<networkx.classes.digraph.DiGraph at 0x7fb5a5f51550>

Once it's finished, we can look at the results:


In [ ]:
!fslmaths /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz -Tmean fmean.nii.gz
!fslmaths /output/susan_smooth/smooth/mapflow/_smooth0/sub-01_ses-test_task-fingerfootlips_bold_smooth.nii.gz \
    -Tmean smean.nii.gz

from nilearn import image, plotting
plotting.plot_epi(
    'fmean.nii.gz', title="mean (no smoothing)", display_mode='z',
    cmap='gray', cut_coords=(-45, -30, -15, 0, 15))
plotting.plot_epi(
    'smean.nii.gz', title="mean (susan smoothed)", display_mode='z',
    cmap='gray', cut_coords=(-45, -30, -15, 0, 15))


Out[ ]:
<nilearn.plotting.displays.ZSlicer at 0x7fb594a63470>

How to change node parameters from existing workflows

What if we want to change certain parameters of a loaded or already existing workflow? Let's first get the names of all the nodes in the workflow:


In [ ]:
print(smoothwf.list_node_names())


['inputnode', 'mask', 'meanfunc2', 'median', 'merge', 'outputnode', 'smooth']

Ok. Hmm, what if we want to change the 'median' node, from 50% to 99%? For this, we first need to get the node.


In [ ]:
median = smoothwf.get_node('median')

Now that we have the node, we can change it's value as we want:


In [ ]:
median.inputs.op_string = '-k %s -p 99'

And we can run the workflow again...


In [ ]:
smoothwf.run('MultiProc', plugin_args={'n_procs': 4})


170904-05:45:11,311 workflow INFO:
	 Workflow susan_smooth settings: ['check', 'execution', 'logging']
170904-05:45:11,318 workflow INFO:
	 Running in parallel.
170904-05:45:11,321 workflow INFO:
	 Executing: mask ID: 0
170904-05:45:11,324 workflow INFO:
	 [Job finished] jobname: mask jobid: 0
170904-05:45:11,326 workflow INFO:
	 Executing: median ID: 2
170904-05:45:11,331 workflow INFO:
	 Executing node median in dir: /output/susan_smooth/median
170904-05:45:11,339 workflow INFO:
	 Executing node _median0 in dir: /output/susan_smooth/median/mapflow/_median0
170904-05:45:11,345 workflow INFO:
	 Running: fslstats /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz -k /output/sub-01_ses-test_task-fingerfootlips_mask.nii.gz -p 99 
170904-05:45:12,387 interface INFO:
	 stdout 2017-09-04T05:45:12.387402:1904.000000 
170904-05:45:12,904 workflow INFO:
	 [Job finished] jobname: median jobid: 2
170904-05:45:12,907 workflow INFO:
	 Executing: meanfunc2 ID: 1
170904-05:45:12,910 workflow INFO:
	 [Job finished] jobname: meanfunc2 jobid: 1
170904-05:45:12,914 workflow INFO:
	 Executing: merge ID: 3
170904-05:45:12,919 workflow INFO:
	 Executing node merge in dir: /output/susan_smooth/merge
170904-05:45:12,933 workflow INFO:
	 [Job finished] jobname: merge jobid: 3
170904-05:45:12,936 workflow INFO:
	 Executing: smooth ID: 4
170904-05:45:12,943 workflow INFO:
	 Executing node smooth in dir: /output/susan_smooth/smooth
170904-05:45:12,952 workflow INFO:
	 Executing node _smooth0 in dir: /output/susan_smooth/smooth/mapflow/_smooth0
170904-05:45:12,962 workflow INFO:
	 Running: susan /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz 1428.0000000000 1.6986436006 3 1 1 /output/susan_smooth/meanfunc2/mapflow/_meanfunc20/sub-01_ses-test_task-fingerfootlips_bold_mask_mean.nii.gz 1428.0000000000 /output/susan_smooth/smooth/mapflow/_smooth0/sub-01_ses-test_task-fingerfootlips_bold_smooth.nii.gz
170904-05:45:51,786 workflow INFO:
	 [Job finished] jobname: smooth jobid: 4
Out[ ]:
<networkx.classes.digraph.DiGraph at 0x7fb5a5f716d8>

And now the output is:


In [ ]:
!fslmaths /output/susan_smooth/smooth/mapflow/_smooth0/sub-01_ses-test_task-fingerfootlips_bold_smooth.nii.gz \
    -Tmean mmean.nii.gz

from nilearn import image, plotting
plotting.plot_epi(
    'smean.nii.gz', title="mean (susan smooth)", display_mode='z',
    cmap='gray', cut_coords=(-45, -30, -15, 0, 15))
plotting.plot_epi(
    'mmean.nii.gz', title="mean (smoothed, median=99%)", display_mode='z',
    cmap='gray', cut_coords=(-45, -30, -15, 0, 15))


Out[ ]:
<nilearn.plotting.displays.ZSlicer at 0x7fb594baf208>