You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use kaleido to make a gif, by writing out a series of png's then concatenating them together. The first few calls to write_image succeed, but at some point it just hangs. I have pickled my plotly figure and attach it to this post, as well as code to reproduce the phenomenon. I am using kaleido==0.0.3.post1 but I have tried it on many other pip install-able versions (0.0.1, 0.0.2, 0.0.3) to get the same effect. BTW, I really like this software! It makes it so much easier to write out plotly figures.
import pickle
import tempfile
from time import time
import numpy as np
with open('example_fig.obj', 'rb') as ff:
figure = pickle.load(ff)
tdir = tempfile.gettempdir()
n_frames=60
zoom=2.5
z_offset=0.5
for i in range(n_frames):
start = time()
theta = (i * 6.28) / n_frames
camera = dict(
eye=dict(x=np.cos(theta) * zoom,
y=np.sin(theta) * zoom, z=z_offset)
)
figure.update_layout(scene_camera=camera)
figure.write_image(tdir + f"/tgif{i}.png")
print(time()-start)
Thanks for the report and example @36000, I was able to reproduce the issue. Here is my output (after stopping the kernel after hanging for a minute or two):
So it looks like we have a object/memory leak in the JavaScript portion of kaleido that shows up here due to the large figure size. This will probably take some time to track down, so here's a workaround you can use in the meantime
importpickleimporttempfilefromtimeimporttimeimportnumpyasnpimportosimportplotly.ioaspioscope=pio.kaleido.scopewithopen('example_fig.obj', 'rb') asff:
figure=pickle.load(ff)
# tdir = tempfile.gettempdir()tdir='./out4'os.makedirs(tdir)
n_frames=60zoom=2.5z_offset=0.5foriinrange(n_frames):
start=time()
theta= (i*6.28) /n_framescamera=dict(
eye=dict(x=np.cos(theta) *zoom,
y=np.sin(theta) *zoom, z=z_offset)
)
figure.update_layout(scene_camera=camera)
figure.write_image(tdir+f"/tgif{i}.png")
# Shutdown kaleido subprocess to free memory, it will# be started again on next image export requestscope._shutdown_kaleido()
print(time()-start)
The scope._shutdown_kaleido() call will shutdown the kaleido subprocess so that it will be started fresh on each export. This will add a couple of seconds to each call to write_image, but there won't be any memory build up.
Hope this holds you over until we can track this down and get a fix out. Thanks again for the report.
Do you have any tips / tricks on how to speed up this gif creation process? I don't know the details, but it seems like each time I call write_image, under the hood, I am opening up the same figure using chromium. Is there an easy way to open the figure once, then rotate and take screenshots within the browser?
I don't know of anything different to do right now.
It would take some benchmarking to hone in on what would actually is taking so long right now. I expect that a lot of the time is being taken up with JSON serialization (Python to C++, then C++ to JavaScript) given the size of the figures.
Do these figures use large numpy arrays? We've talked about optimizing this serialization path in the past, but haven't gotten it off the ground yet.
All that said, I could see a place for adding support to kaleido for batching image export requests. One option would be to support inputting figures that define frames (https://plotly.com/python/animations/) and exporting these to a gif. For your case of rotating a camera, I think the frames could each be pretty small, basically just defining the camera orientation.
My inputs to the scatter3d traces are long numpy arrays, I think those are what make the figures so big. I like that last idea of adding support for inputting figures that define frames.
Activity
jonmmease commentedon Sep 19, 2020
Thanks for the report and example @36000, I was able to reproduce the issue. Here is my output (after stopping the kernel after hanging for a minute or two):
There was also some internally buffered error log
jonmmease commentedon Sep 19, 2020
So it looks like we have a object/memory leak in the JavaScript portion of kaleido that shows up here due to the large figure size. This will probably take some time to track down, so here's a workaround you can use in the meantime
The
scope._shutdown_kaleido()call will shutdown the kaleido subprocess so that it will be started fresh on each export. This will add a couple of seconds to each call towrite_image, but there won't be any memory build up.Hope this holds you over until we can track this down and get a fix out. Thanks again for the report.
36000 commentedon Sep 23, 2020
Do you have any tips / tricks on how to speed up this gif creation process? I don't know the details, but it seems like each time I call write_image, under the hood, I am opening up the same figure using chromium. Is there an easy way to open the figure once, then rotate and take screenshots within the browser?
jonmmease commentedon Sep 23, 2020
I don't know of anything different to do right now.
It would take some benchmarking to hone in on what would actually is taking so long right now. I expect that a lot of the time is being taken up with JSON serialization (Python to C++, then C++ to JavaScript) given the size of the figures.
Do these figures use large numpy arrays? We've talked about optimizing this serialization path in the past, but haven't gotten it off the ground yet.
All that said, I could see a place for adding support to kaleido for batching image export requests. One option would be to support inputting figures that define
frames(https://plotly.com/python/animations/) and exporting these to a gif. For your case of rotating a camera, I think the frames could each be pretty small, basically just defining the camera orientation.36000 commentedon Sep 23, 2020
My inputs to the scatter3d traces are long numpy arrays, I think those are what make the figures so big. I like that last idea of adding support for inputting figures that define frames.
Add explicit shutdown of kaleido scope