{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to scientific computing with Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basics\n", "\n", "We'll start with some basics in the interpreter, covering strings and maths, then come back to this notebook.\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing, slicing, `append()`, `in`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layers = [0.23, ] # Add some more" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Play with lists\n", "# e.g. indexing with layers[4]\n", "# slicing with layers[2:4]" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "uppers = layers[:-1]\n", "lowers = layers# how shall we slice it?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rcs = []\n", "for pair in zip(uppers, lowers):\n", " rc = # Compute the contrast\n", " # Now what?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rcs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Definition, inputs, side-effects, returning, scope, docstrings" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def add(a, b):\n", " return a + b" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Exercise\n", "def compute_rc(layers):\n", " \"\"\"\n", " Computes reflection coefficients given\n", " a list of layer impedances.\n", " \"\"\"\n", " \n", " # Put code here\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# This should give you what you got before\n", "compute_rc(layers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we will put this function in a file and import into a new notebook.\n", "\n", "* Open a text editor and copy the function definition into it\n", "* Save the file as `.py` in your current working directory\n", "* Restart the kernel in this notebook (*Kernel* > *Restart*)\n", "\n", "Run this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layers = [0.23, 0.34, 0.45, 0.25, 0.23, 0.35]\n", "compute_rc(layers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Why didn't that work?\n", "\n", "Change the name here to the name of your file:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import something\n", "something.compute_rc(layers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NumPy" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np # Just like importing file" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "biglog = np.random.random(1000)\n", "%timeit compute_rc(biglog)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need to learn the basics of NumPy arrays." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layers + 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "layers = np.array(layers)\n", "layers + 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Etc." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Exercise\n", "def compute_rc_vector(layers):\n", "\n", " # rewrite using NumPy arrays\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%timeit compute_rc_vector(biglog)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Warning signs:\n", "\n", "\n", "* Writing loops on arrays\n", "* Writing complicated functions on arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plotting" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "plt.plot(biglog)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "fig = plt.figure(figsize=(15,2))\n", "ax = fig.add_subplot(111)\n", "ax.plot(biglog)\n", "ax.set_title(\"big log\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Web" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "url = \"http://en.wikipedia.org/wiki/Jurassic\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use View Source in your browser to figure out where the age range is on the page, and what it looks like.\n", "\n", "Try to find the same string here:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests\n", "r = requests.get(url)\n", "r.text" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import re\n", "# Use regex to extract the age range\n", "\n", "# Make a function to get the start and end data of any geologic period" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "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.9" } }, "nbformat": 4, "nbformat_minor": 0 }