{ "metadata": { "name": "", "signature": "sha256:659c8c264a25d874d2e9a9f3c8074ba62a03d9f8ed5877f5b201eaa14cde9421" }, "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 numpy as np\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will work with a 3D brain image.\n", "\n", "Like the camera image in [the camera notebook](camera.ipynb), the pixel data for the 3D image is in a text file called `anatomical.txt`. I happen to know this image has is length 32 on the third dimension, but I don't know what the size of the first two dimensions are.\n", "\n", "So, I know the image is of shape (`I`, `J`, 32), but I don't know what `I` and `J` are.\n", "\n", "Here are the first four lines of `anatomical.txt`. \n", "\n", "```\n", "0.0000\n", "0.0000\n", "0.0000\n", "0.0000\n", "```\n", "\n", "So the data is in the same floating point text format as the camera picture pixel data.\n", "\n", "Real all the lines of the file into a list of float values, as before." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How many pixel values does this file contain?" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When I have my image array correctly shaped, then, if I take a slice over the third dimension:\n", "\n", "```\n", "slice_on_third = image_array[:, :, 0]\n", "```\n", "\n", "`slice_on_third` will be shape `(I, J)` (the size of the first two dimensions).\n", "\n", "\n", "So, how many pixel values does a slice in the third dimension contain - given that we know the third dimension is length 32? Put another way, what is the value for `I * J`?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Find the size of a slice over the third dimension\n", "# P = ?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Call `P` the number of values per slice on the third dimension (so `P == I * J` where we don't yet know `I` or `J`).\n", "\n", "Is this slice over the third dimension square (does `I == J`)?\n", "\n", "We need to find the values for `I` and `J`.\n", "\n", "Find candidates for `I` by using the modulus operator (`%`) to find a few numbers between 120 and 200 that divide exactly into the slice size `P`. Hint: the first value will be 120." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "These numbers are candidates for `I` - the first number in the pair `(I, J)`. We now need to find the corresponding `J` for each candidate for `I`.\n", "\n", "Use the integer division operator (`//`) to get a list of pairs of numbers `I` and `J` such that `I * J == P`. Hint: the first pair of `I, J` is (120, 221)." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The full image shape will be three values, with one of these `(I, J)` pairs followed by 32. For example, the correct shape might be (120, 221 , 32) (it isn't!). Try reshaping the pixel data with a few of the `(I, J, 32)` candidates to see which one is likely to be right. You might want to plot a slice over the third dimension to see how it looks." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }