matlab_kernel icon indicating copy to clipboard operation
matlab_kernel copied to clipboard

plot commands hang

Open hansenms opened this issue 8 years ago • 14 comments
trafficstars

I am trying to do some Matlab plots in Jupyter, but can't get it working. I can get the matlab_kernel installed and it is working in the sense that I can do some calculations and see the results, but when I try to plot, the kernel hangs.

I am trying this in a virtual environment:

$ virtualenv -p python3 jupyter_python3
$ source jupyter_python3/bin/activate
$ pip install jupyter
$ pip install numpy matplotlib
$ pip install matlab_kernel
$ python -m matlab_kernel install --user

My matlab version is 9.1.0.441655 (R2016b).

If I type something like:

t = [1:10];
plot(t);

in a cell, it simply hangs. Any hints/suggestions would be much appreciated.

hansenms avatar Dec 21 '16 02:12 hansenms

I am not maintaining this repo anymore. You may want to give a try to https://github.com/anntzer/matlab_kernel which provides a slightly different set of capabilities.

anntzer avatar Dec 21 '16 07:12 anntzer

If it's any help, your example code (plot(1:10)) works just fine for me. It takes ~4 seconds to output on a cold page on my laptop with 4720hq CPU. I am using MATLAB version 9.1.0.441655 (R2016b). When the page is warmed up, MATLAB works a lot faster and the plot pops up in less than a second.

twocs avatar Jan 09 '17 06:01 twocs

Likely related to #70 (see https://github.com/Calysto/matlab_kernel/issues/70#issuecomment-264201991 in particular).

Is there really interest in static inline graphics in the notebook? If there was a way to embed the JFrame as an interactive graphic I'd be happy to integrate it in my implementation but otherwise, I think the MATLAB Live Editor probably just works better for that purpose...

anntzer avatar Jan 09 '17 06:01 anntzer

That's a good point. I also came to stumbling blocks because I think that I was trying to use Jupyter when MATLAB live scripts editor is almost as good for publishing to the web. I wonder if the goal that @hansenms has in mind can't just be solved by MATLAB Live Editor.

MATLAB live scripts is missing interactivity and WYSIWIG for the web. With Jupyter and a matlab_kernel, we can allow users to play with the code as well, assuming we have a properly licensed server with MATLAB. Also, the live script is displayed in the IDE, not a browser, so it's possibly a great deal of additional work to ensure that all the formatting carries across when publishing.

twocs avatar Jan 09 '17 06:01 twocs

I think you are making good arguments in favor of the feature. However, the abysmal performance of print in recent MATLAB remains an issue, and one I cannot do anything about...

anntzer avatar Jan 09 '17 08:01 anntzer

@anntzer @twocs My use case is that I use a bunch of different scripting languages in my work, Python, R, Julia, and MATLAB. I have started using Jupyter as a sort of lab notebook that ties it all together. So it is nice to be able to have all the analysis notes that I have in one place and just use MATLAB or Python or whatever depending on what task I am trying to solve. So it would be nice if one could have it integrated in the same way.

I have also struggled with the MATLAB engine performance issues for other projects so I understand. It would just be a nice to have feature to be able to have all the scripting languages live nicely side-by-side.

Let me know if there is more information that you need or if I can help with something.

hansenms avatar Jan 09 '17 15:01 hansenms

I thought about it again and I can think of a way forward.

Another reason why I was reluctant to implement the feature was that I wanted it to be configurable (... so that persons like myself can choose not to use it :-)) but did not want to have a magic system like metakernel has (see discussion at #72). However, I just realized that you could instead configure this via environment variables: the user would have to do something like setenv('MK_INLINE_PLOT', '<some-string>') (MK_ = MATLAB Kernel -- I should rename CONNECT_MATLAB to MK_CONNECT as well) to configure it from within the notebook, or could set it globally in their .bash_profile (or equivalent).

From the point of view of the implementation, you'd have to patch do_execute as follows:

  • Fetch the value of the environment variable relatively early (via self._call("getenv", "MK_INLINE_PLOT"), and, if set, parse it.
  • If it is set, set the relevant options, as in https://github.com/Calysto/matlab_kernel/blob/master/matlab_kernel/kernel.py#L190
  • If it is set, run the relevant blob after execution finished, as in https://github.com/Calysto/matlab_kernel/blob/master/matlab_kernel/kernel.py#L75 (you'd have to inline the definition of self.Display, which comes from metakernel).

What's a good format for MK_INLINE_PLOT? If non-empty, it has to contain the figure size, figure resolution and figure format, so it may as well be a json string (that gets merged with some reasonable defaults), e.g. '{"size": [8, 6], "resolution": 150, "format": "png"}'.

Now you just get to implement it :-)

anntzer avatar Jan 09 '17 16:01 anntzer

Why not use an RC file for this with options, and the environment variable is called MATLAB_KERNEL_RC?

blink1073 avatar Jan 09 '17 17:01 blink1073

I don't think a rcfile is really useful for so few options: MATLAB_KERNEL_RC would have to contain the contents of the file rather than its path (so that it's easily settable from within the notebook), so export MATLAB_KERNEL_RC='{...}' in your bash_profile seems just as easy to do. Plus you'd have to debate on where to put the rcfile :-)

I also think MK_CONNECT (CONNECT_MATLAB) should remain a separate option, so that both can be easily set independently from each other (e.g. I often use CONNECT_MATLAB as it makes starting later kernels much faster).

anntzer avatar Jan 09 '17 18:01 anntzer

Hello,

May I suggest something? What about Plot.Ly support?

Namely, we can advise the user to use MATLAB's figure() with the Visible property set to offand just use Plot.Ly.

The question is, could you embed Ploy.Ly in the Jupyter Notebook?

Thank You.

P. S. Calysto already support In Line Graphics, so it is just a feature request. iMatlab doesn't support it, so it is idea to have in line graphics with full control (As Plot.Ly is HTML based).

RoyiAvital avatar Feb 02 '17 06:02 RoyiAvital

Hi @RoyiAvital, it looks like you've got some pointers from @anntzer in https://github.com/anntzer/imatlab/issues/2#issuecomment-276897172. I can't actively work on this library because I don't have a Matlab license, but will accept a PR if you get something working.

blink1073 avatar Feb 02 '17 12:02 blink1073

The issue is I have zero knowledge of Python and programming outside of the MATLAB world.

What I do have is access to MATLAB. We need some reorganization of resources :-).

RoyiAvital avatar Feb 02 '17 13:02 RoyiAvital

Ha, fair enough.

blink1073 avatar Feb 02 '17 13:02 blink1073

Any chance controlling the size of the figure using figure() command?

For instance for hFigure = figure('Position', [100, 100, 400, 400]); I want the output size in the Notebook to have size of 400 x 400.

It does work on https://github.com/imatlab.

Thank You.

RoyiAvital avatar Mar 23 '18 21:03 RoyiAvital