{ "metadata": { "name": "", "signature": "sha256:a4bce70a803a72046e394e45fa73b3e626ee56f461c154c321607a8ba0bc5460" }, "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": "code", "collapsed": false, "input": [ "# - import numpy.linalg with a shorter name\n", "import numpy.linalg as npl" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "# - make numpy print out values in less verbose form\n", "np.set_printoptions(suppress=True, precision=4)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reslicing between spaces using affines" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the current directory, we have a BOLD (4D image) and a structural image for the same subject:\n", "\n", "* BOLD image : `ds114_sub009_t2r1.nii`;\n", "* structural image : `ds114_sub009_highres.nii`.\n", "\n", "They have different orientations and voxel sizes.\n", "\n", "Confirm this by loading each image, and showing the:\n", "\n", "* image shape\n", "* image affine from the header" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Load structural and BOLD image\n", "# Print image data shape\n", "# Print affine from header" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We know this BOLD image now. It has a bad first volume. Load the BOLD data, drop the first volume, and make a new volume that is the mean of the rest of the BOLD volumes across time. Plot an example slice from the mean volume." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Load BOLD image data. Drop first volume. Make mean volume\n", "# Plot an example slice from the mean volume" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the structural data, plot an example slice:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Load structural data, plot example slice" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are going to reslice the anatomical to match the mean functional volume.\n", "\n", "Remember pull resampling? We are going to need the transform that maps a coordinate in the mean functional volume, to the corresponding coordinate in the structural volume. \n", "\n", "In order to do this, we need:\n", "\n", "* The affine mapping from mean functional voxel coordinates to mm space (`mean_vox2mm`);\n", "* The affine mapping from mm space to structural voxel coordinates (`mm2struct_vox`).\n", "\n", "We combine these by matrix multiplication (*from right to left!*) to give `mean_vox2struct_vox`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Get `mean_vox2mm` mapping\n", "# Get `mm2struct_vox` mapping\n", "# Calculate `mean_vox2struct_vox`" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Split this affine matrix up into its component 3x3 `M` matrix and the 3 element translation vector:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Split `mean_vox2struct_vox` into 3x3 transformation, 3 element translation" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use `affine_transform` to make a new copy of the structural image, resampled into the voxel coordinates of the mean functional image:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Use affine_transform and the tranformation components to resample structural to functional" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use `subplots` to display and example slice from the mean image and the resliced structural side by side:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Display example slice from mean vol and resliced structural side by side" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's go in the opposite direction.\n", "\n", "We are going to resample the mean functional volume into the voxel space of the structural volume.\n", "\n", "We now need the affine transformation from voxel coordinates in the structural to voxel coordinates in the mean functional volume - call this `struct_vox2mean_vox`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Get affine transformation `struct_vox2mean_vox`" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the contents of `struct_vox2mean_vox` to reslice the mean functional to the structural voxel space:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Reslice mean functional to structural voxel space" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display example slice for resliced mean functional side by side with structural" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Display example slices for resliced mean and structural" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can you think of situations where you would prefer to use one direction (eg functional to anatomy) over the other ?" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }