• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ebranlard/matlab2python: Simple matlab2python converter

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

ebranlard/matlab2python

开源软件地址(OpenSource Url):

https://github.com/ebranlard/matlab2python

开源编程语言(OpenSource Language):

Python 98.8%

开源软件介绍(OpenSource 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
python -m pip install --user -r requirements.txt

Usage

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 matlab2python.py file.m -o file.py

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

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!




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap