ContentFull

Transitioning to Python from Matlab – Day 2

  • 5.20.10
  • Comments(0)

A great limit to Matlab the very high price for both individual licenses and distributed/parallel computing toolbox. To this python already has several good alternatives. That said, the documentation and capabilities of these options is highly convoluted.

Parallel Python

Parallel Python (PP) is a great start in getting code to run in several computers. It works on a versatile client/server model, where generic servers must be launched to do the actual computations and the user must right a client which interacts with the several servers. The nice thing about this module is how fast you are up and running. Basically you pass arguments, and a function pointer to PP and it handles the distribution and gathering of results. Load balancing, passing extra data, and anything that can’t be done with a single function will be outside the realm of this module.

MPI4Py

MPI is something that I never used, the documentation is sparing, and overall very mysterious. That said, the potential is great. There are several MPI implementations for Python, and I picked this one because it seemed the easiest to install and start using. Essentially when using MPI4Py your script is called in parallel on multiple CPU cores and/or computers and the MPI element makes sure that all the instances talk to each other. What is cool is that variables in your program are both local to an instance, but at the same time can be shared seamlessly with your other instances. The documentation is very good, but the actual MPI standards are very odd and worth learning more about particularly because it can be used with computer clusters.

Transitioning to Python from Matlab – Day 1

  • 5.14.10
  • Comments(0)

People are fast to point out that transitioning from Matlab to Python is easy with NumPy and SciPy. That is, however, far from the case. Having become fed up with the arcane Matlab programming language I figured that the summer would be a great time to move my work to Python.

The setup

My main computer runs Snow Leopard and I have become a stickler to running all my applications in 64bit. Not for performance gains (although this does enable the use of OpenCL which  is 64bit only), but for consistency. Actual computations are left for my schools computer cluster which uses some linux distribution and are also 64bit machines.

  • Python-2.6
  • Matlab 2010a on the mac (64bit)
  • Matlab 7.6 on linux (32+64bit)

Great, but what about my old code

The first hurdle to the transition is making all that old Matlab code work. Once in 1999 someone discovered this problem and wrote PyMat which interfaced Python and Matlab by using Matlab’s shared libraries. While perhaps it still works, the code felt very antiquated. It has been over 10 years since the last update! So I wrote a new version from scratch which was a delight for learning more of the internals of Python.

The nice thing about the module I wrote is that is allows for all matlab functions to be called directly so that:

X = matlab.rand(numpy.array([100 100]), 1)

The following snippet calls Matlab’s native rand function and the result is stored in a numpy array in X. The 1 at the end of the snippet tells the python interface to expect a single return value. In the future this may not be required.

A difficulty in doing this interface is that each Matlab version has slightly different set of function names. This makes the interface have to be slightly tweaked for each version.

Functions that don’t exist

It surprises to see how many helpful functions Matlab has. Particularly saving and opening images and other assorted data types. Numpy while very good doesn’t have a easy to use save routine nor can Numpy natively open arbitrary images. While naturally this is because these features are not part of the project’s mission statement, it does mean more work for the valiant researcher.

Quickly you will see that a handful of functions must be written to do these simple tasks, and while there are plenty of stable modules including PIL and Pickle for opening images and saving data (respectively), it does mean more steps between you and your work.

IDE

Finally by the end of the day you will begin to miss the ugly, Java-esc, Windows-esc and X11-est IDE that Matlab has featured for probably over a decade. Being able to inspect variables, even see the variable names being used, and the ugly but useful debug features is badly missed as well.

By the end

By the end you realize why Matlab exists and is widely used. However, unless someone ventures out and fixes the wholes that exist with Python as a Matlab alternative, people will never have an alternative. Unifying the tools that exist for Python and adding interoperability with Matlab are the first steps to making a really good alternative to the most widely used piece of software in the sciences.

OpenCV 2.0 for OSX 10.6

  • 12.28.09
  • Comments(0)

There are plenty of people lost after discovering that OpenCV no longer fully functions with OSX 10.6. I have been working on making changes to update OpenCV 2.0 to work with 10.6 at 32/64 bit. This meant deprecating the use of Carbon (who uses that anymore anyway) and updating the Quicktime implementation which now must use QTKit framework. Nick Butko was nice enough to have already implemented the Quicktime features, so it was a matter of just merging all the changes.

A fork of OpenCV 2.0 can be found at https://code.google.com/p/opencv-cocoa/ which has both the Cocoa and Quicktime updates. This is completely tested so use it with caution. Any help and suggestions are greatly appreciated.

Because Cocoa functions strictly inside a MVC framework there is a small change that you must make at initializing HighGUI that is found here. It boils down to the fact that your code cannot run in the main thread, because Cocoa needs to run there. So to update any existing projects you must remove your code from the main function and place it in an auxiliary function which is called by cvInitSystem.

I have not yet made any benchmarks, so there is a chance that using Cocoa and QTKit at either 32 or 64 bit is not imperative for improving performance of your code. The main purpose is to allow the usage of other libraries in conjunction with OpenCV which already work at 64bit and may depend on Cocoa.

About

About me goes here...

Archives

Categories