|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Regression coverage for spatialmath.base.animate. |
| 5 | +
|
| 6 | +animate()/tranimate()/tranimate2() build a matplotlib FuncAnimation, but |
| 7 | +under a non-interactive backend with nothing pumping a real event loop |
| 8 | +(exactly the CI/test environment -- see conftest.py, which forces the Agg |
| 9 | +backend for the whole session) its per-frame update() callback never |
| 10 | +actually fires. Merely constructing the FuncAnimation, or calling |
| 11 | +.animate() and letting the result be garbage collected, proves nothing |
| 12 | +about whether the interpolation code (trinterp/trinterp2) runs or is |
| 13 | +correct -- it only proves construction didn't raise. |
| 14 | +
|
| 15 | +FuncAnimation.save() is different: it synchronously steps through every |
| 16 | +frame and invokes the real update() callback for each one, regardless of |
| 17 | +backend -- the same thing a real playback or movie export would do. We |
| 18 | +drive that here with PillowWriter (pure Python, ships with |
| 19 | +matplotlib+Pillow, no external ffmpeg binary) purely to force every frame |
| 20 | +through the real callback, and record what it actually computed, rather |
| 21 | +than analyzing the saved output itself. |
| 22 | +""" |
| 23 | + |
| 24 | +import os |
| 25 | +import tempfile |
| 26 | +import unittest |
| 27 | +from unittest.mock import patch |
| 28 | + |
| 29 | +import matplotlib |
| 30 | + |
| 31 | +matplotlib.use("Agg") |
| 32 | +import matplotlib.pyplot as plt |
| 33 | +from matplotlib.animation import PillowWriter |
| 34 | + |
| 35 | +import spatialmath.base as smb |
| 36 | +from spatialmath import SE2, SE3 |
| 37 | + |
| 38 | + |
| 39 | +def _drive_all_frames(fa): |
| 40 | + tmp = tempfile.mktemp(suffix=".gif") |
| 41 | + try: |
| 42 | + fa.save(tmp, writer=PillowWriter(fps=5)) |
| 43 | + finally: |
| 44 | + if os.path.exists(tmp): |
| 45 | + os.remove(tmp) |
| 46 | + |
| 47 | + |
| 48 | +class TestAnimate2(unittest.TestCase): |
| 49 | + def test_drives_real_frames(self): |
| 50 | + plt.close("all") |
| 51 | + end = SE2.Rand() |
| 52 | + start = SE2.Rand() |
| 53 | + nframes = 5 |
| 54 | + |
| 55 | + real_trinterp2 = smb.trinterp2 |
| 56 | + calls = [] |
| 57 | + |
| 58 | + def recording_trinterp2(*args, **kwargs): |
| 59 | + calls.append(kwargs["s"]) |
| 60 | + return real_trinterp2(*args, **kwargs) |
| 61 | + |
| 62 | + with patch("spatialmath.base.trinterp2", side_effect=recording_trinterp2): |
| 63 | + fa = end.animate(start=start, dims=[-2, 2], nframes=nframes, repeat=False) |
| 64 | + _drive_all_frames(fa) |
| 65 | + |
| 66 | + self.assertEqual(len(calls), nframes) |
| 67 | + self.assertAlmostEqual(min(calls), 0.0) |
| 68 | + self.assertAlmostEqual(max(calls), 1.0) |
| 69 | + plt.close("all") |
| 70 | + |
| 71 | + |
| 72 | +class TestAnimate3(unittest.TestCase): |
| 73 | + def test_drives_real_frames(self): |
| 74 | + plt.close("all") |
| 75 | + end = SE3.Rand() |
| 76 | + start = SE3.Rand() |
| 77 | + nframes = 5 |
| 78 | + |
| 79 | + real_trinterp = smb.trinterp |
| 80 | + calls = [] |
| 81 | + |
| 82 | + def recording_trinterp(*args, **kwargs): |
| 83 | + calls.append(kwargs["s"]) |
| 84 | + return real_trinterp(*args, **kwargs) |
| 85 | + |
| 86 | + with patch("spatialmath.base.trinterp", side_effect=recording_trinterp): |
| 87 | + fa = end.animate(start=start, dims=[-2, 2], nframes=nframes, repeat=False) |
| 88 | + _drive_all_frames(fa) |
| 89 | + |
| 90 | + self.assertEqual(len(calls), nframes) |
| 91 | + self.assertAlmostEqual(min(calls), 0.0) |
| 92 | + self.assertAlmostEqual(max(calls), 1.0) |
| 93 | + plt.close("all") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + unittest.main() |
0 commit comments