diff --git a/examples/widgets/Interact.ipynb b/examples/widgets/Interact.ipynb new file mode 100644 index 0000000..2f485d8 --- /dev/null +++ b/examples/widgets/Interact.ipynb @@ -0,0 +1,475 @@ +{ + "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": {}, + "outputs": [] + }, + { + "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": {}, + "outputs": [] + }, + { + "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 = '

Arguments:

\\n'\n", + " for k,v in kwargs.items():\n", + " s += '\\n'.format(k,v)\n", + " s += '
{0}{1}
'\n", + " display(HTML(s))" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "show_args(a=10, b='Hi There', c=True)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "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", + " z=(True,False),\n", + " Text=u'Type here!',\n", + " Algorithm=['This','That','Other'],\n", + " a=widgets.FloatRangeWidget(min=-10.0, max=10.0, step=0.1, value=5.0)\n", + " )" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "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": {} + } + ] +} \ No newline at end of file diff --git a/examples/widgets/Lorenz.ipynb b/examples/widgets/Lorenz.ipynb new file mode 100644 index 0000000..88ce478 --- /dev/null +++ b/examples/widgets/Lorenz.ipynb @@ -0,0 +1,269 @@ +{ + "metadata": { + "name": "" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Exploring the Lorenz System of Differential Equations" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this Notebook we explore the Lorenz system of differential equations:\n", + "\n", + "$$\n", + "\\begin{aligned}\n", + "\\dot{x} & = \\sigma(y-x) \\\\\n", + "\\dot{y} & = \\rho x - y - xz \\\\\n", + "\\dot{z} & = -\\beta z + xy\n", + "\\end{aligned}\n", + "$$\n", + "\n", + "This is one of the classic systems in non-linear differential equations. It exhibits a range of different behaviors as the parameters ($\\sigma$, $\\beta$, $\\rho$) are varied." + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Imports" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "First, we import the needed things from IPython, NumPy, Matplotlib and SciPy." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%pylab inline" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from IPython.html.widgets.interact import interact, interactive\n", + "from IPython.display import clear_output, display, HTML" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from scipy import integrate\n", + "\n", + "from matplotlib import pyplot as plt\n", + "from mpl_toolkits.mplot3d import Axes3D\n", + "from matplotlib.colors import cnames\n", + "from matplotlib import animation" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Computing the trajectories and plotting the result" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We define a function that can integrate the differential equations numerically and then plot the solutions. This function has arguments that control the parameters of the differential equation ($\\sigma$, $\\beta$, $\\rho$), the numerical integration (`N`, `max_time`) and the visualization (`angle`)." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def solve_lorenz(N=10, angle=0.0, max_time=4.0, sigma=10.0, beta=8./3, rho=28.0):\n", + "\n", + " fig = plt.figure()\n", + " ax = fig.add_axes([0, 0, 1, 1], projection='3d')\n", + " ax.axis('off')\n", + "\n", + " # prepare the axes limits\n", + " ax.set_xlim((-25, 25))\n", + " ax.set_ylim((-35, 35))\n", + " ax.set_zlim((5, 55))\n", + " \n", + " def lorenz_deriv((x, y, z), t0, sigma=sigma, beta=beta, rho=rho):\n", + " \"\"\"Compute the time-derivative of a Lorentz system.\"\"\"\n", + " return [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]\n", + "\n", + " # Choose random starting points, uniformly distributed from -15 to 15\n", + " np.random.seed(1)\n", + " x0 = -15 + 30 * np.random.random((N, 3))\n", + "\n", + " # Solve for the trajectories\n", + " t = np.linspace(0, max_time, int(250*max_time))\n", + " x_t = np.asarray([integrate.odeint(lorenz_deriv, x0i, t)\n", + " for x0i in x0])\n", + " \n", + " # choose a different color for each trajectory\n", + " colors = plt.cm.jet(np.linspace(0, 1, N))\n", + "\n", + " for i in range(N):\n", + " x, y, z = x_t[i,:,:].T\n", + " lines = ax.plot(x, y, z, '-', c=colors[i])\n", + " setp(lines, linewidth=2)\n", + "\n", + " ax.view_init(30, angle)\n", + " show()\n", + "\n", + " return t, x_t" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's call the function once to view the solutions. For this set of parameters, we see the trajectories swirling around two points, called attractors. " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t, x_t = solve_lorenz(angle=0, N=10)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Using IPython's `interactive` function, we can explore how the trajectories behave as we change the various parameters." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "w = interactive(solve_lorenz, angle=(0.,360.), N=(0,50), sigma=(0.0,50.0), rho=(0.0,50.0))\n", + "display(w)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The object returned by `interactive` is a `Widget` object and it has attributes that contain the current result and arguments:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t, x_t = w.result" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "w.arguments" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "After interacting with the system, we can take the result and perform further computations. In this case, we compute the average positions in $x$, $y$ and $z$." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "xyz_avg = x_t.mean(axis=1)" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "xyz_avg.shape" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Creating histograms of the average positions (across different trajectories) show that on average the trajectories swirl about the attractors." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "hist(xyz_avg[:,0])\n", + "title('Average $x(t)$')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "hist(xyz_avg[:,1])\n", + "title('Average $y(t)$')" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file