{ "metadata": { "name": "" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "# import some things\n", "%pylab --no-import-all inline\n", "import numpy as np\n", "import pylab as pl\n", "from scipy import linalg" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Populating the interactive namespace from numpy and matplotlib\n" ] } ], "prompt_number": 180 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Python for Scientific Computing\n", "===============================\n", "\n", "\n", "\n", "\n", "**Martin Luessi**\n", "\n", "**Martinos Center \"Why N' How\", September 19, 2013**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What is Python and why would I use it?\n", "--------------------------------------\n", "\n", "- Python is an **intepreted high-level programming language**\n", "- Python is **free** (as in speech)\n", "- Python runs on most platforms\n", "- It [**\"combines remarkable power with very clear syntax\"**](http://docs.python.org/faq/general.html#what-is-python)\n", "- Well suited for **high performance numerical computing** (NumPy, ...)\n", "- High quality **2D and 3D visualizations** (pylab, mlab, ...)\n", "- Increasingly **popular in neuroscience** (nipy, nipype, nitime, ...)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "What you should be able to do after this talk\n", "---------------------------------------------\n", "\n", "- Start Python\n", "- Do simple math\n", "- Get started with linear algebra and scientific computing\n", "- Plot some nice figures" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Use Python for what?\n", "--------------------\n", "\n", "- Scripting (like shell scripts, e.g., bash, csh)\n", "- Make web sites\n", "- Build GUI applications\n", "- **Science** (like Matlab, IDL, R, Octave, Scilab)\n", "- Etc.\n", "\n", "**You just need to know one language to do almost anything !**\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Scientific Python building blocks\n", "---------------------------------\n", "\n", "* **Python interpreter**: executes Python code\n", "\n", "* [**IPython**](http://ipython.org): an advanced **Python shell**\n", "\n", "* [**NumPy**](http://www.numpy.org): provides **numerical array** objects\n", "\n", "* [**SciPy**](http://www.scipy.org/): scientific computing\n", " (linear algebra, optimization, regression, etc.)\n", "\n", "* [**Matplotlib**](http://matplotlib.org) a.k.a. Pylab: 2-D visualization, \"publication-ready\" plots\n", "\n", "* [**Mayavi**](http://mayavi.sourceforge.net) : 3-D visualization\n", "\n", "* Many application specific packages for e.g., machine learning,\n", " image processing, symbolic math, .. [incomplete list](http://www.scipy.org/Topical_Software)\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "First Steps\n", "-----------\n", "\n", "Get a scientific-Python environment:\n", "\n", "* Comes with every Linux distribution\n", "* Python(x,y) on Windows: http://www.pythonxy.com\n", "* Enthought Canopy or EPD: http://www.enthought.com\n", "* Continuum Analytics Anaconda http://www.continuum.io\n", "* At the Martinos Center use the EPD based network installation, see [here](http://surfer.nmr.mgh.harvard.edu/fswiki/DevelopersGuide/NMRCenterPython/UsersGuide\n", ")\n", "\n", "Start the **IPython shell** (from terminal or Windows cmd shell):\n", "\n", " $ ipython --pylab" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Hello world!\n", "------------\n", "\n", "The IPython Shell is an interactive shell:\n", "\n", " \n", "\n", "Now we can write our \"Hello World\" program by typing:" ] }, { "cell_type": "code", "collapsed": true, "input": [ "s = \"Hello World!\"\n", "print s" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World!\n" ] } ], "prompt_number": 181 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "My first script\n", "---------------\n", "\n", "Let's say the file ``my_script.py`` contains:\n", "\n", " s = 'Hello World!'\n", " print s\n", " \n", "In IPython you can run it as follows:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%run my_script.py" ], "language": "python", "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Hello World!\n" ] } ], "prompt_number": 182 }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If you are scared of the terminal\n", "---------------------------------\n", "\n", "You can use [**Spyder**](http://code.google.com/p/spyderlib), a scientific Python IDE.\n", "Or the [**IPython Notebook**](http://ipython.org/notebook.html)\n", "\n", "\n", "