Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Puzzling behaviour of 'magic' #67

Open
bs448c opened this issue May 13, 2014 · 4 comments
Open

Puzzling behaviour of 'magic' #67

bs448c opened this issue May 13, 2014 · 4 comments

Comments

@bs448c
Copy link

bs448c commented May 13, 2014

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: oukhan@umich.edu
%        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: oukhan@umich.edu\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
@arokem
Copy link
Owner

arokem commented May 13, 2014

Hello! First of all, concerning the extra space in the in/out to the magic:
yes - it is somewhat finnicky and cares about white-space. This is because
the magic is doing some string handling of that line, and it's rather crude
(for example it simply splits on commas...). I have made a new issue to
improve this, but I am not sure when I will get around to dealing with
this.

Second - I see that you are working with the webserver version of the
bridge. We've made a lot of changes since 0.2, including support for
complex numbers. Do you know how to install pymatbridge from the
source-code available on github? If you pull the recent master and install
that, complex numbers should work for you. Please let me know if you need
help with the installation. We are hoping to make this easier, but it's
still a bit rough around the edges.

On Tue, May 13, 2014 at 3:18 PM, bs448c notifications@github.com wrote:

Hi

I'm trying to port a compressed sensing matlab example from
http://compsens.eecs.umich.edu/sensing_tutorial.php to iphton 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: oukhan@umich.edu
% 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)
in ()
----> 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: oukhan@umich.edu\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 (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


Reply to this email directly or view it on GitHubhttps://github.com//issues/67
.

@bs448c
Copy link
Author

bs448c commented May 13, 2014

Hi, awesome, thanks for the quick reply. Indeed I should have version 0.2 (according to version.py .. is there a way of displaying the version in my ipynb?) as I have pip installed it using cygwin under Windows .. I will try to install it from source and report back. Thanks!!

@arokem
Copy link
Owner

arokem commented May 14, 2014

As of about a minute ago, if you install from master, you should be able to
see something like:

In [*6*]: import pymatbridge


In [*7*]: pymatbridge.__version__

Out[*7*]: '0.3.dev'

On Tue, May 13, 2014 at 3:44 PM, bs448c notifications@github.com wrote:

Hi, awesome, thanks for the quick reply. Indeed I should have version 0.2
(according to version.py .. is there a way of displaying the version in my
ipynb?) as I have pip installed it in cygwin under Windows .. I will try
to install it from source and report back. Thanks!!


Reply to this email directly or view it on GitHubhttps://github.com//issues/67#issuecomment-43022754
.

@bs448c
Copy link
Author

bs448c commented May 14, 2014

pymtbridge.__version__ doesn't work for v 0.2 but it does for 0.3-dev .. I managed to install it and can import, however

Python 2.7.6 |Anaconda 1.9.1 (64-bit)| (default, Nov 11 2013, 10:49:15) [MSC v.1
500 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.

IPython 1.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import pymatbridge

In [2]: pymatbridge.__version__
Out[2]: '0.3.dev'

In [3]: %load_ext pymatbridge
Starting MATLAB on ZMQ socket tcp://127.0.0.1:55555
Send 'exit' command to kill the server
............................................................Matlab session timed
 out after 60 seconds
MATLAB failed to start

In [4]:

MATLAB Command Windows complains

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.

Invalid MEX-file 'C:\Users\Benjamin\Anaconda\lib\site-packages\pymatbridge\matlab\messenger.mexw64': The specified module could not be
found.

Error in matlabserver (line 7)
messenger('init', socket_address);

» 

This is clearly related to #56 .. libzmq.dll is in my PATH but I don't know how to compile the messenger myself ...

@bs448c bs448c mentioned this issue May 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants