Sunday, March 24, 2013

Python, MATLAB and legacy interop.

I promised that this space would see more of my side projects and random recreational efforts, so today, I'd like to introduce another of my stray software libraries: pymex. This library embeds the CPython 2.7 interpreter into MATLAB using MEX, so that Python software may be integrated into legacy MATLAB applications. (I realize I'm showing my bias here, but I consider MATLAB development to be so badly broken as to be legacy, irrespective of when a MATLAB application was made.)

In particular, pymex exposes Python to MATLAB via the py_eval.m function, which evaluates a MATLAB string as a line of Python as if passed to Python eval() inside the embedded interpreter's __main__ module. Moreover, MATLAB code can access the Python __main__ module by using py_eval.m and py_put.m; the resulting Python objects are abstracted within MATLAB by the PyObject.m class (currently very incomplete), which translates MATLAB operators and methods into calls to the corresponding CPython API functions.

If the called Python code needs to execute something back within MATLAB, then this can be accomplished by importing the pymex extension module exposed by pymex_fns.c and calling pymex.mateval(). The pymex extension module also exposes other functionality, such as pymex.get(), which allows Python code to dynamically extract variables from MATLAB workspaces.

Of course, all this leaves the question: why bother? With more and more hardware being exposed not just through C APIs, but also with Python modules, pymex can help in getting legacy MATLAB applications communicating with cutting-edge hardware. For example, I have recently discussed Galvant Industries in various places— their GPIB/USB adapter ships with an impressive array of Python modules for controlling common instruments.

The other main advantage of embedding Python into MATLAB is that legacy MATLAB applications can then gain access to the wealth of scientific Python code that has been written as of late. For example, I hope to bring tools such as the excellent SciKit-Learn and QuTIP2 libraries into the world of MATLAB. I have before collaborated in the creation of Python-language scientific tools for use in quantum information, including QuaEC and Qinfer, that also could potentially be of use to those developing MATLAB applications.

While none of this is anywhere near done, polished or even well-documented yet, I thought I'd share a bit of my software hackery with the world. With a bit of luck, this project will help make interop nightmares a bit less pronounced and increase the range of tools available for scientific development and hardware control.

No comments: