Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Frame comparison pixel threshold (#3053)

Benjamin Hacklab57a6eb3594
diff --git a/manim/utils/testing/_frames_testers.py b/manim/utils/testing/_frames_testers.py
index 7c7fa4678..be0bc3844 100644
--- a/manim/utils/testing/_frames_testers.py
+++ b/manim/utils/testing/_frames_testers.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import contextlib
+import warnings
from pathlib import Path
import numpy as np
@@ -9,6 +10,9 @@ from manim import logger
from ._show_diff import show_diff_helper
+FRAME_ABSOLUTE_TOLERANCE = 1.01
+FRAME_MISMATCH_RATIO_TOLERANCE = 1e-5
+
class _FramesTester:
def __init__(self, file_path: Path, show_diff=False) -> None:
@@ -42,12 +46,27 @@ class _FramesTester:
np.testing.assert_allclose(
frame,
self._frames[frame_number],
- atol=1.01,
+ atol=FRAME_ABSOLUTE_TOLERANCE,
err_msg=f"Frame no {frame_number}. You can use --show_diff to visually show the difference.",
verbose=False,
)
self._frames_compared += 1
except AssertionError as e:
+ number_of_matches = np.isclose(
+ frame, self._frames[frame_number], atol=FRAME_ABSOLUTE_TOLERANCE
+ ).sum()
+ number_of_mismatches = frame.size - number_of_matches
+ if number_of_mismatches / frame.size < FRAME_MISMATCH_RATIO_TOLERANCE:
+ # we tolerate a small (< 0.001%) amount of pixel value errors
+ # in the tests, this accounts for minor OS dependent inconsistencies
+ self._frames_compared += 1
+ warnings.warn(
+ f"Mismatch of {number_of_mismatches} pixel values in frame {frame_number} "
+ f"against control data in {self._file_path}. Below error threshold, "
+ "continuing..."
+ )
+ return
+
if self._show_diff:
show_diff_helper(
frame_number,
diff --git a/tests/test_graphical_units/control_data/utils/pixel_error_threshold.npz b/tests/test_graphical_units/control_data/utils/pixel_error_threshold.npz
new file mode 100644
index 000000000..3221041c4
Binary files /dev/null and b/tests/test_graphical_units/control_data/utils/pixel_error_threshold.npz differ
diff --git a/tests/test_graphical_units/test_utils.py b/tests/test_graphical_units/test_utils.py
new file mode 100644
index 000000000..12ed860bc
--- /dev/null
+++ b/tests/test_graphical_units/test_utils.py
@@ -0,0 +1,11 @@
+from __future__ import annotations
+
+from manim.utils.testing.frames_comparison import frames_comparison
+
+__module_test__ = "utils"
+
+
+@frames_comparison
+def test_pixel_error_threshold(scene):
+ """Scene produces black frame, control data has 11 modified pixel values."""
+ pass