Repository: ManimCommunity/manim
File: manim/utils/testing/_show_diff.py (Python)

Code before:
    control_data_filename: str,
) -> None:
    """Will visually display with matplotlib differences between frame generated and the one expected."""
    import matplotlib.gridspec as gridspec
    import matplotlib.pyplot as plt

    gs = gridspec.GridSpec(2, 2)
    fig = plt.figure()
    fig.suptitle(f"Test difference summary at frame {frame_number}", fontsize=16)

    ax1 = fig.add_subplot(gs[0, 0])
    ax1.imshow(frame_data)
    ax1.set_title("Generated")

    ax2 = fig.add_subplot(gs[0, 1], sharex=ax1, sharey=ax1)
    ax2.imshow(expected_frame_data)
    ax2.set_title("Expected")

    ax3 = fig.add_subplot(gs[1, 0], sharex=ax1, sharey=ax1)
    generated_is_expected = (frame_data == expected_frame_data).all(2)
    expected_is_black = (expected_frame_data == BLACK.to_int_rgba()).all(2)
    frame_is_black = (frame_data == BLACK.to_int_rgba()).all(2)
    diff_im = expected_frame_data.copy()
    diff_im[generated_is_expected & ~expected_is_black] = PURE_GREEN.to_int_rgba()
    diff_im[~generated_is_expected & ~expected_is_black] = PURE_RED.to_int_rgba()
    diff_im[~generated_is_expected & ~frame_is_black] = PURE_RED.to_int_rgba()
    ax3.imshow(diff_im, interpolation="nearest")
    ax3.set_title("Difference")

    fig.text(
        x=0.55,
        y=0.45,
        s=f"Testname:\n {control_data_filename[:-4]}",
        wrap=True,
        transform=fig.transFigure,
        fontsize=14,
        verticalalignment="top",
    )
    number_of_matches = np.isclose(
        frame_data, expected_frame_data, atol=FRAME_ABSOLUTE_TOLERANCE
    ).sum()
    number_of_mismatches = frame_data.size - number_of_matches
    fig.text(
        x=0.55,
        y=0.30,
        s=f"Difference count:\n {number_of_mismatches}",
        transform=fig.transFigure,
        fontsize=14,
        verticalalignment="top",
    )
    fig.text(

Review comment (suggestion):
Indeed, coloring red only those places where the expected frame is not black, as I suggested before, was not correct. _Any_ difference should be colored red.

Because of what I mentioned before, it is not really necessary to define a new `frame_is_black` array: simply color red all the pixels where there is a difference. In other words, the correct condition (which you already have) can be simply reduced to `~generated_is_expected`.

Effectively, if you let `A = generated_is_expected`, `B = expected_is_black`, and `C = frame_is_black`, then the current logic is to paint red when `(~A & ~B) | (~A & ~C)`, which can be factored as `~A & (~B | ~C)` and then expressed as `~(A | (B & C))`. If you notice, having `B & C` be true (expected pixel is black and generated pixel is black) implies that both pixels are the same, i.e. `A` is also true. This means everything can be reduced to simply `~A`. You only need to check `A`: if it's true, you don't need to check anything else. If it's false, there's no way that `B & C` is true, so there's no point in checking it anyways.
```py
    generated_is_expected = (frame_data == expected_frame_data).all(2)
    expected_is_black = (expected_frame_data == BLACK.to_int_rgba()).all(2)
    diff_im = expected_frame_data.copy()
    diff_im[generated_is_expected & ~expected_is_black] = PURE_GREEN.to_int_rgba()
    diff_im[~generated_is_expected] = PURE_RED.to_int_rgba()
```

Code after:
    control_data_filename: str,
) -> None:
    """Will visually display with matplotlib differences between frame generated and the one expected."""
    import matplotlib.gridspec as gridspec
    import matplotlib.pyplot as plt

    gs = gridspec.GridSpec(2, 2)
    fig = plt.figure()
    fig.suptitle(f"Test difference summary at frame {frame_number}", fontsize=16)

    ax1 = fig.add_subplot(gs[0, 0])
    ax1.imshow(frame_data)
    ax1.set_title("Generated")

    ax2 = fig.add_subplot(gs[0, 1], sharex=ax1, sharey=ax1)
    ax2.imshow(expected_frame_data)
    ax2.set_title("Expected")

    ax3 = fig.add_subplot(gs[1, 0], sharex=ax1, sharey=ax1)
    generated_is_expected = (frame_data == expected_frame_data).all(2)
    expected_is_black = (expected_frame_data == BLACK.to_int_rgba()).all(2)
    diff_im = expected_frame_data.copy()
    diff_im[generated_is_expected & ~expected_is_black] = PURE_GREEN.to_int_rgba()
    diff_im[~generated_is_expected] = PURE_RED.to_int_rgba()
    ax3.imshow(diff_im, interpolation="nearest")
    ax3.set_title("Difference")

    fig.text(
        x=0.55,
        y=0.46,
        s=f"Testname:\n {control_data_filename[:-4]}",
        wrap=True,
        transform=fig.transFigure,
        fontsize=12,
        verticalalignment="top",
    )
    number_of_matches = np.isclose(
        frame_data, expected_frame_data, atol=FRAME_ABSOLUTE_TOLERANCE
    ).sum()
    number_of_mismatches = frame_data.size - number_of_matches
    fig.text(
        x=0.55,
        y=0.34,
        s=f"Difference count:\n {number_of_mismatches}",
        transform=fig.transFigure,
        fontsize=12,
        verticalalignment="top",
    )
    fig.text(
        x=0.55,
        y=0.22,