简体   繁体   English

如何从 python 访问 matlab/octave 模块?

[英]How can I access a matlab/octave module from python?

I am looking for a way to access a matlab module from python.我正在寻找一种从 python 访问 matlab 模块的方法。 My current situation is this:我现在的情况是这样的:

  • I have a python code that does numerical computations by calling Lapack routines while the memory is allocated as ctypes and passed as pointers to the Lapack routines.我有一个 python 代码,它通过调用 Lapack 例程进行数值计算,而 memory 被分配为ctypes并作为指向 Lapack 例程的指针传递。
  • I also have a matlab module, which is compatible with octave, that does some mathematical tricks I want to use.我还有一个 matlab 模块,它与八度音程兼容,可以做一些我想使用的数学技巧。

My question now is this:我现在的问题是:
What is an efficient way to keep all the main work in python while at the same time exploit the possibilities that matlab/octave modules offer.什么是在 python 中保留所有主要工作的有效方法,同时利用 matlab/octave 模块提供的可能性。 Also it would be kind of nice, if my ctype arrays do not have to be converted into some other object in order to run octave.如果我的 ctype arrays 不必转换为其他一些 object 以运行八度音程,那也很好。 However, I can see that that last point is hard to accomplish.但是,我可以看到最后一点很难实现。

My current research shows me two possible options:我目前的研究向我展示了两种可能的选择:

  1. Pytave : However it seems that this packages is kind of pre alpha?! Pytave :但是,这个包似乎有点像 pre alpha?!
  2. Go this humpy road: ctypes -> *.mat file (via numpy) -> octave -> *.mat file -> ctypes (via numpy) Go 这条崎岖之路:ctypes -> *.mat 文件(通过 numpy)-> octave -> *.mat 文件 -> ctypes(通过 numpy)

You can use oct2py , which IIUC was started by its author because pytave didn't work on win32.您可以使用oct2py ,IIUC 是由其作者启动的,因为 pytave 在 win32 上不起作用。 It is successfully used in IPython through its octavemagic extension and I can tell it is easy to use on its own, the code is maintained (I reported a little Unicode bug and the author fixed it in a day) and works well.它通过其octavemagic 扩展在 IPython 中成功使用,我可以说它很容易单独使用,代码得到维护(我报告了一点 Unicode 错误,作者在一天内修复了它)并且运行良好。 Most of the times is as simple as:大多数时候很简单:

>>> from oct2py import octave
>>> octave.run("cos(pi / 3)")
'ans =  0.50000'
>>> octave.call("foo", a, b)  # Function in foo.m

For further examples you can check this blog article .有关更多示例,您可以查看此博客文章

Have you considered using OMPC, http://ompc.juricap.com/ ?您是否考虑过使用 OMPC, http://ompc.juricap.com/ I have used it with great success when not wishing to re-write some numerical linear algebra routines.当我不想重写一些数值线性代数例程时,我使用它取得了巨大的成功。 I can imagine that the more esoteric the Matlab commands, the harder it would be to translate... but it might be worth a try.我可以想象,Matlab 命令越深奥,翻译起来就越困难……但可能值得一试。 In the end, you're going to want to convert your Matlab code to Python because it will be a bottleneck on speed and performance.最后,您需要将 Matlab 代码转换为 Python,因为这将成为速度和性能的瓶颈。 The only reason to leave the Matlab code in Matlab format is if it would be an enormous up-front cost to translate it all, which OMPC should mitigate somewhat.将 Matlab 代码保留为 Matlab 格式的唯一原因是,如果将其全部翻译成巨大的前期成本,那么 OMPC 应该会有所减轻。 Otherwise, it's almost always worth that up-front cost to completely rid yourself of Matlab/Octave dependence.否则,完全摆脱对 Matlab/Octave 的依赖几乎总是值得的。

I had some trouble getting OMPC to work because (I) the md5 module is deprecated, (II) Python 2.6 and later no longer accept arguments for Object.__new__() or Object.__init__(), and (III) the byteplay.py script needed to be updated. I had some trouble getting OMPC to work because (I) the md5 module is deprecated, (II) Python 2.6 and later no longer accept arguments for Object.__new__() or Object.__init__(), and (III) the byteplay.py脚本需要更新。

To solve issue (I), I changed line 74 of yacc.py found in the ompc/ directory.为了解决问题 (I),我更改了 ompc/ 目录中的 yacc.py 的第 74 行。 This line imports md5 among other things.此行导入 md5 等。 I deleted the md5 module and added the line below:我删除了 md5 模块并添加了以下行:

from hashlib import md5

Later in the yacc.py script, at line 1160, I changed,后来在 yacc.py 脚本中,在第 1160 行,我更改了,

Signature = md5.new()

to the following,到以下,

Signature = md5()

To run the code generated by ompcply.py, add 'from ompc import *' to the beginning of the file and then run it with an earlier version of Python, as:要运行 ompcply.py 生成的代码,请将“from ompc import *”添加到文件的开头,然后使用早期版本的 Python 运行它,如下所示:

$ python2.5 ompcply.py script.m > newscript.pym
$ python2.5 newscript.pym

Using a version of Python later than 2.5 will give you the following error:使用高于 2.5 的 Python 版本会出现以下错误:

/home/connor/downloads/OMPC/ompclib/ompclib_numpy.py:66: DeprecationWarning: object.__new__() takes no parameters
  a = super(mvar, cls).__new__(cls, *args, **kwargs)

To solve issue (III) I googled byteplay, and replaced the existing script with the newer version.为了解决问题(III),我搜索了 byteplay,并将现有脚本替换为较新的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM