{ "metadata": { "name": "", "signature": "sha256:7508f8e17e6757d23f62d30fb7e5351a9aa0d5d02b32870e3f2c57b7606f2f2f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Some revision and some hints for the exercises" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Compatibility with Python 3, as always\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": "markdown", "metadata": {}, "source": [ "## Tuples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Tuples are lists that you cannot modify.\n", "\n", "Put another way, tuples are *immutable* lists." ] }, { "cell_type": "code", "collapsed": false, "input": [ "my_list = ['list values', False, 10, 10.1] # Lists use square brackets\n", "my_tuple = ('some values', True, 1, 0.1) # Tuples use parentheses" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_list[0] = 'different value'\n", "my_list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_tuple[0] = 'another value'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_list.append('value at end')\n", "my_list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "my_tuple.append('something')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A little detail: a literal value for a tuple with a single value.\n", "\n", "You might think this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "small_tuple = (1)\n", "small_tuple" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(small_tuple)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python assumes that (1) is an expression - 1 - inside parentheses - like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_number = (1 + 2 + 3)\n", "a_number" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want a single element tuple, you need to add a comma to tell Python that is what you meant:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_tuple = (1,)\n", "a_tuple" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is no such ambiguity for lists (square brackets don't enclose expressions):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_list = [1]\n", "a_list" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## List comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List comprehensions are a nice short-cut for simple `for` loops in Python.\n", "\n", "The list comprehension is a single expression that returns a list, element by element." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's say you wanted to create a list of values for squared numbers. You might do it like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "squared_numbers = []\n", "for i in range(10): # numbers 0 through 9\n", " squared_numbers.append(i ** 2)\n", "squared_numbers" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It turns out this kind of thing is a very common pattern in Python. That is, create an empty list, then use a for loop to fill in values for the list.\n", "\n", "List comprehensions are a short cut for that pattern." ] }, { "cell_type": "code", "collapsed": false, "input": [ "squared_numbers = [i ** 2 for i in range(10)]\n", "squared_numbers" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The list comprehesion is an *expression*, starting and ending with square brackets. The first thing inside the square brackets is the expression that will become the element of the list - in this case `i ** 2` - followed by a `for` clause - in this case `for in in range(10)` - that will feed the first expression with values to use.\n", "\n", "See [the Python docs on list comprehensions](https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) for a little more detail." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "List comprehensions can be a little hard to read at first. If you find them confusing, as most of us do at first, then unpack them into the equivalent `for` loop. Over time, as you get used to them, they can be easier to read than the longer `for` loop equivalent.\n", "\n", "We'll practice list comprehensions more over the next few classes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Packages and namespaces" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have already seen examples of *importing* a package." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This Python code *imports* the *module* called `numpy` and gives it the new name `np`.\n", "\n", "What `type()` of thing is `np` now?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "type(np)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A module contains stuff with *names*. For example, numpy contains a *function* named `sqrt`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.sqrt?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because a module contains stuff with names, it is a *namespace*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy is also a *package* because it is a set of modules that get installed together. For example, after you have installed numpy, you not only have the `numpy` module, but other submodules such as `numpy.linalg`, for linear algebra on numpy arrays:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy.linalg as npl\n", "type(npl)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try tab completing on ``npl.`` (`npl` followed by a period - `.`) to see what is in there." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numpy is the module that contains the basic routines for creating and working with 1D and 2D and 3D - and in fact ND arrays in Python. Almost every scientific computing package in Python uses numpy." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will be using two other packages for the exercises - `matplotlib` and `nibabel`. Both of these packages depend heavily on numpy for working with arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matplotlib is the standard Python package for doing high-quality plots. The original author wrote matplotlib to be similar to MATLAB (hence the name)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The best module for standard use is `matplotlib.pyplot` and we will import it like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "type(plt)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Because we are using matplotlib in the IPython notebook, we also run an IPython command to make Matplotlib plots appear inside the notebook rather than putting up the plots as separate windows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lastly, we will be using the `nibabel` package for loading neuroimaging format images:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import nibabel as nib" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you don't know how to do a particular task in Python / Numpy / Matplotlib, then try these steps:\n", "\n", "* In IPython, do tab completion in the module, and have a look around. For example, if you are looking for a routine to do rounding on arrays, then type `np.ro` followed by and you will see `np.round` as one of the suggestions;\n", "* In IPython, get the help for particular functions or classes with the question mark at the end of the function or class name - e.g. ``np.round?`` followed by in the notebook.\n", "* In numpy or scipy (we'll come across scipy later), you can find stuff using `lookfor`. For example, let's say you hadn't guessed that `np.sqrt` was the square root function in numpy, you could try `np.lookfor('square root')`.\n", "* Do a web search : http://lmgtfy.com/?q=numpy+square+root\n", "\n", "Don't forget to ask your instructor - we live to help you acquire Python expertise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Arrays as images, images as arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The main point of this class is to show that you can consider arrays as images, and images as arrays." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's make an array of numbers between 0 through 99:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 0, 0, 0, 9, 99, 99, 94, 0],\n", " [ 0, 0, 0, 25, 99, 99, 79, 0],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0],\n", " [ 0, 0, 0, 56, 99, 99, 49, 0],\n", " [ 0, 0, 0, 73, 99, 99, 31, 0],\n", " [ 0, 0, 0, 91, 99, 99, 13, 0],\n", " [ 0, 0, 9, 99, 99, 94, 0, 0],\n", " [ 0, 0, 27, 99, 99, 77, 0, 0],\n", " [ 0, 0, 45, 99, 99, 59, 0, 0],\n", " [ 0, 0, 63, 99, 99, 42, 0, 0],\n", " [ 0, 0, 80, 99, 99, 24, 0, 0],\n", " [ 0, 1, 96, 99, 99, 6, 0, 0],\n", " [ 0, 16, 99, 99, 88, 0, 0, 0],\n", " [ 0, 0, 0, 0, 0, 0, 0, 0]])\n", "an_array.shape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In fact this array represents a monochrome picture of a letter." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can show arrays as images using the `plt.imshow` command. This is the default output:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The image looks rather blurry. This is because matplotlib is drawing an image with many more pixels than the array has values. For the pixels in-between array values, matplotlib is using *interpolation* to estimate a good value. We will hear a lot more about interpolation in later classes. For now, we turn off interpolation like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array, interpolation='nearest')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The image is weirdly colorful. That is because matplotlib is using the default *colormap*. A colormap is a mapping from values in the array to colors. In this case the default colormap is called `jet` and maps low numbers in the image (0 in our case) to blue, and high numbers (99 in our case) to red.\n", "\n", "We can see what the colormap is doing by asking for a color bar:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array, interpolation='nearest')\n", "plt.colorbar()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In our case, our image would make more sense as grayscale, so we use the `gray` colormap, like this:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array, interpolation='nearest', cmap='gray')\n", "plt.colorbar()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A grayscale image is an array containing numbers giving the pixel intensity values - in our case between 0 and 99." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also plot lines in matplotlib. For example, we might want to plot row 8 out of this array (the 9th row):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.plot(an_array[8])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The x axis is the position in the array (0 through 7) and the y axis is the value of the array row at that position.\n", "\n", "The plot shows us the 0 values at the edges of the bar of the \"i\", an the ramp up to the peak at the middle of the bar of the \"i\", in columns number 3 and 4." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A transpose in numpy uses the ``.T`` method on the array. This has the effect of flipping the rows and columns (in 2D):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array.T" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array.T, interpolation='nearest', cmap='gray')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also reshape the original array to a 1D array, by stacking all the rows end to end:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "a_1d_array = np.reshape(an_array, an_array.shape[0] * an_array.shape[1])\n", "a_1d_array" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a_1d_array.shape" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reshaping the array to one dimension is a common operation, so there is a separate numpy command for that, ``np.ravel``:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "np.ravel(an_array)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One use of the 1D version of the array, is for getting a histogram of the distribution of values in the array:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.hist(a_1d_array)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By default, the `plt.hist` function uses 50 bins, but you can specify how many bins you want with the `bins` keyword:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.hist(a_1d_array, bins=75)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can imagine, it's easy to go back to the 2D shape, by splitting the 1D array back into 15 rows of 8 values each (and therefore 8 columns):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "array_back = np.reshape(a_1d_array, (15, 8))\n", "array_back" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(array_back, interpolation='nearest', cmap='gray')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In numpy, basic operations like multiplication, addition, comparison, are always elementwise. For example, this multiplies every array value by 10:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array * 10" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comparison is also elementwise. For example, this gives True for every value > 50, and False for every value <= 50:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array > 50" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Matplotlib will treat False as 0 and True as 1, so this is one way of binarizing the image at a threshold (of 50 in this case):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array > 50, interpolation='nearest', cmap='gray')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can slice arrays as we slice strings or lists. The difference for arrays is that we can slice in any or all dimensions at the same time. For example, to get the dot of the \"i\" it looks (from the numbers at the sides of the ploat) that we want to the top 4 rows, and the last 5 columns:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "an_array[0:4, 3:]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plt.imshow(an_array[0:4, 3:], interpolation='nearest', cmap='gray')" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }