holoviz / holoviews

With Holoviews, your data visualizes itself.
https://holoviews.org
BSD 3-Clause "New" or "Revised" License
2.69k stars 403 forks source link

Best practices for HoloViews video output #2647

Open zbarry opened 6 years ago

zbarry commented 6 years ago

Hi all,

This is sort of a follow-on to #2095 .

I was wondering if people had played around a bit with making videos from HoloMaps and DynamicMaps generated from Jupyter notebooks. My use case is to run a notebook on a Jupyter server that non-technical people can mess around with and then export their desired animations as videos. From what I can naively tell, it seems that you use the Bokeh backend for visualization in notebooks (what I have some experience in already), and MPL for the video generation. Not sure how I'd go about doing that.

Is there a best/good way that people have found to do this?

Thanks a lot!

jbednar commented 6 years ago

I think this is largely a documentation issue; we should probably have a dedicated page explaining the various animation options and how they work with different backends. Right now it's only covered under MPL, but e.g. the scrubber interface should work with both, and we probably don't cover exporting and how to share them very well.

jlstevens commented 6 years ago

Agreed that this is primarily a docs issue.

From what I can naively tell, it seems that you use the Bokeh backend for visualization in notebooks (what I have some experience in already), and MPL for the video generation.

That is correct. I can imagine using Bokeh to export PNGs frame by frame and then stitching the frames into video, but this would be painfully (if not unusably) slow. Matplotlib is the only reasonable way of turning Python based plots into video output that I know of. Are there any other tools that can do this?

drs251 commented 6 years ago

Video exporting is a pain point for me as well. The approach via matplotlib only seems to work with pure HoloMaps; HoloMaps of Layouts are not supported, so I had to write some custom code. IMO, making a video in Python is quite fast and easy e.g. with the imageio package, the hard part is to get the raw pixel arrays from matplotlib and bokeh. In principle there is a way to do this in matplotlib, but it seems to clip plots under some circumstances, so I ended up exporting to png, which is painfully slow. It would be great to able to generate videos of arbitrary HoloMaps out of the box without all the hassle, but it would probably take some effort.

jlstevens commented 6 years ago

@drs251 Thanks for the pointer to the imageio package. I hadn't heard of it and the dependencies seem fairly reasonable to install.

My worry is that getting the raw pixel arrays from matplotlib/bokeh will be slow unless you use matplotlib's animation support. If this isn't true and it is no faster than capturing PNG output in memory and using imageio, I would be open to making imageio an optional dependency and using the holoviews renderers for generating the PNG data, dropping the old matplotlib animation approach. That way, one code path could handle all code paths and renderers which would make animations/video based on bokeh plots possible. Note that I'm not saying it would work quickly!

Edit: Looks like imageio also needs ffmpeg for video output.

drs251 commented 6 years ago

@jlstevens You'll probably not get around using ffmpeg for video exporting one way or another; afaik matplotlib has this dependency as well. But imageio does wrap it in a convenient interface. Anyway, I wanted to share my video exporting code for reference:

https://gist.github.com/drs251/c0c348e88a1cc95e3e70205bff0ed91b

As I mentioned, getting png frames from matplotlib is kind of slow, but it does give me good looking results (as opposed to the native matplotlib solution). I generally only need a dozen or so frames, so I can live with it, but a faster solution inside holoviews would be fantastic.

philippjfr commented 6 years ago

The approach via matplotlib only seems to work with pure HoloMaps; HoloMaps of Layouts are not supported

This sounds like a regression to me, it definitely used to be supported.

jlstevens commented 6 years ago

One thought I had a long time ago was to use GR, namely the matplotlib GR backend which is supposed to make matplotlib plotting much faster. I haven't tried it myself but it might be of interest to you @drs251 - if it works well, it might be something we could recommend.

drs251 commented 6 years ago

@philippjfr OK, I discovered that it works more or less in the current version (it kept raising exceptions in the last version I tried this in, but I don't remember which one it was). However, there is still a problem with cut colorbars and titles:

%%opts Image [colorbar=True]
%%output holomap='mp4', fps=1
ds = hv.Dataset((np.arange(20), np.arange(20), np.arange(5), np.arange(2),
                    np.random.random((2, 5, 20, 20))), kdims=['w', 'x', 'y', 'z'], vdims=['A'])
ds.to(hv.Image).layout('z')