Monday, December 16, 2013

Interactive plots in ipython notebooks

A couple of ways of doing interactive plots and animations in an ipython notebook:

I am sure there are many other ways, and if you know of nice ways please post in the comments. 

Thursday, September 12, 2013

Monday, September 9, 2013

Easily run fortran code from an IPython notebook cell

I just learned a potentially very useful trick for people running fortran code. By following the instructions on this link, you can copy and paste fortran code to an IPython notebook cell and then use a ipython magic command to run it on-the-fly. I haven't had time to test it yet, so if you have success with it, please let me know. Kudos to @fperez_org.

On a separate note, IPython 1.1 is out!

Update (Sep. 23rd 2013): Here is a tutorial on how to use the fortran IPython magic.

Saturday, July 6, 2013

Parallel Bayes with IPython

Excellent tutorial from the folks at Continuum Analytics/Wakari on how to parallelize bayesian parameter estimation using IPython (IPcluster) and pyMC.

Following this tutorial, I was able to make my bayesian spectral fitting code (for astronomical purposes) parallel -- and much faster -- very quickly.

Bayesian Estimation with PyMC and IPCluster


Thursday, May 30, 2013

Installation instructions (from a Python Boot Camp course)

I wrote some instructions on how to install python and relevant dependencies (numpy, matplotlib, ipython, ipython notebook etc) for OS X, linux and windows. These instructions are for an upcoming Python Boot Camp where I work aimed at students, researchers and engineers (mostly Earth and physical sciences)

I wanted to share these instructions since they may be useful to more people. If you have been keeping up with this blog, note that there is some repetition here.


Python Installation Instructions

This page describes how to install Python and the other packages (Numpy, Scipy, IPython, Matplotlib) required for the course for Mac OS X, Linux and Windows.

Linux

In Linux, the installation instructions are pretty straightforward. Assuming that you are running Debian or Ubuntu, you just need to execute the following command in the terminal:

sudo apt-get install python-numpy python-scipy python-matplotlib ipython-notebook

For Fedora users, you can use the yum tool.

Mac OS X, Windows

If you are affiliated with an academic institution

Then the easiest way to install Python and the other packages is to request an academic license and download the Enthought Canopy Python distribution. Enthought includes all the packages we will use during the course.

The installation instructions are available here, which has installers for Mac OS X and Windows.

Note that you need the academic license in order to install the 64-bit (recommended) version. The 32-bit version is free for all.

If you are not affiliated with an university

This is the case for example for GSFC employees and many GSFC postdocs. In this case, we recommend downloading and installing the Anaconda Python distribution. The installations instructions are available here.

Just download the installer and execute it.

Anaconda includes most of the packages we will use and it is pretty easy to install additional packages if required.

If the above two methods do not work for OS X

The MacPorts way

You can try installing everything using MacPorts. First download and install macports and then issue the following command in a terminal:

sudo port install py27-zmq py27-tornado py27-nose

The avove dependencies are required in order to run IPython notebook. Then run:

sudo port install py27-numpy py27-matplotlib py27-scipy py27-ipython

The advantage of this method is that it easy to do. The downsides:

  • It can take a couple of hours to finish the installation depending on your machine and internet connection, since macports will download and compile everything as it goes. 
  • If you like having the bleeding edge versions, note that it can take a while for them to be released on macports 
  • Finally, macports can create conflicts between different python interpreters installed in your system

Using Apple’s Python interpreted and pip

If you feel adventurous, you can use Apple’s builtin python interpreter and install everything using pip. Please follow the instructions described in this blog.

If you run into trouble

Feel free to contact us. Leave a comment here with the issue you found.

Thursday, May 23, 2013

Easy way of installing Python and scientific packages for OS X (non-academic users): Anaconda

Introduction

Mac OS X users know that getting python and its scientific packages (numpy, scipy, ipython etc) installed properly can be a tricky job.

If you are affiliated with a university, you can try the freely available and easy to install Enthought distribution (if you are not affiliated with a university, you can still get the Enthought dsitribution for free but you are stuck with 32 bits binaries).

One way of getting things installed properly (even if you are not affiliated with a university) is by using the native OS X python interpreter and installing everything via pip as I describe in this post. This method is a little tricky but works well for me. One advantage is that it is easy to install new packages in this way: just use pip.

Another way is using macports. This is relatively straightforward, but can take a long time to compile all the dependencies (hours) and, worse of all, can potentially create conflicts between different libraries and different python interpreters.

Anaconda

I recently came across an easy way of getting python and the scientific packages installed. It also provides a convenient framework for installing extra packages. This is the Anaconda python distribution provided by Continuum Analytics. Anaconda provides the most popular packages: numpy, scipy, ipython (+notebook) and even more - astropy, spyder, pandas etc. And 64 bits binaries! For everybody. For free.

How to install it? 

Pretty easy. Go to the downloads page. Download the installer for your operating system. After downloading the file, for OS X you just need to issue the command

 sh <downloaded file.sh>  

Piece of cake. You can choose to let the installer change your PATH variable. After that, when you invoke python or ipython, it will automatically call the appropriate binaries and you will have available all the important packages.

To install additional packages, you can use their conda package manager or the usual pip.

Wednesday, May 8, 2013

Comparing execution speed of Python, NumPy, Matlab, Fortran, etc

This is an interesting comparison of the numerical performance of python, numpy, fortran, java and matlab in performing some array operations.

The post is outdated (it is from 2009). Still, it demonstrates the importance of vectorizing array operations. It also shows that lower level languages like fortran will almost always have the upper hand in execution speed (but development time can easily balance that in python).

It would be worth repeating this comparison with the newer versions of numpy, python, matlab and fortran compilers to see what has changed in terms of relative speedups among the different languages. It would also be nice to include a "parallel" case of these examples using some multiprocessing module in python in a multicore machine (e.g. multiprocessing, iPython parallel).

Saturday, April 6, 2013

Shortcut to open an iPython notebook and web browser from the terminal

In order to start an iPython notebook server and open a notebook in your browser you can issue the command


>>> ipython notebook --pylab inline

Sometimes, mysteriously, the web browser will not automatically open after issue the above command on Mac OS X. In that case, you can use the command



>>> ipython notebook --pylab inline && open http://127.0.0.1:8888/'

Thursday, February 28, 2013

Library of plotting routines for Python and IDL

Last January I was walking around the place where the posters were being presented at the American Astronomical Society meeting in Long Beach. I stumbled upon a very interesting poster advertising the astroplotlib web page.

The purpose of this page, in the language of its curator, is that it is
[...] a multi-language astronomical library of plots. It is a collection of templates that are useful to create paper-quality figures. So far all codes are written in IDL, some in Python and very few in Mathematica. 

Most of the codes available there are written in IDL (ugh) but there are quite a few for Python. I hope the python codebase will grow in the future.

For instance, I have been using Bayesian modeling of data and I need to plot contours for the joint distribution of parameters as well as the separate posterior distribution histograms, all in the same plot. After a quick search in the website, I found the code I need to get started. 

If you have contributions (codes or suggestions), please contact the webmaster, Leonardo Ubeda.



Thursday, February 21, 2013

Example of how to do a great animation in Python

If you are interested in creating animation of simulation data in Python, perhaps this should give you some inspiration: Animating the Lorenz System in 3D.

The author solved the Lorenz system of equations and plotted the time evolution of the system in 3D. It illustrates how to make a great animation in a simple way.

You should also check out the Matplotlib Animation Tutorial written by the author, which provides the background on the Lorenz animation.

Great gallery of IPython notebooks

You know you can create IPython notebooks "a la Mathematica" and put them in your webpage, right? That's a great way of sharing a step-by-step analysis workflow with someone.

I stumbled upon a great collection of IPython notebooks that illustrate a vast range of capabilities: data analysis, statistics, interfacing with C/Fortran, plots, numpy tricks and a lot more!

You should check it out: A gallery of interesting IPython notebooks at github (via @profjsb).


Monday, January 14, 2013

Nice matplotlib tutorial

While I was searching for a solution on how to plot correctly the ticks in the x axis of a plot, I came across this very nice and clear matplotlib tutorial.

Definitely check it out.