
# Introducing nipype¶

Nipype is a Python module that provides Python interfaces to many imaging tools, including SPM, AFNI and FSL.

We install it with pip in the usual way:

pip3 install --user nipype


After this has run, check that you can import nipype with:

>>> import nipype


We are interested in the nipype interfaces sub-package. Specifically, we want the interfaces to the SPM routines:

>>> from nipype.interfaces import spm


Our first job is to make sure that nipype can run MATLAB. Let’s check with a test call:

>>> import nipype.interfaces.matlab as nim
>>> mlab = nim.MatlabCommand()
>>> mlab.inputs.script = "version"  # get MATLAB version
>>> mlab.run()
<...>


If nipype does not have the right command to start MATLAB, this will fail with an error. We can set the command to start MATLAB like this:

>>> nim.MatlabCommand.set_default_matlab_cmd('/Applications/MATLAB_R2014a.app/bin/matlab')


where /Applications/MATLAB_R2014a.app/bin/matlab is the path to the MATLAB application file.

Check this is working by running the code above.

Next we need to make sure that nipype has SPM on the MATLAB path when it is running MATLAB. Try running this command to get the SPM version.

>>> mlab = nim.MatlabCommand()
>>> mlab.inputs.script = "spm ver"  # get SPM version
>>> mlab.run()
<...>


If this gives an error message, you may not have SPM set up on your MATLAB path by default. You can use Nipype to add SPM to the MATLAB path like this:

>>> nim.MatlabCommand.set_default_paths('/Users/mb312/dev_trees/spm12')


Another option is to use the MATLAB GUI to add this directory to the MATLAB path, and save this path for future sessions.

Now try running the spm ver command again:

>>> mlab = nim.MatlabCommand()
>>> mlab.inputs.script = "spm ver"  # get SPM version
>>> mlab.run()
<...>


We are going to put the setup we need into a Python file we can import from any script that we write that uses nipype.

In your current directory, make a new file called nipype_settings.py with contents like this:

""" Defaults for using nipype
"""
import nipype.interfaces.matlab as nim
# If you needed to set the default matlab command above
nim.MatlabCommand.set_default_matlab_cmd('/Applications/MATLAB_R2014a.app/bin/matlab')
# If you needed to set the SPM path above
nim.MatlabCommand.set_default_paths('/Users/mb312/dev_trees/spm12')


Now try:

>>> import nipype_settings
>>> import nipype.interfaces.matlab as nim
>>> mlab = nim.MatlabCommand()
>>> mlab.inputs.script = "spm ver"  # get SPM version
>>> mlab.run()
<...>


These should run without error.