{ "metadata": { "name": "", "signature": "sha256:bef23ef8fbd80f76061a9b67928003d87464fb34501e3a11f37b4be4ccf6d51f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "Some Background on IPython" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "Let's start with some background on what IPython is,\n", "then we can talk about what parallel IPython is,\n", "and why it makes sense." ] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What is IPython?" ] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Level 0: Interactive Python" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def foo():\n", " \"\"\"docstring\"\"\"\n", " return 'hi'\n", "foo()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def bar():\n", " print 'hi'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from __future__ import print_function\n", "\n", "import time\n", "\n", "for i in range(10):\n", " time.sleep(0.25)\n", " print(i, end=' ')\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import ctypes\n", "libc = ctypes.CDLL('libc.dylib')\n", "libc.time(-1)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(\"I'm okay!\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Level 1: A bit more than Python" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np." ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "np.linspace?" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Shell interaction" ] }, { "cell_type": "code", "collapsed": false, "input": [ "!date" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "ls" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "files = !ls" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "for f in files:\n", " print(repr(f))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Extending Python with `%magics`" ] }, { "cell_type": "code", "collapsed": false, "input": [ "biglist = range(1000000)\n", "bigset = set(biglist)\n", "\n", "item = biglist[0]\n", "%timeit item in biglist\n", "%timeit item in bigset" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "def inner(x):\n", " return 1/x\n", "\n", "def outer(y):\n", " return inner(1-y)\n", "\n", "z = 2\n", "outer(-z + 3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%debug" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%lsmagic" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Level 2: A bit more than the Terminal" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Rich Display" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from scipy.special import jn" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "x = np.linspace(0,10)\n", "for n in range(5):\n", " plt.plot(x, jn(n,x))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "%%latex\n", "\\begin{eqnarray}\n", "\\nabla \\times \\vec{\\mathbf{B}} &\n", " - \\frac{1}{c} \\frac{\\partial\\vec{\\mathbf{E}}}{\\partial t} \n", " & = & \\frac{4 \\pi}{c} \\vec{\\mathbf{j}} \\\\\n", "\\nabla \\cdot \\vec{\\mathbf{E}} && = & 4 \\pi \\rho \\\\\n", "\\nabla \\times \\vec{\\mathbf{E}} &\n", " + \\frac{1}{c} \\frac{\\partial\\vec{\\mathbf{B}}}{\\partial t}\n", " & = & \\vec{\\mathbf{0}} \\\\\n", "\\nabla \\cdot \\vec{\\mathbf{B}} && = & 0 \n", "\\end{eqnarray}\n" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import sympy\n", "sympy.init_printing(use_latex='mathjax')\n", "x = sympy.symbols('x')\n", "expr = x**10-1\n", "expr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "sympy.factor(expr)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import display\n", "from IPython.html.widgets import interact\n", "\n", "@interact\n", "def factorit(n=10):\n", " display(sympy.factor(x**n-1))\n", " " ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.display import Image, YouTubeVideo\n", "Image(\"figs/darts.png\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "YouTubeVideo('BROWqjuTM0g ')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Level 3: Documents" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "In the Notebook: not just code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also have\n", "\n", "- *markdown* and\n", "- $\\LaTeX$\n", "\n", "```javascript\n", "var foo = $(\".class\");\n", "```" ] }, { "cell_type": "heading", "level": 2, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What is a notebook?" ] }, { "cell_type": "code", "collapsed": false, "input": [ "name = \"Background\"\n", "!head -n 100 {name}.ipynb | pygmentize -l json" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "!ipython nbconvert {name}.ipynb --to html\n", "\n", "from IPython.display import display, HTML\n", "display(HTML(\"{name}.html\".format(name=name)))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "But what is IPython, *really*?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[*A Protocol*](http://ipython.org/ipython-doc/dev/development/messaging.html)\n", "\n", "Message type: `execute_request`\n", "\n", "```python\n", "content = {\n", " # Source code to be executed by the kernel, one or more lines.\n", " 'code' : str,\n", "\n", " # A boolean flag which, if True, signals the kernel to execute\n", " # this code as quietly as possible.\n", " 'silent' : bool,\n", "\n", " # A boolean flag which, if True, signals the kernel to populate history\n", " # The default is True if silent is False. If silent is True, store_history\n", " # is forced to be False.\n", " 'store_history' : bool,\n", " ...\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Message type: `execute_reply`\n", "\n", "```python\n", "content = {\n", " # One of: 'ok' OR 'error'\n", " 'status' : str,\n", "\n", " # The global kernel counter that increases by one with each request that\n", " # stores history. This will typically be used by clients to display\n", " # prompt numbers to the user. If the request did not store history, this will\n", " # be the current value of the counter in the kernel.\n", " 'execution_count' : int,\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Message type: `display_data`\n", "\n", "```python\n", "content = {\n", "\n", " # The data dict contains key/value pairs, where the kids are MIME\n", " # types and the values are the raw data of the representation in that\n", " # format.\n", " 'data' : dict,\n", "\n", " # Any metadata that describes the data\n", " 'metadata' : dict,\n", "}\n", "```" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%%javascript\n", "IPython.notebook.kernel.execute(\"a=1\");" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "print(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The protocol is [publicly documented](http://ipython.org/ipython-doc/dev/development/messaging.html), and [language agnostic](https://github.com/ipython/ipython/wiki/Projects-using-IPython#list-of-some-ipython-compatible-kernels).\n" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "So what is IPython, really?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Tools for interactive computing\n", " - documents\n", " - interfaces\n", " - terminal\n", " - qtconsole\n", " - notebook\n", " - introspection\n", " - debugging\n", "- A message protocol for remote execution\n", " - language-agnostic\n", " - rich remote repl" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "How does this become IPython.parallel?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see a [quick demo](examples/Parallel%20image%20processing.ipynb), and then [find out](Overview.ipynb) in more detail." ] } ], "metadata": {} } ] }