<jupyter_start><jupyter_text>Demonstrate animation_tools moduleThis notebook demonstrates the tools in the `clawpack.visclaw.animation_tools` module (new in Version 5.4.0), which facilitates creating a list of figures or images and viewing them as an animation.  This notebook can be found in `$CLAW/apps/notebooks/visclaw`, see  for instructions for cloning the Clawpack `apps` repository.Several approaches are supported: - Using the `ipywidgets.interact` allows generating an interactive widget for sweeping through the frames.  This may be easiest to work with when running a notebook interactively, including when using .  - Using the `JSAnimation` package from https://github.com/jakevdp/JSAnimation creates animations with controls that allow viewing as a movie more easily. You can also create a stand-alone html file of an animation to post on the web.  Moreover movies created with JSAnimation and saved with the notebook will operate properly when viewed via `nbviewer` or when viewing a notebook on GitHub.  However, do not try to run such a movie on a binder instance, or the JavaScript will go into an infinite loop.  - The `JSAnimation` version can also be embedded in another webpage or in Sphinx documentation, using the `animation_tools.make_html` and `animation_tools.make_rst` functions.  - An `mp4` file can also be created using the `animation_tools.make_mp4` function.  Converting frames in a Clawpack `_plots` directory to animations:  The script [make_anim.py](make_anim.py) illustrates how to turn the png files in a Clawpack `_plots` directory into stand-alone `.html`, `.mp4`, or `.rst` files.<jupyter_code>%pylab inline
from __future__ import print_function
from ipywidgets import interact, interact_manual
import ipywidgets
import os
import io
import base64
from IPython.display import display, FileLink, FileLinks, HTML
from clawpack.visclaw import animation_tools<jupyter_output><empty_output><jupyter_text>Generate a set of figures to use for demosYou can make figures however you want.  As they are created, append to a list called `figs`:<jupyter_code>figs = []
x = linspace(0,1,1000)

for k in range(11):
    fig = figure(figsize=(5,3))
    plot(x, sin(2*pi*k*x), 'b')
    ylim(-1.1,1.1)
    title('$\sin(2\pi kx)$ for k = %s' % k)
    figs.append(fig)
    close(fig)<jupyter_output><empty_output><jupyter_text>Animating a list of figuresThe tool `animation_tools.interact_animate_figs` can be used to create an interact that loops over all the frames contained in a list of figures.<jupyter_code>animation_tools.interact_animate_figs(figs)<jupyter_output><empty_output><jupyter_text>Passing in the argument `manual=True` will use the widget `interact_manual` instead of `interact`.  This refrains from updating the image as you move the slider bar.  Instead you move the slider as desired and then click on the `Run` button to re-display the image.  This is useful if there are many frames and you want to be able to jump to around without all the intermediate frames being displayed, which can slow down the response significantly.<jupyter_code>animation_tools.interact_animate_figs(figs, manual=True)<jupyter_output><empty_output><jupyter_text>The argument `TextInput=True` can be specified to produce a text input cell rather than a slider bar:<jupyter_code>animation_tools.interact_animate_figs(figs, manual=True, TextInput=True)<jupyter_output>Valid frameno values: from 0 to 10<jupyter_text>Animating a list of imagesThe tool `animation_tools.interact_animate_images` can be used to create an interact that loops over all the frames contained in a list of images rather than figures. The images can be generated from a list of figures, as illustrated in the next cell.  Or they can be read in from a directory of png files, for example, as illustrated later.This function also takes the arguments `manual` and `TextInput` as described above, with default values `False`.<jupyter_code>images = animation_tools.make_images(figs)
animation_tools.interact_animate_images(images, figsize=(6,3))<jupyter_output><empty_output><jupyter_text>JSAnimationA list of images can also be turned into a JSAnimation inline plot:<jupyter_code>animation_tools.JSAnimate_images(images, figsize=(6,3))<jupyter_output><empty_output><jupyter_text>Stand-alone html fileAlternatively, a separate stand-alone html page can be created with the JSAnimation.  This can be posted on the web for others to view, for example.<jupyter_code>anim = animation_tools.JSAnimate_images(images, figsize=(5,4));
# display(anim)  # to display it in the notebook
file_name = 'SineFunctions.html'
animation_tools.make_html(anim, file_name=file_name, title="Sine Functions", 
                          raw_html="Illustration of html file created by <tt>make_html</tt>")
FileLink(file_name)<jupyter_output>Created SineFunctions.html<jupyter_text>reStructured text fileYou can also produce a `.rst` file containing the animation, which can be incorporated into Sphinx documentation:<jupyter_code>file_name = 'SineFunctions.rst'
animation_tools.make_rst(anim, file_name=file_name)
FileLink(file_name)<jupyter_output>Created SineFunctions.rst
Imbed this in another rst file using:
.. include:: SineFunctions.rst<jupyter_text>Create an mp4 file Note that [ffmpeg](https://ffmpeg.org) must be installed in order to create an `mp4` file.  Give it a try:<jupyter_code>file_name = 'SineFunctions.mp4'
animation_tools.make_mp4(anim, file_name=file_name)
if os.path.isfile(file_name):
    video = io.open(file_name, 'r+b').read()
    encoded = base64.b64encode(video)
    data = '''<video alt="test" controls>
                    <source src="data:video/mp4;base64,{0}" type="video/mp4" />
                 </video>'''.format(encoded.decode('ascii'))
    print("If the mp4 file doesn't display here your browser (e.g. Chrome) might not support it")
else:
    data = "File not found: %s" % file_name
    
HTML(data=data)<jupyter_output>If the mp4 file doesn't display here your browser (e.g. Chrome) might not support it<jupyter_text>Create a directory of png filesSometimes it is useful to create a directory containing png files for each figure, or you might have such a directory generated by other means (e.g. from VisClaw).Here we create a directory named `_plots` to store png files for each frame:<jupyter_code>plotdir = '_plots'
animation_tools.make_plotdir(plotdir, clobber=True)<jupyter_output>Figure files for each frame will be stored in  _plots<jupyter_text>You can create frames any way you wish and then use `animation_tools.save_frame` to save each one.  You can also explicitly call `savefig`, but then you should construct file names such that `glob.glob` can be used to return a list of filenames in the directory that are in the correct order for plotting as frames in an animation.  The `animation_tools.save_frame` creates names such as `frame00000.png` etc. as shown below.  The optional argument `fname_base` can be used to change `frame` to something else.<jupyter_code>x = linspace(0,1,1000)

for k in range(5):
    fig = figure(figsize=(6,3))
    plot(x, cos(2*pi*k*x), 'b')
    ylim(-1.1,1.1)
    title('$\cos(2\pi kx)$ for k = %s' % k)
    animation_tools.save_frame(k, verbose=True)
    close(fig)
FileLinks(plotdir)<jupyter_output><empty_output><jupyter_text>Given such a directory of png files, we can read them in using `animation_tools.read_images` to produce a list of images:<jupyter_code>cosine_images = animation_tools.read_images(plotdir='_plots', fname_pattern='frame*.png')
print("Found %i images" % len(cosine_images))<jupyter_output>Found 5 images<jupyter_text>The resulting list of images `new_images` can now be animated using any of the tools illustrated earlier, e.g.<jupyter_code>animation_tools.interact_animate_images(cosine_images)<jupyter_output><empty_output><jupyter_text>Saving a list of figures or imagesIf you already have a list of figures or images and wish to create a directory containing them, the utilities `animation_tools.save_figs` or `animation_tools.save_images` can be used.  For example:<jupyter_code>animation_tools.save_images(cosine_images, plotdir='cosine_images', fname_base='wavenumber', 
                            format='png', verbose=True, kwargs={'dpi':150})
FileLinks('cosine_images')
reload_images = animation_tools.read_images(plotdir='cosine_images', fname_pattern='wavenumber*.png')
animation_tools.interact_animate_images(reload_images)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08222748815165877, 'fraction_numerical': 0.008530805687203791, 'mean_word_length': 5.656940063091483, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '263788', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2597, 'n_tokens_neox': 2497, 'n_words': 910, 'path': 'doc/_static/notebooks/animation_tools_demo.ipynb', 'repo_name': 'rjleveque/doc', 'size': '8440'}