{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to Python for scientific computing\n", "\n", "(c) 2022 Justin Bois. This work is licensed under a [Creative Commons Attribution License CC-BY 4.0](https://creativecommons.org/licenses/by/4.0/). All code contained herein is licensed under an [MIT license](https://opensource.org/licenses/MIT).\n", "\n", "This document was prepared at [Caltech](http://www.caltech.edu) with support financial support from the [Donna and Benjamin M. Rosen Bioengineering Center](http://rosen.caltech.edu).\n", "\n", "\n", "\n", "*This tutorial was generated from an Jupyter notebook. You can download the notebook [here](intro_to_python.ipynb).*\n", "\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You should have already installed a Python distribution using Anaconda. Anaconda contains most of what we need to do scientific computing with Python. At the most basic level, it has Python 3.8. It contains other packages we will make heavy use of, the three most important ones being [NumPy](http://www.numpy.org/), [Bokeh](http://bokeh.pydata.org/), and [Jupyter](https://jupyter.org). We will also make heavy use of and [scikit-image](http://scikit-image.org/) throughout the course. Importantly, it also has JupyterLab, which we will use to do all of our assignments in Bi 1x.\n", "\n", "In this tutorial, we will first learn some of the basics of the Python programming language at the same time exploring the properties of NumPy's very useful (and as we'll see, ubiquitous) `ndarray` data structure, which we will refer to as a \"NumPy array.\" Finally, we'll load some data and use Bokeh to generate plots.\n", "\n", "You should have already learned how to launch a Jupyter notebook for JupyterLab from the [tutorial on setting up your environment](setting_up_python_environment.html)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Hello, world.\n", "\n", "As a new programmer, your first task is to greet the world with your new set of skills! We'll start by printing \"`Hello, world.`\" In a code cell in your Jupyter notebook, type the following: " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world.\n" ] } ], "source": [ "print('Hello, world.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do you see \"`Hello, world.`\" printed to the screen right below your command? The Python interpreter is taking your input and printing it out in the output of the cell in your Jupyter notebook.\n", "\n", "Here we see the syntax for function calls in Python. Function arguments are enclosed in parentheses. We also learned another important piece of Python syntax. Strings are enclosed in single (or double) quotes.\n", "\n", "Now try the following:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world.\n" ] } ], "source": [ "# This prints hello world\n", "print ('Hello, world.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how even though you wrote text above the `print` statement, only \"`Hello, world.`\" is printed. The `#` starts a comment; anything after the `#`, will not be read or interpreted by Python. This is how you add notes to your program about what certain lines do. Including comments in your code is essential so that other people can read your code and understand what you are trying to accomplish. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variable Assignment\n", "\n", "To assign a variable in python, we use an `=`, just like you would expect from any math class. Arithmetic operations are also as expected: `+`, `-`, `*`, `/`. \n", "\n", "Try the following: " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a is 3\n", "b is 27\n", "c is 16.5\n", "d is 16\n" ] } ], "source": [ "a = 3\n", "\n", "b = a**3\n", "\n", "c = (b + 2*a) / 2\n", "\n", "d = (b + 2*a) // 2\n", "\n", "print('a is', a)\n", "print('b is', b)\n", "print('c is', c)\n", "print ('d is', d)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the assignment of the variable `b`, we used the `**` operator. This means \"raise to the power of.\" In the assignment of the variable `d`, we used the `//` operator. This does integer division, returning the floor of the result of the division as an integer." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists, tuples, slicing, and indexing \n", "\n", "A list is a mutable array, meaning it can be edited. Let's explore below! Notice that a list is created using `[]`. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my_list is [1, 2, 3, 4]\n", "my_list is now [3.14, 2, 3, 4]\n", "the last element in my_list is 4\n" ] } ], "source": [ "my_list = [1,2,3,4]\n", "print('my_list is', my_list)\n", "\n", "my_list[0] = 3.14\n", "print('my_list is now', my_list)\n", "print('the last element in my_list is', my_list[-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that indexing is done with brackets, `[]`. Importantly, **in Python, indexing starts are zero.** Also notice that we can index the last element of a list with `-1`.\n", "\n", "A **tuple** is like a list, but it's immutable, meaning that once created, it cannot be changed. Let's try the same thing as above, but with a tuple, created with `()`. But notice that the indexing of the tuple is still denoted with `[]`. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "my tuple is (1, 2, 3, 4)\n" ] }, { "ename": "TypeError", "evalue": "'tuple' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [5]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmy tuple is\u001b[39m\u001b[38;5;124m'\u001b[39m, my_tuple) \n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# This will make Python scream at us because tuples are immutable.\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m my_tuple[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m3.14\u001b[39m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ], "source": [ "# Create a tuple and print it\n", "my_tuple = (1,2,3,4)\n", "print('my tuple is', my_tuple) \n", "\n", "# This will make Python scream at us because tuples are immutable.\n", "my_tuple[0] = 3.14" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What's the error? The Python interpreter is objecting because it cannot replace the `1` in `my_tuple[0]` with `3.14`; that operation is not supported. If you try printing out `my_tuple` again, you will see it hasn't been changed. \n", "\n", "A **string** is just a bunch of letters, or letters and numbers, strung together in an order. It can also be indexed, like we did above with the list and the tuple. Let's look at our favorite phrase. " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The fifth letter in the phrase is o\n", "The first four letters are Hell\n" ] } ], "source": [ "my_string = 'Hello, world.'\n", "print('The fifth letter in the phrase is', my_string[4])\n", "print('The first four letters are', my_string[0:4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "IMPORTANT! Python interprets `[0:4]` as [0,4), so be careful when pulling out strings of specific length. Pulling small strings out of our larger string is called slicing, and can also be done with lists and tuples. This can be very powerful, as we can even pull out pieces at regular intervals. " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5]\n", "[6, 7, 8, 9, 10]\n", "[1, 3, 5, 7, 9]\n", "[2, 5, 8]\n" ] } ], "source": [ "my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "\n", "a = my_list[0:5]\n", "print(a)\n", "\n", "b = my_list[5:]\n", "print(b)\n", "\n", "c = my_list[0:10:2]\n", "print(c)\n", "\n", "d = my_list[1:10:3]\n", "print(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make sure you notice how we create lists `c` and `d`. We select the entries in the list from position 0 to 9, selecting every 2 or 3, respectively. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Objects, types, and methods\n", "\n", "Python is object-oriented, and all values in a program are objects. An object consists of an **identity** (where it is stored in memory), a **type** (a definition of how the object is represented), and **data** (the value of the object). An object of a given type can have various methods that operate on the data of the object. How do we keep track of what type our variables are? Fortunately, Python has a function for this, called `type()`. Let's try it out. First, we'll create some new objects. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the type of a is \n", "the type of b is \n", "the type of my_list is \n", "the type of my_list[0] is \n", "the type of my_list[1] is \n", "the type of my_list[2] is \n" ] } ], "source": [ "a = 4\n", "b = 4.6\n", "\n", "my_list = [1, 3.49, 'bi1x']\n", "\n", "print('the type of a is', type(a))\n", "print('the type of b is', type(b))\n", "print('the type of my_list is', type(my_list))\n", "print('the type of my_list[0] is', type(my_list[0]))\n", "print('the type of my_list[1] is', type(my_list[1]))\n", "print('the type of my_list[2] is', type(my_list[2]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "What is most important to notice here is that `my_list` is a list, and that it can contain many different objects, from numbers to strings. \n", "\n", "The data are the numbers and values that you associate with your variable. \n", "\n", "Finally, objects have methods that can perform operations on the data. A method is called similarly to a function. This is best seen by example." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the number of 5's in the list is 2\n", "the number of 4's in the list is 1\n", "[1, 1, 3, 3, 4, 5, 5, 13, 17, 19, 31]\n", "[1, 1, 3, 3, 4, 5, 5, 13, 17, 19, 31, 'bi1x']\n" ] } ], "source": [ "my_list = [1 , 5 , 4 , 13 , 3 , 5 , 19 , 31 , 3 , 1 , 17]\n", "\n", "print(\"the number of 5's in the list is\", my_list.count(5))\n", "print(\"the number of 4's in the list is\", my_list.count(4))\n", "\n", "# Sort the list in place\n", "my_list.sort()\n", "print(my_list)\n", "\n", "# Tack on a string to the end of the list\n", "my_list.append('bi1x')\n", "print(my_list)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, a `list` object has several methods including `count()` and `sort()`. They are called just like functions, with arguments in parentheses. The name of the method comes after the object name followed by a dot (`.`).\n", "\n", "The `count()` method takes a single argument and returns the number of times that argument appears in the list. The sort function takes no arguments (but still requires open and closed parentheses to be called), and sorts the list in place. Note that `my_list` has been changed and is now sorted. We also use the method `append()`, which adds another element to the end of a list.\n", "\n", "Within JupyterLab, you can see what methods and data are available by entering the object name followed by a dot (`.`), and then pressing tab. Try it!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Packages\n", "\n", "It is common that scientific software packages such as [Matlab](http://www.mathworks.com/products/matlab/) and [Mathematica](http://www.wolfram.com/mathematica/) are optimized for a flavor of scientific computing (such as matrix computation in the case of Matlab) and are rather full-featured. On the other hand, Python is a generic programming language. It was not specifically designed to do scientific computing. So, plain old Python is very limited in scientific computing capability.\n", "\n", "However, Python is very flexible and allows use of **packages**. A package contains classes, functions, attributes, data types, etc., beyond what is built in to the core language. In order to use a module, you need to import it to make it available for use. So, as we begin working on data analysis and simulations in Bi 1x, we need to import modules we will use. \n", "\n", "It is important to note that until we import a module, its capacities are not available. Keep that in mind: *Plain old Python won't do much until you import a package.*\n", "\n", "Let's now import one of the major workhorses of our class, NumPy!" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "circumference / diameter = 3.141592653589793\n", "cos(pi) = -1.0\n" ] } ], "source": [ "# Importing is done with the import statement\n", "import numpy as np\n", "\n", "# We now have access to some handy things, i.e. np.pi\n", "print('circumference / diameter = ', np.pi)\n", "print('cos(pi) = ', np.cos(np.pi))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that we used the `import as ` construction. This enabled us to abbreviate the name of the module so we do not have to type `numpy` each time. Also, notice that to access the (approximate) value of $\\pi$ in the `numpy` module, we prefaced the name of the attribute (`pi`) with the module name followed by a dot (`np.`). This is generally how you access attributes in modules.\n", "\n", "We're already getting dangerous with Python. So dangerous, in fact, that we'll write our own function!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Writing your own function (and learning a bunch of syntax!)\n", "\n", "We will create a function that finds the roots of the quadratic equation\n", "\n", "\\begin{align}\n", "ax^2 + bx + c = 0.\n", "\\end{align}\n", "\n", "To do this, we will first write a function to compute the discriminant and then one to return the roots." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "def discriminant(a, b, c):\n", " \"\"\"\n", " Returns the discriminant of a quadratic polynomial\n", " a * x**2 + b * x + c = 0. \n", " \"\"\"\n", " return b**2 - 4.0 * a * c\n", "\n", "\n", "def roots(a, b, c):\n", " \"\"\"\n", " Returns the roots of the quadratic equation\n", " a * x**2 + b * x + c = 0.\n", " \"\"\" \n", " delta = discriminant(a, b, c)\n", " root_1 = (-b + np.sqrt(delta)) / (2.0 * a)\n", " root_2 = (-b - np.sqrt(delta)) / (2.0 * a)\n", " \n", " return root_1, root_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is a whole bunch of syntax in there to point out. \n", "\n", "- A function is defined with the **`def`** statement. It has the function prototype, followed by a colon.\n", "- **Indentation in Python matters!** Everything indented after the `def` statement is part of the function. Once the indentation goes back to the level of the `def` statement, you are no longer in the function.\n", "- The `return` statement is used to return the result of a function. If multiple objects are returned, they are separated by commas.\n", "- The text within triple quotes are **doc strings**. They say what the function does. These are essential for people to know what your code is doing.\n", "\n", "Now, let's test our new function out!" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "roots: 3.0 -0.6666666666666666\n" ] } ], "source": [ "# Python has nifty syntax for making multiple definitions on the same line\n", "a, b, c = 3.0, -7.0, -6.0\n", "\n", "# Call the function and print the result.\n", "root_1, root_2 = roots(a, b, c)\n", "print('roots:', root_1, root_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Very nice! Now, let's try another example. This one might have a problem...." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "roots: nan nan\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/0y/05rr2ttn5kv_pp6nzsg15mw00000gn/T/ipykernel_98320/3763812685.py:15: RuntimeWarning: invalid value encountered in sqrt\n", " root_1 = (-b + np.sqrt(delta)) / (2.0 * a)\n", "/var/folders/0y/05rr2ttn5kv_pp6nzsg15mw00000gn/T/ipykernel_98320/3763812685.py:16: RuntimeWarning: invalid value encountered in sqrt\n", " root_2 = (-b - np.sqrt(delta)) / (2.0 * a)\n" ] } ], "source": [ "# Specify a, b, and c that will give imaginary roots\n", "a, b, c = 1.0, -2.0, 2.0\n", "\n", "# Call the function and print the result\n", "root_1, root_2 = roots(a, b, c)\n", "print('roots:', root_1, root_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oh no! It gave us `nan`, which means \"not a number,\" as our roots. It also gave some warning that it encountered invalid (negative) arguments for the `np.sqrt()` function. The roots should be $1 \\pm i$, where $i = \\sqrt{-1}$. We will use this opportunity to introduce Python's control flow, starting with an `if` statement." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Control flow: the if statement\n", "\n", "We will decree that our quadratic equation solver only handles real roots, so it will raise an **exception** if an imaginary root is encountered. So, we modify the `roots()` function as follows." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def roots(a, b, c):\n", " \"\"\"\n", " Returns the roots of the quadratic equation\n", " a * x**2 + b * x + c = 0.\n", " \"\"\" \n", " delta = discriminant(a, b, c)\n", "\n", " if delta < 0.0:\n", " raise RuntimeError('Imaginary roots! We only do real roots!')\n", " else:\n", " root_1 = (-b + np.sqrt(delta)) / (2.0 * a)\n", " root_2 = (-b - np.sqrt(delta)) / (2.0 * a)\n", " \n", " return root_1, root_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have now exposed the syntax for a Python `if` statement. The conditional expression ends with a colon, just like the `def` statement. Note the indentation of blocks of code after the conditionals. (We actually did not need the `else` statement, because the program would just continue without the exception, but I left it there for illustrative purposes. It is actually preferred not to have the `else` statement.)\n", "\n", "Let's try out our new function." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "ename": "RuntimeError", "evalue": "Imaginary roots! We only do real roots!", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [15]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Pass in parameters that will give imaginary roots (use qd.roots)\u001b[39;00m\n\u001b[1;32m 2\u001b[0m a, b, c \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1.0\u001b[39m, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m2.0\u001b[39m, \u001b[38;5;241m2.0\u001b[39m\n\u001b[0;32m----> 3\u001b[0m root_1, root_2 \u001b[38;5;241m=\u001b[39m \u001b[43mroots\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mc\u001b[49m\u001b[43m)\u001b[49m\n", "Input \u001b[0;32mIn [14]\u001b[0m, in \u001b[0;36mroots\u001b[0;34m(a, b, c)\u001b[0m\n\u001b[1;32m 6\u001b[0m delta \u001b[38;5;241m=\u001b[39m discriminant(a, b, c)\n\u001b[1;32m 8\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m delta \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0.0\u001b[39m:\n\u001b[0;32m----> 9\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mImaginary roots! We only do real roots!\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 11\u001b[0m root_1 \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m-\u001b[39mb \u001b[38;5;241m+\u001b[39m np\u001b[38;5;241m.\u001b[39msqrt(delta)) \u001b[38;5;241m/\u001b[39m (\u001b[38;5;241m2.0\u001b[39m \u001b[38;5;241m*\u001b[39m a)\n", "\u001b[0;31mRuntimeError\u001b[0m: Imaginary roots! We only do real roots!" ] } ], "source": [ "# Pass in parameters that will give imaginary roots (use qd.roots)\n", "a, b, c = 1.0, -2.0, 2.0\n", "root_1, root_2 = roots(a, b, c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This threw the appropriate exception." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Loops\n", "\n", "If we want to do something many times, we can use a for loop. This is again best learned by example. Let's make a function that counts the number of times a substring is present in a sequence of DNA." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def n_substring(seq, substr):\n", " \"\"\"\n", " Given a sequence seq , returns the number of occurrances of substring.\n", " \"\"\"\n", " # First make sure the length of substring is shorter than seq.\n", " if len(substr) > len(seq) :\n", " return 0\n", "\n", " # We loop through the sequence to check for matches\n", " num_substr = 0\n", " for i in range(0, len(seq)-len(substr)+1):\n", " if seq [i:i+len(substr)] == substr:\n", " num_substr += 1\n", " \n", " # We are done looping now , so return the number of subsequences\n", " return num_substr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see how it works!" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "There are 3 GATs in the sequence.\n" ] } ], "source": [ "# Specify sequences\n", "seq = 'ACTGTACGATCGAGCGATCGAGCGAGTCATTACGACTGAGATCC'\n", "substr = 'GAT'\n", "\n", "# Call function to get number of GAT substrings\n", "n_gat = n_substring(seq, substr)\n", "\n", "# Print the result\n", "print('There are %d GATs in the sequence.' % n_gat)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we know it works, let's look at how the loop was constructed. In the statement at the beginning of the loop, we use the function `range()` to define an *iterator*. Calling `range(n)` creates an object akin to a list of integers from `0` to `n-1`. The `for` statement says that the variable `i` will successively take the values of the iterator." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Keyword arguments\n", "\n", "Before concluding our quick trip through the very basics of Python and on to NumPy, I want to show a very handy tool in Python, **keyword arguments**. Before, when we defined a function, we specified its arguments as variable names separated by commas in the def statement. We can also specify keyword arguments. Here is an example from our quadratic equation solver." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "def roots(a, b, c, print_discriminant=False, message_to_the_world='Bi 1x rules'):\n", " \"\"\"\n", " Returns the roots of the quadratic equation\n", " a * x**2 + b * x + c = 0.\n", " \n", " If print_discriminant is True, prints discriminant to screen\n", " \"\"\" \n", " delta = discriminant(a, b, c)\n", " if print_discriminant: \n", " print('discriminant =', delta)\n", " \n", " if message_to_the_world is not None: \n", " print('\\n' + '*'*len(message_to_the_world))\n", " print(message_to_the_world)\n", " print('*'*len(message_to_the_world) + '\\n')\n", "\n", " if delta < 0.0:\n", " raise ValueError('Imaginary roots! We only do real roots!')\n", " else:\n", " root_1 = (-b + np.sqrt(delta)) / (2.0 * a)\n", " root_2 = (-b - np.sqrt(delta)) / (2.0 * a)\n", " \n", " return root_1, root_2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function quadratic_roots now has two keyword arguments. They are signified by the equals sign. \n", "\n", "The function has three required arguments, `a`, `b`, `c`. If the keyword arguments are omitted in the function call, they take on the default values, as specified in the function definition. \n", "\n", "In the example, the default for `print_discriminant` is `False` and the default for `message_to_the_world` is `'Bi 1x rules!'`. Furthermore, ordering of keyword arguments does not matter when calling the function. They are called in the function similarly to the way they are defined in the function definition.\n", "\n", "Let's try it!" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "***********\n", "Bi 1x rules\n", "***********\n", "\n", "roots: 3.0 -0.6666666666666666\n" ] } ], "source": [ "a, b, c = 3.0, -7.0, -6.0\n", "\n", "root_1, root_2 = roots(a, b, c)\n", "\n", "print('roots:', root_1, root_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since we did not specify the keyword arguments, the defaults were used. We can specify other values." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "discriminant = 121.0\n", "\n", "***********************\n", "Bi 1x TAs are the best!\n", "***********************\n", "\n", "roots: 3.0 -0.6666666666666666\n" ] } ], "source": [ "root_1, root_2 = roots(\n", " a, \n", " b, \n", " c, \n", " print_discriminant=True, \n", " message_to_the_world=\"Bi 1x TAs are the best!\"\n", ")\n", "\n", "print(\"roots:\", root_1, root_2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above function call to `roots()`, notice that I put the arguments on separate lines. This is valid Python syntax and, when used appropriately, can make your code more readable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Intro to NumPy, SciPy, and Bokeh\n", "\n", "Here are some words to live by: **If you are trying to do a task that you think might be common, it's probably part of NumPy or some other package.** Look, or ask Google, first. In this case, NumPy has a function called `roots()` that computes the roots of a polynomial. To figure out how to use it, we can either look at the doc string, or look in the [NumPy and SciPy documentation online](http://docs.scipy.org/doc/) (the documentation for `np.roots()` is available [here](http://docs.scipy.org/doc/numpy/reference/generated/numpy.roots.html)). To look at the doc string, you can enter the following in a code cell:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[0;31mSignature:\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mroots\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mDocstring:\u001b[0m\n", "Return the roots of a polynomial with coefficients given in p.\n", "\n", ".. note::\n", " This forms part of the old polynomial API. Since version 1.4, the\n", " new polynomial API defined in `numpy.polynomial` is preferred.\n", " A summary of the differences can be found in the\n", " :doc:`transition guide `.\n", "\n", "The values in the rank-1 array `p` are coefficients of a polynomial.\n", "If the length of `p` is n+1 then the polynomial is described by::\n", "\n", " p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]\n", "\n", "Parameters\n", "----------\n", "p : array_like\n", " Rank-1 array of polynomial coefficients.\n", "\n", "Returns\n", "-------\n", "out : ndarray\n", " An array containing the roots of the polynomial.\n", "\n", "Raises\n", "------\n", "ValueError\n", " When `p` cannot be converted to a rank-1 array.\n", "\n", "See also\n", "--------\n", "poly : Find the coefficients of a polynomial with a given sequence\n", " of roots.\n", "polyval : Compute polynomial values.\n", "polyfit : Least squares polynomial fit.\n", "poly1d : A one-dimensional polynomial class.\n", "\n", "Notes\n", "-----\n", "The algorithm relies on computing the eigenvalues of the\n", "companion matrix [1]_.\n", "\n", "References\n", "----------\n", ".. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*. Cambridge, UK:\n", " Cambridge University Press, 1999, pp. 146-7.\n", "\n", "Examples\n", "--------\n", ">>> coeff = [3.2, 2, 1]\n", ">>> np.roots(coeff)\n", "array([-0.3125+0.46351241j, -0.3125-0.46351241j])\n", "\u001b[0;31mFile:\u001b[0m ~/opt/anaconda3/lib/python3.9/site-packages/numpy/lib/polynomial.py\n", "\u001b[0;31mType:\u001b[0m function\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "np.roots?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We see that we need to pass the coefficients of the polynomial we would like the roots of using an \"`array_like`\" object. We will discuss what this means in a moment, but for now, we will just use a **list** to specify our coefficients and call the `np.roots()` function." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Roots for (a, b, c) = (3, -7, -6): [ 3. -0.66666667]\n", "Roots for (a, b, c) = (1, -2, 2): [1.+1.j 1.-1.j]\n" ] } ], "source": [ "# Define the coefficients in a list (using square brackets)\n", "coeffs = [3.0, -7.0, -6.0]\n", "\n", "# Call np.roots. It returns an np.ndarray with the roots\n", "roots = np.roots(coeffs)\n", "print('Roots for (a, b, c) = (3, -7, -6):', roots)\n", "\n", "# It even handles complex roots!\n", "roots = np.roots([1.0, -2.0, 2.0])\n", "print('Roots for (a, b, c) = (1, -2, 2): ', roots)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some `array_like` data types\n", "\n", "In the previous example, we used a list as an `array_like` data type. Python has several native data types. We have already mentioned `int`s and `float`s. We just were not very explicit about it. Python's native `array_like` data types are **lists** and **tuples**. Internally, these things are converted into **NumPy arrays**, which is the most often used `array_like` data type we will use." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The NumPy array: maybe your new best friend\n", "\n", "Lists and tuples can be useful, but for many applications in data analysis, the `np.ndarray`, which we will colloquially call a \"**NumPy array**,\" is most often used. They are created using the `np.array` function with a list or tuple as an argument. Once created, we can do all sorts of things with them. Let's play!\n", "\n", "Let's make some arrays to see what they look like: " ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "array 1:\n", "[1 2 3 4] \n", "\n", "array_2:\n", "[[1 2]\n", " [1 2]] \n", "\n", "array_3:\n", "[[1 2 3]\n", " [1 2 3]\n", " [1 2 3]]\n" ] } ], "source": [ "array_1 = np.array([1, 2, 3, 4])\n", "print('array 1:')\n", "print(array_1, '\\n')\n", "\n", "array_2 = np.array([[1, 2], [1, 2]])\n", "print('array_2:')\n", "print(array_2, '\\n')\n", "\n", "array_3 = np.array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])\n", "print('array_3:')\n", "print(array_3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sometimes you want an array of all zero values, and that can also be done with `numpy`. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0. 0. 0. 0.]\n", " [0. 0. 0. 0.]\n", " [0. 0. 0. 0.]] \n", "\n", "the dimesions are (3, 4)\n" ] } ], "source": [ "zero_array = np.zeros((3,4))\n", "print(zero_array, '\\n')\n", "print('the dimesions are', zero_array.shape)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's see how we can do operations on some arrays. " ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "a: [1 2 3]\n", "b: [4. 5. 6.]\n", "a + b: [5. 7. 9.]\n", "a * b: [ 4. 10. 18.]\n", "1.0 + a: [2. 3. 4.]\n", "a**2: [1 4 9]\n", "b**a: [ 4. 25. 216.]\n" ] } ], "source": [ "a = np.array([1, 2, 3])\n", "b = np.array([4.0, 5.0, 6.0])\n", "\n", "# Arithmetic operations are done elementwise\n", "print('a: ', a)\n", "print('b: ', b)\n", "print('a + b: ', a + b)\n", "print('a * b: ', a * b)\n", "print('1.0 + a:', 1.0 + a)\n", "print('a**2: ', a**2)\n", "print('b**a: ', b**a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can check the data type of our matrix." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "int64\n", "float64\n" ] } ], "source": [ "print(a.dtype)\n", "print(b.dtype)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also change the type of the entries of our arrays, for example, from integers to floating point. " ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 2. 3.]\n", "[4 5 6]\n" ] } ], "source": [ "array_a = a.astype(float)\n", "print(array_a)\n", "\n", "array_b = b.astype(int)\n", "print(array_b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slicing is also intuitive. " ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 9 10 11 12]\n", " [13 14 15 16]] \n", "\n", "[[ 3]\n", " [ 7]\n", " [11]]\n" ] } ], "source": [ "a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])\n", "\n", "print(a, \"\\n\")\n", "print(a[0:3, 2:3])" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2, 6, 10, 14])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# view second column\n", "a[:,1]" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# see all entries below the value of 10\n", "a[a<10]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using slices, we can reassign values to the entries in a NumPy array. For example, say we wanted the third row to be all zeros." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 0 0 0 0]\n", " [13 14 15 16]]\n" ] } ], "source": [ "a[2, :] = np.zeros(a[2 ,:].shape)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also reshape arrays." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3 4 5 6 7 8]\n", " [ 0 0 0 0 13 14 15 16]] \n", "\n", "[[ 1 2 3 4]\n", " [ 5 6 7 8]\n", " [ 0 0 0 0]\n", " [13 14 15 16]]\n" ] } ], "source": [ "a = a.reshape (2 ,8)\n", "print(a, '\\n') \n", "\n", "a = a.reshape(4,4)\n", "print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### NumPy functions\n", "\n", "Here are some useful NumPy functions we think you might want to use! Go through these one-by-one in separate cells to see what they do." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1, 5, 0, 13],\n", " [ 2, 6, 0, 14],\n", " [ 3, 7, 0, 15],\n", " [ 4, 8, 0, 16]])" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# create evenly spaced points\n", "np.linspace(0, 1, 10)\n", "\n", "# matrix or vector dot products\n", "np.dot(a, a)\n", "\n", "# concatenate in row dimensions\n", "np.concatenate((a, a))\n", "\n", "# concatenate in the column dimension\n", "np.concatenate((a, a), axis=1)\n", "\n", "# transpose (the result of this final operation will be displayed)\n", "np.transpose(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bokeh: our primary plotting tool\n", "\n", "Bokeh is a tool for generating interactive plots that can be views in a browser (and therefore also Jupyter notebooks). Its syntax is intuitive, and best learned by example. First, we need to import packages and set things up so that the graphics can be displayed in the notebook." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " Loading BokehJS ...\n", "
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "\n", "(function(root) {\n", " function now() {\n", " return new Date();\n", " }\n", "\n", " const force = true;\n", "\n", " if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n", " root._bokeh_onload_callbacks = [];\n", " root._bokeh_is_loading = undefined;\n", " }\n", "\n", " const JS_MIME_TYPE = 'application/javascript';\n", " const HTML_MIME_TYPE = 'text/html';\n", " const EXEC_MIME_TYPE = 'application/vnd.bokehjs_exec.v0+json';\n", " const CLASS_NAME = 'output_bokeh rendered_html';\n", "\n", " /**\n", " * Render data to the DOM node\n", " */\n", " function render(props, node) {\n", " const script = document.createElement(\"script\");\n", " node.appendChild(script);\n", " }\n", "\n", " /**\n", " * Handle when an output is cleared or removed\n", " */\n", " function handleClearOutput(event, handle) {\n", " const cell = handle.cell;\n", "\n", " const id = cell.output_area._bokeh_element_id;\n", " const server_id = cell.output_area._bokeh_server_id;\n", " // Clean up Bokeh references\n", " if (id != null && id in Bokeh.index) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", "\n", " if (server_id !== undefined) {\n", " // Clean up Bokeh references\n", " const cmd_clean = \"from bokeh.io.state import curstate; print(curstate().uuid_to_server['\" + server_id + \"'].get_sessions()[0].document.roots[0]._id)\";\n", " cell.notebook.kernel.execute(cmd_clean, {\n", " iopub: {\n", " output: function(msg) {\n", " const id = msg.content.text.trim();\n", " if (id in Bokeh.index) {\n", " Bokeh.index[id].model.document.clear();\n", " delete Bokeh.index[id];\n", " }\n", " }\n", " }\n", " });\n", " // Destroy server and session\n", " const cmd_destroy = \"import bokeh.io.notebook as ion; ion.destroy_server('\" + server_id + \"')\";\n", " cell.notebook.kernel.execute(cmd_destroy);\n", " }\n", " }\n", "\n", " /**\n", " * Handle when a new output is added\n", " */\n", " function handleAddOutput(event, handle) {\n", " const output_area = handle.output_area;\n", " const output = handle.output;\n", "\n", " // limit handleAddOutput to display_data with EXEC_MIME_TYPE content only\n", " if ((output.output_type != \"display_data\") || (!Object.prototype.hasOwnProperty.call(output.data, EXEC_MIME_TYPE))) {\n", " return\n", " }\n", "\n", " const toinsert = output_area.element.find(\".\" + CLASS_NAME.split(' ')[0]);\n", "\n", " if (output.metadata[EXEC_MIME_TYPE][\"id\"] !== undefined) {\n", " toinsert[toinsert.length - 1].firstChild.textContent = output.data[JS_MIME_TYPE];\n", " // store reference to embed id on output_area\n", " output_area._bokeh_element_id = output.metadata[EXEC_MIME_TYPE][\"id\"];\n", " }\n", " if (output.metadata[EXEC_MIME_TYPE][\"server_id\"] !== undefined) {\n", " const bk_div = document.createElement(\"div\");\n", " bk_div.innerHTML = output.data[HTML_MIME_TYPE];\n", " const script_attrs = bk_div.children[0].attributes;\n", " for (let i = 0; i < script_attrs.length; i++) {\n", " toinsert[toinsert.length - 1].firstChild.setAttribute(script_attrs[i].name, script_attrs[i].value);\n", " toinsert[toinsert.length - 1].firstChild.textContent = bk_div.children[0].textContent\n", " }\n", " // store reference to server id on output_area\n", " output_area._bokeh_server_id = output.metadata[EXEC_MIME_TYPE][\"server_id\"];\n", " }\n", " }\n", "\n", " function register_renderer(events, OutputArea) {\n", "\n", " function append_mime(data, metadata, element) {\n", " // create a DOM node to render to\n", " const toinsert = this.create_output_subarea(\n", " metadata,\n", " CLASS_NAME,\n", " EXEC_MIME_TYPE\n", " );\n", " this.keyboard_manager.register_events(toinsert);\n", " // Render to node\n", " const props = {data: data, metadata: metadata[EXEC_MIME_TYPE]};\n", " render(props, toinsert[toinsert.length - 1]);\n", " element.append(toinsert);\n", " return toinsert\n", " }\n", "\n", " /* Handle when an output is cleared or removed */\n", " events.on('clear_output.CodeCell', handleClearOutput);\n", " events.on('delete.Cell', handleClearOutput);\n", "\n", " /* Handle when a new output is added */\n", " events.on('output_added.OutputArea', handleAddOutput);\n", "\n", " /**\n", " * Register the mime type and append_mime function with output_area\n", " */\n", " OutputArea.prototype.register_mime_type(EXEC_MIME_TYPE, append_mime, {\n", " /* Is output safe? */\n", " safe: true,\n", " /* Index of renderer in `output_area.display_order` */\n", " index: 0\n", " });\n", " }\n", "\n", " // register the mime type if in Jupyter Notebook environment and previously unregistered\n", " if (root.Jupyter !== undefined) {\n", " const events = require('base/js/events');\n", " const OutputArea = require('notebook/js/outputarea').OutputArea;\n", "\n", " if (OutputArea.prototype.mime_types().indexOf(EXEC_MIME_TYPE) == -1) {\n", " register_renderer(events, OutputArea);\n", " }\n", " }\n", "\n", " \n", " if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n", " root._bokeh_timeout = Date.now() + 5000;\n", " root._bokeh_failed_load = false;\n", " }\n", "\n", " const NB_LOAD_WARNING = {'data': {'text/html':\n", " \"
\\n\"+\n", " \"

\\n\"+\n", " \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n", " \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n", " \"

\\n\"+\n", " \"
    \\n\"+\n", " \"
  • re-rerun `output_notebook()` to attempt to load from CDN again, or
  • \\n\"+\n", " \"
  • use INLINE resources instead, as so:
  • \\n\"+\n", " \"
\\n\"+\n", " \"\\n\"+\n", " \"from bokeh.resources import INLINE\\n\"+\n", " \"output_notebook(resources=INLINE)\\n\"+\n", " \"\\n\"+\n", " \"
\"}};\n", "\n", " function display_loaded() {\n", " const el = document.getElementById(\"1002\");\n", " if (el != null) {\n", " el.textContent = \"BokehJS is loading...\";\n", " }\n", " if (root.Bokeh !== undefined) {\n", " if (el != null) {\n", " el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n", " }\n", " } else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(display_loaded, 100)\n", " }\n", " }\n", "\n", "\n", " function run_callbacks() {\n", " try {\n", " root._bokeh_onload_callbacks.forEach(function(callback) {\n", " if (callback != null)\n", " callback();\n", " });\n", " } finally {\n", " delete root._bokeh_onload_callbacks\n", " }\n", " console.debug(\"Bokeh: all callbacks have finished\");\n", " }\n", "\n", " function load_libs(css_urls, js_urls, callback) {\n", " if (css_urls == null) css_urls = [];\n", " if (js_urls == null) js_urls = [];\n", "\n", " root._bokeh_onload_callbacks.push(callback);\n", " if (root._bokeh_is_loading > 0) {\n", " console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n", " return null;\n", " }\n", " if (js_urls == null || js_urls.length === 0) {\n", " run_callbacks();\n", " return null;\n", " }\n", " console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n", " root._bokeh_is_loading = css_urls.length + js_urls.length;\n", "\n", " function on_load() {\n", " root._bokeh_is_loading--;\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n", " run_callbacks()\n", " }\n", " }\n", "\n", " function on_error(url) {\n", " console.error(\"failed to load \" + url);\n", " }\n", "\n", " for (let i = 0; i < css_urls.length; i++) {\n", " const url = css_urls[i];\n", " const element = document.createElement(\"link\");\n", " element.onload = on_load;\n", " element.onerror = on_error.bind(null, url);\n", " element.rel = \"stylesheet\";\n", " element.type = \"text/css\";\n", " element.href = url;\n", " console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n", " document.body.appendChild(element);\n", " }\n", "\n", " for (let i = 0; i < js_urls.length; i++) {\n", " const url = js_urls[i];\n", " const element = document.createElement('script');\n", " element.onload = on_load;\n", " element.onerror = on_error.bind(null, url);\n", " element.async = false;\n", " element.src = url;\n", " console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n", " document.head.appendChild(element);\n", " }\n", " };\n", "\n", " function inject_raw_css(css) {\n", " const element = document.createElement(\"style\");\n", " element.appendChild(document.createTextNode(css));\n", " document.body.appendChild(element);\n", " }\n", "\n", " \n", " const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n", " const css_urls = [];\n", " \n", "\n", " const inline_js = [\n", " function(Bokeh) {\n", " Bokeh.set_log_level(\"info\");\n", " },\n", " function(Bokeh) {\n", " \n", " \n", " }\n", " ];\n", "\n", " function run_inline_js() {\n", " \n", " if (root.Bokeh !== undefined || force === true) {\n", " \n", " for (let i = 0; i < inline_js.length; i++) {\n", " inline_js[i].call(root, root.Bokeh);\n", " }\n", " if (force === true) {\n", " display_loaded();\n", " }} else if (Date.now() < root._bokeh_timeout) {\n", " setTimeout(run_inline_js, 100);\n", " } else if (!root._bokeh_failed_load) {\n", " console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n", " root._bokeh_failed_load = true;\n", " } else if (force !== true) {\n", " const cell = $(document.getElementById(\"1002\")).parents('.cell').data().cell;\n", " cell.output_area.append_execute_result(NB_LOAD_WARNING)\n", " }\n", "\n", " }\n", "\n", " if (root._bokeh_is_loading === 0) {\n", " console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n", " run_inline_js();\n", " } else {\n", " load_libs(css_urls, js_urls, function() {\n", " console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n", " run_inline_js();\n", " });\n", " }\n", "}(window));" ], "application/vnd.bokehjs_load.v0+json": "\n(function(root) {\n function now() {\n return new Date();\n }\n\n const force = true;\n\n if (typeof root._bokeh_onload_callbacks === \"undefined\" || force === true) {\n root._bokeh_onload_callbacks = [];\n root._bokeh_is_loading = undefined;\n }\n\n \n\n \n if (typeof (root._bokeh_timeout) === \"undefined\" || force === true) {\n root._bokeh_timeout = Date.now() + 5000;\n root._bokeh_failed_load = false;\n }\n\n const NB_LOAD_WARNING = {'data': {'text/html':\n \"
\\n\"+\n \"

\\n\"+\n \"BokehJS does not appear to have successfully loaded. If loading BokehJS from CDN, this \\n\"+\n \"may be due to a slow or bad network connection. Possible fixes:\\n\"+\n \"

\\n\"+\n \"
    \\n\"+\n \"
  • re-rerun `output_notebook()` to attempt to load from CDN again, or
  • \\n\"+\n \"
  • use INLINE resources instead, as so:
  • \\n\"+\n \"
\\n\"+\n \"\\n\"+\n \"from bokeh.resources import INLINE\\n\"+\n \"output_notebook(resources=INLINE)\\n\"+\n \"\\n\"+\n \"
\"}};\n\n function display_loaded() {\n const el = document.getElementById(\"1002\");\n if (el != null) {\n el.textContent = \"BokehJS is loading...\";\n }\n if (root.Bokeh !== undefined) {\n if (el != null) {\n el.textContent = \"BokehJS \" + root.Bokeh.version + \" successfully loaded.\";\n }\n } else if (Date.now() < root._bokeh_timeout) {\n setTimeout(display_loaded, 100)\n }\n }\n\n\n function run_callbacks() {\n try {\n root._bokeh_onload_callbacks.forEach(function(callback) {\n if (callback != null)\n callback();\n });\n } finally {\n delete root._bokeh_onload_callbacks\n }\n console.debug(\"Bokeh: all callbacks have finished\");\n }\n\n function load_libs(css_urls, js_urls, callback) {\n if (css_urls == null) css_urls = [];\n if (js_urls == null) js_urls = [];\n\n root._bokeh_onload_callbacks.push(callback);\n if (root._bokeh_is_loading > 0) {\n console.debug(\"Bokeh: BokehJS is being loaded, scheduling callback at\", now());\n return null;\n }\n if (js_urls == null || js_urls.length === 0) {\n run_callbacks();\n return null;\n }\n console.debug(\"Bokeh: BokehJS not loaded, scheduling load and callback at\", now());\n root._bokeh_is_loading = css_urls.length + js_urls.length;\n\n function on_load() {\n root._bokeh_is_loading--;\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: all BokehJS libraries/stylesheets loaded\");\n run_callbacks()\n }\n }\n\n function on_error(url) {\n console.error(\"failed to load \" + url);\n }\n\n for (let i = 0; i < css_urls.length; i++) {\n const url = css_urls[i];\n const element = document.createElement(\"link\");\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\n element.rel = \"stylesheet\";\n element.type = \"text/css\";\n element.href = url;\n console.debug(\"Bokeh: injecting link tag for BokehJS stylesheet: \", url);\n document.body.appendChild(element);\n }\n\n for (let i = 0; i < js_urls.length; i++) {\n const url = js_urls[i];\n const element = document.createElement('script');\n element.onload = on_load;\n element.onerror = on_error.bind(null, url);\n element.async = false;\n element.src = url;\n console.debug(\"Bokeh: injecting script tag for BokehJS library: \", url);\n document.head.appendChild(element);\n }\n };\n\n function inject_raw_css(css) {\n const element = document.createElement(\"style\");\n element.appendChild(document.createTextNode(css));\n document.body.appendChild(element);\n }\n\n \n const js_urls = [\"https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-gl-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js\", \"https://cdn.bokeh.org/bokeh/release/bokeh-mathjax-2.4.2.min.js\"];\n const css_urls = [];\n \n\n const inline_js = [\n function(Bokeh) {\n Bokeh.set_log_level(\"info\");\n },\n function(Bokeh) {\n \n \n }\n ];\n\n function run_inline_js() {\n \n if (root.Bokeh !== undefined || force === true) {\n \n for (let i = 0; i < inline_js.length; i++) {\n inline_js[i].call(root, root.Bokeh);\n }\n if (force === true) {\n display_loaded();\n }} else if (Date.now() < root._bokeh_timeout) {\n setTimeout(run_inline_js, 100);\n } else if (!root._bokeh_failed_load) {\n console.log(\"Bokeh: BokehJS failed to load within specified timeout.\");\n root._bokeh_failed_load = true;\n } else if (force !== true) {\n const cell = $(document.getElementById(\"1002\")).parents('.cell').data().cell;\n cell.output_area.append_execute_result(NB_LOAD_WARNING)\n }\n\n }\n\n if (root._bokeh_is_loading === 0) {\n console.debug(\"Bokeh: BokehJS loaded, going straight to plotting\");\n run_inline_js();\n } else {\n load_libs(css_urls, js_urls, function() {\n console.debug(\"Bokeh: BokehJS plotting callback run at\", now());\n run_inline_js();\n });\n }\n}(window));" }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# These are the necessary submodules\n", "import bokeh.io\n", "import bokeh.plotting\n", "\n", "# This is necesssary to display in the notebook\n", "bokeh.io.output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now that we have Bokeh ready, we can make some plots." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " const docs_json = {\"6c19cdbc-1ea9-4d67-b590-beb4b5f0f74a\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1012\"}],\"center\":[{\"id\":\"1015\"},{\"id\":\"1019\"}],\"frame_height\":300,\"frame_width\":500,\"left\":[{\"id\":\"1016\"}],\"renderers\":[{\"id\":\"1038\"},{\"id\":\"1044\"},{\"id\":\"1050\"}],\"title\":{\"id\":\"1052\"},\"toolbar\":{\"id\":\"1027\"},\"x_range\":{\"id\":\"1004\"},\"x_scale\":{\"id\":\"1008\"},\"y_range\":{\"id\":\"1006\"},\"y_scale\":{\"id\":\"1010\"}},\"id\":\"1003\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"1052\",\"type\":\"Title\"},{\"attributes\":{},\"id\":\"1064\",\"type\":\"UnionRenderers\"},{\"attributes\":{},\"id\":\"1065\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1060\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1046\"},\"glyph\":{\"id\":\"1047\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1049\"},\"nonselection_glyph\":{\"id\":\"1048\"},\"view\":{\"id\":\"1051\"}},\"id\":\"1050\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"source\":{\"id\":\"1040\"}},\"id\":\"1045\",\"type\":\"CDSView\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1040\"},\"glyph\":{\"id\":\"1041\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1043\"},\"nonselection_glyph\":{\"id\":\"1042\"},\"view\":{\"id\":\"1045\"}},\"id\":\"1044\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1061\",\"type\":\"Selection\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"black\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1048\",\"type\":\"Circle\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"orange\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1042\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1056\",\"type\":\"AllLabels\"},{\"attributes\":{\"overlay\":{\"id\":\"1026\"}},\"id\":\"1022\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1023\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1024\",\"type\":\"ResetTool\"},{\"attributes\":{\"line_color\":\"orange\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1041\",\"type\":\"Line\"},{\"attributes\":{\"data\":{\"x\":{\"__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eTr89egVAUvDFt+CSBUBLxng9JKYFQO6VctD0swVAZIxcZ0S8BUBpVxSLCr8FQA==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[200]}},\"selected\":{\"id\":\"1063\"},\"selection_policy\":{\"id\":\"1062\"}},\"id\":\"1040\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"data\":{\"x\":{\"__ndarray__\":\"TItWFJoKFUCbodONuBfvP4onwYu00/c/yeR4PqBb+D8WOcodiV36P5hYIeQEwxdAGvIGoE8JE0APDVpmrDQHQOh/hXRswgtAAYUf4SGmAkBfDAT6YD/1P9CibXhyWhhAPhU7e4E+EkBTiORnkfUXQAgnuPWRnQdAQoO/+KWc6z/Lj3ntHN0CQHHsnjlc/xhAg3Wbfeo7EEC+nDYoIIwTQA==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[20]},\"y\":{\"__ndarray__\":\"j4+NE6kjB0Cfz8TctIj1P7Rc45cKEvc/Glwg8G5x9z8bVFslajHqP8jdoOWMr9g/YXl9T6lY8T+77+3wq8P4P2h7lr/UZQVAaW66/Pf35D+RH2MqbXQAQP92/qRezeE/1tAMTe8q/z/yvuJTGV0GQGGVgiU+Uv8/GBsK5VGl1j8MORygmiP+P8jwG0rJSrI/To3cyPiJ/T/+j6zzmenXPw==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[20]}},\"selected\":{\"id\":\"1065\"},\"selection_policy\":{\"id\":\"1064\"}},\"id\":\"1046\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1025\",\"type\":\"HelpTool\"},{\"attributes\":{\"data\":{\"x\":{\"__ndarray__\":\"AAAAAAAAAABMJ0jGcCqgP0wnSMZwKrA/8jpsKak/uD9MJ0jGcCrAPx8x2vcMNcQ/8jpsKak/yD/FRP5aRUrMP0wnSMZwKtA/NiwR374v0j8fMdr3DDXUPwg2oxBbOtY/8jpsKak/2D/cPzVC90TaP8VE/lpFStw/rknHc5NP3j9MJ0jGcCrgP8GprNIX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/327hkwVv5j+r/rLDAcnlP84nlwHeKeU/Ra04MHCR5D+1N3eKjf/jP2gPv+UKdOM/2Krl7rzu4j+6hbxheG/iP1L1kTsS9uE/HZ3c6F+C4T8q3lFuNxThP3k6qo1vq+A/HEhY5t9H4D9pJ+8kwtLfP1qicYCZH98/hh8gkPt13j9g1tB/oNXdP5n9PRdDPt0/dIsJ1qCv3D8bT0kLeincP3SLEOmRq9s/sYRmlK412z8vdBMymcfaP3Itp/AdYdo/fnsWEAwC2j8a9UXnNarZP7HE0udwWdk/xrFjoJUP2T8Rqca9f8zYP+sRGQsOkNg/F3szcSJa2D9sj4v1oSrYP4rWubh0Adg/CYfL9IXe1z+0m4P7w8HXP2pzqTQgq9c/vn5+HI+a1z92229CCJDXP+o3FEiGi9c/8uiB4AaN1z+GvwTQipTXP33YNuwVotc/B1N7HK+11z8vi9VaYM/XP0EeE7U279c/Ppk6TkIV2D+TNyxglkHYP2CEXT1JdNg/Z/2TUnSt2D8RAH4oNO3YPztKBGWoM9k/yy8pzPOA2T8pUEVAPNXZPwEUbMGqMNo/XIy8a2uT2j/PcV10rf3aP5MC3SSjb9s/D12n1IHp2z/5uj/ggWvcP4Kq4p3e9dw/KBoxT9aI3T9G1H0PqiTeP4HwUL6dyd4/8OOu5fd33z9ap9XNABjgP9dEZS8DeeA/SDdY9inf4D+Ef0sJnUrhP7lNgjuFu+E/2MYbLgwy4j/W/V0tXK7iP1QJ0QmgMOM/9Uzm7AK54z+1K+oosEfkP2J/AQTT3OQ/hYD4fZZ45T+aXq4QJRvmPwy972qoxOY/xryaJUl15z88QvFyLi3oPxXdCch97Og/VCxdgFqz6T8a23x75YHqP6RrErU8WOs/H/VW13o27D93uUnIthztP0r7ATIDC+4/5qSQBm4B7z/+///////vPw==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[200]}},\"selected\":{\"id\":\"1061\"},\"selection_policy\":{\"id\":\"1060\"}},\"id\":\"1034\",\"type\":\"ColumnDataSource\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"black\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1049\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1062\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"tools\":[{\"id\":\"1020\"},{\"id\":\"1021\"},{\"id\":\"1022\"},{\"id\":\"1023\"},{\"id\":\"1024\"},{\"id\":\"1025\"}]},\"id\":\"1027\",\"type\":\"Toolbar\"},{\"attributes\":{\"line_color\":\"#1f77b4\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1035\",\"type\":\"Line\"},{\"attributes\":{\"axis_label\":\"x\",\"coordinates\":null,\"formatter\":{\"id\":\"1058\"},\"group\":null,\"major_label_policy\":{\"id\":\"1059\"},\"ticker\":{\"id\":\"1013\"}},\"id\":\"1012\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1004\",\"type\":\"DataRange1d\"},{\"attributes\":{\"source\":{\"id\":\"1034\"}},\"id\":\"1039\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1063\",\"type\":\"Selection\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"#1f77b4\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1036\",\"type\":\"Line\"},{\"attributes\":{\"line_alpha\":0.2,\"line_color\":\"#1f77b4\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1037\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1008\",\"type\":\"LinearScale\"},{\"attributes\":{\"fill_color\":{\"value\":\"black\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1047\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1006\",\"type\":\"DataRange1d\"},{\"attributes\":{\"source\":{\"id\":\"1046\"}},\"id\":\"1051\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1055\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1010\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1021\",\"type\":\"WheelZoomTool\"},{\"attributes\":{},\"id\":\"1013\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1012\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1015\",\"type\":\"Grid\"},{\"attributes\":{},\"id\":\"1059\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1058\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1034\"},\"glyph\":{\"id\":\"1035\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1037\"},\"nonselection_glyph\":{\"id\":\"1036\"},\"view\":{\"id\":\"1039\"}},\"id\":\"1038\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"axis_label\":\"y\",\"coordinates\":null,\"formatter\":{\"id\":\"1055\"},\"group\":null,\"major_label_policy\":{\"id\":\"1056\"},\"ticker\":{\"id\":\"1017\"}},\"id\":\"1016\",\"type\":\"LinearAxis\"},{\"attributes\":{\"axis\":{\"id\":\"1016\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1019\",\"type\":\"Grid\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1026\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"line_alpha\":0.2,\"line_color\":\"orange\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1043\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1017\",\"type\":\"BasicTicker\"},{\"attributes\":{},\"id\":\"1020\",\"type\":\"PanTool\"}],\"root_ids\":[\"1003\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n", " const render_items = [{\"docid\":\"6c19cdbc-1ea9-4d67-b590-beb4b5f0f74a\",\"root_ids\":[\"1003\"],\"roots\":{\"1003\":\"4e43a684-5e68-42a9-b850-8d38e3e48fc7\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " let attempts = 0;\n", " const timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " clearInterval(timer);\n", " embed_document(root);\n", " } else {\n", " attempts++;\n", " if (attempts > 100) {\n", " clearInterval(timer);\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " }\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1003" } }, "output_type": "display_data" } ], "source": [ "# Make an x-variable for plotting\n", "x = np.linspace(0, 2 * np.pi, 200)\n", "\n", "# This is a nice function\n", "y_1 = np.exp(np.sin(x))\n", "\n", "# We can make another one\n", "y_2 = np.exp(np.cos(x))\n", "\n", "# We can make some random data to plot as well\n", "x_rand = np.random.rand(20) * 2 * np.pi\n", "y_rand = np.random.rand(20) * 3.0\n", "\n", "# Set up plot\n", "p = bokeh.plotting.figure(\n", " frame_width=500, frame_height=300, x_axis_label=\"x\", y_axis_label=\"y\"\n", ")\n", "\n", "# Populate the plot with glyphs\n", "p.line(x, y_1, line_width=2)\n", "p.line(x, y_2, line_width=2, color=\"orange\")\n", "p.circle(x_rand, y_rand, color=\"black\")\n", "\n", "# Show the plot\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To export the figure for insertion into a document, you can click on the disk image in the Bokeh tools. This will give you a PNG, but for publications, you should use vector graphics. There are ways to save Bokeh plots as vector graphics, but we will not use them here." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Programming style\n", "\n", "[PEP](http://legacy.python.org/dev/peps/pep-0001/)s (Python Enhancement Proposals) document suggestions and guidelines for the Python language, its development, etc. My personal favorite is [PEP8, the Style Guide for Python Code](http://legacy.python.org/dev/peps/pep-0008/). This was largely written by Guido von Rossum, the inventor of Python and its benevolent dictator for life. It details how you should style your code. As Guido says, code is much more often read than written. I strongly urge you to follow PEP8 the best you can. It's a lot to read, so I will highlight important points here. If you follow these guidelines, your code will be much more readable than otherwise. This is particularly useful because you are working in groups and the TAs need to grade your work.\n", "\n", "- Limit line widths to 79 characters (the line break character for Python is `\\`).\n", "- Use whitespace around low-priority operators. E.g., `x = a**2 + b**2` has whitespace around the `+` operator. Another good example is `y = m*x + b`.\n", "- In function calls, use a space after each comma. Use no spaces before and after the equals sign when using keyword arguments. E.g., `my_fun(a, b, c=True)`.\n", "- Do not use excess space when indexing. E.g., `a[i]`, not `a [ i ]`.\n", "- Function names should be lowercase, with words separated by underscores as necessary to improve readability.\n", "- Comment lines should appear immediately before the code they describe. Use in-line comments sparingly.\n", "\n", "For me, an easy way to keep my code looking pretty is to use [Black](https://black.readthedocs.io/en/stable/). If you have installed black and black cell magic, you can use Black in the Jupyter notebook. In a cell at the beginning of your notebook, execute the following code." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "%load_ext blackcellmagic" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, in any code cell where you want Black to adjust the formatting, put `%%black` at the top of the cell and execute it. The code in the cell will not be executed; rather, it will be reformatted according to Black's style." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using NumPy/SciPy to perform a linear regression\n", "\n", "As a case study of how we can load in data and do analysis with it, we will consider a data set from one of the classic organisms in biology: Darwin's finches. Peter and Rosemary Grant have been working on the Galápagos island of Daphne for over forty years. During this time, they have collected lots and lots of data about physiological features of finches. A couple years ago, they published a book with a summary of some of their major results (Grant P. R., Grant B. R., Data from: 40 years of evolution. Darwin's finches on Daphne Major Island, Princeton University Press, 2014). They made their data from the book publicly available via the [Dryad Digital Repository](http://dx.doi.org/10.5061/dryad.g6g3h).\n", "\n", "We will investigate their data on the heritability of beak depth (the distance, top to bottom, of a closed beak) in the ground finch *Geospiza fortis*. The data set consists of the maternal beak depth, the paternal beak depth, and the mean beak depth of their offspring. You can download the data set, which I got from Dryad and tidied up into a CSV file, [here](grant_and_grant_2014.csv). Our goal it so do a linear regression to see how parental beak depth is related to that of the offspring.\n", "\n", "First, we will load in the data set. Before attempting to load the data set, it is important to know where it is on your machine. It should be in the same directory from which you launched Jupyter lab. Otherwise, you can enter the full path below. It is in a comma delimited text file. For this particular file, the data are preceded by comments about their content, with each comment line starting with a `#`. We will use the wonderful package [pandas](http://pandas.pydata.org) to read in the data." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Average offspring beak depth (mm)Paternal beak depth (mm)Maternal beak depth (mm)
010.7010.909.3
19.7810.708.4
29.4810.708.1
39.6010.709.8
410.279.8510.4
\n", "
" ], "text/plain": [ " Average offspring beak depth (mm) Paternal beak depth (mm) \\\n", "0 10.70 10.90 \n", "1 9.78 10.70 \n", "2 9.48 10.70 \n", "3 9.60 10.70 \n", "4 10.27 9.85 \n", "\n", " Maternal beak depth (mm) \n", "0 9.3 \n", "1 8.4 \n", "2 8.1 \n", "3 9.8 \n", "4 10.4 " ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "\n", "# Load the data\n", "df = pd.read_csv('grant_and_grant_2014.csv', comment='#')\n", "\n", "# Take a look\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This pandas `DataFrame` has 413 measurements with three columns. We can index the columns to get Numpy arrays containing the data we want." ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "offspring_bd = df['Average offspring beak depth (mm)'].values\n", "paternal_bd = df['Paternal beak depth (mm)'].values\n", "maternal_bd = df['Maternal beak depth (mm)'].values" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to plot mean offspring beak depth vs. mean parental beak depth, so we need to compute the mean parental beak depth. Conveniently, we do not have to write a loop to compute this for each parent pair. NumPy arrays allow for convenient elementwise calculation in a single command." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "parental_bd = (maternal_bd + paternal_bd) / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have the arrays we need to make our plot." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " const docs_json = {\"c1af5463-42de-4ff8-8db2-bc53f077b717\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1151\"}],\"center\":[{\"id\":\"1154\"},{\"id\":\"1158\"}],\"frame_height\":300,\"frame_width\":300,\"left\":[{\"id\":\"1155\"}],\"renderers\":[{\"id\":\"1177\"}],\"title\":{\"id\":\"1194\"},\"toolbar\":{\"id\":\"1166\"},\"x_range\":{\"id\":\"1143\"},\"x_scale\":{\"id\":\"1147\"},\"y_range\":{\"id\":\"1145\"},\"y_scale\":{\"id\":\"1149\"}},\"id\":\"1142\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"axis_label\":\"offpsring beak depth (mm)\",\"coordinates\":null,\"formatter\":{\"id\":\"1197\"},\"group\":null,\"major_label_policy\":{\"id\":\"1198\"},\"ticker\":{\"id\":\"1156\"}},\"id\":\"1155\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1147\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1203\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1164\",\"type\":\"HelpTool\"},{\"attributes\":{\"axis_label\":\"parental beak depth (mm)\",\"coordinates\":null,\"formatter\":{\"id\":\"1200\"},\"group\":null,\"major_label_policy\":{\"id\":\"1201\"},\"ticker\":{\"id\":\"1152\"}},\"id\":\"1151\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1145\",\"type\":\"DataRange1d\"},{\"attributes\":{\"data\":{\"x\":{\"__ndarray__\":\"NDMzMzMzJECamZmZmRkjQMzMzMzMzCJAAAAAAACAJEAAAAAAAEAkQGZmZmZmZiNAmpmZmZkZIkCamZmZmZkdQAAAAAAAACJAzczMzMxMIUAAAAAAAEAjQMzMzMzMzCNAmpmZmZkZI0CamZmZmRkiQDMzMzMzsyBAMzMzMzMzJEAzMzMzMzMkQMzMzMzMzCNAMzMzMzNzJEAAAAAAAAAkQJqZmZmZGSVAZmZmZmbmJEBmZmZmZmYiQGZmZmZmZiRAZmZmZmbmIUCamZmZmRkkQGZmZmZmZiRAAAAAAAAAI0CamZmZmdkjQGZmZmZm5iNAmpmZmZkZJEAAAAAAAIAhQGZmZmZmZiJAzczMzMxMJECamZmZmZkjQGZmZmZmZiVAAAAAAAAAJUCamZmZmRkjQJqZmZmZGSVAMzMzMzPzJEDMzMzMzEwhQGZmZmZmZiVAMzMzMzMzIkDNzMzMzMwiQJqZmZmZmSRAzczMzMxMI0AAAAAAAAAjQGZmZmZmZiNAzczMzMwMJUBmZmZmZuYjQDMzMzMzMyRAAAAAAACAI0CamZmZmRkkQM3MzMzMzCNAAAAAAAAAJ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PwvUoXCJAmpmZmZmZIUDNzMzMzMwjQOxRuB6F6x5AAAAAAACAIkBmZmZmZmYhQAAAAAAAACNAmpmZmZmZIkCamZmZmRkiQFyPwvUoXCRAMzMzMzOzIUB7FK5H4XoiQM3MzMzMTCJA9ihcj8L1I0AK16NwPYohQJqZmZmZmSNAzczMzMxMIUAAAAAAAAAkQB+F61G4niFAhetRuB4FIECamZmZmZkfQFK4HoXr0SJAXI/C9ShcJECamZmZmRkjQClcj8L1KCJAzczMzMzMIEAAAAAAAIAjQM3MzMzMzCFApHA9CtcjIkAzMzMzM7MiQM3MzMzMzCFA4XoUrkdhIEDNzMzMzEwhQOF6FK5HYSJAzczMzMzMIUCPwvUoXI8iQClcj8L1KCVAAAAAAAAAIkDNzMzMzMwiQFK4HoXr0SFAXI/C9SjcI0AAAAAAAAAkQD0K16NwvSJAmpmZmZmZHUAAAAAAAAAiQJqZmZmZmSFAXI/C9ShcIkCamZmZmZkgQClcj8L1KCRAmpmZmZmZH0DsUbgehesjQM3MzMzMzCRAzczMzMxMI0CamZmZmZkhQM3MzMzMTCFAZmZmZmZmI0D2KFyPwnUiQFyPwvUo3CJAXI/C9SjcI0BxPQrXo/AgQJqZmZmZGSNAj8L1KFyPIkAzMzMzM7MhQM3MzMzMzCFAAAAAAACAIUDD9Shcj0IhQAAAAAAAACJAXI/C9SjcIkCPwvUoXI8gQPYoXI/CdSJAzczMzMzMJEAAAAAAAAAiQJqZmZmZmSNACtejcD2KI0BxPQrXo/AhQD0K16NwvSBAzczMzMzMHkCamZmZmZkfQAAAAAAAACNAZmZmZmZmIECamZmZmZkhQA==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[413]}},\"selected\":{\"id\":\"1203\"},\"selection_policy\":{\"id\":\"1202\"}},\"id\":\"1173\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1163\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1202\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"1194\",\"type\":\"Title\"},{\"attributes\":{\"tools\":[{\"id\":\"1159\"},{\"id\":\"1160\"},{\"id\":\"1161\"},{\"id\":\"1162\"},{\"id\":\"1163\"},{\"id\":\"1164\"}]},\"id\":\"1166\",\"type\":\"Toolbar\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1173\"},\"glyph\":{\"id\":\"1174\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1176\"},\"nonselection_glyph\":{\"id\":\"1175\"},\"view\":{\"id\":\"1178\"}},\"id\":\"1177\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.3},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.3},\"line_alpha\":{\"value\":0.3},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1174\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1143\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1149\",\"type\":\"LinearScale\"},{\"attributes\":{\"axis\":{\"id\":\"1151\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1154\",\"type\":\"Grid\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1165\",\"type\":\"BoxAnnotation\"},{\"attributes\":{},\"id\":\"1200\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"source\":{\"id\":\"1173\"}},\"id\":\"1178\",\"type\":\"CDSView\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1175\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1156\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1155\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1158\",\"type\":\"Grid\"},{\"attributes\":{\"overlay\":{\"id\":\"1165\"}},\"id\":\"1161\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1162\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1197\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1160\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1176\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1198\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1201\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1159\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1152\",\"type\":\"BasicTicker\"}],\"root_ids\":[\"1142\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n", " const render_items = [{\"docid\":\"c1af5463-42de-4ff8-8db2-bc53f077b717\",\"root_ids\":[\"1142\"],\"roots\":{\"1142\":\"63ffbf09-7584-4399-b7d7-f360b98b2be6\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " let attempts = 0;\n", " const timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " clearInterval(timer);\n", " embed_document(root);\n", " } else {\n", " attempts++;\n", " if (attempts > 100) {\n", " clearInterval(timer);\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " }\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1142" } }, "output_type": "display_data" } ], "source": [ "p = bokeh.plotting.figure(\n", " frame_width=300,\n", " frame_height=300,\n", " x_axis_label=\"parental beak depth (mm)\",\n", " y_axis_label=\"offpsring beak depth (mm)\",\n", ")\n", "p.circle(parental_bd, offspring_bd, alpha=0.3)\n", "\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I chose to use the `alpha` keyword argument in the `p.circle()` function because it helps visualize overlapping data points. We see that parental and offspring beak depth are correlated, as we might expect. \n", "\n", "Let's perform a linear regression to get the slope and intercept. To perform the linear regression, we will use `scipy.optimize.curve_fit()`. This function takes a set of data and returns the parameters of the model function that best describe the data. Its first input is the model function (which we have defined as linear); next is the $x$-data (in our case parental beak depth, `parental_bd`); the third input is the $y$-data (offspring beak depth, `offspring_bd`). The last piece that we will need to address is a best guess for our parameters. In this case, we have to provide two values, the slope and the intercept. `curve_fit()` works by finding the curve that minimizes the distance of every point from the curve (in our case, this will be minimizing the vertical distance between measured data points and the line describing the data).\n", "\n", "More explicitly, `scipy.optimize.curve_fit()` will take the sum of the squares of the vertical differences of each data point to the model function and try to minimize this value by adjusting the model parameters. For nonlinear fits, you will need to provide reasonable guesses for the best-fit parameters, as there may be many local minima for minimizing the error. In the case of linear regression, we do not need to be too careful, as there will actually be one minimum that best fits the data. For more on the `scipy.optimize.curve_fit()` function, please read the [documentation](http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html).\n", "\n", "When we run `scipy.optimize.curve_fit()`, we will get two arrays out. The first array contains the optimal set of parameters for the curve fit. The second array is a 2d array with the covariance of the optimal parameters. For this course, we will ignore the second array and will only use the first array, which, for linear regressions, will give us the slope and intercept." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "slope = 0.7229051885446406\n", "intercept = 2.448418309793462 mm\n" ] } ], "source": [ "import scipy.optimize\n", "\n", "# Define linear function\n", "def linear_fun(x, slope, intercept):\n", " return slope * x + intercept\n", "\n", "\n", "# Compute the curve fit (Guess is unit slope and zero intercept)\n", "popt, _ = scipy.optimize.curve_fit(linear_fun, parental_bd, offspring_bd, p0=[1, 0])\n", "\n", "# Parse the results\n", "slope, intercept = popt\n", "\n", "# Print the results\n", "print(\"slope =\", slope)\n", "print(\"intercept =\", intercept, \"mm\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, we got our values for the slope and intercept. We can plot the best fit line along with the data." ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/javascript": [ "(function(root) {\n", " function embed_document(root) {\n", " \n", " const docs_json = {\"cfb60e88-7b10-4203-852a-99f0ebc7ff7a\":{\"defs\":[],\"roots\":{\"references\":[{\"attributes\":{\"below\":[{\"id\":\"1151\"}],\"center\":[{\"id\":\"1154\"},{\"id\":\"1158\"}],\"frame_height\":300,\"frame_width\":300,\"left\":[{\"id\":\"1155\"}],\"renderers\":[{\"id\":\"1177\"},{\"id\":\"1264\"}],\"title\":{\"id\":\"1194\"},\"toolbar\":{\"id\":\"1166\"},\"x_range\":{\"id\":\"1143\"},\"x_scale\":{\"id\":\"1147\"},\"y_range\":{\"id\":\"1145\"},\"y_scale\":{\"id\":\"1149\"}},\"id\":\"1142\",\"subtype\":\"Figure\",\"type\":\"Plot\"},{\"attributes\":{\"line_alpha\":0.2,\"line_color\":\"orange\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1263\",\"type\":\"Line\"},{\"attributes\":{\"axis_label\":\"offpsring beak depth (mm)\",\"coordinates\":null,\"formatter\":{\"id\":\"1197\"},\"group\":null,\"major_label_policy\":{\"id\":\"1198\"},\"ticker\":{\"id\":\"1156\"}},\"id\":\"1155\",\"type\":\"LinearAxis\"},{\"attributes\":{\"line_alpha\":0.1,\"line_color\":\"orange\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1262\",\"type\":\"Line\"},{\"attributes\":{},\"id\":\"1147\",\"type\":\"LinearScale\"},{\"attributes\":{},\"id\":\"1203\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1164\",\"type\":\"HelpTool\"},{\"attributes\":{\"axis_label\":\"parental beak depth (mm)\",\"coordinates\":null,\"formatter\":{\"id\":\"1200\"},\"group\":null,\"major_label_policy\":{\"id\":\"1201\"},\"ticker\":{\"id\":\"1152\"}},\"id\":\"1151\",\"type\":\"LinearAxis\"},{\"attributes\":{},\"id\":\"1145\",\"type\":\"DataRange1d\"},{\"attributes\":{\"data\":{\"x\":{\"__ndarray__\":\"NDMzMzMzJECamZmZmRkjQMzMzMzMzCJAAAAAAACAJEAAAAAAAEAkQGZmZmZmZiNAmpmZmZkZIkCamZmZmZkdQAAAAAAAACJAzczMzMxMIUAAAAAAAEAjQMzMzMzMzCNAmpmZmZkZI0CamZmZmRkiQDMzMzMzsyBAMzMzMzMzJEAzMzMzMzMkQMzMzMzMzCNAMzMzMzNzJEAAAAAAAAAkQJqZmZmZGSVAZmZmZmbmJEBmZmZmZmYiQGZmZmZmZiRAZmZmZmbmIUCamZmZmRkkQGZmZmZmZiRAAAAAAAAAI0CamZmZmdkjQGZmZmZm5iNAmpmZmZkZJEAAAAAAAIAhQGZmZmZmZiJAzczMzMxMJECamZmZmZkjQGZmZmZmZiVAAAAAAAAAJUCamZmZmRkjQJqZmZmZGSVAMzMzMzPzJEDMzMzMzEwhQGZmZmZmZiVAMzMzMzMzIkDNz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}},\"id\":\"1173\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1163\",\"type\":\"ResetTool\"},{\"attributes\":{},\"id\":\"1202\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"coordinates\":null,\"group\":null},\"id\":\"1194\",\"type\":\"Title\"},{\"attributes\":{\"tools\":[{\"id\":\"1159\"},{\"id\":\"1160\"},{\"id\":\"1161\"},{\"id\":\"1162\"},{\"id\":\"1163\"},{\"id\":\"1164\"}]},\"id\":\"1166\",\"type\":\"Toolbar\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1173\"},\"glyph\":{\"id\":\"1174\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1176\"},\"nonselection_glyph\":{\"id\":\"1175\"},\"view\":{\"id\":\"1178\"}},\"id\":\"1177\",\"type\":\"GlyphRenderer\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.3},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.3},\"line_alpha\":{\"value\":0.3},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1174\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1143\",\"type\":\"DataRange1d\"},{\"attributes\":{},\"id\":\"1149\",\"type\":\"LinearScale\"},{\"attributes\":{\"axis\":{\"id\":\"1151\"},\"coordinates\":null,\"group\":null,\"ticker\":null},\"id\":\"1154\",\"type\":\"Grid\"},{\"attributes\":{\"source\":{\"id\":\"1260\"}},\"id\":\"1265\",\"type\":\"CDSView\"},{\"attributes\":{\"bottom_units\":\"screen\",\"coordinates\":null,\"fill_alpha\":0.5,\"fill_color\":\"lightgrey\",\"group\":null,\"left_units\":\"screen\",\"level\":\"overlay\",\"line_alpha\":1.0,\"line_color\":\"black\",\"line_dash\":[4,4],\"line_width\":2,\"right_units\":\"screen\",\"syncable\":false,\"top_units\":\"screen\"},\"id\":\"1165\",\"type\":\"BoxAnnotation\"},{\"attributes\":{\"data\":{\"x\":[7,12],\"y\":{\"__ndarray__\":\"cWI/+fYIHkCPxZShHj8mQA==\",\"dtype\":\"float64\",\"order\":\"little\",\"shape\":[2]}},\"selected\":{\"id\":\"1292\"},\"selection_policy\":{\"id\":\"1291\"}},\"id\":\"1260\",\"type\":\"ColumnDataSource\"},{\"attributes\":{},\"id\":\"1200\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{\"source\":{\"id\":\"1173\"}},\"id\":\"1178\",\"type\":\"CDSView\"},{\"attributes\":{},\"id\":\"1291\",\"type\":\"UnionRenderers\"},{\"attributes\":{\"line_color\":\"orange\",\"line_width\":2,\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1261\",\"type\":\"Line\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.1},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.1},\"line_alpha\":{\"value\":0.1},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1175\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1156\",\"type\":\"BasicTicker\"},{\"attributes\":{\"axis\":{\"id\":\"1155\"},\"coordinates\":null,\"dimension\":1,\"group\":null,\"ticker\":null},\"id\":\"1158\",\"type\":\"Grid\"},{\"attributes\":{\"overlay\":{\"id\":\"1165\"}},\"id\":\"1161\",\"type\":\"BoxZoomTool\"},{\"attributes\":{},\"id\":\"1162\",\"type\":\"SaveTool\"},{\"attributes\":{},\"id\":\"1197\",\"type\":\"BasicTickFormatter\"},{\"attributes\":{},\"id\":\"1160\",\"type\":\"WheelZoomTool\"},{\"attributes\":{\"fill_alpha\":{\"value\":0.2},\"fill_color\":{\"value\":\"#1f77b4\"},\"hatch_alpha\":{\"value\":0.2},\"line_alpha\":{\"value\":0.2},\"line_color\":{\"value\":\"#1f77b4\"},\"x\":{\"field\":\"x\"},\"y\":{\"field\":\"y\"}},\"id\":\"1176\",\"type\":\"Circle\"},{\"attributes\":{},\"id\":\"1198\",\"type\":\"AllLabels\"},{\"attributes\":{\"coordinates\":null,\"data_source\":{\"id\":\"1260\"},\"glyph\":{\"id\":\"1261\"},\"group\":null,\"hover_glyph\":null,\"muted_glyph\":{\"id\":\"1263\"},\"nonselection_glyph\":{\"id\":\"1262\"},\"view\":{\"id\":\"1265\"}},\"id\":\"1264\",\"type\":\"GlyphRenderer\"},{\"attributes\":{},\"id\":\"1292\",\"type\":\"Selection\"},{\"attributes\":{},\"id\":\"1201\",\"type\":\"AllLabels\"},{\"attributes\":{},\"id\":\"1159\",\"type\":\"PanTool\"},{\"attributes\":{},\"id\":\"1152\",\"type\":\"BasicTicker\"}],\"root_ids\":[\"1142\"]},\"title\":\"Bokeh Application\",\"version\":\"2.4.2\"}};\n", " const render_items = [{\"docid\":\"cfb60e88-7b10-4203-852a-99f0ebc7ff7a\",\"root_ids\":[\"1142\"],\"roots\":{\"1142\":\"afc3be40-2cc7-4d98-8fb3-167e732ff24b\"}}];\n", " root.Bokeh.embed.embed_items_notebook(docs_json, render_items);\n", "\n", " }\n", " if (root.Bokeh !== undefined) {\n", " embed_document(root);\n", " } else {\n", " let attempts = 0;\n", " const timer = setInterval(function(root) {\n", " if (root.Bokeh !== undefined) {\n", " clearInterval(timer);\n", " embed_document(root);\n", " } else {\n", " attempts++;\n", " if (attempts > 100) {\n", " clearInterval(timer);\n", " console.log(\"Bokeh: ERROR: Unable to run BokehJS code because BokehJS library is missing\");\n", " }\n", " }\n", " }, 10, root)\n", " }\n", "})(window);" ], "application/vnd.bokehjs_exec.v0+json": "" }, "metadata": { "application/vnd.bokehjs_exec.v0+json": { "id": "1142" } }, "output_type": "display_data" } ], "source": [ "# Plot best fit line\n", "x = np.array([7, 12])\n", "y = linear_fun(x, slope, intercept)\n", "\n", "p.line(x, y, line_width=2, color='orange')\n", "\n", "bokeh.io.show(p)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This does not look exactly like what we would draw by eye, but this is because of overlapping data points. This is, indeed, the best fit line." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Including images and converting to HTML\n", "\n", "If you want to include images in your notebook, you can do that directly in a Markdown cell using HTML tags. For example:\n", "\n", " \n", "\n", "You may have scans of equations you want to include, and you can use this technique to do it. However, if you are displaying images from your experiments, you should load them in using `skimage` and display them as shown in the next [tutorial on image processing](image_processing_1.html).\n", "\n", "For all of your assignment, you will need to submit a Jupyter notebook and the notebook conversion of the notebook. To convert to HTML, click through the following menu option. \n", "\n", " File -> Export Notebook As... -> HTML\n", " \n", "Note that if you are displaying images using HTML tags, you must also include the images with your submission. The best way to do the submission is to zip everything together in a single ZIP file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conclusions\n", "\n", "This concludes our introductory tour of Python with some NumPy, SciPy, Pandas, and Bokeh thrown in for good measure. There is still *much* to learn, but you will pick up more and more tricks and syntax as we go along.\n", "\n", "For the next tutorial, we will use Python to do some image processing. That is, we will write code to extract data of interest from images. As you get more and more proficient, coding (particularly in Python, in my opinion) will be more and more empowering and FUN!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Computing environment" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python implementation: CPython\n", "Python version : 3.9.7\n", "IPython version : 8.1.1\n", "\n", "numpy : 1.21.2\n", "scipy : 1.7.3\n", "pandas : 1.4.1\n", "bokeh : 2.4.2\n", "jupyterlab: 3.3.2\n", "\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -v -p numpy,scipy,pandas,bokeh,jupyterlab" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }