{ "metadata": { "name": "", "signature": "sha256:78138dc17f10550cd721240f4580b77dd56bdba366ab9062c72f97239c746c50" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Motivating example: Parallel image processing with scikit-image" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a sense of what IPython.parallel might be used for,\n", "we start with an example of some batch processing of image files with [scikit-image](http://scikit-image.org/).\n", "We will revisit pieces of this example as we learn about the different components of IPython.\n", "\n", "You can download images with [this notebook](../images.ipynb), or get a zip [here](https://s3.amazonaws.com/ipython-parallel-data/images.zip), or find any images on your computer." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys,os,re,time\n", "import urllib\n", "\n", "import numpy as np\n", "\n", "from IPython import parallel" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from skimage.io import imread\n", "from skimage import measure" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a function to " ] }, { "cell_type": "code", "collapsed": false, "input": [ "def find_contours(path, low=0.1, high=0.8):\n", " \"\"\"Find contours in an image at path\n", " \n", " Returns the image and the contour lists.\n", " \"\"\"\n", " img = imread(path, flatten=True)\n", " \n", " # Find contours at a constant value of 0.1 and 0.8\n", " dark = measure.find_contours(img, low)\n", " light = measure.find_contours(img, high)\n", " return img, dark, light\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def plot_contours(img, dark, light, show=True):\n", " \"\"\"Display the image and plot all contours found\"\"\"\n", " plt.imshow(img, cmap='gray')\n", " \n", " for n, contour in enumerate(dark):\n", " plt.plot(contour[:, 1], contour[:, 0], c='r', linewidth=1)\n", " \n", " for n, contour in enumerate(light):\n", " plt.plot(contour[:, 1], contour[:, 0], c='b', linewidth=1)\n", "\n", " plt.axis('image')\n", " plt.xticks([])\n", " plt.yticks([])\n", " if show:\n", " plt.show()\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def get_contours_image(path):\n", " \"\"\"Given a path, return a PNG of the image with contour lines\n", " \n", " Calls both find_contours and plot_contours\n", " \"\"\"\n", " from IPython.core.pylabtools import print_figure\n", " \n", " img, dark, light = find_contours(path)\n", " plot_contours(img, dark, light, show=False)\n", " fig = plt.gcf()\n", " pngdata = print_figure(fig)\n", " plt.close(fig)\n", " return pngdata\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "pictures_dir = os.path.join('..', 'images', 'castle')\n", "\n", "pictures = []\n", "for directory, subdirs, files in os.walk(pictures_dir):\n", " for fname in files:\n", " if fname.lower().endswith(('.jpg', '.png')):\n", " pictures.append(os.path.join(directory, fname))\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's test our function locally, to see what it does." ] }, { "cell_type": "code", "collapsed": false, "input": [ "for p in pictures[:3]:\n", " img, dark, light = find_contours(p)\n", " plot_contours(img, dark, light)\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Now in parallel" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we connect our parallel Client" ] }, { "cell_type": "code", "collapsed": false, "input": [ "rc = parallel.Client()\n", "all_engines = rc[:]\n", "view = rc.load_balanced_view()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we initialize the namespace on all of the engines with imports" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%px import os; os.chdir(\"{os.getcwd()}\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%px\n", "import matplotlib\n", "matplotlib.use('Agg')\n", "import matplotlib.pyplot as plt\n", "\n", "from skimage.io import imread\n", "from skimage import measure" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and make sure some functions are defined everywhere (this is only necessary for the `contours_in_url` case)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "all_engines.push(dict(\n", " plot_contours=plot_contours,\n", " find_contours=find_contours,\n", "))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can iterate through all of our pictures, and detect and display any faces we find" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display, Image\n", "\n", "amr = view.map_async(get_contours_image, pictures[:20], ordered=False)\n", "for pngdata in amr:\n", " display(Image(data=pngdata))\n" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }