|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import numpy as np |
| 4 | +from matplotlib import pyplot as plt |
| 5 | +from matplotlib import animation |
| 6 | +from matplotlib.testing.noseclasses import KnownFailureTest |
| 7 | + |
| 8 | + |
| 9 | +# Smoke test for saving animations. In the future, we should probably |
| 10 | +# design more sophisticated tests which compare resulting frames a-la |
| 11 | +# matplotlib.testing.image_comparison |
| 12 | +def test_save_animation_smoketest(): |
| 13 | + writers = ['ffmpeg', 'ffmpeg_file', |
| 14 | + 'mencoder', 'mencoder_file', |
| 15 | + 'avconv', 'avconv_file', |
| 16 | + 'imagemagick', 'imagemagick_file'] |
| 17 | + |
| 18 | + for writer in writers: |
| 19 | + if writer.startswith('imagemagick'): |
| 20 | + extension = '.gif' |
| 21 | + else: |
| 22 | + extension = '.mp4' |
| 23 | + |
| 24 | + yield check_save_animation, writer, extension |
| 25 | + |
| 26 | + |
| 27 | +def check_save_animation(writer, extension='.mp4'): |
| 28 | + if not animation.writers.is_available(writer): |
| 29 | + raise KnownFailureTest("writer '%s' not available on this system" |
| 30 | + % writer) |
| 31 | + fig, ax = plt.subplots() |
| 32 | + line, = ax.plot([], []) |
| 33 | + |
| 34 | + def init(): |
| 35 | + line.set_data([], []) |
| 36 | + return line, |
| 37 | + |
| 38 | + def animate(i): |
| 39 | + x = np.linspace(0, 10, 100) |
| 40 | + y = np.sin(x + i) |
| 41 | + line.set_data(x, y) |
| 42 | + return line, |
| 43 | + |
| 44 | + fid, fname = tempfile.mkstemp(suffix=extension) |
| 45 | + |
| 46 | + anim = animation.FuncAnimation(fig, animate, init_func=init, frames=5) |
| 47 | + anim.save(fname, fps=30, writer=writer) |
| 48 | + |
| 49 | + os.remove(fname) |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + import nose |
| 54 | + nose.runmodule() |
0 commit comments