# - compatibility with Python 3
from __future__ import print_function # print('me') instead of print 'me'
from __future__ import division # 1/2 == 0.5, not 0
# - show figures inside the notebook
%matplotlib inline
# - import common modules
import numpy as np # the Python array package
import matplotlib.pyplot as plt # the Python plotting package
# - set gray colormap and nearest neighbor interpolation by default
plt.rcParams['image.cmap'] = 'gray'
plt.rcParams['image.interpolation'] = 'nearest'
Nipype is a Python module that provides Python interfaces to many imaging tools, including SPM, AFNI and FSL.
We install it with the Python pip tool.
In our case we need the current development version to give us the most recent SPM interfaces.
We install like this (from your cmd or Terminal.app window):
pip install git+https://github.com/nipy/nipype.git
After this has run, check that you can import nipype with:
import nipype
nipype.__version__
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')
Check this is working by running the same code as previously:
mlab = nim.MatlabCommand()
mlab.inputs.script = "version" # get MATLAB version
mlab.run()
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 does not work, you may not have SPM set up on your MATLAB path by default. You can add SPM to the path like this:
nim.MatlabCommand.set_default_paths('/Users/mb312/spm')
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.
Change directory to your pna_code directory:
cd ~/pna_code
In this 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/spm')
Finally, open cmd or Terminal.app in this current directory, and do the following:
ipython
This starts the IPython console. Next, at the IPython prompt, type these lines:
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. Call us over for help if you did get an error.