在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):ewiger/mlab开源软件地址(OpenSource Url):https://github.com/ewiger/mlab开源编程语言(OpenSource Language):Python 100.0%开源软件介绍(OpenSource Introduction):mlabMlab is a high-level python to Matlab® bridge that lets Matlab look like a normal python library. This python library is based on the work of original mlabwrap project http://mlabwrap.sourceforge.net/ and Dani Valevski (from Dana Pe'er's lab): PrimerQuick installation: pip install mlab Start working with the library by picking a MATLAB release that you have locally installed: from mlab.releases import latest_release as matlab from matlab import matlabroot print matlabroot() where latest_release is a MlabWrap instance, matlabroot is wrapper around MATLAB function. Please note that matlab module is dynamically created instance, which is in this case referencing latest_release object. MATLAB installation discovery mechanism is implemented by mlab.releases module in such a way, that you have to specify the release version you want to use first, by importing it. Only then you can import from matlab module: from mlab.releases import R2010b as matlab from matlab import matlabroot Also see mlab.releases.get_available_releases(). Original READMENOTE Below is the original README from the mlabwrap project. Most of it still applies, but the underlying implementation is different (COM/Pipes replaced the use of the MATLAB Engine API). The code samples have been updated for use with this project. Contents DescriptionMlabwrap is a high-level python to Matlab® bridge that lets Matlab look like a normal python library.
mlab is a repackaging effort to make things up-to-date. RelatedThereis is a copy of mlabwrap v1.1-pre (http://mlabwrap.sourceforge.net/) patched as described here: http://sourceforge.net/mailarchive/message.php?msg_id=27312822 with a patch fixing the error: mlabraw.cpp:225: *error*: invalid conversion from ‘const mwSize*’ to ‘const int*’ Also note that in Ubuntu you need to For details see http://github.com/aweinstein/mlabwrap News2014-08-26 1.1.3 Applying patch to add support for Windows via COM. Credits to Sergey Matyunin, Amro@stackoverflow 2013-07-26 1.1.1 Repacking a library as mlab project. Including code for Windows (matlabraw.cpp is off for now). 2009-10-26 1.1 fixes an incorrect declaration in 2009-09-14 1.1-pre finally brings N-D array support, thanks to Vivek Rathod who joined the project! Also fixed a missing import for saveVarsInMat (thanks to Nicolas Pinto). Since a few people have run into problems that appear to relate to compiling Matlab® C-extensions in general and aren't mlabwrap-specific, I should probably stress that in case of any problems that look C-related, verifying whether engdemo.c works is a great litmus test (see Troubleshooting ). 2009-03-23 1.0.1 is finally out. This is a minor release that fixes some annoying but mostly minor bugs in mlabwrap (it also slightly improves the indexing support for proxy-objects, but the exact semantics are still subject to change.)
Many thanks to Iain Murray at Toronto and Nicolas Pinto at MIT for letting themselves be roped into helping me test my stupidly broken release candidates. Licensemlab (and mlabwrap) is under MIT license, see LICENSE.txt. mlabraw is under a BSD-style license, see the mlabraw.cpp. Download<http://github.com/ewiger/mlab> Installationmlab should work with python>=2.7 (downto python 2.2, with minor coaxing) and either numpy (recommended) or Numeric (obsolete) installed and Matlab 6, 6.5, 7.x and 8.x under Linux, OS X® and Windows® (see OS X) on 32- or 64-bit machines. LinuxIf you're lucky (linux, Matlab binary in python setup.py install (As usual, if you want to install just in your homedir add If things do go awry, see Troubleshooting. WindowsAssuming you have python 2.7.5 (e.g. C:Python27) and setuptools ("easy_install.exe") installed and on your PATH. 1) Download and install numpy package. You can use packages provided by Christoph Gohlke: http://www.lfd.uci.edu/~gohlke/pythonlibs/ Also see official SciPy website for latest status, it might that: easy_install.exe numpy would do the trick.
easy_install.exe pywin32 also see Windows in Troubleshooting. Documentation
Tutorial[This is adapted from an email I wrote someone who asked me about mlabwrap.
Compatibility Note: Since matlab is becoming increasingly less
Legend: [...] = omitted output Let's say you want to do use Matlab® to calculate the singular value
decomposition of a matrix. So first you import the >>> from mlab.releases import latest_release as matlab
>>> import numpy Now you want to find out what the right function is, so you simply do: >>> matlab.lookfor('singular value')
GSVD Generalized Singular Value Decompostion.
SVD Singular value decomposition.
[...] Then you look up what >>> help(matlab.svd)
mlab_command(*args, **kwargs)
SVD Singular value decomposition.
[U,S,V] = SVD(X) produces a diagonal matrix S, of the same
dimension as X and with nonnegative diagonal elements in
[...] Then you try it out: >>> matlab.svd(array([[1,2], [1,3]]))
array([[ 3.86432845],
[ 0.25877718]]) Notice that we only got 'U' back -- that's because python hasn't got something like Matlab's multiple value return. Since Matlab functions can have completely different behavior depending on how many output parameters are requested, you have to specify explicitly if you want more than 1. So to get 'U' and also 'S' and 'V' you'd do: >>> U, S, V = matlab.svd([[1,2],[1,3]], nout=3) The only other possible catch is that Matlab (to a good approximation) basically represents everything as a double matrix. So there are no scalars, or 'flat' vectors. They correspond to 1x1 and 1xN matrices respectively. So, when you pass a flat vector or a scalar to a mlab-function, it is autoconverted. Also, integer values are automatically converted to double floats. Here is an example: >>> matlab.abs(-1)
array([ [ 1.]]) Strings also work as expected: >>> matlab.upper('abcde')
'ABCDE' However, although matrices and strings should cover most needs and can be directly converted, Matlab functions can also return structs or indeed classes and other types that cannot be converted into python equivalents. However, rather than just giving up, mlabwrap just hides this fact from the user by using proxies: E.g. to create a netlab neural net with 2 input, 3 hidden and 1 output node: >>> net = matlab.mlp(2,3,1,'logistic') Looking at >>> net
<MLabObjectProxy of matlab-class: 'struct'; internal name: 'PROXY_VAL0__';
has parent: no>
type: 'mlp'
nin: 3
nhidden: 3
nout: 3
nwts: 24
outfn: 'linear'
w1: [3x3 double]
b1: [0.0873 -0.0934 0.3629]
w2: [3x3 double]
b2: [-0.6681 0.3572 0.8118] When >>> net = matlab.mlptrain(net, [[1,1], [0,0], [1,0], [0,1]], [0,0,1,1], 1000) And test with: >>> matlab.mlpfwd(net2, [[1,0]])
array([ [ 1.]])
>>> matlab.mlpfwd(net2, [[1,1]])
array([ [ 7.53175454e-09]]) As previously mentioned, normally you shouldn't notice at all when you are working with proxy objects; they can even be pickled (!), although that is still somewhat experimental. mlabwrap also offers proper error handling and exceptions! So trying to pass only one input to a net with 2 input nodes raises an Exception: >>> matlab.mlpfwd(net2, 1)
Traceback (most recent call last):
[...]
mlabraw.error: Error using ==> mlpfwd
Dimension of inputs 1 does not match number of model inputs 2 Warning messages (and messages to stdout) are also displayed: >>> matlab.log(0.)
Warning: Log of zero.
array([ [ -inf]]) Comparison to other existing modulesTo get a vague impression just how high-level all this, consider attempting to do something similar to the first example with pymat (upon which the underlying mlabraw interface to Matlab® is based). this: >>> A, B, C = matlab.svd([[1,2],[1,3]], 0, nout=3) becomes this: >>> session = pymat.open()
>>> pymat.put(session, "X", [[1,2], [1,3]])
>>> pymat.put(session, "cheap", 0)
>>> pymat.eval(session, '[A, B, C] = svd(X, cheap)')
>>> A = pymat.get(session, 'A')
>>> B = pymat.get(session, 'B')
>>> C = pymat.get(session, 'C') Plus, there is virtually no error-reporting at all, if something goes wrong in
the However should you need low-level access, then that is equally available
(and with error reporting); basically just replace >>> from mlab.releases import latest_release as matlab
>>> from mlab import mlabraw
>>> mlabraw.put(matlab._session, "X", [[1,2], [1,3]])
[...] Before you resort to this you should ask yourself if it's really a good idea;
the inherent overhead associated with Matlab's C interface appears to be quite
high, so the additional python overhead shouldn't normally matter much -- if
efficiency becomes an issue it's probably better to try to chunk together
several matlab commands in an What's Missing?
Implementation NotesSo how does it all work? I've got a C extension module (a heavily bug-fixed and somewhat modified version of pymat, an open-source, low-level python-matlab interface) to take care of opening Matlab sessions, sending Matlab commands as strings to a running Matlab session and and converting Numeric arrays (and sequences and strings...) to Matlab matrices and vice versa. On top of this I then built a pure python module that with various bells and whistles gives the impression of providing a Matlab "module". This is done by a class that manages a single Matlab session (of which TroubleshootingStrange hangs under Matlab® R2008aIt looks like this particular version of matlab might be broken (I was able to
reproduced the problem with just a stripped down matlab not in path
"Can't open engine"If you see something like mex -f /opt/MatlabR14/bin/engopts.sh engdemo.c ./engdemo if you get The code I'd run (from within Matlab) is... > mex -setup; # then select: 2 - gcc Mex options > optsfile = [matlabroot '/bin/engopts.sh']; > mex -v -f optsfile 'engdemo.c'; > !./engdemo; Update John Bender reports that under unix csh needs to be installed in
"`GLIBCXX_3.4.9' not found" on importing mlab (or similar)As above, first try to see if you can get engdemo.c to work, because as long as even the examples that come with Matlab® don't compile, chances of mlabwrap compiling are rather slim. On the plus-side if the problem isn't mlabwrap specific, The Mathworks® and/or Matlab®-specific support forums should be able to help. Old Matlab versionIf you get something like this on mlabraw.cpp:634: `engGetVariable' undeclared (first use this function) Then you're presumably using an old version of Matlab (i.e. < 6.5);
OS XJosh Marshall tried it under OS X and sent me the following notes (thanks!). Notes on running
WindowsI'm thankfully not using windows myself, but I try to keep mlabwrap working under windows, for which I depend on the feedback from windows users. Since there are several popular C++ compilers under windows, you might have to tell setup.py which one you'd like to use (unless it's VC 7). George A. Blaha sent me a patch for Borland C++ support; search for "Borland C++" in setup.py and follow the instructions. Dylan T Walker writes mingw32 will also work fine, but for some reason (distuils glitch?) the following invocation is required: > setup.py build --compiler=mingw32 > setup.py install --skip-build Function Handles and callbacks into pythonPeople sometimes try to pass a python function to a matlab function (e.g.
Directly manipulating variables in Matlab® spaceIn certain (rare!) certain cases it might be necessary to directly access or
set a global variable in matlab. In these cases you can use Support and FeedbackPost your questions directly on Stack overflow with tags CreditsYauhen Yakimovich is maintaining the current mlab branch https://github.com/ewiger Amro for recent patch to matlab discovery via COM on Windows https://github.com/amroamroamro Alejandro Weinstein for patches of 1.1pre https://github.com/aweinstein/mlabwrap Alexander Schmolck and Vivek Rathod for mlabwrap: http://mlabwrap.sourceforge.net/ Andrew Sterian for writing pymat without which this module would never have existed. Matthew Brett contributed numpy compatibility and nice setup.py improvements (which I adapted a bit) to further reduce the need for manual user intervention for installation. I'm only using linux myself -- so I gratefully acknowledge the help of Windows and OS X users to get things running smoothly under these OSes as well; particularly those who provided patches to setup.py or mlabraw.cpp (Joris van Zwieten, George A. Blaha and others). Matlab is a registered trademark of The Mathworks. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论