{ "metadata": { "name": "", "signature": "sha256:15fcff9fe5340bc44eac117d1861b4dff5e67d31bc58eb5e843226549c20e8e3" }, "name": "spm_slice_timing", "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": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# - show figures inside the notebook\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [] }, { "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": [ "