{ "metadata": { "name": "", "signature": "sha256:ee13bff5f8afd89506a788a447196670da588a4ef80b3365606aabce6e8da1b7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "# - compatibility with Python 3\n", "from __future__ import print_function # print('me') instead of print 'me'\n", "from __future__ import division # 1/2 == 0.5, not 0" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "# - show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "# - import common modules\n", "import numpy as np # the Python array package\n", "import matplotlib.pyplot as plt # the Python plotting package" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "# - set gray colormap and nearest neighbor interpolation by default\n", "plt.rcParams['image.cmap'] = 'gray'\n", "plt.rcParams['image.interpolation'] = 'nearest'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introducing nipype" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Nipype](http://nipy.sourceforge.net/nipype) is a Python module that provides Python interfaces to many imaging tools, including SPM, AFNI and FSL." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We install it with the Python `pip` tool.\n", "\n", "In our case we need the current development version to give us the most recent SPM interfaces.\n", "\n", "We install like this (from your `cmd` or `Terminal.app` window):\n", "\n", "```\n", "pip install git+https://github.com/nipy/nipype.git\n", "```\n", "\n", "After this has run, check that you can import nipype with:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import nipype\n", "nipype.__version__" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "'1.0.0dev'" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are interested in the nipype `interfaces` sub-package. Specifically, we want the interfaces to the `spm` routines:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from nipype.interfaces import spm" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Our first job is to make sure that nipype can run MATLAB. Let's check with a test call:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import nipype.interfaces.matlab as nim\n", "mlab = nim.MatlabCommand()\n", "mlab.inputs.script = \"version\" # get MATLAB version\n", "mlab.run()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "IOError", "evalue": "matlab could not be found on host shanghai.local\nInterface MatlabCommand failed to run. ", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mIOError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmlab\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnim\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMatlabCommand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mmlab\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscript\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"version\"\u001b[0m \u001b[0;31m# get MATLAB version\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mmlab\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/nipype/interfaces/base.pyc\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, **inputs)\u001b[0m\n\u001b[1;32m 1001\u001b[0m \u001b[0mruntime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menviron\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'DISPLAY'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m':%s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mvdisplay_num\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1002\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1003\u001b[0;31m \u001b[0mruntime\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run_interface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1004\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1005\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_redirect_x\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/nipype/interfaces/matlab.pyc\u001b[0m in \u001b[0;36m_run_interface\u001b[0;34m(self, runtime)\u001b[0m\n\u001b[1;32m 136\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_run_interface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 137\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mterminal_output\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'allatonce'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 138\u001b[0;31m \u001b[0mruntime\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mMatlabCommand\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run_interface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 139\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 140\u001b[0m \u001b[0;31m# Matlab can leave the terminal in a barbbled state\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/nipype/interfaces/base.pyc\u001b[0m in \u001b[0;36m_run_interface\u001b[0;34m(self, runtime, correct_return_codes)\u001b[0m\n\u001b[1;32m 1445\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mexist_val\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1446\u001b[0m raise IOError(\"%s could not be found on host %s\" %\n\u001b[0;32m-> 1447\u001b[0;31m (self.cmd.split()[0], runtime.hostname))\n\u001b[0m\u001b[1;32m 1448\u001b[0m \u001b[0msetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'command_path'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcmd_path\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1449\u001b[0m setattr(runtime, 'dependencies', get_dependencies(executable_name,\n", "\u001b[0;31mIOError\u001b[0m: matlab could not be found on host shanghai.local\nInterface MatlabCommand failed to run. " ] } ], "prompt_number": 14 }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nim.MatlabCommand.set_default_matlab_cmd('/Applications/MATLAB_R2014a.app/bin/matlab')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 15 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Check this is working by running the same code as previously:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mlab = nim.MatlabCommand()\n", "mlab.inputs.script = \"version\" # get MATLAB version\n", "mlab.run()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 16, "text": [ "" ] } ], "prompt_number": 16 }, { "cell_type": "markdown", "metadata": {}, "source": [ "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?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mlab = nim.MatlabCommand()\n", "mlab.inputs.script = \"spm ver\" # get SPM version\n", "mlab.run()" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "Command:\n/Applications/MATLAB_R2014a.app/bin/matlab -nodesktop -nosplash -singleCompThread -r \"addpath('/Users/mb312/dev_trees/pna2015/day12');pyscript;exit\"\nStandard output:\n\n < M A T L A B (R) >\n Copyright 1984-2014 The MathWorks, Inc.\n R2014a (8.3.0.532) 64-bit (maci64)\n February 11, 2014\n\n[\bWarning: Name is nonexistent or not a directory: /Users/mb312/matlab/m2html]\b \n[\bWarning: Duplicate directory name: /Users/mb312/matlab]\b \n[\bWarning: Name is nonexistent or not a directory: /Users/mb312/matlab/m2html]\b \n[\bWarning: Duplicate directory name: /Users/mb312/matlab]\b \n \nTo get started, type one of these: helpwin, helpdesk, or demo.\nFor product information, visit www.mathworks.com.\n \nExecuting pyscript at 14-May-2015 18:59:57:\n----------------------------------------------------------------------------------------------------\nMATLAB Version: 8.3.0.532 (R2014a)\nMATLAB License Number: 731130\nOperating System: Mac OS X Version: 10.9.5 Build: 13F1077 \nJava Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode\n----------------------------------------------------------------------------------------------------\nMATLAB Version 8.3 (R2014a)\nImage Processing Toolbox Version 9.0 (R2014a)\nMATLAB Compiler Version 5.1 (R2014a)\nSignal Processing Toolbox Version 6.21 (R2014a)\nStatistics Toolbox Version 9.0 (R2014a)\nWavelet Toolbox Version 4.13 (R2014a)\n\nStandard error:\nMATLAB code threw an exception:\nUndefined function 'spm' for input arguments of type 'char'.\nFile:/Users/mb312/dev_trees/pna2015/day12/pyscript.m\nName:pyscript\nLine:3\n\nReturn code: 0\nInterface MatlabCommand failed to run. ", "output_type": "pyerr", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mmlab\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnim\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mMatlabCommand\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mmlab\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscript\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"spm ver\"\u001b[0m \u001b[0;31m# get SPM version\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mmlab\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;32m/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/nipype/interfaces/base.pyc\u001b[0m in \u001b[0;36mrun\u001b[0;34m(self, **inputs)\u001b[0m\n\u001b[1;32m 1001\u001b[0m \u001b[0mruntime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0menviron\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'DISPLAY'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m':%s'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0mvdisplay_num\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1002\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1003\u001b[0;31m \u001b[0mruntime\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_run_interface\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1004\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1005\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_redirect_x\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/nipype/interfaces/matlab.pyc\u001b[0m in \u001b[0;36m_run_interface\u001b[0;34m(self, runtime)\u001b[0m\n\u001b[1;32m 144\u001b[0m \u001b[0;32mpass\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;34m'MATLAB code threw an exception'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mruntime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstderr\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 146\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mraise_exception\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 147\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mruntime\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 148\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m/Users/mb312/.virtualenvs/test/lib/python2.7/site-packages/nipype/interfaces/base.pyc\u001b[0m in \u001b[0;36mraise_exception\u001b[0;34m(self, runtime)\u001b[0m\n\u001b[1;32m 1381\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;34m\"Standard error:\\n\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mruntime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstderr\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\"\\n\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1382\u001b[0m \u001b[0mmessage\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;34m\"Return code: \"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mstr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mruntime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreturncode\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1383\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRuntimeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1384\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1385\u001b[0m \u001b[0;34m@\u001b[0m\u001b[0mclassmethod\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mRuntimeError\u001b[0m: Command:\n/Applications/MATLAB_R2014a.app/bin/matlab -nodesktop -nosplash -singleCompThread -r \"addpath('/Users/mb312/dev_trees/pna2015/day12');pyscript;exit\"\nStandard output:\n\n < M A T L A B (R) >\n Copyright 1984-2014 The MathWorks, Inc.\n R2014a (8.3.0.532) 64-bit (maci64)\n February 11, 2014\n\n[\bWarning: Name is nonexistent or not a directory: /Users/mb312/matlab/m2html]\b \n[\bWarning: Duplicate directory name: /Users/mb312/matlab]\b \n[\bWarning: Name is nonexistent or not a directory: /Users/mb312/matlab/m2html]\b \n[\bWarning: Duplicate directory name: /Users/mb312/matlab]\b \n \nTo get started, type one of these: helpwin, helpdesk, or demo.\nFor product information, visit www.mathworks.com.\n \nExecuting pyscript at 14-May-2015 18:59:57:\n----------------------------------------------------------------------------------------------------\nMATLAB Version: 8.3.0.532 (R2014a)\nMATLAB License Number: 731130\nOperating System: Mac OS X Version: 10.9.5 Build: 13F1077 \nJava Version: Java 1.7.0_11-b21 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode\n----------------------------------------------------------------------------------------------------\nMATLAB Version 8.3 (R2014a)\nImage Processing Toolbox Version 9.0 (R2014a)\nMATLAB Compiler Version 5.1 (R2014a)\nSignal Processing Toolbox Version 6.21 (R2014a)\nStatistics Toolbox Version 9.0 (R2014a)\nWavelet Toolbox Version 4.13 (R2014a)\n\nStandard error:\nMATLAB code threw an exception:\nUndefined function 'spm' for input arguments of type 'char'.\nFile:/Users/mb312/dev_trees/pna2015/day12/pyscript.m\nName:pyscript\nLine:3\n\nReturn code: 0\nInterface MatlabCommand failed to run. " ] } ], "prompt_number": 17 }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "nim.MatlabCommand.set_default_paths('/Users/mb312/spm')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now try running the `spm ver` command again:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "mlab = nim.MatlabCommand()\n", "mlab.inputs.script = \"spm ver\" # get SPM version\n", "mlab.run()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 19, "text": [ "" ] } ], "prompt_number": 19 }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "Change directory to your `pna_code` directory:\n", "\n", "```\n", "cd ~/pna_code\n", "```\n", "\n", "In this directory, make a new file called `nipype_settings.py` with contents like this:\n", "\n", "```\n", "\"\"\" Defaults for using nipype\n", "\"\"\"\n", "import nipype.interfaces.matlab as nim\n", "# If you needed to set the default matlab command above\n", "nim.MatlabCommand.set_default_matlab_cmd('/Applications/MATLAB_R2014a.app/bin/matlab')\n", "# If you needed to set the SPM path above\n", "nim.MatlabCommand.set_default_paths('/Users/mb312/spm')\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, open `cmd` or `Terminal.app` in this current directory, and do the following:\n", "\n", "```\n", "ipython\n", "```\n", "\n", "This starts the IPython console. Next, at the IPython prompt, type these lines:\n", "\n", "```\n", "import nipype_settings\n", "import nipype.interfaces.matlab as nim\n", "mlab = nim.MatlabCommand()\n", "mlab.inputs.script = \"spm ver\" # get SPM version\n", "mlab.run()\n", "```\n", "\n", "These should run without error. Call us over for help if you did get an error." ] } ], "metadata": {} } ] }