Code Monkey home page Code Monkey logo

python-matlab-bridge's Introduction

Python-MATLAB(R) Bridge and IPython matlab magic

Build Status Coverage Status Latest Version License Join the chat at https://gitter.im/arokem/python-matlab-bridge

A python interface to call out to Matlab(R). Original implementation by Max Jaderberg. His original repo of the project can be found here, but please note that the development of the two repositories has significantly diverged.

This implementation also includes an IPython matlab_magic extension, which provides a simple interface for weaving python and Matlab code together (requires ipython > 0.13).

Installation

pymatbridge can be installed from PyPI:

$ pip install pymatbridge

If you intend to use the Matlab magic extension, you'll also need IPython.

Finally, if you want to handle sparse arrays, you will need to install Scipy. This can also be installed from PyPI, or using distributions such as Anaconda or Enthought Canopy

Usage

To use the pymatbridge you need to connect your python interpreter to a Matlab session. This is done in the following manner:

from pymatbridge import Matlab
mlab = Matlab()

This creates a matlab session class instance, into which you will be able to inject code and variables, and query for results. By default, when you use start, this will open whatever gets called when you type matlab in your Terminal, but you can also specify the location of your Matlab application when initializing your matlab session class:

mlab = Matlab(executable='/Applications/MATLAB_R2011a.app/bin/matlab')

You can then start the Matlab server, which will kick off your matlab session, and create the connection between your Python interpreter and this session:

mlab.start()

which will return True once connected.

results = mlab.run_code('a=1')

Should now run that line of code and return a results dict into your Python namespace. The results dict contains the following fields:

{u'content': {u'code': u'a=1',
 u'datadir': u'/private/tmp/MatlabData/',
 u'figures': [],
 u'stdout': u'\na =\n\n     1\n\n'},
 u'success': u'true'}

In this case, the variable a is available on the Python side, by using the get_variable method:

mlab.get_variable('a')

You can run any MATLAB functions contained within a .m file of the same name. For example, to call the function jk in jk.m:

%% MATLAB
function lol = jk(args)
    arg1 = args.arg1;
    arg2 = args.arg2;
    lol = arg1 + arg2;
end

you would call:

res = mlab.run_func('path/to/jk.m', {'arg1': 3, 'arg2': 5})
print(res['result'])

This would print 8.

You can shut down the MATLAB server by calling:

mlab.stop()

Tip: you can execute MATLAB code at the beginning of each of your matlab sessions by adding code to the ~/startup.m file.

Octave support & caveats

A pymatbridge.Octave class is provided with exactly the same interface as pymatbridge.Matlab:

from pymatbridge import Octave
octave = Octave()

Rather than looking for matlab at the shell, this will look for octave. As with pymatbridge.Matlab, you can override this by specifying the executable keyword argument.

Rather than ~/startup.m, Octave looks for an ~/.octaverc file for commands to execute before every session. (This is a good place to manipulate the runtime path, for example).

Requires Version 3.8 or higher. Notice: Neither the MXE 3.8.1 nor the Cygwin 3.8.2 version is compatible on Windows. No Windows support will be available until a working version of Octave 3.8+ with Java support is released.

Matlab magic:

The Matlab magic allows you to use pymatbridge in the context of the IPython notebook format.

%load_ext pymatbridge

These lines will automatically start the matlab session for you. Then, you can simply decorate a line/cell with the '%matlab' or '%%matlab' decorator and write matlab code:

%%matlab
a = linspace(0.01,6*pi,100);
plot(sin(a))
grid on
hold on
plot(cos(a),'r')

If %load_ext pymatbridge doesn't work for you use:

import pymatbridge as pymat
pymat.load_ipython_extension(get_ipython(), matlab='/your_matlab_installation_dir/bin/matlab')

More examples are provided in the examples directory

Building the pymatbridge messenger from source

The installation of pymatbridge includes a binary of a mex function to communicate between Python and Matlab using the 0MQ messaging library. This should work without any need for compilation on most computers. However, in some cases, you might want to build the pymatbridge messenger from source. To do so, you will need to follow the instructions below:

Install zmq library

Please refer to the official guide on how to build and install zmq. On Ubuntu, it is as simple as sudo apt-get install libzmq3-dev. On Windows, suggest using the following method:

  • Install MSYS2
  • Run $ pacman -S make
  • From the zmq source directory, run: $ sh configure --prefix=$(pwd) --build=x86_64-w64-mingw32
  • Run $ make.

After zmq is installed, make sure you can find the location where libzmq is installed. The library extension name and default location on different systems are listed below.

Platform library name Default locations
MacOS libzmq.dylib /usr/lib or /usr/local/lib
Linux libzmq.so.3 /usr/lib or /usr/local/lib
Windows libzmq.dll C:\Program Files\ZeroMQ 3.2.4\bin

If you specified a prefix when installing zmq, the library file should be located at the same prefix location.

The pymatbridge MEX extension needs to be able to locate the zmq library. If it's in a standard location, you may not need to do anything; if not, there are two ways to accomplish this:

Using the dynamic loader path

One option is to set an environment variable which will point the loader to the right directory.

On MacOS, you can do this by adding the following line to your .bash_profile (or similar file for your shell):

export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:<Path to your zmq lib directory>

On Linux, add the following line to your .bash_profile (or similar file for your shell):

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<Path to your zmq lib directory>

On Windows, add the install location of libzmq.dll to the PATH environment variable. On Windows 7+, typing "environment variables" into the start menu will bring up the apporpriate Control Panel links.

Pointing the binary at the right place

Another option is to modify the MEX binary to point to the right location. This is preferable in that it doesn't change loader behavior for other libraries than just the pymatbridge messenger.

On MacOS, you can do this from the root of the pymatbridge code with:

install_name_tool -change /usr/local/lib/libzmq.3.dylib <Path to your zmq lib directory>/libzmq.3.dylib messenger/maci64/messenger.mexmaci64

On Linux, you can add it to the RPATH:

    patchelf --set-rpath <Path to your zmq lib directory> messenger/mexa64/messenger.mexa64

Install pyzmq

After step 1 is finished, please grab the latest version of pyzmq and follow the instructions on the official page. Note that pymatbridge is developed with pyzmq 14.0.0 and older versions might not be supported. If you have an old version of pyzmq, please update it.

Install pymatbridge

After the steps above are done, you can install pymatbridge. Download the zip file of the latest release. Unzip it somewhere on your machine.

For Matlab:

cd messenger
# edit local.cfg in the directory for your platform
python make.py matlab
cd ..
python setup.py install

For Octave:

cd messenger/octave
# edit local_octave.cfg in the directory for your platform
python make.py octave
cd ..
python setup.py

This should make the python-matlab-bridge import-able.

Warnings

Python communicates with Matlab via an ad-hoc zmq messenger. This is inherently insecure, as the Matlab instance may be directed to perform arbitrary system calls. There is no sandboxing of any kind. Use this code at your own risk.

python-matlab-bridge's People

Contributors

aebrahim avatar ahmadia avatar arokem avatar auneri avatar auxiliary avatar bischofs avatar blink1073 avatar brunobeltran avatar dhermes avatar djsutherland avatar dukebody avatar gitter-badger avatar gpiantoni avatar haoxingz avatar isbadawi avatar jaderberg avatar jakirkham avatar jenshnielsen avatar josephcslater avatar mailerdaimon avatar mateusz-was avatar mpollow avatar ovidner avatar shabbychef 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-matlab-bridge's Issues

core.displaypub issue

Hi,

As I try to import pymatbridge this happens:

Traceback (most recent call last):
File "", line 1, in
File "pymatbridge/init.py", line 2, in
from matlab_magic import *
File "pymatbridge/matlab_magic.py", line 32, in
from IPython.core.displaypub import publish_display_data
ImportError: No module named core.displaypub

I'd appreciate if you could help me troubleshoot.

Matlab figures

Looking for best options for rendering matlab figure outputs in notebook. I am using a toolbox (Tor Wager's 'mediation' toolbox) that generates some particuarly stubborn figures that don't render or scale well, and output blank images for most file writing functions.

Ideally I would like to use the 'export_fig' matlab function, which is designed to generate publication quality figures.

http://www.mathworks.com/matlabcentral/fileexchange/23629-exportfig

But I get error messages saying 'OpenGL' mode cannot be used in terminal emulation mode (same for 'zbuffer')

%%matlab
figure;
a = linspace(0.01,6*pi,100);
plot(sin(a))
grid on
hold on
plot(cos(a),'r')
export_fig '/tmp/figtemp.png'
close all

{Warning: OpenGL mode can not be used in terminal emulation mode; ignoring
option.}

In graphics/private/inputcheck at 131
In print at 171
In print2array at 140
In export_fig at 334
In web_eval at 44
In webserver at 241

Is there any way round this?

Alternatively: does anyone have any general notes / notebooks for matlab figure plotting / saving to image and rendering in notebooks that I can try out?

Exchance of complex variables not working

Trying to use the magic syntax with a variable transfer of a complex matrix using
%%matlab -o Y
gives the following error:

/python/virtualenvs/main/lib/python2.7/site-packages/tables/group.py:1200: UserWarning: problems loading leaf /Y::
the HDF5 class H5T_COMPOUND is not supported yet
The leaf will become an UnImplemented node.
% (self._g_join(childname), exc))

The workaround of storing the real and imaginary part in Matlab, transfer them and combine them later in python works, but is a little cumbersome to do. (I guess this is more a feature request than a bug.)

Octave support?

I was wondering if there was interest in making this project compatible with Octave in addition to Matlab? I looked into it a bit and it seems like it might not be too much work (but I haven't tried anything yet so there might be more issues).

  • The command line arguments for Octave are different (e.g. --eval instead of -r to run code), but that's not hard to fix
  • The json package being used won't work as is because Octave doesn't support packages (namespaces) and also doesn't support the same syntax for calling Java objects (can use these functions for compatibility); could use a different json package, or try to fix up this one
  • I'm also not sure whether the mex part will work as is, but it might.

Is there any other potential problem you can think of? I could work on this if there's interest.

Unable to install

I have install the pymatbridge but I was unable to open it because I'm getting some errors. Kindly help me out as to how to solve this, since I'm new to programming and not comfortable with ubuntu as well. I'm using Ubuntu 12.04, Matlab version R2013a and python 2.7.3.

from pymatbridge import Matlab
mlab=Matlab(matlab='/usr/local/MATLAB/R2013a/bin/matlab')
mlab.start()

Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge
Send 'exit' command to kill the server
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/pymatbridge/pymatbridge.py", line 135, in start
if (self.is_connected()):
File "/usr/local/lib/python2.7/dist-packages/pymatbridge/pymatbridge.py", line 168, in is_connected
resp = self.socket.recv_string(flags=zmq.NOBLOCK)
File "socket.pyx", line 456, in zmq.core.socket.Socket.getattr (zmq/core/socket.c:4576)
AttributeError: Socket has no such option: RECV_STRING
< M A T L A B (R) >
Copyright 1984-2013 The MathWorks, Inc.
R2013a (8.1.0.604) 64-bit (glnxa64)
February 15, 2013

To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com.
Socket created at: ipc:///tmp/pymatbridge

and it got stuck and does nothing.

Regards,
Gokul

Need to create symlink in OSX for this to work. matlab command should be accessible.

test.py and the ipython notebook magic don't work unless the matlab command can be accessed from the shell (that wasn't the case in my MATLAB instalation).

In order to make it work, create a symlink to the matlab executable.

For my MATLAB version, I had to do:

sudo ln -s /Applications/MATLAB_R2012b.app/bin/matlab /Users/username/bin/matlab

Then everything worked perfectly.
You may want to add this to the README file.

Thanks for the good work.

matlabpath

Using %load_ext pymatbridge,
matlab cannot swallow:
matlabpath(pathdef)

and matlab has to be restarted.

what is bizarre is that the following code works:
from pymatbridge import Matlab
mlab = Matlab()
mlab.start()
res=mlab.run_code("matlabpath(pathdef))");

Any idea?

better JSON munging or workaround

The following will choke pymatbridge:

%%matlab
fprintf('got quotes? -->  "    ');

The JSON parser gets confused, of course. We can work on escaping and unescaping the I/O to and from Matlab, but it might make more sense to use temporary files (blech), or another mechanism, to move text around.

messenger / ZMQ session error upon "Matlab clear all" using 'magic'

As outlined in #56 and #69 I manage to build pymatbridge-0.3-dev under WIndows 8.1 64 bit. I can start the matlabserver and pass back and forth variables between MATLAB and my iPython notebook (Anaconda 1.9.1, Python 2.7.6) using 'magic' (the x=x+1 and a=[1,2,3]

%%matlab -i a -o b,c
b = a + 3
c = b + 3

sort of examples)

However when trying to port a more complex example,

%%matlab -o x,xf

%Author: Osama Ullah Khan,
%        Phd Student, University of Michigan-Ann Arbor.
%        Email: [email protected]
%        Version: 1.0
%
%This code demonstrate compressive sensing example. In this
%example the signal is sparse in frequency domain and random samples
%are taken in time domain.

close all;
clear all;

%setup path for the subdirectories of l1magic
path(path, 'C:\MATLAB\R2013a\l1magic\Optimization');
path(path, 'C:\MATLAB\R2013a\l1magic\Data');


%length of the signal
N=1024;

%Number of random observations to take
K=256;

%Discrete frequency of two sinusoids in the input signal
k1=29;
k2=59;
k3=109;

n=0:N-1;

%Sparse signal in frequency domain.
x=sin(2*pi*(k1/N)*n)+sin(2*pi*(k2/N)*n)+sin(2*pi*(k3/N)*n);

xf=abs(fft(x));

my matlabserver fails:

MATLAB is running in headless mode.  Figure windows will not be displayed.

To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

Socket created at: tcp://127.0.0.1:55555
Error using messenger
Error: ZMQ session not initialized

Error in matlabserver (line 30)
            messenger('respond', resp);

ยป 

I'd like to point out this exact snippet has been working flawlessly using pymatbridge-0.2 and the 'old' communication protocol.

I don't know if this is an issue of Windows or my build -- can pelase someone on a different OS try to replicate this behaviour?
Thanks!

EDIT: This seems to be due to the clear all; which seems to kill the matlabserver. I modified the issue title accordingly

undefined function 'messenger'

I am trying to run matlab code in ipython notebook. But I cannot manage to start mlab.start() following the guide after installation. My system information: ubuntu1 14.04, 32bit, matlab r2009b 32bit, anaconda 2.0.0 (with zeromq 4.0.4, pyzmq 14.3.0, pymatbridge 0.3). I also add
export LD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/home/fehiepsi/anaconda/lib/libzmq.so.3 to my .profile file.

It seems that the messenger function in matlabserver.m is not defined. Here is the error

< M A T L A B (R) >
                  Copyright 1984-2009 The MathWorks, Inc.
                 Version 7.9.0.529 (R2009b) 32-bit (glnx86)
                              August 12, 2009


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

??? Undefined function or method 'messenger' for input arguments of type
'char'.

Error in ==> matlabserver at 7
messenger('init', socket_address);

How exactly is the Windows version broken?

Beyond Windows inherently being a broken OS, what is the status of Windows compatibility? The main gotchas for Matlab in Windows are filepath issues and bare system calls. What remains?

Crash under Windows 7

Configuration :

Windows 7 pro SP1
Intel i5 32 bit
Python 2.7 installed via Anaconda (conda 2.3.0)
ipython 1.1.0
Matlab R2012b

I installed pymatbridge with

conda install pymatbridge 

which successfully installed it with pip.

The issue :

In an ipython console :

import pymatbridge as pymat
ip = get_ipython()
pymat.load_ipython_extension(ip)
Starting MATLAB on http://localhost:53961
visit http://localhost:53961/exit.m to shut down same
Traceback (most recent call last):

  File "<ipython-input-6-d23fa381f8e8>", line 1, in <module>
    pymat.load_ipython_extension(ip)

  File "C:\Anaconda\lib\site-packages\pymatbridge\matlab_magic.py", line 275, in load_ipython_extension
    ip.register_magics(MatlabMagics(ip, **kwargs))

  File "C:\Anaconda\lib\site-packages\pymatbridge\matlab_magic.py", line 130, in __init__
    self.Matlab.start()

  File "C:\Anaconda\lib\site-packages\pymatbridge\pymatbridge.py", line 108, in start
    self.server_process.start()

  File "C:\Anaconda\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)

  File "C:\Anaconda\lib\multiprocessing\forking.py", line 277, in __init__
    dump(process_obj, to_child, HIGHEST_PROTOCOL)

  File "C:\Anaconda\lib\multiprocessing\forking.py", line 199, in dump
    ForkingPickler(file, protocol).dump(obj)

  File "C:\Anaconda\lib\pickle.py", line 224, in dump
    self.save(obj)

  File "C:\Anaconda\lib\pickle.py", line 331, in save
    self.save_reduce(obj=obj, *rv)

  File "C:\Anaconda\lib\pickle.py", line 419, in save_reduce
    save(state)

  File "C:\Anaconda\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "C:\Anaconda\lib\pickle.py", line 649, in save_dict
    self._batch_setitems(obj.iteritems())

  File "C:\Anaconda\lib\pickle.py", line 681, in _batch_setitems
    save(v)

  File "C:\Anaconda\lib\pickle.py", line 286, in save
    f(self, obj) # Call unbound method with explicit self

  File "C:\Anaconda\lib\pickle.py", line 748, in save_global
    (obj, module, name))

PicklingError: Can't pickle <function _run_matlab_server at 0x07884AF0>: it's not found as pymatbridge.pymatbridge._run_matlab_server

Thank you for your hindsight

isrow.m

The function isrow is not available on older Matlab versions.

Can you use an iPython variable in a Matlab function?

Hi,

I am wondering if it is possible to use a python variable in the matlab function call, specifically with the magic commands (e.g. not using run_func)?

Also, how do you deal with python lists or numpy vectors/arrays?

Thanks.

Shaun

How to install?

Thanks for making this! What are the best ways to install it?

Repeated make code

In the messenger directory, the file make.py appears to be copied (with very minor modifications) separately into the Linux and OS X build directories. It's not copied into the Windows directory, making for an inconsistent build experience.

The intention and usage of the make.py bootstrap script should be clarified.

imshow get nodisplay error

Matlab error message: u'IMSHOW unable to display image.

I'm using OSX Mountain Lion with Matlab 2012b
changing matlab_magic.py line 129 to:
self.Matlab = pymat.Matlab(matlab, maxtime=maxtime, startup_options='-noFigureWindows')
will have it fixed :)

Won't connect to Matlab in Windows 7.

I'm trying this on Windows 7, using Python 2.7. I've downloaded the bridge, run the setup.py install script. In IPython I can import the library and try and connect via:

mlab = Matlab()

However, when I call

mlab.is_connected()

it returns false.

I've verified that I can call "matlab" from the command line and it starts. I'm running Matlab 2011b

imshow() does not show picture

Hi,

I was trying some image recognition but now imshow does not show the picture. No figure window opens, the program runs smoothly, no errors, imhist() works fine. It worked a couple of hours ago until Matlab R2013a crashed, I guess because calculations were a bit too complicated. It does "show" the number array in the command window if imshow(I) is called there...

Many thanks for your help!

control size of figure output a la rmagic

The %R magic extension allows control of displayed figures in the iPython notebook, like so:

%R -w 700 -h 500
hist(rnorm(1000))
# etc

It should be easy to nick this code from the rmagic extension.

Python3 Support?

It looks like this module doesn't support Python3. Is anyone working on this?

Python3 is coming down the pipe pretty fast, both Ubuntu and Fedora are switching to it by default this year...

figure out copyright

We should figure out the copyright of the entire project, and restore copyright info, attribution, etc. to code from the webserver project. I am fine with the 'BSD 2-clause' that came with webserver.m.

imshow() on mac os x

This does not work:

%%matlab
image = imread('../mp.tif');
matlab_F = fft2(image);
F = log(abs(fftshift(matlab_F)));
imshow(F);

It does work in console (but only with omitted -nodisplay):

$ /Applications/MATLAB_R2014b.app/bin/matlab -nodesktop

                                    < M A T L A B (R) >
                          Copyright 1984-2014 The MathWorks, Inc.
                           R2014b (8.4.0.150421) 64-bit (maci64)
                                     September 15, 2014


To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

>> image = imread('../mp.tif');
>> matlab_F = fft2(image);
>> F = log(abs(fftshift(matlab_F)));
>> imshow(F, [min(F(:)), max(F(:))]);
Warning: Image is too big to fit on screen; displaying at 17% 
> In initSize at 71
  In imshow at 309 
>> exit
$ /Applications/MATLAB_R2014b.app/bin/matlab -nodesktop -nodisplay

                                    < M A T L A B (R) >
                          Copyright 1984-2014 The MathWorks, Inc.
                           R2014b (8.4.0.150421) 64-bit (maci64)
                                     September 15, 2014


To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.

>> image = imread('../mp.tif');
>> matlab_F = fft2(image);
>> F = log(abs(fftshift(matlab_F)));
>> imshow(F, [min(F(:)), max(F(:))]);
Error using imshow (line 212)
IMSHOW unable to display image. 

I've tried setting Matlab().startup_options to only -nodesktop (matlab logo shows up, so startup_options are executed correctly), but the error is the same:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-4-ade840d015dc> in <module>()
----> 1 get_ipython().run_cell_magic(u'matlab', u'', u"image = imread('../mp.tif');\nmatlab_F = fft2(image);\nF = log(abs(fftshift(matlab_F)));\nimshow(F, [min(F(:)), max(F(:))]);")

/Users/arve/.virtualenvs/brew/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2160             magic_arg_s = self.var_expand(line, stack_depth)
   2161             with self.builtin_trap:
-> 2162                 result = fn(magic_arg_s, cell)
   2163             return result
   2164 

/Users/arve/.virtualenvs/brew/lib/python2.7/site-packages/pymatbridge/matlab_magic.pyc in matlab(self, line, cell, local_ns)

/Users/arve/.virtualenvs/brew/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/Users/arve/.virtualenvs/brew/lib/python2.7/site-packages/pymatbridge/matlab_magic.pyc in matlab(self, line, cell, local_ns)
    215             e_s += "\n-----------------------"
    216             e_s += "\nAre you sure Matlab is started?"
--> 217             raise RuntimeError(e_s)
    218 
    219 

RuntimeError: There was an error running the code:
 image = imread('../mp.tif');
matlab_F = fft2(image);
F = log(abs(fftshift(matlab_F)));
imshow(F, [min(F(:)), max(F(:))]);
-----------------------
Are you sure Matlab is started?

The matlab version is 2014b, and here are the running process:

/Applications/MATLAB_R2014b.app/bin/maci64/../../Contents/MacOS/MATLAB_maci64 -r warning('off','all');addpath(genpath('/Users/arve/.virtualenvs/brew/lib/python2.7/site-packages/pymatbridge/matlab'));warning('on', 'all');matlabserver('ipc:///tmp/pymatbridge');exit -nodesktop

Any ideas?

Hanging on startup

mlab.start()

Starting MATLAB on http://localhost:59699
visit http://localhost:59699/exit.m to shut down same
..................................

The dots continue to generate until I get bored, then ctrl-c:

KeyboardInterrupt Traceback (most recent call last)
in ()
----> 1 mlab.start()

/home/alstottjd/Code/python-matlab-bridge/pymatbridge/pymatbridge.pyc in start(self)
109 while not self.is_connected():
110 np.disp(".", linefeed=False)
--> 111 time.sleep(1)
112 print "MATLAB started and connected!"
113 return True

KeyboardInterrupt

clear potential hang conditions

Some Matlab code passed to the server could cause it to hang indefinitely. For example, calling a keyboard statement. Also, it is common practice to have a
dbstop if error
in one's startup.m file. This has the same effect as %pdb in iPython: when an error is encountered, the Matlab interpreter waits for user input. This would be bad in the 'headless' environment of the web server.
At the very least we should have a
dbclear all in webserver.m
We should also try to overload keyboard.m
I have done this before, and it works, but it may depend on the order in which paths are added.

There may be other lurking such gotchas, but these are the principal ones, I think.

Not working on OSX

I am on OSX and I cloned the repo, did a python setup.py install from the main directory, then tried to run the example .ipynb files, but it didn't work and I am not sure why...

Clear the file-system at the end of each run

Make sure that nothing remains of a run in the file-system, regardless of what happens when it runs (for example if it crashes). This is important, because we run the risk of pulling in stuff from old runs into new runs.

have Matlab terminate when extension is unloaded

I tried putting a self.Matlab.stop() in the __del___ method of matlab_magic.py, but this does not seem to be called when ipython closes. I have found no actual working examples of unload_python_extension among the magic extensions shipped with ipython, so I have nothing to work off of.

Should work on public-facing notebook server, right?

I'm trying to get pymatbridge going on a publically-facing notebook server.

Does that present any special issues?

When I login to the server machine (as the user that runs the public notebook), I can start matlab from a terminal just fine.

When I try using the notebook interface and load pymatbridge, I get:

Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge
Send 'exit' command to kill the server
............................................................Matlab session timed out after 60 seconds
MATLAB failed to start

Any ideas?

Thanks,
Rich

Memory used by MATLAB process keeps increasing

First of all, thanks a lot for creating such an awesome extension!

I've been using matlab magic recently on a project, which repeatedly calls the following function with random parameters:

# mlab is initiated:
#     import pymatbridge as pymat
#     mlab = pymat.Matlab()

def matlab_bilinear(Bs, As, fs, fp):
    res = mlab.run_code('[Bz, Az] = bilinear(%s, %s, %s, %s);' % (Bs, As, fs, fp))
    Bz = mlab.get_variable('Bz')
    Az = mlab.get_variable('Az')
    return Bz, Az

After calling matlab_bilinear over 4 million times, the MATLAB process is using a substantial amount of memory (8% out of a 48GB-memory server) and apparently is still increasing. I don't think it is related specifically to the code I wrote, so I am wondering if there is any part which could cause memory leak very slowly or it's simply because the parameters are internally stored in memory by Matlab and keep accumulating. Any comments or suggestions are welcome. Thanks!

Best,
Dawen

Connection becomes invalid when getting and unexisting variable

I start a new pymatbridge server and don't define the variable 'x' in Matlab. Now I execute:

In [6]: mlab.get_variable('x')
??? Error using ==> evalin
Undefined function or variable 'x'.

Error in ==> pymat_get_variable at 27
response.var = evalin('base', varname);

Error in ==> matlabserver at 34
            resp = feval(fhandle, req);

>> 

It gets stuck, and if I press ^C I get the traceback:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-6-5e01d5fc1178> in <module>()
----> 1 mlab.get_variable('x')

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/pymatbridge/pymatbridge.pyc in get_variable(self, varname, maxtime)                                                                                                             
    225         req = json.dumps(req, cls=ComplexEncoder)
    226         self.socket.send(req)
--> 227         resp = self.socket.recv_string()
    228         resp = json.loads(resp, object_hook=as_complex)
    229 

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/sugar/socket.pyc in recv_string(self, flags, encoding)                                                                                                                      
    342             The Python unicode string that arrives as encoded bytes.
    343         """
--> 344         b = self.recv(flags=flags)
    345         return b.decode(encoding)
    346 

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:5787)()

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket.Socket.recv (zmq/backend/cython/socket.c:5587)()

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket._recv_copy (zmq/backend/cython/socket.c:1720)()

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6037)()

Now the connection doesn't work anymore:

In [7]: mlab.get_variable('a')
---------------------------------------------------------------------------
ZMQError                                  Traceback (most recent call last)
<ipython-input-26-a3a6f8279f49> in <module>()
----> 1 mlab.get_variable('a')

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/pymatbridge/pymatbridge.pyc in get_variable(self, varname, maxtime)
    224         req['varname'] = varname
    225         req = json.dumps(req, cls=ComplexEncoder)
--> 226         self.socket.send(req)
    227         resp = self.socket.recv_string()
    228         resp = json.loads(resp, object_hook=as_complex)

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket.Socket.send (zmq/backend/cython/socket.c:5449)()

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket.Socket.send (zmq/backend/cython/socket.c:5209)()

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.socket._send_copy (zmq/backend/cython/socket.c:2030)()

/home/dukebody/.virtualenvs/pymatbridge/lib/python2.7/site-packages/zmq/backend/cython/socket.so in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6263)()

ZMQError: Operation cannot be accomplished in current state

This is certainly not ideal. I assume that the connection gets blocked waiting for a kind of response that never arrives.

/bin/sh: matlab: command not found

Anyone met the same problem I did? I think I followed the installation instruction step by step, then tried to run the example matlab_magic.ipynb. However, I kept getting the above message.

Puzzling behaviour of 'magic'

Hi

I'm trying to port a compressed sensing matlab example from http://compsens.eecs.umich.edu/sensing_tutorial.php to ipython notebook / pymatbridge.

I've noticed some strange behaviour of passing arguments back and forth which I think causes me trouble in my port.

import pymatbridge as pymat

ip = get_ipython()

pymat.load_ipython_extension(ip)
Starting MATLAB on http://localhost:53257
 visit http://localhost:53257/exit.m to shut down same
....MATLAB started and connected!

so far so good .. but now it gets weird

%%matlab -i a -o b,c
b = a + 3
c = b + 3
b =

           4           5           6


c =

           7           8           9

while this makes sense .. that doesn't:

%%matlab -i a -o b, c
b = a + 3
c = b + 3
cb =

           4           5           6


c =

           7           8           9

note the additional blank in passing the -o, and the altered variable name in the output of matlab.

Related or not, here's my attempt on the port which fails when I try to pass back a (complex) array:

%%matlab -o xf

%Author: Osama Ullah Khan,
%        Phd Student, University of Michigan-Ann Arbor.
%        Email: [email protected]
%        Version: 1.0
%
%This code demonstrate compressive sensing example. In this
%example the signal is sparse in frequency domain and random samples
%are taken in time domain.

close all;
clear all;

%setup path for the subdirectories of l1magic
path(path, 'C:\MATLAB\R2013a\l1magic\Optimization');
path(path, 'C:\MATLAB\R2013a\l1magic\Data');


%length of the signal
N=1024;

%Number of random observations to take
K=128;

%Discrete frequency of two sinusoids in the input signal
k1=29;
k2=34;
k3=39;

n=0:N-1;

%Sparse signal in frequency domain.
x=sin(2*pi*(k1/N)*n)+sin(2*pi*(k2/N)*n)+sin(2*pi*(k3/N)*n);

xf=fft(x);

which gives me

---------------------------------------------------------------------------
SystemError                               Traceback (most recent call last)
<ipython-input-10-b7fe87abb3ba> in <module>()
----> 1 get_ipython().run_cell_magic(u'matlab', u'-o xf', u"\n%Author: Osama Ullah Khan,\n%        Phd Student, University of Michigan-Ann Arbor.\n%        Email: [email protected]\n%        Version: 1.0\n%\n%This code demonstrate compressive sensing example. In this\n%example the signal is sparse in frequency domain and random samples\n%are taken in time domain.\n\nclose all;\nclear all;\n\n%setup path for the subdirectories of l1magic\npath(path, 'C:\\MATLAB\\R2013a\\l1magic\\Optimization');\npath(path, 'C:\\MATLAB\\R2013a\\l1magic\\Data');\n\n\n%length of the signal\nN=1024;\n\n%Number of random observations to take\nK=128;\n\n%Discrete frequency of two sinusoids in the input signal\nk1=29;\nk2=34;\nk3=39;\n\nn=0:N-1;\n\n%Sparse signal in frequency domain.\nx=sin(2*pi*(k1/N)*n)+sin(2*pi*(k2/N)*n)+sin(2*pi*(k3/N)*n);\n\nxf=fft(x);")

C:\Users\Benjamin\Anaconda\lib\site-packages\IPython\core\interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2127             magic_arg_s = self.var_expand(line, stack_depth)
   2128             with self.builtin_trap:
-> 2129                 result = fn(magic_arg_s, cell)
   2130             return result
   2131 

C:\Users\Benjamin\Anaconda\lib\site-packages\pymatbridge\matlab_magic.pyc in matlab(self, line, cell, local_ns)

C:\Users\Benjamin\Anaconda\lib\site-packages\IPython\core\magic.pyc in <lambda>(f, *a, **k)
    189     # but it's overkill for just that one bit of state.
    190     def magic_deco(arg):
--> 191         call = lambda f, *a, **k: f(*a, **k)
    192 
    193         if callable(arg):

C:\Users\Benjamin\Anaconda\lib\site-packages\pymatbridge\matlab_magic.pyc in matlab(self, line, cell, local_ns)
    296                 for output in ','.join(args.output).split(','):
    297                     self.shell.push({output:self.matlab_converter(self.Matlab,
--> 298                                                               output)})
    299             else:
    300                 raise RuntimeError(no_io_str)

C:\Users\Benjamin\Anaconda\lib\site-packages\pymatbridge\matlab_magic.pyc in matlab_converter(matlab, key)
    115                     maxtime=matlab.maxtime)
    116 
--> 117     return loadmat('%s/%s.mat'%(tempdir, key))
    118 
    119 

C:\Users\Benjamin\Anaconda\lib\site-packages\pymatbridge\matlab_magic.pyc in loadmat(fname)
     71         if isinstance(f[var_name], h5py.Dataset):
     72             # Currently only supports numerical array
---> 73             data = f[var_name].value
     74             if len(data.dtype) > 0:
     75                 # must be complex data

C:\Users\Benjamin\Anaconda\lib\site-packages\h5py\_hl\dataset.pyc in value(self)
    174         DeprecationWarning("dataset.value has been deprecated. "
    175             "Use dataset[()] instead.")
--> 176         return self[()]
    177 
    178     @property

C:\Users\Benjamin\Anaconda\lib\site-packages\h5py\_hl\dataset.pyc in __getitem__(self, args)
    437         mspace = h5s.create_simple(mshape)
    438         fspace = selection._id
--> 439         self.id.read(mspace, fspace, arr, mtype)
    440 
    441         # Patch up the output for NumPy

SystemError: error return without exception set

Performance using ZMQ

I've recently upgraded to the dev-0.4 version (from this repo) and am experiencing poorer performance on variable I/O compared to previous versions.

For example running the following in a Python cell creates a variable called e at 2.5MB:

e = np.random.rand(32768,10)
mb = e.nbytes / 1024**2.
mb

Passing this into a Matlab cell and processing it as follows takes ~ 1m40s:

%%matlab -i e -o f
f=mean(e);

This is a lot slower than the previous implementation (I can't benchmark until I find where I installed it from!)

Is it possible to select the protocol/communication method when initialising the extension? Thanks

Support for 0mq 4.x

I just installed pymatbridge and, when I try to start the server, it complains about not finding the file libzmq.so.3. I have 0mq 4.0.5 installed so I have the file libzmq.so.4.

I symlinked that one to libzmq.so.3 but I'm afraid this won't work realiably. Could you add 0mq 4.x support to pymatbridge?

Is it possible to leverage only one Matlab instance?

I have noticed that the two different ways to access matlab create two different instances:

from pymatbridge import Matlab
mlab = Matlab() mlab.start()
mlab.run_code("1+1");

and

%load_ext pymatbridge

create two different Matlab instances.

Is it possible to create only one Matlab instance, but that can be accessed with both methods?

Problem for opengl display

Apparently opengl rendering is not working properly with pymatbridge.

For instance, a call to

%%matlab
membrane

is ok. But apparently then it does resort to zbuffer for rendering, so probably non opengl, and using

shading interp

gives weird looking figure. And then calling

light('Position',[0 -2 1])

makes the notebook freeze.

And also, display of large 3d meshes becomes very slow.

IMatlab โ€” an IPython profile for exclusively Matlab code

I have not spent any time looking into this yet, so I have no idea how feasible it may be with your current design (perhaps it should be a different project entirely).

But I love working within IPython. And I'd love to be able to use full Matlab syntax (@ anonymous functions, % comments, etc), with real Matlab-style syntax highlighting. Could your project possibly be extended to add a profile for a Matlab backend to IPython? I'm imagining something similar to IJulia.

This issue is just to test the waters for this approach. Feel free to close if you think this is out of scope for this project.

Publish skript fails if IPython not installed

After installing pymatbridge succesfully on Windows using Python3 (only small tweaks were neccessary) i ecnounterd this problem.
On importing pymatbridge i get the error:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import pymatbridge
  File "C:\Python33\lib\site-packages\pymatbridge\__init__.py", line 2, in <module>
    from .publish import *
  File "C:\Python33\lib\site-packages\pymatbridge\publish.py", line 1, in <module>
    import IPython.nbformat.current as nbformat
ImportError: No module named 'IPython'

Fix: init.py
change:

from publish import *

to:

try:
    from publish import *
except ImportError:
    pass    

As soon as i get familiar with GIT i could submit a Patch including this changes if you want to.
If you are intrested in the changes I made to get this running using Python3 just let me know.

Use the output of ipython in a variable

Hi, is this possible?
I need to run some code in Matlab and then read the output which is only one number. I don't want to bring and brought back all the structures from Matlab.
When I use the iPython option I can see the correct answer (ans = ) in the iPython console, but I don't know how to use it in a variable.

thanks for the project!
Gari

error with plot commands

Any command related to plotting, e.g.

%%matlab
a = linspace(0.01,6*pi,100);
plot(sin(a));

fails with the following error message:

...
/usr/local//python2.7/dist-packages/pymatbridge/matlab_magic.pyc in matlab(self, line, cell, local_ns)
235
236 for disp_d in display_data:
--> 237 publish_display_data(disp_d)
238
239 # Delete the temporary data files created by matlab:

TypeError: publish_display_data() takes at least 2 arguments (1 given)

This is for Ipython 1.2 and IPython 2.1. I checked the source code of IPython and the publish_display_data function requires 2 arguments.

Replacing the following code in matlab_magic.py solves the problem:

for disp_d in display_data:
publish_display_data(disp_d)

with
for disp_d in display_data:
publish_display_data("dummy", disp_d)

This fix should probably be adapted such that older version of IPython also work

Matlab magic "The pymatbridge module is not an IPython extension"

Hi, all

My platform is

  • Windows 7 64bit
  • Python 2.7.8 64bit
  • IPython 2.3.1
  • Matlab 2011a 64bit
  • Visual Studio 2010

I download the master source code, comment the win32 test out and, after installing, build the messenger "messenger.mexw64" myself using mex.

I run the following code:

from pymatbridge import Matlab
mlab = Matlab()
mlab.start()
results = mlab.run_code('a=1;')
print(results)
mlab.stop()

It works. Here is the output:

Starting MATLAB on ZMQ socket tcp://127.0.0.1:55555
Send 'exit' command to kill the server
...MATLAB started and connected!
{u'content': {u'datadir': u'C:\\Users\\Admin\\AppData\\Local\\Temp\\MatlabData\\', u'code': u'a=1;', u'figures': [], u'stdout': u''}, u'success': u'true'}
MATLAB closed

But when I use it in the IPython notebook, it shows

In [2]: %load_ext pymatbridge
The pymatbridge module is not an IPython extension.

Does anyone have a clue?
Best

bug for sequential transfer of same variable

The following lines are copied from a notebook console. Exporting fails if the same filename is used for differently shaped data. There is no error message, but still the old data is in memory in python, see the example below. The HDF5 data on file gets updated correctly, so the bug must occur when importing to python.

By the way, what also looks like a bug is actually fine: The dimension in Matlab in the example is (4,3) and in Python (3,4). As Matlab stores the data column wise in memory and Python does so row wise, the data does not have to be resorted and is thus probably faster.

Here the example:

import pymatbridge as pymat
ip = get_ipython()
pymat.load_ipython_extension(ip)

%%matlab -o c
c = rand(4,3);

%%matlab -o c
c = rand(4,4);

c.shape
(3, 4)

passing matrix (dtype('float64')) containing NaNs to MATLAB

However, what I got in MATLAB is a cell array, which flatten the matrix. Assume the original matrix is 5 by 4, I end up with a 20 by1 cell array. Moreover, all NaNs are treated as strings 'NaN' in the cell. A bug or it is designed to be like this?

Startup of server works but then hangs with '...'

Hello

I'm attempting to install and use python-matlab-bridge on Mac OS X10.8. I've tried the following with the PyPi install and from this github repo with the same results.

mxf793@COLMDS-MIBR307B Desktop $ python
Python 2.7.5 (default, Jun  4 2013, 11:54:07) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymatbridge import Matlab
>>> mlab = Matlab('/Applications/MATLAB_R2011a_Student.app/bin/matlab')
>>> mlab.start()
Starting MATLAB on http://localhost:58921
 visit http://localhost:58921/exit.m to shut down same
..Warning: No window system found.  Java option 'MWT' ignored

                                                                             < M A T L A B (R) >
                                                                   Copyright 1984-2010 The MathWorks, Inc.
                                                                 Version 7.12.0.635 (R2011a) 64-bit (maci64)
[...]

Student License -- for use in conjunction with courses offered at a 
degree-granting institution.  Professional and commercial use prohibited.

logging to /usr/local/lib/python2.7/site-packages/pymatbridge/matlab/www/log/webserver_58921_20130821T154127272.log
Webserver available on http://localhost:58921/
Visit http://localhost:58921/exit.m to shut down
...............................

It now hangs here for ever printing out dots. If I interrupt with Ctrl-D I get the following...

File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/pymatbridge/pymatbridge.py", line 119, in start
time.sleep(1)
KeyboardInterrupt
>>> ??? Operation terminated by user during ==> JavaTcpServer at 31

In ==> webserver at 165
    TCP=JavaTcpServer('accept',TCP,[],config);
EDU>> 
EDU>> 
EDU>> 

...after which it drops to a MATLAB prompt (of sorts) that is usable.

Any suggestions of how to fix this would be much appreciated!

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.