Code Monkey home page Code Monkey logo

matlab2python's Introduction

Build status Donate just a small amount, buy me a coffee!

matlab2python

A python script to convert matlab files or lines of matlab code to python. This project is in alpha phase. This implementation relies heavily on the project SMOP by Victor Leikehman. The current implementation wraps around SMOP, with the following differences:

  • It attempts to produce code that does not rely on libsmop, but only on usual python modules such as numpy.
  • It uses typical shortcuts such as np instead of numpy.
  • It attemps to reindex arrays and loops, starting from 0 instead of 1.
  • It doesn't use the external classes matlabarray and cellarray from libsmop
  • Basic support for Matlab classes is added. The properties declared in the body of the class are initialized in the constructor.
  • As a consequenc of all the above, the resulting code is "less safe" but maybe slightly closer to what a user would write.

This implementation is made straightforward, since it basically use another backend script than the one used by SMOP, here called smop\backend_m2py.py. Some function replacements were added directly there. Additional support for classes, import modules and other fine-tuning replacements (or hacks...) are done in the file matlabparser\parser.py.

Install

The code is written in python, you can access it as follows:

git clone https://github.com/ebranlard/matlab2python
cd matlab2python
# install dependencies
python -m pip install --user -r requirements.txt 
# make the packages available from any directory
python -m pip install -e .  
#Optional: run tests
pytest
#Optional: try it directly
python matlab2python.py tests/files/fSpectrum.m -o fSpectrum.py

Usage

Main script

The main script at the root of the repository is executable and has a couple of command line flags (some of them taken directly from SMOP). To convert the file file.m to file.py, simply type:

python path/to/matlab2python.py file.m -o file.py

where path/to is the path to the script matlab2python.py

From python

The python package can also be used directly to perform conversion of files or lines of code.

import matlabparser as mpars
# --- Convert a matlab file 
pylines = mpars.matlab2python('path/to/file.m', output='file.py')

# --- Convert matlab lines (string or list of strings)
mlines="""# a comment
x = linspace(0,1,100);
y = cos(x) + x**2;
"""
pylines = mpars.matlablines2python(mlines, output='stdout')

Should I use this

If you need a script that performs the obvious conversions from matlab to python, matlab2python will hopefully work for you. These conversions are for instance:

  • syntax (def, if, for, __init__, no more end)
  • indentation
  • parenthesis to brackets
  • simple builtin functions replacements (fprintf, disp, error, fopen)
  • simple numpy replacements like zeros(3,4) to np.zeros((3,4)), or cosd(x), to np.cosd(np.pi/180 x)
  • other misc functions like strcmp, strrep, reshape replaced by their python ,
  • etc

As mentioned above, SMOP does a great job to produce safe code. Yet, neither SMOP nor matlab2python will generate code that is production-ready (it might in some cases). Most of the time, the user will have to go through the code and perform adjustements and some rewritting. In fact, matlab2python will likely be slightly worse than SMOP in producing a code that works out of the box. But at the end, the code produced by matlab2python should require less refactoring and help the user in its conversion. As mentioned by the author of SMOP, it is difficult not to hide the matlab flavor from the code that is generated and it's also difficult to fully convert the code without introducing wrapped classes such that matlabarray. The implemenation of matlab2python attempts to do that, at the price of less safety.

I've written this wrapper script for my own needs. I was ready to convert manually a bunch of matlab scripts, but I thought I could have a script to automate some of the simple conversions and formatting. I started a quick and dirty implementation before discovering SMOP. At the end, I merged my quick and dirty implemenation with the more powerful parsing framework used by SMOP. Hopefully this can be useful to someone else! If so, feel free to contribute.

Contributing

Any contributions to this project are welcome! If you find this project useful, you can also buy me a coffee (donate a small amount) with the link below:

Donate just a small amount, buy me a coffee!

matlab2python's People

Contributors

ebranlard avatar pablo1990 avatar pberger514 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

matlab2python's Issues

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1

Hey guys
I ran matlap2python on a matlab code but it seems like it gives me this exact error:

Traceback (most recent call last):
  File "/home/arshia/Desktop/matlab2python/matlab2python.py", line 49, in <module>
    main(sys.argv[1:])
  File "/home/arshia/Desktop/matlab2python/matlab2python.py", line 44, in main
    mparser.matlab2python(opts.filelist,opts)
  File "/home/arshia/Desktop/matlab2python/matlabparser/parser.py", line 433, in matlab2python
    matlab2python(f,opts)
  File "/home/arshia/Desktop/matlab2python/matlabparser/parser.py", line 437, in matlab2python
    MF=MatlabFile(filename=filename)
  File "/home/arshia/Desktop/matlab2python/matlabparser/parser.py", line 185, in __init__
    self.raw_lines=f.readlines()
  File "/usr/lib/python3.10/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa1 in position 242: invalid start byte

I will upload the matlab file to show what I mean exactly

matlab code.zip

The error seems to have migrated from smop cause smop had the same exact issue

AttributeError: 'DiGraph' object has no attribute 'node' when running

I runned "pip install -e . "to install this and run
"smop xxx.m"
to run smop, but I met this problem.

/home/explcre/anaconda3/lib/python3.9/site-packages/ply/lex.py:760: FutureWarning: Possible nested set at position 65
c = re.compile('(?P<%s>%s)' % (fname, _get_regex(f)), self.reflags)
/home/explcre/anaconda3/lib/python3.9/site-packages/ply/lex.py:498: FutureWarning: Possible nested set at position 118
lexre = re.compile(regex, reflags)
str
Traceback (most recent call last):
File "/mnt/f/Downloads/smop-master/smop-master/smop/main.py", line 66, in main
G = resolve.resolve(stmt_list)
File "/mnt/f/Downloads/smop-master/smop-master/smop/resolve.py", line 54, in resolve
u = G.node[n]["ident"]
AttributeError: 'DiGraph' object has no attribute 'node'
Errors: 1

Would you please tell me how to fix it?

Too many return statements

Hello,

First off let me say thank you for writing this code! It is tremendously helpful to me. I am running into a problem where when the software converts a function it adds multiple return statements. I'm looking through the code to trying to find why but I thought you may be able to point to where I should look.

Thanks!

Opening single quote Exception in comment line

Hi there, thanks for the interesting Matlab/Octave parser.

I plan to add an Matlab/Python analyser which compares the class stucture for Matlab and Python implementation to support porting from one language into another.

I'm getting now an Excpetion 'Opening single quote found, should not happen!' in extract_quotedstring() at line 162 of parsing_tools.py
for the following pure comment line in a Matlab class:
% * '': automatic by file extension: '.zip' or '.snap' as zip

I could try to fix it, however I am not familiar with the code so it could be not optimal. Please let know if you would fix it.

Regards, Reimund

parse.py: name 'ret_expr' is not defined

WARNING: Token 'CLASSDEF' defined, but not used
WARNING: Token 'END_UNEXPECTED' defined, but not used
WARNING: There are 2 unused tokens
Generating LALR tables
/temp/env/lib/python3.10/site-packages/ply/lex.py:760: FutureWarning: Possible nested set at position 65
c = re.compile('(?P<%s>%s)' % (fname, _get_regex(f)), self.reflags)
/temp/env/lib/python3.10/site-packages/ply/lex.py:498: FutureWarning: Possible nested set at position 118
lexre = re.compile(regex, reflags)
Traceback (most recent call last):
File "/temp/env/lib/python3.10/site-packages/smop/main.py", line 61, in main
stmt_list = parse.parse(buf if buf[-1] == '\n' else buf + '\n')
File "/temp/env/lib/python3.10/site-packages/smop/parse.py", line 849, in parse
p = parser.parse(
File "/temp/env/lib/python3.10/site-packages/ply/yacc.py", line 331, in parse
return self.parseopt(input, lexer, debug, tracking, tokenfunc)
File "/temp/env/lib/python3.10/site-packages/ply/yacc.py", line 823, in parseopt
p.callable(pslice)
File "/temp/env/lib/python3.10/site-packages/smop/parse.py", line 696, in p_return_stmt
p[0] = node.return_stmt(ret=ret_expr)
NameError: name 'ret_expr' is not defined
Errors: 1

Contributing

Hi there,

Thanks for this project!

I have a small feature I've added for the TODO for parent objects and ampersands in Matlab. I've tested it on a pretty large Objective Matlab codebase that I'm porting and it works for all the cases I've encountered there. I have it on a feature branch which I tried to push after cloning with Github CLI but I'm getting permission denied. What's the best way for me to PR?

TypeError or ImportError when using

Dear ebranlard,

I successfully downloaded and installed matlab2python, but when I try to use it I get the following errors:
python matlab2python.py file.m
gives
TypeError: 'errors' is an invalid keyword argument for this function
while
python3 matlab2python.py file.m
gives
ImportError: cannot import name 'cached_property' from 'functools' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/functools.py)

Any ideas?

Thanks in advance!

wrong handling of array indexing

First of all, thank you for this nice project.
When I try to convert the following lines,

  x = zeros(4,4)
  y = x(1,:)

I get a python output which is obviously wrong

import numpy as np
x = np.zeros((4,4))
y = x(1,:)

since x is an array and should be indexed using [ ] brackets and python indexing starting from 0.
Is there an easy way to fix? This would be very helpful.

Can't Find Parser.py

After following the installation steps outlined, I receive this error when trying to run:
python matlab2python.py file file.m -o file.py

(smop) c:\Users\user\Downloads\matlab2python-master>python matlab2python.py file pset3.m
Traceback (most recent call last):
File "matlab2python.py", line 49, in
main(sys.argv[1:])
File "matlab2python.py", line 44, in main
mparser.matlab2python(opts.filelist,opts)
File "c:\Users\user\Downloads\matlab2python-master\matlabparser\parser.py", line 433, in matlab2python
matlab2python(f,opts)
File "c:\Users\user\Downloads\matlab2python-master\matlabparser\parser.py", line 436, in matlab2python
raise Exception('FileNotFound:'+filename)
Exception: FileNotFound:file

The folder structure is the exact same as it is on GitHub... what did I do wrong?

TypeError: 'numpy.ndarray' object is not callable matlab2python

Dear ebranlard,
Thank you for making this amazing tool.
We have almost everything converted out-of-the-box with minor bearable changes.

Issue:
The recognizing of a matlab array or matrix is needed many times.
Matlab2Python, currently, doesn't recognize and thus takes slicing as a function call.
This probably makes it not recognize the index change that is needed as well.

Recreation:

test.m

x = [1:4,11:14];
y = x(1,1);
disp(y);

test.py

import numpy as np
x = np.array([np.arange(1,4+1),np.arange(11,14+1)])
y = x(1,1)
print(y)

y = x(1,1), at run, will cause the following error
TypeError: 'numpy.ndarray' object is not callable

Will be glad for a fix or a direction towards what to change in the code.

Best Regards,
SDido

Function Definition Indent Bug

Hello, thanks for a very useful language conversion tool. I have identified a bug where the definition of an output function is not indented, resulting in an Indentation Error. I tested the conditional statement indent and it worked as expected. Please see the below test using matlabparser/parser.py:

file.m
% if statement indent test
if true
    disp("if statement")
end

% function test
function ret = add_one(a)
    ret = a + 1;
end
x = add_one(2)
file.py
 # if statement indent test
if True:
    print('if statement')
 
# function test
def add_one(a = None):
ret = a + 1
return ret
x = add_one(2)

I will look more into this issue if it is found to be reproducible.

Matlab and OpenFAST related issues

Dear sir, l'm sorry to bother you. l'm a student from China. We are working on a project. Now their are some problems. I know you're an expert at this, so l'd like to consult with you. OpenFAST has a dynamic link library Hydro-dynamics, which can directly call the calculation, but it is DLL. How can the dynamic link library be called in Matlab, or how can it be used in Matlab?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.