Animations in ipython (jupyter) notebook - ValueEr

2020-03-27 11:22发布

问题:

I have a jupyter notebook that produces an animation. It was working on my old laptop (xubuntu gnu/linux). Now on my new laptop (trisquel gnu/linux) it's not working. This makes me think I have a missing library or something like that but I can't work it out.

As a test, I went back to the tutorial where I learned how to do jupyter notebook animations in the first place (http://louistiao.me/posts/notebooks/embedding-matplotlib-animations-in-jupyter-notebooks/). I get the same problem when I run code cell [7].

The error is shown below.

Any help in solving this problem would be greatly appreciated.

Many thanks :)

[edit 1] If I try anim.save(...) instead of HTML(anim.to_html5_video()), I get the same problem.

[edit 2] If I download the notebook as a *.py file and run it using regular python (rather than ipython), then I get the same problem.

[edit 3] My ffmpeg installation appears to be working as I am able to manually create an animation from a series of images. I can use this as a work-around for now but it's very clunky and the HTML(anim.to_html5_video()) solution should work so still needs to be sorted.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-5114ccf53b4c> in <module>()
----> 1 HTML(anim.to_html5_video())

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in to_html5_video(self)
   1157                                 bitrate=rcParams['animation.bitrate'],
   1158                                 fps=1000. / self._interval)
-> 1159                 self.save(f.name, writer=writer)
   1160 
   1161             # Now open and base64 encode

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in save(self, filename, writer, fps, dpi, codec, bitrate, extra_args, metadata, extra_anim, savefig_kwargs)
   1011                         # TODO: See if turning off blit is really necessary
   1012                         anim._draw_next_frame(d, blit=False)
-> 1013                     writer.grab_frame(**savefig_kwargs)
   1014 
   1015         # Reconnect signal for first draw if necessary

/usr/local/lib/python2.7/contextlib.pyc in __exit__(self, type, value, traceback)
     33                 value = type()
     34             try:
---> 35                 self.gen.throw(type, value, traceback)
     36                 raise RuntimeError("generator didn't stop after throw()")
     37             except StopIteration, exc:

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in saving(self, *args, **kw)
    254             yield self
    255         finally:
--> 256             self.finish()
    257 
    258     def _run(self):

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in finish(self)
    274     def finish(self):
    275         'Finish any processing for writing the movie.'
--> 276         self.cleanup()
    277 
    278     def grab_frame(self, **savefig_kwargs):

/usr/local/lib/python2.7/site-packages/matplotlib/animation.pyc in cleanup(self)
    309     def cleanup(self):
    310         'Clean-up and collect the process used to write the movie file.'
--> 311         out, err = self._proc.communicate()
    312         self._frame_sink().close()
    313         verbose.report('MovieWriter -- '

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in communicate(self, input, timeout)
    925 
    926         try:
--> 927             stdout, stderr = self._communicate(input, endtime, timeout)
    928         finally:
    929             self._communication_started = True

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in _communicate(self, input, endtime, orig_timeout)
   1711             if _has_poll:
   1712                 stdout, stderr = self._communicate_with_poll(input, endtime,
-> 1713                                                              orig_timeout)
   1714             else:
   1715                 stdout, stderr = self._communicate_with_select(input, endtime,

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in _communicate_with_poll(self, input, endtime, orig_timeout)
   1767             select_POLLIN_POLLPRI = select.POLLIN | select.POLLPRI
   1768             if self.stdout:
-> 1769                 register_and_append(self.stdout, select_POLLIN_POLLPRI)
   1770                 stdout = self._fd2output[self.stdout.fileno()]
   1771             if self.stderr:

/usr/local/lib/python2.7/site-packages/subprocess32.pyc in register_and_append(file_obj, eventmask)
   1746             poller = select.poll()
   1747             def register_and_append(file_obj, eventmask):
-> 1748                 poller.register(file_obj.fileno(), eventmask)
   1749                 self._fd2file[file_obj.fileno()] = file_obj
   1750 

ValueError: I/O operation on closed file

回答1:

I ran into the same issue (on a mac os 10.12.2), and fixed it by reinstalling ffmpeg and enabling the x264 codec , which the to_html5_video() function needs to convert the animation to html5.

To install x264:

cd /my/path/where/i/keep/compiled/stuff
git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-static --enable-shared
make
make install
ldconfig # note: use for linux, not necessary for mac

source

To install ffmpeg with the x264 codec enabled:

cd /my/path/where/i/keep/compiled/stuff
git clone http://source.ffmpeg.org/git/ffmpeg.git
cd ffmpeg
./configure --enable-gpl --enable-libx264
make
make install
ldconfig # note: use for linux, not necessary for mac

source