Use matlab code in python or get something similar - python

In my program in need to reconstruct matlab code in python but not in very low level. The problem is that i got following lines of matlab
p = sobolset(problem_size, 'Skip', 1e4, 'Leap', 1e3);
p = scramble(p, 'MatousekAffineOwen');
rand0 = net(p, pop_size);
And I'm not able to reconstruct it on my own. Didn't find python functions in python libraries that would do exacly the same. So my question is if i could easly blend in this matlab code to python code or do someone have other idea how I can deal with it?

Your best bet here is probably to use either MATLAB Compiler SDK to generate a Python package from your MATLAB code or to use MATLAB Engine for Python.
If you are going to share this code with others that don't have a MATLAB installation or if you're running on a machine that doesn't have a MATLAB installation, you CANNOT use MATLAB Engine. Compiler SDK does require you to have that specific license though. If you are licensed through a university, you probably already have access to that license.

Related

Transfering Matlab scripts into python

I have a number of Matlab functions and scripts which have been developed over a couple of years. I want to continue using these scripts, but can't afford MatLab. That's why I would like to transfer them to python. Is there an easy way to do this? I am a beginner in Python and I have Spyder 3.7 installed on my computer.
Thank you!
You can try Octave - an open source free Matlab equivalent.
If you can run your functions and scripts in Octave, then you can call them from within Python using oct2py package in Python.
Look e.g. here.
By that, you wouldn't have to translate any code into another language (like Python), which would mean you could introduce new bugs which then you have to debug etc etc ...

Is there a way to implement fmu export to an c++ based simulation software to use fmi?

I have a simulation software (self written) based in c++ and a simulation software (self written) based in python.
Goal: Co-Simulation of these two software packages
Question: Is there a way to export c++ code to a fmu file. I know there is the FMU SDK package, but I do not think that it serves my needs, if I understood it correctly.
Maybe someone already has some experience creating a fmu file from a self-written c++ code.
If the FMU SDK is already the key for my problem, I have troubles to understand, how it actually works, so I would be very grateful for Your help.
Thanks!
You could use the CMake sources from PythonFMU as a reference on how to do this. Internally PythonFMU works by wrapping a C++ library that utilizes the Python C API into an FMU. You could replace the use of the Python C API with your own code. This project uses CPPFMU, which IMO is easier to understand than FMU SDK.
PS: I'm one of the PythonFMU authors.
Additionally, here is a set of plain C++ demo FMUs using CPPFMU.
Another option is the FMICodeGenerator.

Python equivalent of MATLAB function findchangepts

I need a python equivalent of the MATLAB command findchangepts . I am unable to find one yet. Essentially I want to find K abrupt changes in my time series.
I am able to use the matlab command but unable to find an equivalent function in python. Any help/pointers to existing libraries will be very helpful. I am not proficient in the optimization modules of scipy and thus would prefer an existing python package.
Thanks.
If you cannot use Matlab under the hood there is e.g. ruptures which implements change point detection (docs).
Just in case the answer might help:
I have both MATLAB and Python on my computer. I was able to use MATLAB bindings for python to use findchangepts in my python code.
Source for bindings : MATLAB API for Python

Connecting Matlab to Tensorflow

I am using an open-source Matlab toolbox for brain-computer interface (BCI). I want to send the brain imaging data over to Tensorflow for classification and get the results back to Matlab. Is there any way to pass data structures from Matlab to Tensorflow and get the results back into Matlab?
In case someone lands here with a similar question, I'd like to suggest a Matlab package I am currently writing. It's called tensorflow.m and it's available on GitHub. There's no stable release yet, but simple functionality like importing a frozen graph and running an inference is already possible (see the examples) - this is all you'd need to classify the images in Matlab (only).
The advantage is that you don't need any expensive toolbox nor a Python/Tensorflow installation on your machine. The Python interface of Matlab also seems to be rather adventurous, while tensorflow.m is pure Matlab/C.
I'd be glad if the package can be of use for someone looking for similar solutions; even more so, in case you extend/implement something and open a PR.
So far the best way I found is to run your python module in matlab through matlab's now built-in mechanism for connecting to python:
I wrote my python script in a .py file and in there I imported tensorflow and used it in different functions. You can then return the results to matlab by calling
results = py.myModule.myFunction(arg1,arg2,...,argN)
More detailed instructions for calling user-defined python modules in matlab could be found in the following link:
http://www.mathworks.com/help/matlab/matlab_external/call-user-defined-custom-module.html

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