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 the Nipype library

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% percentile:

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 [ ]:
from nilearn import plotting
%matplotlib inline
import matplotlib.pyplot as plt
from IPython.display import Image
smoothwf.write_graph(graph2use='colored', format='png', simple_form=True)
Image(filename='/output/susan_smooth/graph.png')
180514-09:24:55,167 workflow INFO:
	 Generated workflow graph: /home/neuro/nipype_tutorial/notebooks/graph.png (graph2use=colored, simple_form=True).
Out[ ]:

And we're ready to go:

In [ ]:
smoothwf.run('MultiProc', plugin_args={'n_procs': 4})
180514-09:21:34,310 workflow INFO:
	 Workflow susan_smooth settings: ['check', 'execution', 'logging', 'monitoring']
180514-09:21:34,320 workflow INFO:
	 Running in parallel.
180514-09:21:34,324 workflow INFO:
	 [MultiProc] Running 0 tasks, and 2 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:21:34,413 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.mask".
180514-09:21:34,421 workflow INFO:
	 [Node] Setting-up "susan_smooth.mask" in "/output/susan_smooth/mask".
180514-09:21:34,425 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.median".
180514-09:21:34,428 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.mask".
180514-09:21:34,431 workflow INFO:
	 [Node] Setting-up "susan_smooth.median" in "/output/susan_smooth/median".
180514-09:21:34,438 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.median".
180514-09:21:34,446 workflow INFO:
	 [Node] Setting-up "_mask0" in "/output/susan_smooth/mask/mapflow/_mask0".
180514-09:21:34,457 workflow INFO:
	 [Node] Outdated cache found for "_mask0".
180514-09:21:34,468 workflow INFO:
	 [Node] Setting-up "_median0" in "/output/susan_smooth/median/mapflow/_median0".180514-09:21:34,469 workflow INFO:
	 [Node] Running "_mask0" ("nipype.interfaces.fsl.utils.ImageMaths"), a CommandLine Interface with command:
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

180514-09:21:34,472 workflow INFO:
	 [Node] Outdated cache found for "_median0".
180514-09:21:34,490 workflow INFO:
	 [Node] Running "_median0" ("nipype.interfaces.fsl.utils.ImageStats"), a CommandLine Interface with command:
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
180514-09:21:35,617 workflow INFO:
	 [Node] Finished "_median0".
180514-09:21:35,623 workflow INFO:
	 [Node] Finished "susan_smooth.median".
180514-09:21:36,325 workflow INFO:
	 [Job 2] Completed (susan_smooth.median).
180514-09:21:36,328 workflow INFO:
	 [MultiProc] Running 1 tasks, and 0 jobs ready. Free memory (GB): 53.74/53.94, Free processors: 3/4.
                     Currently running:
                       * susan_smooth.mask
180514-09:21:36,649 workflow INFO:
	 [Node] Finished "_mask0".
180514-09:21:36,655 workflow INFO:
	 [Node] Finished "susan_smooth.mask".
180514-09:21:38,327 workflow INFO:
	 [Job 0] Completed (susan_smooth.mask).
180514-09:21:38,330 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:21:38,378 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.meanfunc2".
180514-09:21:38,381 workflow INFO:
	 [Node] Setting-up "susan_smooth.meanfunc2" in "/output/susan_smooth/meanfunc2".
180514-09:21:38,433 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.meanfunc2".
180514-09:21:38,442 workflow INFO:
	 [Node] Setting-up "_meanfunc20" in "/output/susan_smooth/meanfunc2/mapflow/_meanfunc20".
180514-09:21:38,445 workflow INFO:
	 [Node] Outdated cache found for "_meanfunc20".
180514-09:21:38,451 workflow INFO:
	 [Node] Running "_meanfunc20" ("nipype.interfaces.fsl.utils.ImageMaths"), a CommandLine Interface with command:
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
180514-09:21:39,165 workflow INFO:
	 [Node] Finished "_meanfunc20".
180514-09:21:39,171 workflow INFO:
	 [Node] Finished "susan_smooth.meanfunc2".
180514-09:21:40,331 workflow INFO:
	 [Job 1] Completed (susan_smooth.meanfunc2).
180514-09:21:40,338 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:21:40,401 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.merge".
180514-09:21:40,404 workflow INFO:
	 [Node] Setting-up "susan_smooth.merge" in "/output/susan_smooth/merge".
180514-09:21:40,449 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.merge".
180514-09:21:40,463 workflow INFO:
	 [Node] Running "merge" ("nipype.interfaces.utility.base.Merge")
180514-09:21:40,470 workflow INFO:
	 [Node] Finished "susan_smooth.merge".
180514-09:21:42,332 workflow INFO:
	 [Job 3] Completed (susan_smooth.merge).
180514-09:21:42,340 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:21:42,404 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.multi_inputs".
180514-09:21:42,407 workflow INFO:
	 [Node] Setting-up "susan_smooth.multi_inputs" in "/output/susan_smooth/multi_inputs".
180514-09:21:42,410 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.multi_inputs".
180514-09:21:42,416 workflow INFO:
	 [Node] Running "multi_inputs" ("nipype.interfaces.utility.wrappers.Function")
180514-09:21:42,425 workflow INFO:
	 [Node] Finished "susan_smooth.multi_inputs".
180514-09:21:44,334 workflow INFO:
	 [Job 4] Completed (susan_smooth.multi_inputs).
180514-09:21:44,341 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:21:44,412 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.smooth".
180514-09:21:44,415 workflow INFO:
	 [Node] Setting-up "susan_smooth.smooth" in "/output/susan_smooth/smooth".
180514-09:21:44,419 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.smooth".
180514-09:21:44,426 workflow INFO:
	 [Node] Setting-up "_smooth0" in "/output/susan_smooth/smooth/mapflow/_smooth0".
180514-09:21:44,430 workflow INFO:
	 [Node] Outdated cache found for "_smooth0".
180514-09:21:44,455 workflow INFO:
	 [Node] Running "_smooth0" ("nipype.interfaces.fsl.preprocess.SUSAN"), a CommandLine Interface with command:
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
180514-09:21:46,335 workflow INFO:
	 [MultiProc] Running 1 tasks, and 0 jobs ready. Free memory (GB): 53.74/53.94, Free processors: 3/4.
                     Currently running:
                       * susan_smooth.smooth
180514-09:22:15,7 workflow INFO:
	 [Node] Finished "_smooth0".
180514-09:22:15,11 workflow INFO:
	 [Node] Finished "susan_smooth.smooth".
180514-09:22:16,365 workflow INFO:
	 [Job 5] Completed (susan_smooth.smooth).
180514-09:22:16,372 workflow INFO:
	 [MultiProc] Running 0 tasks, and 0 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
Out[ ]:
<networkx.classes.digraph.DiGraph at 0x7f4a5954b908>

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

In [ ]:
%%bash
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
In [ ]:
from nilearn import image, plotting
In [ ]:
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));

Inspect inputs and outputs of a loaded or created workflow

If you want to see a summary of all possible inputs and outputs of a given workflow, use the _get_inputs() and the _get_outputs() function.

In [ ]:
# Show all possible inputs
smoothwf._get_inputs()
Out[ ]:
inputnode =
fwhm = 4
in_files = /data/ds000114/sub-01/ses-test/func/sub-01_ses-test_task-fingerfootlips_bold.nii.gz
mask_file = /output/sub-01_ses-test_task-fingerfootlips_mask.nii.gz

mask =
args = <undefined>
environ = {'FSLOUTPUTTYPE': 'NIFTI_GZ'}
ignore_exception = False
mask_file = <undefined>
op_string = -mas
out_data_type = <undefined>
out_file = <undefined>
output_type = NIFTI_GZ
suffix = _mask
terminal_output = <undefined>

meanfunc2 =
args = <undefined>
environ = {'FSLOUTPUTTYPE': 'NIFTI_GZ'}
ignore_exception = False
in_file2 = <undefined>
mask_file = <undefined>
op_string = -Tmean
out_data_type = <undefined>
out_file = <undefined>
output_type = NIFTI_GZ
suffix = _mean
terminal_output = <undefined>

median =
args = <undefined>
environ = {'FSLOUTPUTTYPE': 'NIFTI_GZ'}
ignore_exception = False
op_string = -k %s -p 50
output_type = NIFTI_GZ
split_4d = <undefined>
terminal_output = <undefined>

merge =
axis = hstack
ignore_exception = False
no_flatten = False
ravel_inputs = False

multi_inputs =
function_str = def cartesian_product(fwhms, in_files, usans, btthresh):
    from nipype.utils.filemanip import ensure_list
    # ensure all inputs are lists
    in_files = ensure_list(in_files)
    fwhms = [fwhms] if isinstance(fwhms, (int, float)) else fwhms
    # create cartesian product lists (s_<name> = single element of list)
    cart_in_file = [
        s_in_file for s_in_file in in_files for s_fwhm in fwhms
    ]
    cart_fwhm = [s_fwhm for s_in_file in in_files for s_fwhm in fwhms]
    cart_usans = [s_usans for s_usans in usans for s_fwhm in fwhms]
    cart_btthresh = [
        s_btthresh for s_btthresh in btthresh for s_fwhm in fwhms
    ]

    return cart_in_file, cart_fwhm, cart_usans, cart_btthresh

ignore_exception = False

outputnode =


smooth =
args = <undefined>
dimension = 3
environ = {'FSLOUTPUTTYPE': 'NIFTI_GZ'}
ignore_exception = False
out_file = <undefined>
output_type = NIFTI_GZ
terminal_output = <undefined>
use_median = 1
In [ ]:
# Show all possible outputs
smoothwf._get_outputs()
Out[ ]:
inputnode =
fwhm = None
in_files = None
mask_file = None

mask =
out_file = None

meanfunc2 =
out_file = None

median =
out_stat = None

merge =
out = None

multi_inputs =
cart_btthresh = None
cart_fwhm = None
cart_in_file = None
cart_usans = None

outputnode =
smoothed_files = None

smooth =
smoothed_file = None

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', 'multi_inputs', '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 its 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})
180514-09:22:21,267 workflow INFO:
	 Workflow susan_smooth settings: ['check', 'execution', 'logging', 'monitoring']
180514-09:22:21,276 workflow INFO:
	 Running in parallel.
180514-09:22:21,280 workflow INFO:
	 [MultiProc] Running 0 tasks, and 2 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:22:21,367 workflow INFO:
	 [Job 0] Cached (susan_smooth.mask).
180514-09:22:21,373 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.median".
180514-09:22:21,377 workflow INFO:
	 [Node] Setting-up "susan_smooth.median" in "/output/susan_smooth/median".
180514-09:22:21,402 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.median".
180514-09:22:21,408 workflow INFO:
	 [Node] Setting-up "_median0" in "/output/susan_smooth/median/mapflow/_median0".
180514-09:22:21,413 workflow INFO:
	 [Node] Outdated cache found for "_median0".
180514-09:22:21,421 workflow INFO:
	 [Node] Running "_median0" ("nipype.interfaces.fsl.utils.ImageStats"), a CommandLine Interface with command:
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
180514-09:22:22,557 workflow INFO:
	 [Node] Finished "_median0".
180514-09:22:22,562 workflow INFO:
	 [Node] Finished "susan_smooth.median".
180514-09:22:23,281 workflow INFO:
	 [Job 2] Completed (susan_smooth.median).
180514-09:22:23,286 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:22:23,339 workflow INFO:
	 [Job 1] Cached (susan_smooth.meanfunc2).
180514-09:22:25,354 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.merge".
180514-09:22:25,357 workflow INFO:
	 [Node] Setting-up "susan_smooth.merge" in "/output/susan_smooth/merge".
180514-09:22:25,385 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.merge".
180514-09:22:25,393 workflow INFO:
	 [Node] Running "merge" ("nipype.interfaces.utility.base.Merge")
180514-09:22:25,403 workflow INFO:
	 [Node] Finished "susan_smooth.merge".
180514-09:22:27,287 workflow INFO:
	 [Job 3] Completed (susan_smooth.merge).
180514-09:22:27,294 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:22:27,355 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.multi_inputs".
180514-09:22:27,358 workflow INFO:
	 [Node] Setting-up "susan_smooth.multi_inputs" in "/output/susan_smooth/multi_inputs".
180514-09:22:27,380 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.multi_inputs".
180514-09:22:27,387 workflow INFO:
	 [Node] Running "multi_inputs" ("nipype.interfaces.utility.wrappers.Function")
180514-09:22:27,396 workflow INFO:
	 [Node] Finished "susan_smooth.multi_inputs".
180514-09:22:29,290 workflow INFO:
	 [Job 4] Completed (susan_smooth.multi_inputs).
180514-09:22:29,300 workflow INFO:
	 [MultiProc] Running 0 tasks, and 1 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
180514-09:22:29,373 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.smooth".
180514-09:22:29,376 workflow INFO:
	 [Node] Setting-up "susan_smooth.smooth" in "/output/susan_smooth/smooth".
180514-09:22:29,397 workflow INFO:
	 [Node] Outdated cache found for "susan_smooth.smooth".
180514-09:22:29,404 workflow INFO:
	 [Node] Setting-up "_smooth0" in "/output/susan_smooth/smooth/mapflow/_smooth0".
180514-09:22:29,408 workflow INFO:
	 [Node] Outdated cache found for "_smooth0".
180514-09:22:29,417 workflow INFO:
	 [Node] Running "_smooth0" ("nipype.interfaces.fsl.preprocess.SUSAN"), a CommandLine Interface with command:
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
180514-09:22:31,289 workflow INFO:
	 [MultiProc] Running 1 tasks, and 0 jobs ready. Free memory (GB): 53.74/53.94, Free processors: 3/4.
                     Currently running:
                       * susan_smooth.smooth
180514-09:23:00,215 workflow INFO:
	 [Node] Finished "_smooth0".
180514-09:23:00,221 workflow INFO:
	 [Node] Finished "susan_smooth.smooth".
180514-09:23:01,318 workflow INFO:
	 [Job 5] Completed (susan_smooth.smooth).
180514-09:23:01,324 workflow INFO:
	 [MultiProc] Running 0 tasks, and 0 jobs ready. Free memory (GB): 53.94/53.94, Free processors: 4/4.
Out[ ]:
<networkx.classes.digraph.DiGraph at 0x7f4a3e70dc18>

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
In [ ]:
from nilearn import image, plotting
In [ ]:
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 0x7f4a3e9d71d0>

Home | github | Nipype