Setup basic frame comparison unit testing.

This commit is contained in:
Alec Helbling
2023-01-31 10:27:51 -05:00
parent bae4543576
commit ae6fd8a230
7 changed files with 403 additions and 8 deletions

58
tests/conftest.py Normal file
View File

@ -0,0 +1,58 @@
from __future__ import annotations
import os
import sys
import pytest
from manim import config, tempconfig
def pytest_addoption(parser):
parser.addoption(
"--skip_slow",
action="store_true",
default=False,
help="Will skip all the slow marked tests. Slow tests are arbitrarily marked as such.",
)
parser.addoption(
"--show_diff",
action="store_true",
default=False,
help="Will show a visual comparison if a graphical unit test fails.",
)
parser.addoption(
"--set_test",
action="store_true",
default=False,
help="Will create the control data for EACH running tests. ",
)
@pytest.fixture(scope="session")
def python_version():
# use the same python executable as it is running currently
# rather than randomly calling using python or python3, which
# may create problems.
return sys.executable
@pytest.fixture
def reset_cfg_file():
cfgfilepath = os.path.join(os.path.dirname(__file__), "test_cli", "manim.cfg")
with open(cfgfilepath) as cfgfile:
original = cfgfile.read()
yield
with open(cfgfilepath, "w") as cfgfile:
cfgfile.write(original)
@pytest.fixture
def using_opengl_renderer():
"""Standard fixture for running with opengl that makes tests use a standard_config.cfg with a temp dir."""
with tempconfig({"renderer": "opengl"}):
yield
# as a special case needed to manually revert back to cairo
# due to side effects of setting the renderer
config.renderer = "cairo"

Binary file not shown.

63
tests/test_camera_move.py Normal file
View File

@ -0,0 +1,63 @@
from manim import *
from manim_ml.neural_network.layers.convolutional_2d import Convolutional2DLayer
from manim_ml.neural_network.layers.feed_forward import FeedForwardLayer
from manim_ml.neural_network.layers.image import ImageLayer
from manim_ml.neural_network.neural_network import NeuralNetwork
from PIL import Image
import numpy as np
# Make the specific scene
config.pixel_height = 1200
config.pixel_width = 1900
config.frame_height = 6.0
config.frame_width = 6.0
class NeuralNetworkScene(ThreeDScene):
"""Test Scene for the Neural Network"""
def play_camera_follow_forward_pass(
self,
neural_network,
buffer=0.1
):
per_layer_animations = neural_network.make_forward_pass_animation(
return_per_layer_animations=True
)
all_layers = neural_network.all_layers
# Compute the width and height of the frame
max_width = 0
max_height = 0
for layer_index in range(1, len(all_layers) - 1):
prev_layer = all_layers[layer_index - 1]
current_layer = all_layers[layer_index]
next_layer = all_layers[layer_index + 1]
group = Group(prev_layer, current_layer, next_layer)
max_width = max(max_width, group.width)
max_height = max(max_height, group.height)
frame_width = max_width * (1 + buffer)
frame_height = max_height * (1 + buffer)
# Go through each animation
for layer_index in range(1, len(all_layers)):
layer_animation = per_layer_animations[layer_index]
def construct(self):
# Make the Layer object
image = Image.open("../assets/mnist/digit.jpeg")
numpy_image = np.asarray(image)
nn = NeuralNetwork([
ImageLayer(numpy_image, height=1.5),
Convolutional2DLayer(1, 7, filter_spacing=0.32),
Convolutional2DLayer(3, 5, 3, filter_spacing=0.32),
Convolutional2DLayer(5, 3, 3, filter_spacing=0.18),
FeedForwardLayer(3),
FeedForwardLayer(3),
],
layer_spacing=0.25,
)
nn.move_to(ORIGIN)
# Make Animation
self.add(nn)
# self.play(Create(nn))
self.play_camera_follow_forward_pass(nn)

View File

@ -1,14 +1,17 @@
from manim import *
from manim_ml.utils.testing.frames_comparison import frames_comparison
from manim_ml.neural_network import NeuralNetwork, FeedForwardLayer
class FeedForwardScene(Scene):
__module_test__ = "feed_forward"
def construct(self):
nn = NeuralNetwork([
FeedForwardLayer(3),
FeedForwardLayer(5),
FeedForwardLayer(3)
])
@frames_comparison
def test_FeedForwardScene(scene):
"""Tests the appearance of a feed forward network"""
nn = NeuralNetwork([
FeedForwardLayer(3),
FeedForwardLayer(5),
FeedForwardLayer(3)
])
self.add(nn)
scene.add(nn)