Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add extract_frames utility to help visualize test control data (#2872)

Kevin Lubick92e9ceee6163
diff --git a/docs/source/contributing/testing.rst b/docs/source/contributing/testing.rst
index 7f60a5207..f17bac1d1 100644
--- a/docs/source/contributing/testing.rst
+++ b/docs/source/contributing/testing.rst
@@ -36,8 +36,8 @@ At the moment there are three types of tests:
#. Graphical unit tests:
Because ``manim`` is a graphics library, we test frames. To do so, we create test scenes that render a specific feature.
When pytest runs, it compares the result of the test to the control data, either at 6 fps or just the last frame. If it matches, the tests
- pass. If the test and control data differ, the tests fail. You can
- use ``--show_diff`` flag with ``pytest`` to visually see the differences.
+ pass. If the test and control data differ, the tests fail. You can use ``--show_diff`` flag with ``pytest`` to visually
+ see the differences. The ``extract_frames.py`` script lets you see all the frames of a test.
#. Videos format tests:
@@ -222,6 +222,15 @@ For the example above, it would be
(``-s`` is here to see manim logs, so you can see what's going on).
+If you want to see all the control data frames (e.g. to make sure your test is doing what you want), use the
+``extract_frames.py`` script. The first parameter is the path to a ``.npz`` file and the second parameter is the
+directory you want the frames created. The frames will be named ``frame0.png``, ``frame1.png``, etc.
+
+.. code-block:: bash
+
+ python scripts/extract_frames.py tests/test_graphical_units/control_data/plot/axes.npz output
+
+
Please make sure to add the control data to git as soon as it is produced with ``git add <your-control-data.npz>``.
diff --git a/scripts/extract_frames.py b/scripts/extract_frames.py
new file mode 100644
index 000000000..8771bd391
--- /dev/null
+++ b/scripts/extract_frames.py
@@ -0,0 +1,42 @@
+import os
+import sys
+
+import numpy as np
+from PIL import Image
+
+
+def main():
+ if len(sys.argv) != 3:
+ print_usage()
+ sys.exit(1)
+ npz_file = sys.argv[1]
+ output_folder = sys.argv[2]
+ if not os.path.exists(output_folder):
+ os.makedirs(output_folder)
+
+ data = np.load(npz_file)
+ if "frame_data" not in data:
+ print("The given file did not have frame_data.")
+ print("Are you sure this is from a Manim Graphical Unit Test?")
+ sys.exit(2)
+ frames = data["frame_data"]
+ for i, frame in enumerate(frames):
+ img = Image.fromarray(frame)
+ img.save(os.path.join(output_folder, f"frame{i}.png"))
+ print(f"Saved {len(frames)} frames to {output_folder}")
+
+
+def print_usage():
+ print("Manim Graphical Test Frame Extractor")
+ print(
+ "This tool outputs the frames of a Graphical Unit Test "
+ "stored within a .npz file, typically found under "
+ r"//tests/test_graphical_units/control_data"
+ )
+ print()
+ print("usage:")
+ print("python3 extract_frames.py npz_file output_directory")
+
+
+if __name__ == "__main__":
+ main()