##// END OF EJS Templates
Updating interact to new APIs.
Updating interact to new APIs.

File last commit:

r15134:2a9791de
r15134:2a9791de
Show More
Interact.ipynb
601 lines | 14.4 KiB | text/plain | TextLexer
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 {
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Interact Demos"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This Notebook shows basic demonstrations of IPython `interact` module. This provides a high-level interface for creating user interface controls to use in exploring code and data interactively."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%pylab inline"
],
"language": "python",
"metadata": {},
Brian E. Granger
Updating interact to new APIs.
r15134 "outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Populating the interactive namespace from numpy and matplotlib\n"
]
}
],
"prompt_number": 1
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 },
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.html.widgets.interact import interact, interactive\n",
"from IPython.html import widgets\n",
"from IPython.display import clear_output, display, HTML"
],
"language": "python",
"metadata": {},
Brian E. Granger
Updating interact to new APIs.
r15134 "outputs": [],
"prompt_number": 2
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 },
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Basic interact"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is a simple function that displays its arguments as an HTML table:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def show_args(**kwargs):\n",
" s = '<h3>Arguments:</h3><table>\\n'\n",
" for k,v in kwargs.items():\n",
" s += '<tr><td>{0}</td><td>{1}</td></tr>\\n'.format(k,v)\n",
" s += '</table>'\n",
" display(HTML(s))"
],
"language": "python",
"metadata": {},
Brian E. Granger
Updating interact to new APIs.
r15134 "outputs": [],
"prompt_number": 3
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 },
{
"cell_type": "code",
"collapsed": false,
"input": [
"show_args(a=10, b='Hi There', c=True)"
],
"language": "python",
"metadata": {},
Brian E. Granger
Updating interact to new APIs.
r15134 "outputs": [
{
"html": [
"<h3>Arguments:</h3><table>\n",
"<tr><td>a</td><td>10</td></tr>\n",
"<tr><td>c</td><td>True</td></tr>\n",
"<tr><td>b</td><td>Hi There</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x108142490>"
]
}
],
"prompt_number": 4
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's use this function to explore how `interact` works."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"interact(show_args,\n",
" Temp=(0,10),\n",
" Current=(0.,10.,0.01),\n",
Brian E. Granger
Updating interact to new APIs.
r15134 " z=True,\n",
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 " Text=u'Type here!',\n",
" Algorithm=['This','That','Other'],\n",
Brian E. Granger
Updating interact to new APIs.
r15134 " a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0)\n",
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 " )"
],
"language": "python",
"metadata": {},
Brian E. Granger
Updating interact to new APIs.
r15134 "outputs": [
{
"html": [
"<h3>Arguments:</h3><table>\n",
"<tr><td>a</td><td>5.0</td></tr>\n",
"<tr><td>Algorithm</td><td>This</td></tr>\n",
"<tr><td>Temp</td><td>5</td></tr>\n",
"<tr><td>Text</td><td>Type here!</td></tr>\n",
"<tr><td>Current</td><td>5.0</td></tr>\n",
"<tr><td>z</td><td>True</td></tr>\n",
"</table>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x10814e610>"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%debug"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"> \u001b[0;32m/Users/bgranger/Documents/Computing/IPython/code/ipython/IPython/html/widgets/interact.py\u001b[0m(38)\u001b[0;36m_min_max_value\u001b[0;34m()\u001b[0m\n",
"\u001b[0;32m 37 \u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mmax\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0mmin\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0m\u001b[0;32m---> 38 \u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'max must be greater than min: (min={0}, max={1})'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmin\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmax\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0m\u001b[0;32m 39 \u001b[0;31m \u001b[0mvalue\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmin\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mabs\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"ipdb> print o\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(True, False)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"ipdb> print min, max\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"True False\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"ipdb> q\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"isinstance(10, bool)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"False"
]
}
],
"prompt_number": 8
Brian E. Granger
Adding Interact and Lorenz examples.
r15133 },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The keyword arguments to `interact` can be any `Widget` instance that has a `value` and `description` attribute, or one of the shorthand notations shown above."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Factoring polynomials"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is an example that uses [SymPy](http://sympy.org/en/index.html) to factor polynomials."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sympy import Symbol, Eq, factor, init_printing\n",
"init_printing(use_latex=True)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x = Symbol('x')"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def factorit(n):\n",
" display(Eq(x**n-1, factor(x**n-1)))"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice how the output of the `factorit` function is properly formatted LaTeX."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"interact(factorit, n=(2,40))"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"A simple image browser"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example shows how to browse through a set of images with a slider."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from sklearn import datasets"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will use the digits dataset from [scikit-learn](http://scikit-learn.org/stable/)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"digits = datasets.load_digits()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def browse_images(digits):\n",
" n = len(digits.images)\n",
" def view_image(i):\n",
" imshow(digits.images[i], cmap=cm.gray_r, interpolation='nearest')\n",
" title('Training: %s' % digits.target[i])\n",
" show()\n",
" interact(view_image, i=(0,n-1))"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"browse_images(digits)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Explore random graphs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, we build a simple UI for exploring random graphs with [NetworkX](http://networkx.github.io/)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import networkx as nx"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def plot_random_graph(n, p, generator):\n",
" g = generator(n,p)\n",
" nx.draw(g)\n",
" show()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"interact(plot_random_graph, n=(2,30), p=(0.0, 1.0, 0.001),\n",
" generator={'gnp': nx.gnp_random_graph,\n",
" 'erdos_renyi': nx.erdos_renyi_graph,\n",
" 'binomial': nx.binomial_graph})"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Image manipulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example builds a simple UI for performing basic image manipulation with [scikit-image](http://scikit-image.org/)."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import skimage\n",
"from skimage import data, filter, io"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"i = data.coffee()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"io.Image(i)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def edit_image(image):\n",
" def apply_filter(sigma, r, g, b):\n",
" new_image = filter.gaussian_filter(image, sigma=sigma)\n",
" new_image[:,:,0] = r*new_image[:,:,0]\n",
" new_image[:,:,1] = g*new_image[:,:,1]\n",
" new_image[:,:,2] = b*new_image[:,:,2]\n",
" new_image = io.Image(new_image)\n",
" display(new_image)\n",
" return new_image\n",
" lims = (0.0,1.0,0.01)\n",
" return interactive(apply_filter, sigma=(0.1,10.0,0.01), r=lims, g=lims, b=lims)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w = edit_image(i)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"display(w)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w.arguments"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"w.result"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Playing with audio"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example uses the `Audio` object and Matplotlib to explore the phenomenon of beat frequencies."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import Audio\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def beat_freq(f1=220.0, f2=224.0):\n",
" max_time = 3\n",
" rate = 8000.0\n",
" times = np.linspace(0,max_time,rate*max_time)\n",
" signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)\n",
" print f1, f2, abs(f1-f2)\n",
" display(Audio(data=signal, rate=rate))\n",
" return signal"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))\n",
"display(v)"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"plot(v.result[0:6000])"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}