簡単なmatlab/Octaveスクリプトをpythonスクリプトに変換するツール

12866 ワード

http://stackoverflow.com/questions/9845292/a-tool-to-convert-matlab-code-to-python
これは既存のソースコードの書き換えに役立ちます. 
しかし、プログラミングの観点からは、原生態のほうがいいはずだ.
OMPCは使いやすいですが、Python 2.5のサポートが一番いいです.
SMOPはずっと更新中(github)、これは現在最も良いはずです.Small Matlab Octave Pythonの頭文字の略語.
OctaveとMatlabの代わりにPythonを完全に使うことは期待できない.これも著者の初志ではない.ましてソフトウェアがこんなに小さいからといって、そんなに複雑な仕事を完成するのは難しい.
これはプラットフォームにまたがるコマンドラインで使用されるツールですが、Linuxでしかテストされていません. 
SMOP
SMOP stands for Small Matlab/Octave to Python compiler. It is used to convert Matlab programs to Python.
SMOP is not a polished product, nor a replacement to Octave and Matlab. Taking into account its size (less than 3000 lines), this is not surprizing. There are no toolboxes. Small everyday functions (max, length, etc.) are recognized and supported, but that's all.
SMOP is written in Python, using PLY -- Python Lex/Yacc for lexical analysis and parsing, and numpy for runtime environment. SMOP is platform-independent, but is tested only on Linux. It is a command-line utility.
Example
It is possible to run an example without installing smop. Just unzip it somewhere, and cd there. In your current directory you will find a bunch of .py files and a file named fastsolver.m. It is taken from the winning submission to Matlab programming competition in 2004 (Moving Furniturehttp://www.mathworks.cn/matlabcentral/contest/contests/12/submissions/29989).
Now type python main.py fastsolver.m -o fastsolver.py. If you don't specify the output file with -o option, it is written to a.py. Each time a function is translated, its name is written.
lei@fuji:~/smop/smop$ python main.py fastsolver.m
fastsolver.m
        solver
        cbest
        mainsolver
        imoves
        easysolver
        localfiddler
        findoverlaps
        dijkstra
        improve
        TLL79
        solverA
        solver1
        movefrompos
        onemove
        solver2
        SearchPath
        Faster10IntReps2
        matrixsolver
        outoftheway
        ismember1
        ismember2
        setdiff
        unique
        sub2ind
        randperm
        perms
        itTakesAThief
        movefurniture
        findshortestpath
        dealWall1
lei@fuji:~/smop/smop$

The entire submission contains 2093 lines, and it is automatically translated to Python by smop. These are the good news. The bad news are that generating the code is not enough to run the program, so there are no performance numbers yet.
  • While the submission itself --- the solver program --- does not use graphics, the envelope code that is responsible to run the submission, collect and display the results, does. So about 100 lines of the envelope must be rewritten by hand.
  • Many standard functions are not yet implemented --- rand, find, and others. They are on the issues list.
  • Some matlab constructs, especially creating arrays by out of bound assignment, are used in the submission, but not yet supported by smop. Meanwhile, these lines should be rewritten in the Matlab code.
  • 01 function moves=solver(A,B,w0)
    02 [moves,optmove,optscore]=cbest(A,B,w0);
    03 curscore=sum(w0(moves(:,1)));
    04 lots=1;
    05 if length(moves)-optmove<20||curscore/optscore<1.05
    06     lots=2; return
    07 else
    08     lenw=length(w0);
    09  [xx,nseq]=sort(rand(1,lenw));
    10  A1=A;
    11  B1=B;
    12  w01=w0;
    13  for i=1:lenw
    14      A1(A==i)=nseq(i);
    15      B1(B==i)=nseq(i);
    16      w01(nseq(i))=w0(i);
    17  end;
    18  [moves2,optmove,optscore]=cbest(A1,B1,w01);
    

    becomes
    TBD
    

    The table below tries to summarize various features.
    Implemented features
     
    Lexical and syntactical analysis
    Mostly complete, including some weird Matlab features
    Name resolution
    For each occurrence of a variable, find a set of its possible definitions
    Inlining of small functions
     
    Array subscripts translated from 1-based (Matlab and Fortran style) to 0-based (C and Python style)
    Also, end subscript implemented
    from:step:to translated to from:to:step
     
    Upper bound is n+1
     
    Unimplemented features
     
    Structs
    To be implemented as soon as cc possible.
    Arrays silently become C=style (rows first).
    In some cases it may break the code. Not detected.
    Function handles and lambda expressions
    Handles break the heuristic that tells between function calls and array references.
    Graphics,
    Never
    Auto-expanding arrays
    Unlike other languages, matlab allows out-of-bounds assignment. As MathWorks tries to phase out this feature, there is a lot of legacy code depending on it.
    Sparse matrices
    Have good chances of being implemented, especially taking into account that scipy have several implementations to choose from.
    Full support for boolean indexing. Currently, some expressions don't work
    For example, x(x>0.5) = 1 works, but y=x>0.5; x(y)=1 does not work.
    Command syntax
    Too complex to support
    Type, rank and shape inference
     
    Strings
    up vote
    22 down vote
    accepted
    There are several alternative tools for converting Matlab code to Python code (not tested yet):
    Small Matlab to Python compiler: convert Matlab code to Python code, also developed here:SMOP@chiselapp
    LiberMate: translate from Matlab to Python and SciPy
    OMPC: Matlab to Python (a bit outdated)
    Matlab to Python conversion: No download files available
    Also, for those interested in an interface between the two languages and not conversion: pymatlab : communicate from Python by sending data to the MATLAB workspace, operating on them with scripts and pulling back the resulting data
    Python-Matlab wormholes: both directions of interaction supported
    Python-Matlab bridge: use Matlab from within Python, offers matlab_magic for iPython, to execute normal matlab code from within ipython
    PyMat: Control Matlab session from Python pymat2 : continuation of the appearingly abandoned PyMat. mlabwrap , mlabwrap-purepy: make Matlab look like Python library (based on PyMat) oct2py : run GNU Octave commands from within Python pymex : Embeds the Python Interpreter in Matlab, also on File Exchange matpy : Access MATLAB in various ways: create variables, access .mat files, direct interface to MATLAB engine (requires MATLAB be installed).
    MatPy: Python package for numerical linear algebra and plotting with a MatLab-like interface
    Btw might be helpful to look here for other migration tips:
    http://bci2000.org/downloads/BCPy2000/Migration.html
    On a different note, though I'm not a  fortran  fan at all, for people who might find it useful there is: matlab2fortran
    share
    | edit
    | flag
    edited 
    Mar 20 at 8:26

    Marco
    104
    1
    12
    answered 
    Jul 8 '13 at 20:45

    johntex
    602
    5
    12
     
    add comment