{ "metadata": { "name": "", "signature": "sha256:31b9b150b5137b4658c380c04ed6ec21910e2ef579c19a404f49a6af928d8218" }, "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": [ "## What transforms did I add?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I took this image\n", "\n", "* `ds114_sub009_highres.nii`\n", "\n", "and I made a new copy, in `img, hdr` format:\n", "\n", "* `ds114_sub009_highres_moved.img`\n", "\n", "I then modified the affine in `ds114_sub009_highres_moved.hdr` to add an extra rotation and translation.\n", "\n", "What extra translation and rotation did I add?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hint: remember the rotation formulae:\n", "\n", "$$\n", "\\begin{alignat}{1}\n", "R_x(\\theta) &= \\begin{bmatrix}\n", "1 & 0 & 0 \\\\\n", "0 & \\cos \\theta & -\\sin \\theta \\\\[3pt]\n", "0 & \\sin \\theta & \\cos \\theta \\\\[3pt]\n", "\\end{bmatrix} \\\\[6pt]\n", "R_y(\\theta) &= \\begin{bmatrix}\n", "\\cos \\theta & 0 & \\sin \\theta \\\\[3pt]\n", "0 & 1 & 0 \\\\[3pt]\n", "-\\sin \\theta & 0 & \\cos \\theta \\\\\n", "\\end{bmatrix} \\\\[6pt]\n", "R_z(\\theta) &= \\begin{bmatrix}\n", "\\cos \\theta & -\\sin \\theta & 0 \\\\[3pt]\n", "\\sin \\theta & \\cos \\theta & 0\\\\[3pt]\n", "0 & 0 & 1\\\\\n", "\\end{bmatrix}\n", "\\end{alignat}\n", "$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You might also want to compare against results from our `rotations` code:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# - get our functions to make rotation matrices\n", "# If this gives an error - maybe you didn't set your PYTHONPATH? Let us know!\n", "from rotations import x_rotmat, y_rotmat, z_rotmat" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Final hint:\n", "\n", "* inverse sine, cosine are ``np.arcsin, np.arccos``." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Get the original image affine\n", "import nibabel as nib\n", "orig_img = nib.load('ds114_sub009_highres.nii')\n", "print(orig_img.affine)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[[ 0.9989 -0.0605 0.0109 -129.8257]\n", " [ 0.0427 1.263 0.2336 -119.0906]\n", " [ -0.0215 -0.3028 0.9723 -143.4178]\n", " [ 0. 0. 0. 1. ]]\n" ] } ], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "# Get the new image affine\n", "moved_img = nib.load('ds114_sub009_highres_moved.img')\n", "moved_img.affine" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "array([[ 0.9416, -0.4311, -0.0586, -98.8336],\n", " [ 0.336 , 1.1887, 0.2264, -164.1377],\n", " [ -0.0215, -0.3028, 0.9723, -158.4178],\n", " [ 0. , 0. , 0. , 1. ]])" ] } ], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "# Work out what transform has been added in the new affine\n", "extra = moved_img.affine.dot(npl.inv(orig_img.affine))\n", "extra" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 10, "text": [ "array([[ 0.9553, -0.2955, 0. , -10. ],\n", " [ 0.2955, 0.9553, 0. , -12. ],\n", " [ 0. , -0. , 1. , -15. ],\n", " [ 0. , 0. , 0. , 1. ]])" ] } ], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "# What rotation and translation has been applied?\n", "z_angle = np.arccos(extra[0, 0])\n", "translation = extra[:3, 3]\n", "z_angle, translation" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "(0.30000001985761038, array([-10., -12., -15.]))" ] } ], "prompt_number": 11 } ], "metadata": {} } ] }