FFMPEG: png to mpeg

I have been lucky enough to have a chance to do some of my more basic data processing in Python (with numpy/scipy/matplotlib). One of the things I’ve been working on is the effect of interference between the direct path and surface bounce arrivals from a fin whale that is near the surface. Hopefully I’ll have a post dedicated to that problem in particular, but for the time being, I’ve been trying to figure out the best way to animate a series of plots output each time the code goes through a loop.

There are a few options for doing this. From what I can gather, it’s not something that is easily done within Python (someone correct me if that’s wrong!). So instead, I’m creating a series of image files (.png’s), and converting to video outside Python. I looked at a couple of options and settled on ffmpeg, which seemed really simple.

I grabbed it from fink:

fink install ffmpeg

All of my image files were saved in a single folder, with names like 001.png, 002.png, … 198.png. Converting them to video format:

ffmpeg -i %03d.png -vb 1024K video.mpg

The -i flag is for input file(s) and -vb is video bitrate. Here’s the output:

Adventures in ObsPy

I finally got around to trying out ObsPy… well, okay, I am still trying to figure out how to install it properly. Finally made some headway though. I followed the instructions on the ObsPy installation page.  I thought everything was going smoothly.  For starters, I had all of the dependencies – most importantly Python, SciPy, NumPy, and Matplotlib.  I also had the Easy_install package already from when I was installing Mercurial.

The installations all seemed to go just fine, I was getting messages telling me it was all good.  But then it just didn’t work.  I opened up iPython, tried to give it a whirl, and it just didn’t recognize those packages at all.  Turns out the default installation location was not actually in the Python path.  It sounds like it should be pretty easy to fix, but it took me a long time to figure it out.

So, normally, if you want to import from a Python library, you would use something like this:

from numpy import *

But when I tried importing obspy.core, like this:

from obspy.core import read

it couldn’t find the library. After much searching, I finally figured out how to add to my python path. First, say in iPython, import sys, and check the path:

import sys

This will list what’s in the path. So for me, it was:


To add to that path, I just used:


And it worked! Although I’m still not sure how to change it permanently. But until I figure that out, at least I can play with ObsPy.

53 New Posts

So, you might have noticed (okay, probably not) that I am now on post number 159.  Yup, that’s a big jump from the 100-post mark a few days ago.  How did I do that, you ask?  Well, for one, I finally realized that I could still import posts from my original WordPress.com blog (duh.  I’ve even done it before).  So I added those 53 posts from late 2009/early 2010 – sweet!  Now you can search back through the glorious dorkiness of my pre-grad school days.  Well, actually my between grad school days.  I used to be so into linux and python.  What happened?  I am going to make an effort to get back into it.  What better time then summer, during the few sunny days we get here in Seattle.

Kim was asking me lots of questions about Python and Linux this afternoon before she left.  It made me realize just how much I’ve forgotten (or how little I knew to begin with).  And it was a good kick in the butt to remind me to get back to learning about, and embracing, the world of Ubuntu and Numpy and Matplotlib…  Nerdy posts will resume!  But since a few people very dear to me enjoy my daily sketches, I will endeavor to continue those as well.  (I love you guys!)

Interpolation using Scipy/Numpy

As part of a little script I’m writing, I need to do some simple linear interpolation. The Matlab equivalent of what I’m doing is called ‘interp1’. So far I’ve come across two ways to do 1-d, linear interpolation. One is ‘interp’ in numpy. The other is ‘interp1d’ in scipy.interpolate. I’m not completely sure of the difference. Is one faster? Is one older? I’ve googled around a bit, but still haven’t found a clear answer. For now, I’ve implemented ‘interp1d’, which is less similar to Matlab’s ‘interp1’. You first define an interpolated object given your x and y vectors. Then you call that object with your new x-values to generate the new y-values.

And here is an example, chopped out of my code:

[sourcecode language=”python”]
depthvector1 = r_[0:nadirdepth:DepthIncrement]
interpfun1 = interp1d(depth1,ssp1) # my x and y vectors
sspvector1 = interpfun1(depthvector1) # find the new y’s for this x

“Repmat” function in Numpy/Scipy?

I’m such a Matlab user. I just want to repeat an array or matrix X times, which I can do easily in Matlab using ‘repmat’. I’m sure this is super easy, and I’m just not seeing something obvious. Any tips? Is it something like concatenating copies of an array with itself? vstack?

Hey – I just found what I was looking for: I can do what I need to do here using the ’tile’ function in numpy. It works like this:

from numpy import *

x = array([1,2]) # create a simple array
bigx = tile(x,(2,1))

And the output is:

Awesome! Now I can get rid of those pesky nested loops!

Also something interesting that I just discovered while messing with this in iPython: even if ‘x’ in this example is not created as an array (say it’s a list or a tuple) – it doesn’t matter – numpy still understands it, and then outputs an array object! Amazing. 🙂

Python – matrices or arrays?

Sometimes I think I have my biggest breakthroughs while biking home. Tonight, for instance, it dawned on me (on the stretch between the beach and Patterson) just how awful the Matlab version of my ray tracing code really is. And I realised that I could vastly improve its simplicity and efficiency by using matrices (Yikes! Why on earth did I implement all of those nested loops?). I pictured the steps and the math involved, the Matlab commands that I would need, and got really excited – until I realized that I don’t know how to do any of the same stuff in Python yet. First of all, I need to figure out how to work with arrays in Matlab. Thankfully, there’s a special page on Scipy.org Matlab users who are trying to migrate to Numpy/Scipy. Yes! And here it is: Numpy for Matlab Users. My first question was, “How do I make it do element-wise operations?” In Matlab, you need to modify the operator, for example using “.*” to multiply by elements, and just “*” to do matrix multiplication. Matlab thinks in linear algebra for 2d matrices. But, as I quickly learned, the default array in Numpy does element by element operations, unless you create a special matrix object. Awesome.

Installing SciPy and Matplotlib

Hm.  Well, in all the excitement of installing NumPy, and configuring Emacs for Python, I neglected to actually install SciPy.  I thought I was going to have to do something pretty complicated, but it turns out that SciPy was available through Synaptic.  That makes my life much easier.  I also never got around to installing Matplotlib…also found in Synaptic!  I guess that the versions that come with the OS’s aren’t always the most up-to-date and stable, but I’m going to go ahead and take that risk in the interest of moving forward in my quest.  (whatever that is).

I think I like Matplotlib and iPython because they are both somewhat reminiscent of Matlab (my favorite!).  Why would I want to go to all this trouble to figure out Matplotlib and iPython, Numpy and Scipy?  Because they are FREE!  And it’s fun to figure it all out.  Keeps me entertained.

Learning Python (the book)

I just picked up my second python book – although this is probably the one I should have bought first.  It’s ‘Learning Python’ from O’Reilly.  I already had Beginning Python.  I’ve been wanting to get the O’Reilly one for a while now, but never really wanted to spend the 50 bucks.  I finally broke down and got it at Borders this morning.  I hope it’s a good reference.  The first thing I’d like to do is to remember how to do things like read files, parse data, create plots.  I think I remember having to add Scipy and Matplotlib.

If I want Scipy, I need to also get Numpy.  In the Scipy/Numpy installation instructions for Ubuntu, it gives a couple of options for installation.  One of these is from a guy called Andrew Straw, who has an unofficial repository for NumPy .deb packages.  I’m going to try that first.  I just checked out his repository and he just updated for Karmic on 2 December.  I’m using the instructions here.