Archive | October 2014

The IPython Notebook


The IPython notebook is amazing. It is so amazing that I could not resist writing yet another blog entry about it, when there are probably hundreds of sources of information about the notebook.

Imagine you have run some calculations with a certain code. Then you have a short script that copies output files and/or extracts important data into a text file, which you then import into a spreadsheet. You work with the data, and produce some plots which you copy&paste into a word-processor file where you have explained your aims, your set-up and the data manipulation.

Are you familiar with this? Don’t you think that the lab notebooks your colleagues have are more efficient? and more trustful? Aren’t you tired of repeating this cycle again and again every time you re-run some of the calculations?
If you answered ‘yes’ to some if these questions, the IPython notebook is for you. Even if you have never used python!

The Ipython notebook is the lab notebook of the computational scientists. You can insert complex mathematical formulas as with .\LaTeX. The recent extension to use a spell-check render the notebook even more useful. You can also insert videos and images from the web. For example to compare your results with that of a published paper a couple of lines suffice:

from IPython.display import Image
Image(url='http://www.ploscompbiol.org/article/fetchObject.action?uri=info:doi/10.1371/journal.pcbi.1000497\
.g001&representation=PNG_L')

Of course, you can also have python code in the notebook, But also bash, Ruby or Fortran code!

%%fortran
real function sincos(x)
    implicit none
    real*8 :: x
    sincos = sin(x)*cos(x)
end function

And then use the Fortran function directly in your python cells (you’ll need to install the
%fortranmagic extension)

sincos(1.5) - math.sin(1.5)*math.cos(1.5) < 1e-7
True

You can share the code as static notebooks with the http://nbviewer.ipython.org or convert them into LaTeX, PDFs or HTML.

I’ve only sketched a few capabilities of the notebook. Take a look at these examples to decide where you want to focus. And after all the praise, some criticism:

  • The notebook is evolving so fast that you never know if your installed version is able to do some of the awesome tricks you read somewhere. And the versions packed in the repositories usually lag behind. That means you are forced to do a manual installation.

  • For some reason the image linked in my notebooks disappeared from the exported PDF version.

I finish this post with a confession. I should have used IPython to blog directly. This is possible but I am new to wordpress.com and I still have to check how to do it. Shame on me!

Advertisements