##// END OF EJS Templates
Adding Interact and Lorenz examples.
Adding Interact and Lorenz examples.

File last commit:

r15133:73078b3d
r15133:73078b3d
Show More
Interact.ipynb
474 lines | 10.6 KiB | text/plain | TextLexer

Interact Demos

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.

In [ ]:
%pylab inline
In [ ]:
from IPython.html.widgets.interact import interact, interactive
from IPython.html import widgets
from IPython.display import clear_output, display, HTML

Basic interact

Here is a simple function that displays its arguments as an HTML table:

In [ ]:
def show_args(**kwargs):
    s = '<h3>Arguments:</h3><table>\n'
    for k,v in kwargs.items():
        s += '<tr><td>{0}</td><td>{1}</td></tr>\n'.format(k,v)
    s += '</table>'
    display(HTML(s))
In [ ]:
show_args(a=10, b='Hi There', c=True)

Let's use this function to explore how interact works.

In [ ]:
interact(show_args,
         Temp=(0,10),
         Current=(0.,10.,0.01),
         z=(True,False),
         Text=u'Type here!',
         Algorithm=['This','That','Other'],
         a=widgets.FloatRangeWidget(min=-10.0, max=10.0, step=0.1, value=5.0)
         )

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.

Factoring polynomials

Here is an example that uses SymPy to factor polynomials.

In [ ]:
from sympy import Symbol, Eq, factor, init_printing
init_printing(use_latex=True)
In [ ]:
x = Symbol('x')
In [ ]:
def factorit(n):
    display(Eq(x**n-1, factor(x**n-1)))

Notice how the output of the factorit function is properly formatted LaTeX.

In [ ]:
interact(factorit, n=(2,40))

A simple image browser

This example shows how to browse through a set of images with a slider.

In [ ]:
from sklearn import datasets

We will use the digits dataset from scikit-learn.

In [ ]:
digits = datasets.load_digits()
In [ ]:
def browse_images(digits):
    n = len(digits.images)
    def view_image(i):
        imshow(digits.images[i], cmap=cm.gray_r, interpolation='nearest')
        title('Training: %s' % digits.target[i])
        show()
    interact(view_image, i=(0,n-1))
In [ ]:
browse_images(digits)

Explore random graphs

In this example, we build a simple UI for exploring random graphs with NetworkX.

In [ ]:
import networkx as nx
In [ ]:
def plot_random_graph(n, p, generator):
    g = generator(n,p)
    nx.draw(g)
    show()
In [ ]:
interact(plot_random_graph, n=(2,30), p=(0.0, 1.0, 0.001),
        generator={'gnp': nx.gnp_random_graph,
                   'erdos_renyi': nx.erdos_renyi_graph,
                   'binomial': nx.binomial_graph})

Image manipulation

This example builds a simple UI for performing basic image manipulation with scikit-image.

In [ ]:
import skimage
from skimage import data, filter, io
In [ ]:
i = data.coffee()
In [ ]:
io.Image(i)
In [ ]:
def edit_image(image):
    def apply_filter(sigma, r, g, b):
        new_image = filter.gaussian_filter(image, sigma=sigma)
        new_image[:,:,0] = r*new_image[:,:,0]
        new_image[:,:,1] = g*new_image[:,:,1]
        new_image[:,:,2] = b*new_image[:,:,2]
        new_image = io.Image(new_image)
        display(new_image)
        return new_image
    lims = (0.0,1.0,0.01)
    return interactive(apply_filter, sigma=(0.1,10.0,0.01), r=lims, g=lims, b=lims)
In [ ]:
w = edit_image(i)
In [ ]:
display(w)
In [ ]:
w.arguments
In [ ]:
w.result

Playing with audio

This example uses the Audio object and Matplotlib to explore the phenomenon of beat frequencies.

In [ ]:
from IPython.display import Audio
import numpy as np
In [ ]:
def beat_freq(f1=220.0, f2=224.0):
    max_time = 3
    rate = 8000.0
    times = np.linspace(0,max_time,rate*max_time)
    signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)
    print f1, f2, abs(f1-f2)
    display(Audio(data=signal, rate=rate))
    return signal
In [ ]:
v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))
display(v)
In [ ]:
plot(v.result[0:6000])