Neuroimaging in Python: Pipelines and Interfaces (Nipype)

1.11.0 · active · verified Wed Apr 15

Nipype is a Python project that provides a uniform interface to existing neuroimaging software packages (e.g., FSL, SPM, AFNI, ANTS, FreeSurfer). It enables users to easily create and execute robust, reproducible, and efficient pipelines for neuroimaging data analysis. The current version is 1.11.0, and it maintains an active release cadence with regular feature and bug-fix updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Nipype workflow that takes two inputs, performs a simple multiplication using a custom Python function wrapped in a `Function` node, and executes the pipeline. Nipype workflows typically generate a working directory (`base_dir`) where intermediate and final results are stored. For complex workflows, consider `DataSink` or inspecting the cache for outputs.

import os
from nipype.pipeline.engine import Workflow, Node
from nipype.interfaces.utility import Function, IdentityInterface

def simple_multiply(a, b):
    return a * b

# Create a workflow
wf = Workflow(name="simple_math_workflow")
wf.base_dir = os.environ.get('NIPYPE_WORKDIR', os.path.abspath('nipype_work_dir'))

# Input node to define initial data
inputnode = Node(IdentityInterface(fields=['val1', 'val2']), name='inputnode')
inputnode.inputs.val1 = 10
inputnode.inputs.val2 = 5

# Function node to perform multiplication
multiply_node = Node(Function(input_names=['a', 'b'],
                              output_names=['result'],
                              function=simple_multiply),
                     name='multiply_node')

# Connect the nodes
wf.connect(inputnode, 'val1', multiply_node, 'a')
wf.connect(inputnode, 'val2', multiply_node, 'b')

# Run the workflow
try:
    print(f"Running workflow, output will be in {wf.base_dir}")
    # Use 'MultiProc' plugin for local parallel execution
    wf.run(plugin='MultiProc')
    print("Workflow completed successfully. Check the base_dir for results.")
except Exception as e:
    print(f"Workflow failed: {e}")

view raw JSON →