{ "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import Image\n", "Image(url='http://imgs.xkcd.com/comics/new_pet.png')" ] }, { "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", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('xe_ATRmw0KM') # Part 1/3, the whole 3-part thing is 3 hours" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "YouTubeVideo('A8VbS-YX2Lo') # Part 2/3" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "YouTubeVideo('4tJKZWWRs6s') # Part 3/3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Development goodies\n", "\n", "### Documentation and source" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "from matplotlib.pyplot import plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plot?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%pdoc plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%psource plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The IPython kernel/client model" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%connect_info" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%qtconsole" ] }, { "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": "markdown", "metadata": {}, "source": [ "## 2. Integration\n", "\n", "### R (statistical programming package)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Might need to install R and rpy2\n", "%load_ext rpy2.ipython" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n", "X = np.arange(100)\n", "Y = 200.*X + 100." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%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)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(XYcoeff)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Octave" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Might need to install Octave and oct2py\n", "%load_ext octavemagic" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%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()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cython (C and Python mashup)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%load_ext cython" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a, b, N = 0, 2, 10000\n", "%timeit integrate_f(a, b, N)\n", "%timeit cyintegrate_f(a, b, N)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ruby" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%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" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" }, "widgets": { "state": {}, "version": "1.1.1" } }, "nbformat": 4, "nbformat_minor": 1 }