Can I interact with a running Python session from MATLAB? - python

I have a Fortran library wrapped to Python (with f2py), and I want to call the library from MATLAB (2016b). Although it's possible to call the Fortran library from MATLAB using MEX files, to save some work I'm hoping it's possible to call it using the Python interface. I know that one can call Python code from MATLAB (e.g. py.calendar.isleap(2016)), but unfortunately it seems that this only gives access to standard Python libraries.
Since the library has initialization cost, I prefer to initialize once and maintain a running Python session, and write MATLAB wrappers for library initialization and the library routines I wish to call. I'm not entirely sure how the MATLAB and Python sessions can best communicate, but I guess writing and reading temporary text files is a viable option.
Any suggestions for a good way to maintain an active Python session in MATLAB, and pass input to Python and output back to MATLAB?

Related

how to call matlab function in python script?

All my previous work (some big functions) are programmed by Matlab (some .m files). Now I get some robots of which the embedded functions are programmed by python. I want to apply my Matlab functions into the robots, but it will be very hard to recode the functions by python. I find two methods:
call matlab function in python script: it need the Matlab environment, but the robot is independent of my PC when it works. I can not install Matlab in the robot.
translate matlab to python. I installed numpy and scipy, and tried to configure the tool SMOP (under Win7), but there is always an import error: no module named readline.
Another possibility is to write an interface with C to call the .M function, and another interface of C called by python, and then link these two interfaces. But I have no idea with this work.
So I can not continue my work. Can you give me some advice? thank you.

Rewrite part of a c/c++ program in python

I am trying to develop a plugin interface in python for a popular 3d program, called Blender 3D. The purpose of the plugin interface is to extend some functionality of the program through python.
Now my problem is that I am trying to asses the performance impact. I will replace an existing functionality written in c code with something written in python.
I am worried that this might slow the application because the functionality that I am replacing is executed in real time and has to be really fast. It consists of a plain c function that just splits some polygons in triangles.
So the operations that I executing work on pieces of data that usually do not have more than 30 or 40 input points. And at most the operations that I am executing on them have a complexity of log(n) * n^2.
But I will be creating plenty of python objects each second, so I am already prepared to implement pooling to recycle the objects.
Now I am mostly worried that the python code will run 100 times slower than the c code and slow down the application. Should I be worried?
At most I will be doing 8500 computations in a single python function call. This function will be called each time when rendering the application interface.
The question of using c or python will depend on the use of your work. Is this a function that the blender developers will accept into the blender development? Do you expect many blender users will want to use it? A python addon allows you to develop your work outside of the main blender development and give many users access to it, while a patch to the c code that requires the user to compile their own version will reduce users.
You could also look at compiling your c code to a binary library that is included with the python addon and loaded as a python module. See two addons by Pyroevil created using cython - molecular and cubesurfer, some pre-built binaries are available on his main website. I'm not sure if using cython makes the python module creation easier or not, you could also use cython only as glue between python and your library.

What is the best way to interface C++ code with Python?

I am trying to create an application in which the GUI is programmed in Python (Tkinter) and I have a library in C++ that I want to interface with this GUI code. (Please, no comments on why the GUI and the application library are in separate languages).
One approach that immediately comes to mind is to compile the C++ library into an executable and write python wrappers that call this executable ( via system() ) with specific arguments and consume the output.
I am not sure what the performance implications are for such an implementation. Also I do not want to change the library into Python. Any better suggestions or comments on the approach?
There are several ways for doing this. One obvious way was already stated by chis. Another good way of interfacing C++ with Python is by using swig. It all comes down how complex your structures / classes are.
So the C++ code is going to be a module in python and can be called by the interface as any other python function.

How to run an executable from Python?

I have an executable (face recognition) written by one of our programmers who's no longer with us. It takes a commandline argument and prints the result to STDOUT like this.
I need to be able to use this in Python but performance is important so I'm not sure if I should just use subprocess. I need to capture the output of the program so is it a better idea to modify the code to make it a C++ library instead and write a wrapper for it in Python? How would that impact performance compared to using subprocess?
I have looked into the Python docs for running C code and found this but the answer here is different. The second method does not even use Python.h so I'm a little confused. What's the best way to do what I need when performance is important?
At first I have to tell you I think subprocess method and running external application and reading output from STD output would be slow and unreliable. I won't recommend that method.
Instead, here is some suggestions:
You can create a Python extension in C or C++, you can find a lot of resources online about it. So you can convert your current code to Python extension format and just use Python extension skeleton template, integrate your functions and code in it and you'll have a python extension, simply you'll be able to call it's functions from Python code. See: http://docs.python.org/2/extending/index.html
Convert your current C++ code directly into a DLL and exports functions of your code you want to call from python if you are in Windows or simply convert it to a shared library if you are using Linux. It will be compiled to .so . Then you can call functions as an external library in your Python code. Example with explanation: http://www.linuxforu.com/2010/05/extending-python-via-shared-libraries/
I've never tried them, but it seems these projects can help you with what you want:
http://sourceforge.net/projects/pygccxml/?source=dlp
https://code.google.com/p/pybindgen/
http://www.cython.org/

Creating a shared library in MATLAB

A researcher has created a small simulation in MATLAB and we want to make it accessible to others. My plan is to take the simulation, clean up a few things and turn it into a set of functions. Then I plan to compile it into a C library and use SWIG to create a Python wrapper. At that point, I should be able to call the simulation from a small Django application. At least I hope so.
Do I have the right plan? Are there are any serious pitfalls that I'm not aware of at the moment?
One thing to remember is that the MATLAB compiler does not actually compile the MATLAB code into native machine instructions. It simply wraps it into a stand-alone executable or a library with its own runtime engine that runs it. You would be able to run your code without MATLAB installed, and you would be able to interface it with other languages, but it will still be interpreted MATLAB code, so there would be no speedup.
Matlab Coder, on the other hand, is the thing that can generate C code from Matlab. There are some limitations, though. Not all Matlab functions are supported for code generation, and there are things you cannot do, like change the type of a variable on the fly.
I remember that I was able to wrap a MATLAB simulation into a DLL file and then call it from a Delphi application. It worked really well.
I'd also try ctypes first.
Use the MATLAB compiler to compile the code into C.
Compile the C code into a DLL.
Use ctypes to load and call code from this DLL
The hardest step is probably 1, but if you already know MATLAB and have used the MATLAB compiler, you should not have serious problems with it.
Perhaps try ctypes instead of SWIG. If it has been included as a part of Python 2.5, then it must be good :-)

Categories