You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Chameleon/integration/ptz17_causal_image_rate/test_image_rate.py

74 lines
2.9 KiB
Python

from __future__ import annotations
import ast
from dataclasses import dataclass
import math
import os
from pathlib import Path
from types import SimpleNamespace
import pytest
@pytest.fixture(scope="module")
def controller():
source = Path(os.environ["PTZ17_RATE_SOURCE"])
names = {"_PTZ17ZoomTuning", "_ImageRateSample", "_update_ptz17_image_rate", "_update_image_rate_ema"}
selected = [node for node in ast.parse(source.read_bytes()).body
if isinstance(node, (ast.FunctionDef, ast.ClassDef)) and node.name in names]
assert {node.name for node in selected} == names
ns = {"config": SimpleNamespace(VEL_SMOOTH=.75, AIM_IMAGE_RATE_FAST_SMOOTH=.45),
"math": math, "clipf": lambda value, lo, hi: min(hi, max(lo, value)),
"dataclass": dataclass, "__name__": __name__}
exec(compile(ast.Module(body=selected, type_ignores=[]), str(source), "exec"), ns)
ns["_PTZ17_ZOOM"] = ns["_PTZ17ZoomTuning"]()
return ns["_update_ptz17_image_rate"]
def state() -> dict:
return dict(vx=0., vy=0., vx_fast=0., vy_fast=0.)
@pytest.mark.parametrize("timestamp", [10.0, 9.992, math.nan, math.inf])
def test_bad_sample_retains_the_causal_baseline(controller, timestamp):
st = state()
assert controller(st, 10.0, .5, .5) == "seed"
snapshot = st.copy()
assert controller(st, timestamp, .8, .6) in ("noncausal", "invalid")
assert st == snapshot
assert controller(st, 10.1, .502, .5) == "updated"
assert st["vx"] == pytest.approx(.005)
assert st["vx_fast"] == pytest.approx(.011)
def test_short_gaps_accumulate_without_polluting_filter(controller):
st = state()
assert controller(st, 10., .5, .5) == "seed"
assert controller(st, 10.02, .55, .5) == "short"
assert st["vx"] == 0
assert controller(st, 10.015, .5, .5) == "noncausal"
assert controller(st, 10.05, .503, .5) == "updated"
assert st["vx"] == pytest.approx(.015)
assert st["vx_fast"] == pytest.approx(.033)
def test_short_valid_sample_cannot_suppress_a_large_excursion(controller):
st = state()
controller(st, 10., .5, .5)
controller(st, 10.1, .5, .5)
assert controller(st, 10.112, .5, .5) == "short_checked"
assert controller(st, 10.118, .505, .5) == "short"
assert st["vx"] == 0.0
def test_patch_is_on_the_control_path_and_reset_on_owner_changes():
source = Path(os.environ["PTZ17_RATE_SOURCE"])
tree = ast.parse(source.read_bytes())
methods = {node.name: ast.unparse(node) for node in tree.body if isinstance(node, ast.FunctionDef)}
control = methods["postprocess_control"]
assert "image_rate_sample = _update_ptz17_image_rate(st, now, cxn, cyn)" in control
assert "and image_rate_ready" in control
assert "frame_clock='visual_receipt' if visual_only else 'control_entry'" in control
for name in ("_reset_aim_motion_feedforward", "_reset_aim_controller_state"):
assert "st.pop('ptz17_image_rate_ref', None)" in methods[name]