{ "metadata": { "name": "", "signature": "sha256:b44d369e91f4f5407b1530ba655a5ff3223997e5b667333ff105a5dc2dd71ef9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": [], "source": [ "> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3.1. Teaching programming in the notebook with IPython blocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You need to install ipythonblocks for this recipe. You can just type in a terminal `pip install ipythonblocks`. Note that you can also execute this shell command from the IPython notebook by prefixing this command with `!`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "!pip install ipythonblocks" ], "language": "python", "metadata": { "strip_output": [ 0, 3 ] }, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For the last part of this recipe, you also need to install Pillow: you will find more instructions in Chapter 11. (http://python-imaging.github.io)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally, you need to download the *Portrait* image on the [book's website](http://ipython-books.github.io) and extract it in the current directory. You can also play with your own images!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. First, we import some modules." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "from IPython.display import clear_output\n", "from ipythonblocks import BlockGrid, colors" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Now, we create a **block grid** with 5 columns and 5 rows, and we fill each block in purple." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(width=5, height=5, fill=colors['Purple'])\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. We can access individual blocks with 2D indexing. This illustrates the indexing syntax in Python. We can also access an entire row or line with `:` (colon). Each block is represented by an RGB color. The library comes with a handy dictionary of colors, assigning RGB tuples to standard color names." ] }, { "cell_type": "code", "collapsed": false, "input": [ "grid[0,0] = colors['Lime']\n", "grid[-1,0] = colors['Lime']\n", "grid[:,-1] = colors['Lime']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Now, we are going to illustrate **matrix multiplication**, a fundamental notion in linear algebra. We will represent two $(n,n)$ matrices $A$ (in cyan) and $B$ (lime) aligned with $C=A \\cdot B$ (yellow). To do this, we use a small trick consisting in creating a big white grid of size $(2n+1,2n+1)$. The matrices $A$, $B$ and $C$ are just *views* on parts of the grid." ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = 5\n", "grid = BlockGrid(width=2*n+1, \n", " height=2*n+1, \n", " fill=colors['White'])\n", "A = grid[n+1:,:n]\n", "B = grid[:n,n+1:]\n", "C = grid[n+1:,n+1:]\n", "A[:,:] = colors['Cyan']\n", "B[:,:] = colors['Lime']\n", "C[:,:] = colors['Yellow']\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "5. Let's turn to matrix multiplication itself. We perform a loop over all rows and columns, and we highlight the corresponding rows and columns in $A$ and $B$ that are multiplied together during the matrix product. We combine IPython's `clear_output()` method with `grid.show()` and `time.sleep()` (pause) to implement the animation." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for i in range(n):\n", " for j in range(n):\n", " # We reset the matrix colors.\n", " A[:,:] = colors['Cyan']\n", " B[:,:] = colors['Lime']\n", " C[:,:] = colors['Yellow']\n", " # We highlight the adequate rows\n", " # and columns in red.\n", " A[i,:] = colors['Red']\n", " B[:,j] = colors['Red']\n", " C[i,j] = colors['Red']\n", " # We animate the grid in the loop.\n", " clear_output()\n", " grid.show()\n", " time.sleep(.25)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "6. Finally, we will display an image with IPython blocks. We import the JPG image with `Image.open()` and we retrieve the data with `getdata()`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from PIL import Image\n", "imdata = Image.open('data/photo.jpg').getdata()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can create a `BlockGrid` with the appropriate number of rows and columns, and set each block's color to the corresponding pixel's color in the image. We use a small block size, and we remove the lines between the blocks." ] }, { "cell_type": "code", "collapsed": false, "input": [ "rows, cols = imdata.size\n", "grid = BlockGrid(width=rows, height=cols,\n", " block_size=4, lines_on=False)\n", "for block, rgb in zip(grid, imdata):\n", " block.rgb = rgb\n", "grid.show()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n", "\n", "> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (500 pages)." ] } ], "metadata": {} } ] }