Log in

No account? Create an account
From Matlab to Python - Tobin's Lab Notebook [entries|archive|friends|userinfo]
Tobin Fricke's Lab Notebook

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

From Matlab to Python [Feb. 27th, 2013|05:38 pm]
Tobin Fricke's Lab Notebook
[Tags|, ]

Since college, Matlab has been my go-to environment for all kinds of numerical simulations, analysis, and plotting. Matlab is an exceptionally well-designed environment and I love it.

The downside to Matlab is that it is expensive. Happily my employer provides not only a Matlab license but also licenses to the many add-on toolboxes that quickly become indispensable. Nonetheless, since the license server is on the network, work grinds to a halt when sitting with the laptop on a train or an airplane or anywhere else without network access.

I've heard tell that many of the useful features of Matlab have been ported over to Python. The idea of a free, open-source environment that's just as good as Matlab is very appealing. To be honest, I'm not really sure what's needed to make this Python environment work, or really, what all the pieces are. I've heard of matplotlib (for making Matlab-like plots), SciPy, NumPy, and Pylab. Here's a first notebook entry in trying to sort all of this out.

Here's what I have so far:

First, install "matplotlib" and numerical python ("numpy"). Matplotlib is the package that lets us make nice Matlab-style plots, and numpy contains lots of Matlab-like numerical functions. They work together... somehow. On Ubuntu, installation is just one shell command:
sudo apt-get install python-matplotlib python-numpy
As a first simple task, let's plot an Airy function. Here's my equivalent Matlab script:

% Matlab code to plot a cavity resonance

F = 10; % Finesse

f = linspace(-0.5, 1.5, 201);
P = 1./(1 + (2/pi) * F^2 * sin(pi*f).^2);

plot(f, P);
xlabel('free spectral ranges');
ylabel('power buildup');
And now the python:
# Python code to plot a cavity resonance

import numpy as numpy
F = 10
f = numpy.linspace(-0.5, 1.5, 201)
P = 1 / (1 + (2/numpy.pi) * F**2 * numpy.sin(numpy.pi * f) ** 2)

import matplotlib.pyplot as plt
plt.plot(f, P)
plt.xlabel("free spectral ranges")
plt.ylabel("power buildup")
The package names (numpy and plt) make that code a bit verbose and cumbersome. I'm not sure whether it's considered good style, but it's possible to import numpy and the plotting library into the default namespace. The resulting code is almost exactly the same as Matlab, except the power operator is ** instead of ^ and you need to call show() to make the plot appear. Also, the regular division operator seems to work in Python (instead of Matlab's element-wise ./ operator).
# Python!
from numpy import *
from matplotlib.pyplot import *
F = 10
f = linspace(-0.5, 1.5, 201)
P = 1/ (1 + (2/pi) * F**2 * sin(pi*f)**2)
plot(f, P)
xlabel("free spectral ranges")
ylabel("power buildup")
To run that, I just started the regular python interpreter (by typing "python" at a command prompt) and typed it in by hand.

The results:

That's Matlab's plot window on the left, and Python's Matplotlib on the right. Not bad!

[User Picture]From: alexander_mikh
2013-02-27 04:54 pm (UTC)
Have a loot at ipython notebook. I found python is far superior to matlab.

Some useful python links for the same purpose:

IPython Notebook: Finally, the research notebook I have always been looking for is http://www.randalolson.com/2012/05/10/ipython-notebook/



Applied machine learning in Python with sci-learn http://scikit-learn.github.com/scikit-learn-tutorial/

Getting started with Ramp: Detecting insults http://www.kenvanharen.com/2012/11/getting-started-with-ramp-detecting.html

Scikit image kit http://scikit-image.org/

NN and backpropagation with python http://jeremykun.wordpress.com/2012/12/09/neural-networks-and-backpropagation/ and other examples from same author: http://code.google.com/p/math-intersect-programming/downloads/list

(Building heatmap) http://datahackermd.com/2013/language-use-on-github/

Anagram Picture - Real or Fake? http://continuum.io/blog/anagram

Visualise asteroids: http://possiblywrong.wordpress.com/2013/02/10/visualizing-asteroid-2012-da14/
(Reply) (Thread)
[User Picture]From: nibot_lab
2013-02-27 04:56 pm (UTC)
Cool, thanks for the links!
(Reply) (Parent) (Thread)
[User Picture]From: alexander_mikh
2013-02-27 04:55 pm (UTC)
I left comment with a lot of links, automarked as spam - I suggest have a look at them.
(Reply) (Thread)
From: falseanimal
2013-02-28 01:07 am (UTC)
Have you used Python to handle a lot of imported data yet? I've mostly been using Matlab to deal with sensor data but here at LHO they've been pushing for everyone to start scripting in Python.
(Reply) (Thread)
[User Picture]From: nibot_lab
2013-02-28 08:31 am (UTC)
I haven't yet used (numerical) python for anything really. I knew the data analysts were using it, but I didn't know that it was being used in commissioning/assembly/installation at LIGO! Why are they promoting it over Matlab?
(Reply) (Parent) (Thread)
From: falseanimal
2013-02-28 09:47 pm (UTC)
I think they are concerned about the longevity of the code. I think all of the ISI testing is still being done on Matlab but they have been encouraging any new testing to be done with Python to avoid any future issues with Matlab licensing.

It really hasn't taken hold as everyone is most comfortable with Matlab, though.
(Reply) (Parent) (Thread)
[User Picture]From: nibot
2013-03-01 12:54 pm (UTC)
Yeah, if stability is a concern, then I would trust Matlab much more than numerical python.
(Reply) (Parent) (Thread)
[User Picture]From: nibot_lab
2013-02-28 09:01 am (UTC)
BTW, I'm just curious, how you noticed this entry? via Twitter?
(Reply) (Parent) (Thread)
From: falseanimal
2013-02-28 09:49 pm (UTC)
I noticed this entry because I googled GPS time and Matlab. Then I figured out whose blog this is and looked at your more recent entries.
(Reply) (Parent) (Thread)
[User Picture]From: nibot
2013-03-01 12:53 pm (UTC)
Funny, I guess it's a coincidence that you happened to do this just hours after I wrote my first post in several months. (-:
(Reply) (Parent) (Thread)
From: dph3dg
2013-02-28 09:15 am (UTC)
This is quick a good intro to using Python from an astronomer's perspective:
I think he also has a course on ItunesU

If you want something like Matlab as an environment you should also look at:
(Reply) (Thread)