From MATLAB Guide to IPython Notebook

By Don MacMillen

Back in the mid-2000s, I realized I was late to the party and determined to learn one of the matrix and numerical mathematics oriented languages. A Python-based language was the natural choice for me since I already knew the language. The numerical packages for Python, Numpy, and Scipy, seemed to be just what I was looking for as well. The only problem, at that time, was in the lack of good learning examples. Things have changed greatly since then, and there are now a host of great references for Python, Numpy, Scipy, and IPython [1-5].

Back then I noticed that a book advertised in SIAM News had received outstanding reviews. It was D.J. Higham and N.J. Higham’s book “MATLAB Guide” [6]. The obvious issue was that it was written for MATLAB, but I thought why not try to replicate the examples in Python/Numpy?

So I gave that a try and had a great experience working through the book. One of the things I liked best about “MATLAB Guide” was the first chapter, titled “A Brief Tutorial”. There the authors dive right in to show the range of what can be done. That struck me as much more effective than starting with “for loops” and file reads and writes. At some point a feature by feature approach to a language may be more efficient, but it can leave you with the feeling of “just how in the heck do I solve a problem?”

If we now fast forward to 2014, there are now more great tools to learn for scientific computing in Python. Specifically here I mean IPython [7] and the IPython Notebook. The IPython Notebook is a fantastic tool for both exploratory computing and teaching. The Notebook uses a web browser and an IPython shell back end to “render” Python/Numpy code that is typed into “cells” on the web page. You can make a change and then hit the “play” button to re-evaluate a cell, or the whole page, if desired. The results, including any graphics from the matplotlib module, are then rendered into the browser.

A notebook will consist of a series of cells. Cells can be of the type of code, markdown, and raw. Code cells are what you might expect: Python code. But the IPython Notebook can also support R, Julia, and Perl and is designed to be language agnostic. Markdown cells are cells that contain text that will be first translated to HTML and then rendered onto the web page. The Markdown language “allows you to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML).” The IPython Notebook also supports embedded LaTeX, which is rendered by the MathJax package for formatting equations. It makes it remarkably easy to create attractive text and equations.

So how would I learn to use the Notebook? By going back to the well and translating the first chapter of “MATLAB Guide”. Each page of the chapter was scanned and then fed to OCRfeeder. The resulting text was put into Markdown cells and the MATLAB code was put into code cells. The text produced by OCRFeeder, with the exception of some randomly capitalized “V”s, was fairly good. The equations needed to be re-set with LaTeX of course, but there was nothing much more needed than a couple of LaTeX cheatsheets found via a Google search.

Translating the MATLAB code to Python/Numpy was perhaps the easiest (and most fun) part the the endeavor. Many of the details on just how to do that can be found at NumPy_for_Matlab_Users. Briefly, the basic Numpy data type is a multi-dimensional ndarray, which can have up to 64 dimensions. Indexing starts at zero, not one. Where MATLAB seems to have a flat namespace, in Python the various Numpy and Scipy packages need to be imported. The downside is that it is sometimes harder to find exactly what you are looking for, but the upside is a finer degree of control on the visibilty of functions and better management of potential name clashes.

The resulting IPython Notebook yields a dynamic version of “MATLAB Guide” Chapter 1. If you have a local IPython Notebook server running (which you can install from here) you can download this notebook, which is hosted as a gist at github, from the url [https://gist.github.com/macd/10562695], and run it locally. A small section of the notebook is shown in the following image:

A screenshot from the IPython Notebook.

There is also a way to share and view static versions of notebooks. Users who don’t have a local notebook server running can use the nbviewer service. By typing the gist location above into this site, a static version of the notebook is served to your web browser. The static version is not as flexible as a dynamic view, but using the nbviewer and a github gist is a great way to share a notebook with a colleague or a classroom of students. Note that you can make these gists either public or private.

The future looks very bright for both IPython and the IPython Notebook. The project was awarded a $1.15M Sloan Foundation grant in December of 2012 and has ambitious goals in the areas of interactive scientific computing and reproducible research. For my part, I have started using the Notebook to explore and document the many little decisions that go into making commercial software.

References

[1] Ivan Idris, “NumPy Beginner’s Guide“, Packt Publishing, 2013, ISBN 1782166084
[2] Cyrille Rossant, “Learning IPython for Interactive Computing and Data Visualization“, Packt Publishing, 2013, ISBN 1782169938
[3] Mark Neuman, “Computational Physics with Python“, CreateSpace Independent Publishing Platform, 2013, ISBN 1480145513
[4] Hans Petter Langtangen, “A Primer on Scientific Programming with Python” Springer, 2012, ISBN 3642302920
[5] Rubin H. Landau, Manuel Jose Paez, Cristian C. Bordeianu, “A Survey of Computational Physics: Introductory Computational Science“, Princeton University Press, Princeton, New Jersey, 2012, ISBN 978–0–691–13137–5
[6] D.J. Higham and N.J. Higham, “MATLAB Guide, Second edition“, Society for Industrial and Applied Mathematics, Philadelphia, PA, USA, 2005, ISBN 0-89871-578-4
[7] Fernando Pérez, Brian E. Granger, “IPython: A System for Interactive Scientific Computing”, Computing in Science and Engineering, vol. 9, no. 3, pp. 21-29, May/June 2007, doi:10.1109/MCSE.2007.53. URL: [http://ipython.org]

Don MacMillen is Vice President of Engineering at Nimbic, Inc. and an affiliate professor of electrical engineering at the University of Washington.


Research
blog comments powered by Disqus
Videos More Videos