Created
April 20, 2013 07:28
-
-
Save NelleV/5425122 to your computer and use it in GitHub Desktop.
AGramfort's Python introduction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "Lecture-0-Scientific-Computing-with-Python" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"So Python...\n", | |
"=============\n", | |
"\n", | |
"A. Gramfort ([email protected]) http://alexandre.gramfort.net\n", | |
"\n", | |
"based on the work of J.R. Johansson ([email protected]) http://dml.riken.jp/~rob/" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"What is Python?\n", | |
"---------------\n", | |
"\n", | |
"[Python](http://www.python.org/) is a modern, general-purpose, object-oriented, high-level programming language.\n", | |
"\n", | |
"General characteristics of Python:\n", | |
"\n", | |
"* **clean and simple language:** Easy-to-read and intuitive code, easy-to-learn minimalistic syntax, maintainability scales well with size of projects\n", | |
"* **expressive language:** Fewer lines of code, fewer bugs, easier to maintain.\n", | |
"\n", | |
"Technical details:\n", | |
"\n", | |
"* **dynamically typed:** No need to define the type of variables, function arguments or return types.\n", | |
"* **automatic memory management:** No need to explicitly allocate and deallocate memory for variables and data arrays. No memory leak bugs. \n", | |
"* **interpreted:** No need to compile the code. The Python interpreter reads and executes the python code directly.\n", | |
"\n", | |
"Advantages:\n", | |
"\n", | |
"* **Ease of programming**: minimize development time, debug and code maintainance.\n", | |
"* **Encourage good programming practices**:\n", | |
" * Modular, *relatively* good system for packaging and re-use of code.\n", | |
" * Documentation tightly integrated with the code.\n", | |
" * Forces code indentation\n", | |
"* A **large standard library**, and a **large collection of add-on packages**.\n", | |
"\n", | |
"Disadvantages:\n", | |
"\n", | |
"* Since Python is an **interpreted** and **dynamically typed** programming language, the execution of python can be code slow compared to compiled statically typed programming languages, such as C and Fortran. \n", | |
"* Somewhat decentralized, with different environment, packages and documentation spread out at different places. Can make it harder to get started." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"What makes python suitable for scientific computing?\n", | |
"----------------------------------------------------\n", | |
"\n", | |
"<img src=\"https://raw.github.com/jrjohansson/scientific-python-lectures/master/images/optimizing-what.png\" width=\"600\">\n", | |
"\n", | |
"* Python has a strong position in scientific computing: \n", | |
" * Large community of users, easy to find help and documentation.\n", | |
"\n", | |
"* Extensive ecosystem of scientific libraries and environments\n", | |
" * numpy: http://numpy.scipy.org - Numerical Python\n", | |
" * scipy: http://www.scipy.org - Scientific Python\n", | |
" * matplotlib: http://www.matplotlib.org - graphics library\n", | |
"\n", | |
"* Great performance due to close integration with time-tested and highly optimized codes written in C and Fortran:\n", | |
" * blas, altas blas, lapack, arpack, Intel MKL, ...\n", | |
"\n", | |
"* Good support for \n", | |
" * Parallel processing with processes and threads (have a look at http://pythonhosted.org/joblib/)\n", | |
" * Interprocess communication (MPI)\n", | |
" * GPU computing (OpenCL and CUDA)\n", | |
"\n", | |
"* Readily available and suitable for use on high-performance computing clusters. \n", | |
"\n", | |
"* No license costs, no unnecessary use of research budget." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Python interpreter\n", | |
"\n", | |
"The standard way to use Python is via the Python interpreter. It is a program that reads and executes the python code in files passed to it as arguments. At the command prompt, the command ``python`` is used to invoke the Python interpreter.\n", | |
"\n", | |
"For example, to run a file ``my_program.py`` that contains python code from the command prompt, use::\n", | |
"\n", | |
" $ python my_program.py\n", | |
"\n", | |
"We can also start the interpreter by simply typing ``python`` at the command line, and interactively type python code into the interpreter. \n", | |
"\n", | |
"<!-- <img src=\"files/images/python-screenshot.jpg\" width=\"600\"> -->\n", | |
"<img src=\"https://raw.github.com/jrjohansson/scientific-python-lectures/master/images/python-screenshot.jpg\" width=\"600\">\n", | |
"\n", | |
"**Works but not good enough** when developing scientific applications...." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### IPython\n", | |
"\n", | |
"IPython is a powerful and user friendly interactive shell.\n", | |
"\n", | |
"<!-- <img src=\"files/images/ipython-screenshot.jpg\" width=\"600\"> -->\n", | |
"<img src=\"https://raw.github.com/jrjohansson/scientific-python-lectures/master/images/ipython-screenshot.jpg\" width=\"600\">\n", | |
"\n", | |
"Some of the many useful features of IPython includes:\n", | |
"\n", | |
"* Command history, which can be browsed with the up and down arrows on the keyboard.\n", | |
"* Tab auto-completion.\n", | |
"* In-line editing of code.\n", | |
"* Object introspection, and automatic extract of documentation strings from python objects like classes and functions.\n", | |
"* Good interaction with operating system shell.\n", | |
"* Support for multiple parallel back-end processes, that can run on computing clusters or cloud services like Amazon EE2.\n", | |
"\n", | |
"WATCH : https://www.youtube.com/watch?v=2G5YTlheCbw\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"IPython notebook\n", | |
"----------------\n", | |
"\n", | |
"[IPython notebook](<http://ipython.org/ipython-doc/dev/interactive/htmlnotebook.html/) is an HTML-based notebook environment for Python, similar to Mathematica or Maple.\n", | |
"\n", | |
"<!-- <img src=\"files/images/ipython-notebook-screenshot.jpg\" width=\"800\"> -->\n", | |
"<img src=\"https://raw.github.com/jrjohansson/scientific-python-lectures/master/images/ipython-notebook-screenshot.jpg\" width=\"800\">\n", | |
"\n", | |
"To start a new IPython notebook session, run the following command:\n", | |
"\n", | |
" $ ipython notebook\n", | |
"\n", | |
"from a directory where you want the notebooks to be stored. This will open a new browser window (or a new tab in an existing window) with an index page where existing notebooks are shown and from which new notebooks can be created." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Spyder\n", | |
"------\n", | |
"\n", | |
"[Spyder](http://code.google.com/p/spyderlib/) is a MATLAB-like IDE for scientific computing with python. It has the many advantages of a traditional IDE environment, for example that everything from code editing, execution and debugging is carried out in a single environment, and work on different calculations can be organized as projects in the IDE environment.\n", | |
"\n", | |
"<!-- <img src=\"files/images/spyder-screenshot.jpg\" width=\"800\"> -->\n", | |
"<img src=\"https://raw.github.com/jrjohansson/scientific-python-lectures/master/images/spyder-screenshot.jpg\" width=\"800\">\n", | |
"\n", | |
"Some advantages of Spyder:\n", | |
"\n", | |
"* Powerful code editor, with syntax high-lighting, dynamic code introspection and integration with the python debugger.\n", | |
"* Variable explorer, IPython command prompt.\n", | |
"* Integrated documentation and help." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Versions of Python\n", | |
"\n", | |
"There are currently two versions of python: Python 2 and Python 3. Python 3 will supercede Python 2 some day...\n", | |
"\n", | |
"To see which version of Python you have, run\n", | |
" \n", | |
" $ python --version\n", | |
" Python 2.7.3\n", | |
" $ python3.2 --version\n", | |
" Python 3.2.3\n", | |
"\n", | |
"Several versions of Python can be installed in parallel, as shown above.\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Installation\n", | |
"\n", | |
"### Linux\n", | |
"\n", | |
"In Ubuntu Linux, to installing python and all the requirements run:\n", | |
"\n", | |
" $ sudo apt-get install python ipython ipython-notebook\n", | |
" $ sudo apt-get install python-numpy python-scipy python-matplotlib python-sympy\n", | |
" $ sudo apt-get install spyder\n", | |
"\n", | |
"### MacOS X\n", | |
"\n", | |
"*Macports*\n", | |
"\n", | |
"Python is included by default in Mac OS X, but for our purposes it will be useful to install a new python environment using [Macports](http://www.macports.org/), because it makes it much easier to install all the required additional packages. Using Macports, we can install what we need with:\n", | |
"\n", | |
" $ sudo port install py27-ipython +pyside+notebook+parallel+scientific\n", | |
" $ sudo port install py27-scipy py27-matplotlib py27-sympy\n", | |
" $ sudo port install py27-spyder\n", | |
"\n", | |
"These will associate the commands `python` and `ipython` with the versions installed via macports (instead of the one that is shipped with Mac OS X), run the following commands:\n", | |
"\n", | |
" $ sudo port select python python27\n", | |
" $ sudo port select ipython ipython27\n", | |
"\n", | |
"*Fink*\n", | |
"\n", | |
"Or, alternatively, you can use the [Fink](http://www.finkproject.org/) package manager. After installing Fink, use the following command to install python and the packages that we need:\n", | |
"\n", | |
" $ sudo fink install python27 ipython-py27 numpy-py27 matplotlib-py27 scipy-py27 sympy-py27\n", | |
" $ sudo fink install spyder-mac-py27\n", | |
"\n", | |
"*EPD*\n", | |
"\n", | |
"See https://www.enthought.com/products/epd (Free for academic use)\n", | |
"or EPD Free\n", | |
"\n", | |
"### Windows\n", | |
"\n", | |
"Windows lacks a good packaging system, so the easiest way to setup a Python environment is to install a pre-packaged distribution. Some good alternatives are:\n", | |
"\n", | |
" * [Enthought Python Distribution](http://www.enthought.com/products/epd.php). EPD is a commercial product but is available free for academic use.\n", | |
" * [Anaconda CE](http://continuum.io/downloads.html). Anaconda Pro is a commercial product, but Anaconda Community Edition is free.\n", | |
" * [Python(x,y)](http://code.google.com/p/pythonxy/). Fully open source.\n", | |
"\n", | |
"\n", | |
"#### Note\n", | |
"\n", | |
"Anaconda CE is also available for Linux and Max OS X." | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Further reading\n", | |
"\n", | |
" * [Python](http://www.python.org>). The official Python web site.\n", | |
" * [Python tutorials](http://docs.python.org/2/tutorial/). The official Python tutorials.\n", | |
" * [Think Python](http://www.greenteapress.com/thinkpython/). A free book on Python." | |
] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment