{ "metadata": { "name": "", "signature": "sha256:f22bd85e82a2a92bfc795829d7b53aefc09c1f824389a0a4e041ab89dcd00399" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced IPython session\n", "\n", "* IPython magics for development\n", "* Integration in IPython\n", "* Parallelization in IPython (in separate notebook)\n", "* Advanced Display Capabilities (in separate notebook)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image\n", "Image(url='http://imgs.xkcd.com/comics/new_pet.png')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Comic: [XKCD: New Pet](http://xkcd.com/413/)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Where can I find more of this advanced information?\n", "\n", "We have an [IPython in-depth tutorial](https://github.com/ipython/ipython-in-depth) that you can download. And you can watch it all online:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('xe_ATRmw0KM') # Part 1/3, the whole 3-part thing is 3 hours" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "YouTubeVideo('A8VbS-YX2Lo') # Part 2/3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "YouTubeVideo('4tJKZWWRs6s') # Part 3/3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Development goodies\n", "\n", "### Documentation and source" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%pylab inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "plot?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%pdoc plot" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%psource plot" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The IPython kernel/client model" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%connect_info" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%qtconsole" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Loading from the web\n", "\n", "Finding useful examples in the [matplotlib gallery](http://matplotlib.org/gallery.html)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load http://matplotlib.org/mpl_examples/showcase/xkcd.py" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Integration\n", "\n", "### R (statistical programming package)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Might need to install R and rpy2\n", "%load_ext rpy2.ipython" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "X = np.arange(100)\n", "Y = 200.*X + 100." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%R -i X,Y -o XYcoeff\n", "XYlm = lm(Y~X)\n", "print(summary(XYlm))\n", "XYcoeff = coef(XYlm)\n", "par(mfrow=c(2,2))\n", "plot(XYlm)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print XYcoeff" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Octave" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Might need to install Octave and oct2py\n", "%load_ext octavemagic" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%octave -s 600,200 -f png\n", "\n", "subplot(121);\n", "[x,y] = meshgrid(0:0.1:3);\n", "r = sin(x-0.5).^2 + cos(y-0.5).^2;\n", "surf(x,y,r);\n", "\n", "subplot(122);\n", "sombrero()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cython (C and Python mashup)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%load_ext cythonmagic" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def f(x):\n", " return x**2-x\n", "\n", "def integrate_f(a, b, N):\n", " s = 0\n", " dx = (b-a)/N\n", " for i in range(N):\n", " s += f(a+i*dx)\n", " return s * dx" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%cython\n", "\n", "cdef double f(double x):\n", " return x**2-x\n", "\n", "def cyintegrate_f(double a, double b, int N):\n", " cdef int i\n", " cdef double s, dx\n", " s = 0\n", " dx = (b-a)/N\n", " for i in range(N):\n", " s += f(a+i*dx)\n", " return s * dx" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "a, b, N = 0, 2, 10000\n", "%timeit integrate_f(a, b, N)\n", "%timeit cyintegrate_f(a, b, N)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ruby (wuhuu)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%ruby\n", "s = \"Yo IPython, Ruby's here 4 real!\"\n", "s.split(\" \").each do |word| \n", " puts word if word.to_i.to_s == word\n", "end" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Interactive Widgets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**NOTE** Only in IPython 2.0 and later!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The IPython website has [documentation and examples](http://nbviewer.ipython.org/github/ipython/ipython/blob/2.x/examples/Interactive%20Widgets/Index.ipynb)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.html.widgets import interactive\n", "from IPython.display import Audio, display" ], "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\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": [ "v.kwargs" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "f1, f2 = v.children\n", "f1.value = 255\n", "f2.value = 260\n", "plt.plot(v.result[0:6000])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }