Skip to content

Commit ab57a6e

Browse files
Frame comparison pixel threshold (#3053)
* implemented threshold for number of pixel value errors * fix implementation, throw proper warning * added test for pixel value error threshold * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove forgotten debug print * introduce constants for frame_comparison tolerance * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * rename constant Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2044ef8 commit ab57a6e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

manim/utils/testing/_frames_testers.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import contextlib
4+
import warnings
45
from pathlib import Path
56

67
import numpy as np
@@ -9,6 +10,9 @@
910

1011
from ._show_diff import show_diff_helper
1112

13+
FRAME_ABSOLUTE_TOLERANCE = 1.01
14+
FRAME_MISMATCH_RATIO_TOLERANCE = 1e-5
15+
1216

1317
class _FramesTester:
1418
def __init__(self, file_path: Path, show_diff=False) -> None:
@@ -42,12 +46,27 @@ def check_frame(self, frame_number: int, frame: np.ndarray):
4246
np.testing.assert_allclose(
4347
frame,
4448
self._frames[frame_number],
45-
atol=1.01,
49+
atol=FRAME_ABSOLUTE_TOLERANCE,
4650
err_msg=f"Frame no {frame_number}. You can use --show_diff to visually show the difference.",
4751
verbose=False,
4852
)
4953
self._frames_compared += 1
5054
except AssertionError as e:
55+
number_of_matches = np.isclose(
56+
frame, self._frames[frame_number], atol=FRAME_ABSOLUTE_TOLERANCE
57+
).sum()
58+
number_of_mismatches = frame.size - number_of_matches
59+
if number_of_mismatches / frame.size < FRAME_MISMATCH_RATIO_TOLERANCE:
60+
# we tolerate a small (< 0.001%) amount of pixel value errors
61+
# in the tests, this accounts for minor OS dependent inconsistencies
62+
self._frames_compared += 1
63+
warnings.warn(
64+
f"Mismatch of {number_of_mismatches} pixel values in frame {frame_number} "
65+
f"against control data in {self._file_path}. Below error threshold, "
66+
"continuing..."
67+
)
68+
return
69+
5170
if self._show_diff:
5271
show_diff_helper(
5372
frame_number,
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
from manim.utils.testing.frames_comparison import frames_comparison
4+
5+
__module_test__ = "utils"
6+
7+
8+
@frames_comparison
9+
def test_pixel_error_threshold(scene):
10+
"""Scene produces black frame, control data has 11 modified pixel values."""
11+
pass

0 commit comments

Comments
 (0)