{
 "metadata": {
  "name": "",
  "signature": "sha256:abf2a5adf64437227df38799ed8c3ef5b7c8e0fc09162b48491c2916b78573d0"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Interactive Image Processing with Numba and Bokeh\n",
      "-------------------------------------------------\n",
      "\n",
      "This demo shows off an interactive image blur using [Numba](http://numba.pydata.org) for numerics, [Bokeh](http://bokeh.pydata.org) for plotting, and Ipython interactors for widgets. The demo runs entirely inside the Ipython notebook, with no Bokeh server required.\n",
      "\n",
      "Numba must be installed in order to run this demo. To run, click on, `Cell->Run All` in the top menu, then scroll to the bottom and move the slider. "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from bokeh.plotting import *\n",
      "import numba\n",
      "\n",
      "import numpy as np\n",
      "import scipy.misc"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# smaller image\n",
      "img = (scipy.misc.ascent()[::-1,:]/255.0)[:250, :250].copy(order='C')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "output_notebook()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "palette = ['#%02x%02x%02x' %(i,i,i) for i in range(256)]\n",
      "width, height = img.shape\n",
      "p = figure(x_range=(0, width), y_range=(0, height))\n",
      "p.image(image=[img], x=[0], y=[0], dw=[width], dh=[height], palette=palette, name='foo')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "options= [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]\n",
      "\n",
      "@numba.njit\n",
      "def blur(outimg, img, amt):\n",
      "    iw, ih = img.shape\n",
      "    for i in range(amt, iw-amt):\n",
      "        for j in range(amt, ih-amt):\n",
      "            px = 0.\n",
      "            for w in range(-amt//2, amt//2):\n",
      "                for h in range(-amt//2, amt//2):\n",
      "                    px += img[i+w, j+h]\n",
      "            outimg[i, j]= px/(amt*amt)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from timeit import default_timer as timer\n",
      "def update(i):\n",
      "    out = img.copy()\n",
      "    \n",
      "    ts = timer()\n",
      "    blur(out, img, options[i])\n",
      "    te = timer()\n",
      "    print('blur takes:', te - ts)\n",
      "    \n",
      "    renderer = p.select(dict(name=\"foo\"))\n",
      "    ds = renderer[0].data_source\n",
      "    ds.data['image'] = [out]\n",
      "    ds.push_notebook()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "show(p)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from IPython.html.widgets import *\n",
      "interact(update, i=(0, len(options)-1))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}