{ "metadata": { "name": "", "signature": "sha256:15fcff9fe5340bc44eac117d1861b4dff5e67d31bc58eb5e843226549c20e8e3" }, "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": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "# - show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "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": 3 }, { "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": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Parameters for SPM slice timing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we need the number of 'slices' (length of the third image dimension):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - load image to get parameters\n", "import nibabel as nib\n", "img = nib.load('fds114_sub009_t2r1.nii')\n", "num_slices = img.shape[2]\n", "num_slices" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "30" ] } ], "prompt_number": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we need the TR from the image header" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - get the TR from this image\n", "TR = img.header.get_zooms()[-1]\n", "TR" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 6, "text": [ "2.5" ] } ], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need the time to acquire all but the last slice (don't ask!). SPM calls this `TA` (time of acqusition?):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - calculate TA\n", "time_for_one_slice = TR / num_slices\n", "TA = TR - time_for_one_slice\n", "TA" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 7, "text": [ "2.4166666666666665" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Next we need the acquisition order. This is a list `acq_order` of length `num_slices` where `acq_order[i]` gives the slice index of the slice acquired at position `i` in time. \n", "\n", "This is for MATLAB, so the array indices start at 1 rather than 0 as for Python.\n", "\n", "So, if `acq_order` is `[1, 3, 5, 7, 9, 2, 4, 6, 8, 10]` that means that the first slice acquired was (1-based) slice index 1, followed by slice index 3, followed by slice index 5, and so on." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For our image, the scanner collected the odd index slices first, starting at the bottom, and then came back and collected the even index slices:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - generate acq_order list\n", "odd = range(1, num_slices+1, 2)\n", "even = range(2, num_slices+1, 2)\n", "acq_order = list(odd) + list(even)\n", "print(acq_order)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]\n" ] } ], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lastly, SPM slice timing also asks for a *reference slice*. This is the (1-based) index of the slice to which the others will be matched in terms of time. For example, if we want to use the interpolation to match the times of all the slices to the acquisition time of the first slice, the reference slice would be 1 (bottom slice, with 1-based indexing)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use the SPM interface to run the slice timing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Batching using the nipype interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are going to use the nipype interface to run the same thing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's delete the image that was the result of slice timing via the SPM GUI:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - import the routines for working with the operating system\n", "import os\n", "# Delete file if it exists\n", "if os.path.exists('afds114_sub009_t2r1.nii'):\n", " os.unlink('afds114_sub009_t2r1.nii') # delete file" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "# - import our initilization of nipype / MATLAB interface\n", "import nipype_settings\n", "# - import SliceTiming from nipype SPM interfaces\n", "from nipype.interfaces.spm import SliceTiming" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Your job now is to create the `SliceTiming` SPM batch job from the parameters you need.\n", "\n", "Try looking for help on the `SliceTiming` object for clues how to do this." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Initialize the SliceTiming batch object, and fill parameters\n", "# Run the slice timing on the fixed image `fds114_sub009_t2r1.nii`\n", "st = SliceTiming()\n", "st.inputs.in_files = 'fds114_sub009_t2r1.nii'\n", "st.inputs.num_slices = num_slices\n", "st.inputs.time_repetition = TR\n", "st.inputs.time_acquisition = TA\n", "st.inputs.slice_order = acq_order\n", "st.inputs.ref_slice = 1" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "# Run the batch job\n", "st.run()" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ "" ] } ], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check we did write a new slice-time corrected file:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - check for file created by nipype slice timing\n", "assert os.path.exists('afds114_sub009_t2r1.nii')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 } ], "metadata": {} } ] }