From 1427fe5cec9abd8f0a7a71f341cee0b699c5634b Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Thu, 7 Nov 2013 12:27:59 -0800 Subject: [PATCH] Able to add movies to the python notebook. using utils.animate(fig,animate,init) --- SimPEG/utils/__init__.py | 1 + SimPEG/utils/ipythonUtils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 SimPEG/utils/ipythonUtils.py diff --git a/SimPEG/utils/__init__.py b/SimPEG/utils/__init__.py index ae83222e..6a681f57 100644 --- a/SimPEG/utils/__init__.py +++ b/SimPEG/utils/__init__.py @@ -9,3 +9,4 @@ from matutils import getSubArray, mkvc, ndgrid, ind2sub, sub2ind from sputils import spzeros, kron3, speye, sdiag from lomutils import volTetra, faceInfo, inv2X2BlockDiagonal, inv3X3BlockDiagonal, indexCube, exampleLomGird from interputils import interpmat +from ipythonUtils import easyAnimate as animate diff --git a/SimPEG/utils/ipythonUtils.py b/SimPEG/utils/ipythonUtils.py new file mode 100644 index 00000000..ca77a2e3 --- /dev/null +++ b/SimPEG/utils/ipythonUtils.py @@ -0,0 +1,29 @@ +from tempfile import NamedTemporaryFile +import matplotlib.pyplot as plt +from matplotlib import animation +from IPython.display import HTML + +# http://jakevdp.github.io/blog/2013/05/12/embedding-matplotlib-animations/ +# http://www.renevolution.com/how-to-install-ffmpeg-on-mac-os-x/ + +VIDEO_TAG = """""" + +def anim_to_html(anim): + if not hasattr(anim, '_encoded_video'): + with NamedTemporaryFile(suffix='.mp4') as f: + anim.save(f.name, fps=20, extra_args=['-vcodec', 'libx264', '-pix_fmt', 'yuv420p']) + video = open(f.name, "rb").read() + anim._encoded_video = video.encode("base64") + + return VIDEO_TAG.format(anim._encoded_video) + +def display_animation(anim): + plt.close(anim._fig) + return anim_to_html(anim) + +animation.Animation._repr_html_ = display_animation + +easyAnimate = animation.FuncAnimation