{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Jupyter notebooks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*This tutorial was modified from materials of the Caltech course Data Analysis in the Biological Sciences taught by Justin Bois* http://bebi103.caltech.edu/2015/tutorials.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first thing we'll do, [discussed later](#Best-practices-for-code-cells), is import all the modules we'll need. You should in general do this at the very beginning of each notebook, and in fact each `.py` file you write." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Import numerical tools\n", "import numpy as np\n", "import scipy.integrate\n", "\n", "# Import pyplot for plotting\n", "import matplotlib.pyplot as plt\n", "\n", "# Magic function to make matplotlib inline; other style specs must come AFTER\n", "%matplotlib inline\n", "\n", "%config InlineBackend.figure_formats = {'svg',}\n", "#%config InlineBackend.figure_formats = {'png', 'retina'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this tutorial, you will learn the basics on how to use Jupyter notebooks. Your problem sets will be submitted as Jupyter notebooks, so this is something you will need to master.\n", "\n", "Reading [the official Jupyter documentation](http://jupyter-notebook.readthedocs.org/) can also be helpful." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Contents\n", "* [What is Jupyter](#What-is-Jupyter?)\n", "* [Launching a Jupyter notebook](#Launching-a-Jupyter-notebook)\n", "* [Cells](#Cells)\n", "* [Code cells](#Code-cells)\n", " - [Display of graphics](#Display-of-graphics)\n", " - [Interactive plotting with Bokeh](#Interactive-plotting-with-Bokeh)\n", " - [Proper formatting of cells](#Proper-formatting-of-cells)\n", " - [Best practices for code cells](#Best-practices-for-code-cells)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What is Jupyter?\n", "[Jupyter](http://jupyter.org) is a way to combine text and code (which runs and can display graphic output!) in an easy-to-read document that renders in a web browser. The notebook itself is stored as a text file in [JSON](http://json.org) format. This text file is what you will submit to bCourses for your problem sets.\n", "\n", "Many different types of programming languages can be run within a Jupyter notebook. We will be using the language [Python](http://python.org/) which provides flexible and powerful tools for data analysis and plotting." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Launching a Jupyter notebook\n", "\n", "To launch a Jupyter notebook, you can do the following.\n", "* **Mac**: Use the Anaconda launcher and select Jupyter notebook.\n", "* **Windows**: Under \"Search programs and files\" from the Start menu, type `jupyter notebook` and select \"Jupyter notebook.\"\n", "\n", "A Jupyter notebook will then launch in your default web browser.\n", "\n", "You can also launch Jupyter from the command line. To do this, simply enter\n", "\n", " jupyter notebook\n", "\n", "on the command line and hit enter. This also allows for greater flexibility, as you can launch Jupyter with command line flags. For example, I launch Jupyter using\n", "\n", " jupyter notebook --browser=safari\n", "\n", "This fires up Jupyter with Safari as the browser. If you launch Jupyter from the command line, your shell will be occupied with Jupyter and will occasionally print information to the screen. \n", "\n", "When you launch Jupyter, you will be presented with a menu of files in your current working directory to choose to edit. You can also navigate around the files on your computer to find a file you wish to edit by clicking the \"Upload\" button in the upper right corner. You can also click \"New\" in the upper right corner to get a new Jupyter notebook. After selecting the file you wish to edit, it will appear in a new window in your browser, beautifully formatted and ready to edit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cells\n", "A Jupyter notebook consists of **cells**. The two main types of cells you will use are **code cells** and **markdown cells**, and we will go into their properties in depth momentarily. First, an overview.\n", "\n", "A code cell contains actual code that you want to run. You can specify a cell as a code cell using the pulldown menu in the toolbar in your Jupyter notebook. Otherwise, you can can hit `esc` and then `y` (denoted \"`esc, y`\") while a cell is selected to specify that it is a code cell. Note that you will have to hit enter after doing this to start editing it.\n", "\n", "If you want to execute the code in a code cell, hit \"`shift + enter`.\" Note that code cells are executed in the order you execute them. That is to say, the ordering of the cells for which you hit \"`shift + enter`\" is the order in which the code is executed. If you did not explicitly execute a cell early in the document, its results are not known to the Python interpreter.\n", "\n", "Markdown cells contain text. The text is written in **markdown**, a lightweight markup language. You can read about its syntax [here](http://daringfireball.net/projects/markdown/syntax). Note that you can also insert HTML or $\\LaTeX$ expressions into markdown cells, and they will be rendered properly.\n", "\n", "As you are typing the contents of these cells, the results appear as text. Hitting \"`shift + enter`\" renders the text in the formatting you specify. You can specify a cell as being a markdown cell in the Jupyter toolbar, or by hitting \"`esc, m`\" in the cell. Again, you have to hit enter after using the quick keys to bring the cell into edit mode (or you can just double-click on the cell).\n", "\n", "In general, when you want to add a new cell, you can use the \"Insert\" pulldown menu from the Jupyter toolbar. The shortcut to insert a cell below is \"`esc, b`\" and to insert a cell above is \"`esc, a`.\" Alternatively, you can execute a cell and automatically add a new one below it by hitting \"`alt + enter`.\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Code cells\n", "Below is an example of a code cell printing `hello, world.` Notice that the output of the print statement appears in the same cell, though separate from the code block. Notice also that the first line of the code cell has the hash-tag (`#`) in front of it. This symbol designates a line as a \"comment.\" These lines are ignored by the compiler (i.e., not treated as code)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Say hello to the world.\n", "print('hello, world.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you evaluate a Python expression that returns a value, that value is displayed as output of the code cell. This only happens, however, for the last line of the code cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Would show 9 if this were the last line, but it is not, so shows nothing\n", "4 + 5\n", "\n", "# I hope we see 11.\n", "5 + 6" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note, however, if the last line does not return a value, such as if we assigned a variable, there is no visible output from the code cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Variable assignment, so no visible output.\n", "a = 5 + 6" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# However, now if we ask for a, its value will be displayed\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display of graphics\n", "When displaying graphics, you should have them **inline**, meaning that they are displayed directly in the IPython notebook and not in a separate window. You can specify that, as I did at the top of this document, using the `%matplotlib inline` magic function. Below is an example of graphics displayed inline.\n", "\n", "Generally, I prefer presenting graphics as scalable vector graphics (SVG). Vector graphics are infinitely zoom-able; i.e., the graphics are represented as points, lines, curves, etc., in space, not as a set of pixel values as is the case with raster graphics (such as PNG). By default, graphics are displayed as PNGs, but you can specify SVG as I have at the top of this document in the first code cell. \n", "\n", " %config InlineBackend.figure_formats = {'svg',}\n", "\n", "If SVG graphics aren't working in your browser PNG graphics at a high resolution can be used instead\n", "\n", " %config InlineBackend.figure_formats = {'png', 'retina'}\n", " \n", "at the top of your file, as we have here." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Generate data to plot\n", "x = np.linspace(0, 2 * np.pi, 200)\n", "y = np.exp(np.sin(x))\n", "\n", "# Make plot\n", "plt.plot(x, y)\n", "plt.xlim((0, 2 * np.pi))\n", "plt.xlabel(r'$x$')\n", "plt.ylabel(r'$\\mathrm{e}^{\\sin{x}}$')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Proper formatting of cells\n", "Generally, it is a good idea to keep cells simple. You can define one function, or maybe two or three closely related functions, in a single cell, and that's about it. When you define a function, you should make sure it is properly commented with descriptive doc strings. Below is an example of how I might generate a plot of the Lorenz attractor (a classic system of differential equations that exhibits chaotic behavior and is very interesting-looking) with code cells and markdown cells with discussion of what I am doing. Don't worry about the details of the math here; it isn't too relevant to what we'll be doing in this course. Instead, what I want you to pay attention to is how functions are used and defined." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first function defines the system of equations that constitutes the Lorenz attractor, which will ultimately be used in another equation that solves the system of equations. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def lorenz_attractor(r, t, p):\n", " \"\"\"\n", " Compute the right hand side of system of the equations for Lorenz attractor.\n", " \n", " Parameters\n", " ----------\n", " r : array_like, shape (3,)\n", " (x, y, z) position of trajectory.\n", " t : dummy_argument\n", " Dummy argument, necessary to pass function into \n", " scipy.integrate.odeint\n", " p : array_like, shape (3,)\n", " Parameters (s, k, b) for the attractor.\n", " \n", " Returns\n", " -------\n", " output : ndarray, shape (3,)\n", " Time derivatives of Lorenz attractor.\n", " \n", " Notes\n", " -----\n", " .. Returns the right hand side of the system of differential equations describing\n", " the Lorenz attractor.\n", " x' = s * (y - x)\n", " y' = x * (k - z) - y\n", " z' = x * y - b * z\n", " \"\"\"\n", " # Unpack variables and parameters. \n", " x, y, z = r #r is one of the inputs of the function. It is an array containing three elements, which end up getting assigned to the variables x, y, and z.\n", " s, p, b = p #Similarly, p is another 3-element array, in which the elements are assigned to the variables s, p, and b.\n", " \n", " return np.array([s * (y - x), \n", " x * (p - z) - y, \n", " x * y - b * z])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With this function in hand, we just have to pick our initial conditions and time points, run the numerical integration, and then plot the result." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Parameters to use\n", "p = np.array([10.0, 28.0, 8.0 / 3.0]) #This is the command that we use to define an array.\n", "\n", "# Initial condition\n", "r0 = np.array([0.1, 0.0, 0.0])\n", "\n", "# Time points to sample\n", "t = np.linspace(0.0, 80.0, 10000) #linspace produces an array of linearly spaced points between 0 and 10000, with a time step of 80\n", "\n", "# Use scipy.integrate.odeint to integrate Lorentz attractor\n", "r = scipy.integrate.odeint(lorenz_attractor, r0, t, args=(p,)) #This is a specialized built-in function that solves the system of equations for x, y, and z at any instant in time.\n", "\n", "# Unpack results into x, y, z.\n", "x, y, z = r.transpose() #This assigns the output of the solution to three variables: x, y, and z (easier to refer to in plotting!)\n", "\n", "# Plot the result\n", "plt.plot(x, z, '-', linewidth=0.5) #plt means that we are now calling on functions form the plotting library. This shows trajectories of the solution in x-z space. Ooh, pretty, huh?\n", "plt.xlabel(r'$x(t)$', fontsize=18)\n", "plt.ylabel(r'$z(t)$', fontsize=18)\n", "plt.title(r'$x$-$z$ proj. of Lorenz attractor traj.')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Best practices for code cells\n", "Here is a summary of some general rules for composing and formatting your code cells.\n", "1. Do not exceed the width of the code cell.\n", "2. Keep your code cells short. If you find yourself having one massive code cell, break it up.\n", "3. Always properly comment your code. Provide complete doc strings for any functions you define.\n", "4. Do all of your imports in the first code cell at the top of the notebook. Import one module per line.\n", "5. For submitting problem sets, **always** display your graphics inline. You can render the graphics as PNGs if your browser starts experiencing performance issues, but SVG is preferred." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }