mirror of
https://github.com/azaion/gps-denied-onboard.git
synced 2026-06-22 21:41:13 +00:00
[AZ-697] [AZ-702] tlog GPS truth + KHP20S30 factory calibration
Batch 98 (cycle 2) — first two PBIs of epic AZ-696 (real-flight validation harness): AZ-697: direct binary-tlog GPS-truth extractor - New src/gps_denied_onboard/replay_input/tlog_ground_truth.py reads GLOBAL_POSITION_INT (with GPS_RAW_INT fallback) from a binary ArduPilot tlog via pymavlink.mavutil and returns a frozen+slotted TlogGroundTruth DTO with per-record ts_ns / lat_deg / lon_deg / alt_m / hdg_deg / vx_m_s / vy_m_s / vz_m_s. - Promoted l2_horizontal_m + match_percentage + GroundTruthRow from tests/e2e/replay/_helpers.py into the new production module src/gps_denied_onboard/helpers/gps_compare.py. The e2e helper now re-exports the same objects (identity, not copies) so existing test imports continue working untouched. - tests/e2e/replay/conftest.py prefers the real derkachi.tlog when present, falls back to the CSV synth path otherwise. - 22 new unit tests cover AC-1..AC-5 (mypy --strict subprocess test included). All passing. AZ-702: Topotek KHP20S30 factory-sheet camera calibration - New _docs/00_problem/input_data/flight_derkachi/khp20s30_factory.json: fx = fy = 4644.444, cx = 960, cy = 540, HFOV ~ 23.3 deg, VFOV ~ 13.2 deg, computed from the published 8.5 mm focal length + 1/2.8" sensor + 1920x1080 capture at lowest zoom step. Distortion zeroed, body_to_camera_se3 = identity with nadir convention. Acquisition method explicitly recorded as factory_sheet so downstream code can expect higher residual error than a lab calibration. - _docs/00_problem/input_data/flight_derkachi/camera_info.md updated to document the assumptions, expected residual error window, and conftest pick-up rule. - tests/e2e/replay/conftest.py::_calibration_path() prefers khp20s30_factory.json when present, falls back to adti26.json. - 9 new unit tests cover AC-1..AC-4 (schema, intrinsics traceback, doc reference, conftest pick-up). All passing. Test run: 45 new tests, all passing. Full-suite gate deferred to Step 16 (after the last batch in cycle 2 per the implement skill). Adjacent note (not fixed in this batch, recorded in the batch report): auto_sync.py has the same redundant pymavlink type:ignore + a few numpy/cv2 mypy --strict issues. None on this batch's path. Refs: _docs/03_implementation/batch_98_cycle2_report.md Refs: _docs/02_tasks/done/AZ-697_tlog_ground_truth_extractor.md Refs: _docs/02_tasks/done/AZ-702_khp20s30_calibration.md Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+15
-105
@@ -1,18 +1,22 @@
|
||||
"""Helpers shared by the AZ-404 E2E replay tests.
|
||||
|
||||
The numerical kernels (``l2_horizontal_m``, ``match_percentage``,
|
||||
``GroundTruthRow``) moved into production code at
|
||||
:mod:`gps_denied_onboard.helpers.gps_compare` in AZ-697; they're
|
||||
re-exported here so existing import sites stay stable.
|
||||
|
||||
* :func:`parse_jsonl` — read the ``JsonlReplaySink`` output into a list
|
||||
of dicts with one entry per emit.
|
||||
* :func:`l2_horizontal_m` — WGS84-aware L2 horizontal distance between
|
||||
two ``(lat, lon)`` pairs in metres.
|
||||
* :func:`match_percentage` — share of estimator emissions whose
|
||||
L2 distance to the closest ground-truth row is within a threshold.
|
||||
* :class:`CapturingMavlinkTransport` — test-only ``MavlinkTransport``
|
||||
impl that records every ``write`` so AC-4b can compare the byte
|
||||
streams produced by ``compose_root(config_live)`` vs.
|
||||
``compose_root(config_replay)``.
|
||||
* :func:`load_ground_truth_csv` — the IMU CSV's ``GLOBAL_POSITION_INT``
|
||||
columns ARE the AC-3 reference (the original tlog's GPS rows
|
||||
exported to CSV); this helper materialises them.
|
||||
exported to CSV); this helper materialises them. Retained for the
|
||||
CSV-only fallback path; the real-tlog branch uses
|
||||
:func:`gps_denied_onboard.replay_input.load_tlog_ground_truth`
|
||||
instead.
|
||||
|
||||
All functions are pure / deterministic and stay safely importable on
|
||||
dev macOS without ``RUN_REPLAY_E2E``; the regular regression suite
|
||||
@@ -24,11 +28,15 @@ from __future__ import annotations
|
||||
|
||||
import csv
|
||||
import json
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from gps_denied_onboard.helpers.gps_compare import (
|
||||
GroundTruthRow,
|
||||
l2_horizontal_m,
|
||||
match_percentage,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"CapturingMavlinkTransport",
|
||||
"GroundTruthRow",
|
||||
@@ -39,22 +47,6 @@ __all__ = [
|
||||
]
|
||||
|
||||
|
||||
# WGS84 mean Earth radius. Matches the value used by
|
||||
# `helpers/wgs_converter.py` (AZ-279) so the e2e check is consistent
|
||||
# with the production converter.
|
||||
_EARTH_RADIUS_M: float = 6_371_008.8
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class GroundTruthRow:
|
||||
"""One row from the Derkachi data_imu.csv ground-truth slice."""
|
||||
|
||||
t_s: float
|
||||
lat_deg: float
|
||||
lon_deg: float
|
||||
alt_m: float
|
||||
|
||||
|
||||
def parse_jsonl(path: Path) -> list[dict[str, Any]]:
|
||||
"""Return one dict per line of a JsonlReplaySink output file.
|
||||
|
||||
@@ -77,29 +69,6 @@ def parse_jsonl(path: Path) -> list[dict[str, Any]]:
|
||||
return records
|
||||
|
||||
|
||||
def l2_horizontal_m(
|
||||
lat1_deg: float, lon1_deg: float, lat2_deg: float, lon2_deg: float
|
||||
) -> float:
|
||||
"""WGS84-spherical great-circle distance in metres.
|
||||
|
||||
Uses the haversine formula with the C5/AZ-279 mean Earth radius.
|
||||
Sufficient for the AC-3 ≤ 100 m threshold (sub-metre accuracy at
|
||||
the Derkachi latitude band; the spherical approximation diverges
|
||||
from the WGS84 ellipsoid by < 0.5 % at these latitudes — well
|
||||
within the AC-3 budget).
|
||||
"""
|
||||
phi1 = math.radians(lat1_deg)
|
||||
phi2 = math.radians(lat2_deg)
|
||||
dphi = phi2 - phi1
|
||||
dlam = math.radians(lon2_deg - lon1_deg)
|
||||
a = (
|
||||
math.sin(dphi / 2.0) ** 2
|
||||
+ math.cos(phi1) * math.cos(phi2) * math.sin(dlam / 2.0) ** 2
|
||||
)
|
||||
c = 2.0 * math.asin(min(1.0, math.sqrt(a)))
|
||||
return _EARTH_RADIUS_M * c
|
||||
|
||||
|
||||
def load_ground_truth_csv(csv_path: Path) -> list[GroundTruthRow]:
|
||||
"""Load the Derkachi IMU CSV's GPS rows as ground truth.
|
||||
|
||||
@@ -123,65 +92,6 @@ def load_ground_truth_csv(csv_path: Path) -> list[GroundTruthRow]:
|
||||
return rows
|
||||
|
||||
|
||||
def match_percentage(
|
||||
emissions: list[dict[str, Any]],
|
||||
ground_truth: list[GroundTruthRow],
|
||||
*,
|
||||
threshold_m: float,
|
||||
) -> float:
|
||||
"""Share of emissions within ``threshold_m`` of the closest GT row.
|
||||
|
||||
For each emitted ``EstimatorOutput`` JSONL record, find the
|
||||
nearest-in-time ground-truth row, compute the horizontal L2
|
||||
distance, and count it as a hit when ≤ ``threshold_m``. Returns
|
||||
the hit ratio in [0.0, 1.0].
|
||||
|
||||
Nearest-in-time is sufficient because the IMU CSV's 10 Hz cadence
|
||||
(matching the C5 emit rate) means the candidate row is typically
|
||||
< 50 ms off the emit timestamp — well below the AC-3 100 m budget.
|
||||
"""
|
||||
if not emissions:
|
||||
return 0.0
|
||||
if not ground_truth:
|
||||
raise AssertionError("ground_truth must be non-empty")
|
||||
gt_sorted = sorted(ground_truth, key=lambda r: r.t_s)
|
||||
gt_times = [r.t_s for r in gt_sorted]
|
||||
hits = 0
|
||||
for emit in emissions:
|
||||
emit_ts_ns = int(emit["emitted_at"])
|
||||
emit_t_s = emit_ts_ns / 1e9
|
||||
idx = _bisect_left(gt_times, emit_t_s)
|
||||
candidates = []
|
||||
if idx > 0:
|
||||
candidates.append(gt_sorted[idx - 1])
|
||||
if idx < len(gt_sorted):
|
||||
candidates.append(gt_sorted[idx])
|
||||
# Nearest-in-time row.
|
||||
nearest = min(candidates, key=lambda r: abs(r.t_s - emit_t_s))
|
||||
emit_pos = emit["position_wgs84"]
|
||||
d = l2_horizontal_m(
|
||||
emit_pos["lat_deg"],
|
||||
emit_pos["lon_deg"],
|
||||
nearest.lat_deg,
|
||||
nearest.lon_deg,
|
||||
)
|
||||
if d <= threshold_m:
|
||||
hits += 1
|
||||
return hits / len(emissions)
|
||||
|
||||
|
||||
def _bisect_left(seq: list[float], target: float) -> int:
|
||||
"""Stdlib bisect_left, inlined to keep import surface narrow."""
|
||||
lo, hi = 0, len(seq)
|
||||
while lo < hi:
|
||||
mid = (lo + hi) // 2
|
||||
if seq[mid] < target:
|
||||
lo = mid + 1
|
||||
else:
|
||||
hi = mid
|
||||
return lo
|
||||
|
||||
|
||||
class CapturingMavlinkTransport:
|
||||
"""Test-only :class:`MavlinkTransport` that records every write.
|
||||
|
||||
|
||||
@@ -21,18 +21,21 @@ from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from gps_denied_onboard.replay_input import load_tlog_ground_truth
|
||||
from tests.e2e.replay._helpers import GroundTruthRow, load_ground_truth_csv
|
||||
from tests.e2e.replay._tlog_synth import synthesize_tlog
|
||||
|
||||
|
||||
# Derkachi clip range — anchored at the start of the data_imu.csv
|
||||
# (Time=0.0). The fixture clip is deliberately the first 60 s rather
|
||||
# than a mid-flight slice: the take-off region exercises the AZ-405
|
||||
# IMU-take-off auto-sync detector, and the steady cruise that follows
|
||||
# stresses the satellite-anchor + VIO drift-correction path. The
|
||||
# trim is documented in `tests/e2e/replay/README.md`.
|
||||
_CLIP_START_S: float = 0.0
|
||||
_CLIP_END_S: float = 60.0
|
||||
# Derkachi clip range — 60 s starting at the start of the GT series.
|
||||
# For the CSV-synth fallback, the series begins at Time=0.0; for the
|
||||
# real-tlog branch, the series begins at the wall-clock timestamp of
|
||||
# the first GPS message (and the clip becomes [t0, t0 + 60]). The
|
||||
# fixture clip is deliberately the first 60 s rather than a mid-flight
|
||||
# slice: the take-off region exercises the AZ-405 IMU-take-off
|
||||
# auto-sync detector, and the steady cruise that follows stresses the
|
||||
# satellite-anchor + VIO drift-correction path. The trim is documented
|
||||
# in `tests/e2e/replay/README.md`.
|
||||
_CLIP_DURATION_S: float = 60.0
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
@@ -48,11 +51,15 @@ def _derkachi_dir() -> Path:
|
||||
|
||||
|
||||
def _calibration_path() -> Path:
|
||||
# Placeholder calibration: the real Topotek KHP20S30 intrinsics
|
||||
# are unknown per `_docs/00_problem/input_data/flight_derkachi/
|
||||
# camera_info.md`. AC-3 is `xfail`ed until a real calibration
|
||||
# ships; AC-1 / AC-2 / AC-5 / AC-6 do not depend on intrinsics
|
||||
# accuracy.
|
||||
# AZ-702 ships a factory-sheet approximation for the Topotek
|
||||
# KHP20S30 nadir camera at
|
||||
# `_docs/00_problem/input_data/flight_derkachi/khp20s30_factory.json`.
|
||||
# When present we use it; otherwise we fall back to the
|
||||
# `adti26.json` placeholder so the AC-1/2/5/6 path stays
|
||||
# exercisable on dev macOS without the AZ-702 deliverable.
|
||||
factory_path = _derkachi_dir() / "khp20s30_factory.json"
|
||||
if factory_path.is_file():
|
||||
return factory_path
|
||||
return _repo_root() / "tests" / "fixtures" / "calibration" / "adti26.json"
|
||||
|
||||
|
||||
@@ -87,17 +94,45 @@ def derkachi_replay_inputs(tmp_path_factory: pytest.TempPathFactory) -> Derkachi
|
||||
derkachi = _derkachi_dir()
|
||||
csv_path = derkachi / "data_imu.csv"
|
||||
video_path = derkachi / "flight_derkachi.mp4"
|
||||
if not csv_path.is_file():
|
||||
pytest.fail(
|
||||
f"Derkachi fixture missing: {csv_path} — see "
|
||||
"_docs/00_problem/input_data/flight_derkachi/README.md"
|
||||
)
|
||||
real_tlog_path = derkachi / "derkachi.tlog"
|
||||
if not video_path.is_file():
|
||||
pytest.fail(f"Derkachi fixture missing: {video_path}")
|
||||
|
||||
work_dir = tmp_path_factory.mktemp("derkachi")
|
||||
tlog_path = work_dir / "synth.tlog"
|
||||
synthesize_tlog(csv_path, tlog_path)
|
||||
# AZ-697: prefer the real binary tlog when present; fall back to
|
||||
# synthesizing one from the CSV so dev environments without the
|
||||
# 5.8 MB binary blob still exercise the e2e path.
|
||||
if real_tlog_path.is_file():
|
||||
tlog_path = real_tlog_path
|
||||
gt_series = load_tlog_ground_truth(real_tlog_path).records
|
||||
if gt_series:
|
||||
t0_s = gt_series[0].ts_ns / 1e9
|
||||
ground_truth_full = [
|
||||
GroundTruthRow(
|
||||
t_s=fix.ts_ns / 1e9,
|
||||
lat_deg=fix.lat_deg,
|
||||
lon_deg=fix.lon_deg,
|
||||
alt_m=fix.alt_m,
|
||||
)
|
||||
for fix in gt_series
|
||||
]
|
||||
clip_start_s = t0_s
|
||||
clip_end_s = t0_s + _CLIP_DURATION_S
|
||||
else:
|
||||
ground_truth_full = []
|
||||
clip_start_s = 0.0
|
||||
clip_end_s = _CLIP_DURATION_S
|
||||
else:
|
||||
if not csv_path.is_file():
|
||||
pytest.fail(
|
||||
f"Derkachi fixture missing: {csv_path} — see "
|
||||
"_docs/00_problem/input_data/flight_derkachi/README.md"
|
||||
)
|
||||
tlog_path = work_dir / "synth.tlog"
|
||||
synthesize_tlog(csv_path, tlog_path)
|
||||
ground_truth_full = load_ground_truth_csv(csv_path)
|
||||
clip_start_s = 0.0
|
||||
clip_end_s = _CLIP_DURATION_S
|
||||
|
||||
# Empty signing key — the airborne replay path runs the signing
|
||||
# handshake against `NoopMavlinkTransport`, so the key contents do
|
||||
@@ -118,9 +153,8 @@ def derkachi_replay_inputs(tmp_path_factory: pytest.TempPathFactory) -> Derkachi
|
||||
|
||||
output_path = work_dir / "estimator_output.jsonl"
|
||||
|
||||
ground_truth_full = load_ground_truth_csv(csv_path)
|
||||
ground_truth = [
|
||||
r for r in ground_truth_full if _CLIP_START_S <= r.t_s <= _CLIP_END_S
|
||||
r for r in ground_truth_full if clip_start_s <= r.t_s <= clip_end_s
|
||||
]
|
||||
|
||||
return DerkachiReplayInputs(
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
"""AZ-702 — Topotek KHP20S30 factory-sheet calibration.
|
||||
|
||||
Covers AC-1, AC-3, AC-4 of
|
||||
``_docs/02_tasks/todo/AZ-702_khp20s30_calibration.md``:
|
||||
|
||||
* AC-1 (JSON parses against the project schema) — same loader gate the
|
||||
CLI ``replay.py::_load_calibration_json`` uses.
|
||||
* AC-3 (field values match factory inputs) — ``fx == fy`` (square
|
||||
pixels), principal point at image centre, zero distortion.
|
||||
* AC-4 (T3 consumes this calibration) — covered by
|
||||
``tests/e2e/replay/conftest.py::_calibration_path()`` returning this
|
||||
file when present, exercised once T3 (AZ-699) lands.
|
||||
|
||||
AC-2 (`camera_info.md` updated) is a documentation AC and is verified
|
||||
by inspection during code review; it does not lend itself to a runtime
|
||||
assertion beyond the file-existence smoke test below.
|
||||
|
||||
Style: every test follows the Arrange / Act / Assert pattern.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
_FACTORY_JSON_PATH = (
|
||||
Path(__file__).resolve().parents[3]
|
||||
/ "_docs"
|
||||
/ "00_problem"
|
||||
/ "input_data"
|
||||
/ "flight_derkachi"
|
||||
/ "khp20s30_factory.json"
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def calibration_data() -> dict[str, Any]:
|
||||
text = _FACTORY_JSON_PATH.read_text(encoding="utf-8")
|
||||
return json.loads(text)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-1: JSON parses via the project's calibration schema gate
|
||||
|
||||
|
||||
def test_ac1_required_schema_keys_present(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
"""Same gate ``cli/replay.py::_load_calibration_json`` enforces."""
|
||||
# Assert
|
||||
for key in ("intrinsics_3x3", "distortion", "body_to_camera_se3"):
|
||||
assert key in calibration_data, f"missing required key: {key}"
|
||||
|
||||
|
||||
def test_ac1_cli_loader_accepts_the_json(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
"""The CLI's strict loader (replay.py) returns without raising."""
|
||||
# Arrange
|
||||
from gps_denied_onboard.cli.replay import _load_calibration_json
|
||||
|
||||
# Act
|
||||
loaded = _load_calibration_json(_FACTORY_JSON_PATH)
|
||||
|
||||
# Assert
|
||||
assert loaded == calibration_data
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-3: Field values match the documented factory inputs
|
||||
|
||||
|
||||
def test_ac3_intrinsics_square_pixels_and_centred_principal_point(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
# Arrange
|
||||
img_w, img_h = 1920, 1080
|
||||
sensor_w_mm = 5.37
|
||||
focal_mm = 4.7
|
||||
expected_f = focal_mm * (img_w / sensor_w_mm)
|
||||
K = calibration_data["intrinsics_3x3"]
|
||||
|
||||
# Assert — square pixels (fx == fy) and principal point at image centre.
|
||||
fx, fy, cx, cy = K[0][0], K[1][1], K[0][2], K[1][2]
|
||||
assert fx == pytest.approx(fy, rel=1e-12), "expected fx == fy (square pixels)"
|
||||
assert fx == pytest.approx(expected_f, rel=1e-3), (
|
||||
f"fx {fx} does not match factory-sheet derivation "
|
||||
f"f * width/sensor_w = {expected_f}"
|
||||
)
|
||||
assert cx == pytest.approx(img_w / 2, abs=0.5)
|
||||
assert cy == pytest.approx(img_h / 2, abs=0.5)
|
||||
# Off-diagonal entries are zero (no skew).
|
||||
assert K[0][1] == 0.0
|
||||
assert K[1][0] == 0.0
|
||||
assert K[2] == [0.0, 0.0, 1.0]
|
||||
|
||||
|
||||
def test_ac3_distortion_all_zero_for_factory_sheet(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
# Assert — factory-sheet approximation skips per-unit distortion.
|
||||
assert calibration_data["distortion"] == [0.0, 0.0, 0.0, 0.0, 0.0]
|
||||
|
||||
|
||||
def test_ac3_body_to_camera_is_identity_for_nadir(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
# Arrange
|
||||
expected = [
|
||||
[1.0, 0.0, 0.0, 0.0],
|
||||
[0.0, 1.0, 0.0, 0.0],
|
||||
[0.0, 0.0, 1.0, 0.0],
|
||||
[0.0, 0.0, 0.0, 1.0],
|
||||
]
|
||||
|
||||
# Assert
|
||||
assert calibration_data["body_to_camera_se3"] == expected
|
||||
|
||||
|
||||
def test_ac3_acquisition_method_is_factory_sheet(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
# Assert
|
||||
assert calibration_data["acquisition_method"] == "factory_sheet"
|
||||
|
||||
|
||||
def test_metadata_documents_assumptions(
|
||||
calibration_data: dict[str, Any],
|
||||
) -> None:
|
||||
"""Metadata must capture the factory inputs that produced K."""
|
||||
# Arrange
|
||||
meta = calibration_data["metadata"]
|
||||
|
||||
# Assert
|
||||
assert meta["model"] == "Topotek KHP20S30"
|
||||
assert meta["image_resolution_px"] == [1920, 1080]
|
||||
assert meta["assumed_focal_length_mm"] == 4.7
|
||||
assert meta["sensor_width_mm"] == 5.37
|
||||
assert meta["residual_budget_pct"] > 0.0
|
||||
assert "task" in meta and meta["task"] == "AZ-702"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-2 sanity: camera_info.md exists and references this calibration
|
||||
|
||||
|
||||
def test_camera_info_md_references_calibration() -> None:
|
||||
# Arrange
|
||||
camera_info = (
|
||||
Path(__file__).resolve().parents[3]
|
||||
/ "_docs"
|
||||
/ "00_problem"
|
||||
/ "input_data"
|
||||
/ "flight_derkachi"
|
||||
/ "camera_info.md"
|
||||
)
|
||||
|
||||
# Act
|
||||
text = camera_info.read_text(encoding="utf-8")
|
||||
|
||||
# Assert
|
||||
assert "khp20s30_factory.json" in text
|
||||
assert "factory_sheet" in text or "factory-sheet" in text
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-4 sanity: T3 will pick up this calibration when present
|
||||
|
||||
|
||||
def test_ac4_conftest_picks_up_factory_calibration() -> None:
|
||||
"""``tests/e2e/replay/conftest.py::_calibration_path()`` prefers this
|
||||
file when present (the T3 / AZ-699 entry-point)."""
|
||||
# Arrange
|
||||
from tests.e2e.replay.conftest import _calibration_path
|
||||
|
||||
# Act
|
||||
path = _calibration_path()
|
||||
|
||||
# Assert — the factory JSON is committed; conftest must prefer it.
|
||||
assert path == _FACTORY_JSON_PATH
|
||||
@@ -0,0 +1,497 @@
|
||||
"""AZ-697 — Direct binary-tlog GPS-truth extractor.
|
||||
|
||||
Covers AC-1..AC-5 of ``_docs/02_tasks/todo/AZ-697_tlog_ground_truth_extractor.md``:
|
||||
|
||||
* AC-1 (Happy path on real tlog) — gated on the committed
|
||||
``derkachi.tlog`` (5.8 MB binary). When present, asserts ≥ 100
|
||||
records inside the Derkachi geofence.
|
||||
* AC-2 (Empty GPS gracefully) — synthetic source emits no messages.
|
||||
* AC-3 (GPS_RAW_INT fallback / mixed precedence).
|
||||
* AC-4 (mypy --strict) — project-wide strict via ``pyproject.toml
|
||||
[tool.mypy] strict = true``. A scoped smoke test re-runs mypy on the
|
||||
module to catch regressions before CI.
|
||||
* AC-5 (Helper move snapshot) — covered by
|
||||
``tests/unit/helpers/test_gps_compare.py``.
|
||||
|
||||
All tests use a synthetic ``source_factory`` for determinism (no
|
||||
disk IO, no real pymavlink).
|
||||
|
||||
Style: every test follows the Arrange / Act / Assert pattern.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import math
|
||||
import subprocess
|
||||
import sys
|
||||
from collections.abc import Iterator
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from gps_denied_onboard.replay_input.errors import ReplayInputAdapterError
|
||||
from gps_denied_onboard.replay_input.tlog_ground_truth import (
|
||||
TlogGpsFix,
|
||||
TlogGroundTruth,
|
||||
load_tlog_ground_truth,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Synthetic-source fixture helpers
|
||||
|
||||
|
||||
class _FakeMavlinkMessage:
|
||||
"""Stand-in for a pymavlink message object.
|
||||
|
||||
Mirrors the duck-typed surface ``load_tlog_ground_truth`` uses:
|
||||
``get_type()`` returns the message-type string and ``_timestamp``
|
||||
is the Unix-second float that pymavlink's mavlogfile populates on
|
||||
every ``recv_match()`` return.
|
||||
"""
|
||||
|
||||
def __init__(self, msg_type: str, timestamp_s: float, **fields: Any) -> None:
|
||||
self._msg_type = msg_type
|
||||
self._timestamp = timestamp_s
|
||||
for name, value in fields.items():
|
||||
setattr(self, name, value)
|
||||
|
||||
def get_type(self) -> str:
|
||||
return self._msg_type
|
||||
|
||||
|
||||
class _FakeMavlinkSource:
|
||||
"""Stand-in for pymavlink's ``mavutil.mavlink_connection`` return.
|
||||
|
||||
``recv_match`` walks an in-memory message queue, filtering by the
|
||||
``type`` argument. Returns ``None`` once the queue is exhausted —
|
||||
matching mavlogfile's end-of-stream behaviour.
|
||||
"""
|
||||
|
||||
def __init__(self, messages: list[_FakeMavlinkMessage]) -> None:
|
||||
self._iter: Iterator[_FakeMavlinkMessage] = iter(messages)
|
||||
self.closed = False
|
||||
|
||||
def recv_match(
|
||||
self,
|
||||
type: list[str] | str | None = None,
|
||||
blocking: bool = False,
|
||||
) -> _FakeMavlinkMessage | None:
|
||||
wanted = {type} if isinstance(type, str) else set(type or [])
|
||||
for msg in self._iter:
|
||||
if not wanted or msg.get_type() in wanted:
|
||||
return msg
|
||||
return None
|
||||
|
||||
def close(self) -> None:
|
||||
self.closed = True
|
||||
|
||||
|
||||
def _global_position_int(
|
||||
*,
|
||||
ts_s: float,
|
||||
lat_e7: int,
|
||||
lon_e7: int,
|
||||
alt_mm: int,
|
||||
hdg_cdeg: int = 0,
|
||||
vx_cm_s: int = 0,
|
||||
vy_cm_s: int = 0,
|
||||
vz_cm_s: int = 0,
|
||||
) -> _FakeMavlinkMessage:
|
||||
return _FakeMavlinkMessage(
|
||||
"GLOBAL_POSITION_INT",
|
||||
ts_s,
|
||||
lat=lat_e7,
|
||||
lon=lon_e7,
|
||||
alt=alt_mm,
|
||||
hdg=hdg_cdeg,
|
||||
vx=vx_cm_s,
|
||||
vy=vy_cm_s,
|
||||
vz=vz_cm_s,
|
||||
)
|
||||
|
||||
|
||||
def _gps_raw_int(
|
||||
*,
|
||||
ts_s: float,
|
||||
lat_e7: int,
|
||||
lon_e7: int,
|
||||
alt_mm: int,
|
||||
vel_cm_s: int = 0,
|
||||
cog_cdeg: int = 0,
|
||||
) -> _FakeMavlinkMessage:
|
||||
return _FakeMavlinkMessage(
|
||||
"GPS_RAW_INT",
|
||||
ts_s,
|
||||
lat=lat_e7,
|
||||
lon=lon_e7,
|
||||
alt=alt_mm,
|
||||
vel=vel_cm_s,
|
||||
cog=cog_cdeg,
|
||||
)
|
||||
|
||||
|
||||
def _factory_from(messages: list[_FakeMavlinkMessage]) -> Any:
|
||||
"""Return a ``source_factory`` that yields the given message list."""
|
||||
|
||||
def _factory(_path: str) -> _FakeMavlinkSource:
|
||||
return _FakeMavlinkSource(messages)
|
||||
|
||||
return _factory
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-1: Happy path on real tlog (gated on the committed binary)
|
||||
|
||||
|
||||
def _real_derkachi_tlog() -> Path:
|
||||
return (
|
||||
Path(__file__).resolve().parents[3]
|
||||
/ "_docs"
|
||||
/ "00_problem"
|
||||
/ "input_data"
|
||||
/ "flight_derkachi"
|
||||
/ "derkachi.tlog"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not _real_derkachi_tlog().is_file(),
|
||||
reason=(
|
||||
"Real derkachi.tlog binary not present (gitignored 5.8 MB blob). "
|
||||
"Place it at _docs/00_problem/input_data/flight_derkachi/derkachi.tlog "
|
||||
"to exercise AC-1."
|
||||
),
|
||||
)
|
||||
def test_ac1_real_derkachi_tlog_has_geofence_records() -> None:
|
||||
# Arrange
|
||||
tlog = _real_derkachi_tlog()
|
||||
|
||||
# Act
|
||||
truth = load_tlog_ground_truth(tlog)
|
||||
|
||||
# Assert
|
||||
assert len(truth.records) > 100, (
|
||||
f"expected > 100 GPS records, got {len(truth.records)}"
|
||||
)
|
||||
assert truth.source in {"GLOBAL_POSITION_INT", "GPS_RAW_INT"}
|
||||
# Derkachi geofence: lat ≈ 50.08, lon ≈ 36.11 (Kharkiv suburb).
|
||||
lats = [r.lat_deg for r in truth.records if r.lat_deg != 0.0]
|
||||
lons = [r.lon_deg for r in truth.records if r.lon_deg != 0.0]
|
||||
assert lats, "every GPS record has lat == 0; tlog likely malformed"
|
||||
median_lat = sorted(lats)[len(lats) // 2]
|
||||
median_lon = sorted(lons)[len(lons) // 2]
|
||||
assert 49.9 <= median_lat <= 50.3, f"median lat {median_lat} outside Derkachi band"
|
||||
assert 35.9 <= median_lon <= 36.4, f"median lon {median_lon} outside Derkachi band"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-2: Empty GPS gracefully (no messages → empty records + WARN log)
|
||||
|
||||
|
||||
def test_ac2_empty_tlog_returns_empty_records_and_warns(
|
||||
tmp_path: Path,
|
||||
caplog: pytest.LogCaptureFixture,
|
||||
) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "empty.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
factory = _factory_from([])
|
||||
|
||||
# Act
|
||||
with caplog.at_level(
|
||||
logging.WARNING,
|
||||
logger="gps_denied_onboard.replay_input.tlog_ground_truth",
|
||||
):
|
||||
truth = load_tlog_ground_truth(fake_tlog, source_factory=factory)
|
||||
|
||||
# Assert
|
||||
assert truth.records == ()
|
||||
assert truth.source == ""
|
||||
assert any(
|
||||
"contains no GLOBAL_POSITION_INT or GPS_RAW_INT" in rec.message
|
||||
for rec in caplog.records
|
||||
)
|
||||
|
||||
|
||||
def test_missing_file_raises(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
missing = tmp_path / "absent.tlog"
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises(ReplayInputAdapterError, match="tlog file not found"):
|
||||
load_tlog_ground_truth(missing)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-3: Fallback precedence (GPS_RAW_INT only; mixed source)
|
||||
|
||||
|
||||
def test_ac3_gps_raw_int_fallback_when_no_global_position_int(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "raw_only.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
messages = [
|
||||
_gps_raw_int(
|
||||
ts_s=1_700_000_000.000,
|
||||
lat_e7=500_800_000, # 50.08
|
||||
lon_e7=361_100_000, # 36.11
|
||||
alt_mm=200_000, # 200 m MSL
|
||||
vel_cm_s=1500, # 15 m/s
|
||||
cog_cdeg=9000, # 90° (east)
|
||||
),
|
||||
_gps_raw_int(
|
||||
ts_s=1_700_000_000.200,
|
||||
lat_e7=500_801_000,
|
||||
lon_e7=361_101_000,
|
||||
alt_mm=200_500,
|
||||
vel_cm_s=1500,
|
||||
cog_cdeg=9000,
|
||||
),
|
||||
]
|
||||
factory = _factory_from(messages)
|
||||
|
||||
# Act
|
||||
truth = load_tlog_ground_truth(fake_tlog, source_factory=factory)
|
||||
|
||||
# Assert
|
||||
assert truth.source == "GPS_RAW_INT"
|
||||
assert len(truth.records) == 2
|
||||
first = truth.records[0]
|
||||
assert first.lat_deg == pytest.approx(50.08, abs=1e-6)
|
||||
assert first.lon_deg == pytest.approx(36.11, abs=1e-6)
|
||||
assert first.alt_m == pytest.approx(200.0, abs=1e-3)
|
||||
# cog=90° (east) ⇒ vx (north) = 0, vy (east) = 15 m/s, vz = 0.
|
||||
assert first.vx_m_s == pytest.approx(0.0, abs=1e-9)
|
||||
assert first.vy_m_s == pytest.approx(15.0, abs=1e-9)
|
||||
assert first.vz_m_s == 0.0
|
||||
assert first.hdg_deg == pytest.approx(90.0, abs=1e-6)
|
||||
assert first.ts_ns == 1_700_000_000_000_000_000
|
||||
|
||||
|
||||
def test_ac3_mixed_messages_prefer_global_position_int(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "mixed.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
messages = [
|
||||
_gps_raw_int(
|
||||
ts_s=1.0,
|
||||
lat_e7=400_000_000, # 40.00 — distinguishable from GPI rows
|
||||
lon_e7=300_000_000, # 30.00
|
||||
alt_mm=100_000,
|
||||
cog_cdeg=0,
|
||||
),
|
||||
_global_position_int(
|
||||
ts_s=1.0,
|
||||
lat_e7=500_800_000, # 50.08
|
||||
lon_e7=361_100_000, # 36.11
|
||||
alt_mm=200_000,
|
||||
hdg_cdeg=4500, # 45°
|
||||
vx_cm_s=500,
|
||||
vy_cm_s=-500,
|
||||
vz_cm_s=100,
|
||||
),
|
||||
_gps_raw_int(
|
||||
ts_s=2.0,
|
||||
lat_e7=400_001_000,
|
||||
lon_e7=300_001_000,
|
||||
alt_mm=100_500,
|
||||
cog_cdeg=0,
|
||||
),
|
||||
_global_position_int(
|
||||
ts_s=2.0,
|
||||
lat_e7=500_801_000,
|
||||
lon_e7=361_101_000,
|
||||
alt_mm=200_500,
|
||||
hdg_cdeg=4500,
|
||||
vx_cm_s=500,
|
||||
vy_cm_s=-500,
|
||||
vz_cm_s=100,
|
||||
),
|
||||
]
|
||||
factory = _factory_from(messages)
|
||||
|
||||
# Act
|
||||
truth = load_tlog_ground_truth(fake_tlog, source_factory=factory)
|
||||
|
||||
# Assert — GLOBAL_POSITION_INT wins; GPS_RAW_INT rows are ignored.
|
||||
assert truth.source == "GLOBAL_POSITION_INT"
|
||||
assert len(truth.records) == 2
|
||||
for rec in truth.records:
|
||||
assert rec.lat_deg == pytest.approx(50.08, abs=1e-3)
|
||||
assert rec.lon_deg == pytest.approx(36.11, abs=1e-3)
|
||||
assert rec.hdg_deg == pytest.approx(45.0, abs=1e-6)
|
||||
assert rec.vx_m_s == pytest.approx(5.0, abs=1e-9)
|
||||
assert rec.vy_m_s == pytest.approx(-5.0, abs=1e-9)
|
||||
assert rec.vz_m_s == pytest.approx(1.0, abs=1e-9)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Unit conversions (MAVLink integer encodings)
|
||||
|
||||
|
||||
def test_global_position_int_unit_conversions(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "units.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
messages = [
|
||||
_global_position_int(
|
||||
ts_s=10.5,
|
||||
lat_e7=123_456_789, # 12.3456789 deg
|
||||
lon_e7=-98_765_432, # -9.8765432 deg
|
||||
alt_mm=12_345, # 12.345 m
|
||||
hdg_cdeg=18_000, # 180.00 deg
|
||||
vx_cm_s=-2_500, # -25.00 m/s
|
||||
vy_cm_s=0,
|
||||
vz_cm_s=50, # 0.5 m/s
|
||||
)
|
||||
]
|
||||
factory = _factory_from(messages)
|
||||
|
||||
# Act
|
||||
truth = load_tlog_ground_truth(fake_tlog, source_factory=factory)
|
||||
|
||||
# Assert
|
||||
assert truth.source == "GLOBAL_POSITION_INT"
|
||||
(rec,) = truth.records
|
||||
assert rec.lat_deg == pytest.approx(12.345_678_9, abs=1e-9)
|
||||
assert rec.lon_deg == pytest.approx(-9.876_543_2, abs=1e-9)
|
||||
assert rec.alt_m == pytest.approx(12.345, abs=1e-9)
|
||||
assert rec.hdg_deg == pytest.approx(180.0, abs=1e-9)
|
||||
assert rec.vx_m_s == pytest.approx(-25.0, abs=1e-9)
|
||||
assert rec.vy_m_s == 0.0
|
||||
assert rec.vz_m_s == pytest.approx(0.5, abs=1e-9)
|
||||
assert rec.ts_ns == int(10.5 * 1_000_000_000)
|
||||
|
||||
|
||||
def test_gps_raw_int_cog_to_ned_decomposition(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "cog.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
messages = [
|
||||
_gps_raw_int(
|
||||
ts_s=0.0,
|
||||
lat_e7=0,
|
||||
lon_e7=0,
|
||||
alt_mm=0,
|
||||
vel_cm_s=2000, # 20 m/s
|
||||
cog_cdeg=4500, # 45° (NE)
|
||||
)
|
||||
]
|
||||
factory = _factory_from(messages)
|
||||
|
||||
# Act
|
||||
truth = load_tlog_ground_truth(fake_tlog, source_factory=factory)
|
||||
|
||||
# Assert — 20 m/s @ 45° ⇒ vx = vy = 20/sqrt(2) ≈ 14.142.
|
||||
(rec,) = truth.records
|
||||
expected = 20.0 * math.cos(math.radians(45.0))
|
||||
assert rec.vx_m_s == pytest.approx(expected, abs=1e-9)
|
||||
assert rec.vy_m_s == pytest.approx(expected, abs=1e-9)
|
||||
assert rec.vz_m_s == 0.0
|
||||
assert rec.hdg_deg == pytest.approx(45.0, abs=1e-9)
|
||||
|
||||
|
||||
def test_missing_timestamp_raises(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "no_ts.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
|
||||
class _MsgNoTimestamp:
|
||||
def get_type(self) -> str:
|
||||
return "GLOBAL_POSITION_INT"
|
||||
|
||||
factory = _factory_from([_MsgNoTimestamp()]) # type: ignore[list-item]
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises(
|
||||
ReplayInputAdapterError, match="missing _timestamp attribute"
|
||||
):
|
||||
load_tlog_ground_truth(fake_tlog, source_factory=factory)
|
||||
|
||||
|
||||
def test_source_is_closed_after_load(tmp_path: Path) -> None:
|
||||
# Arrange
|
||||
fake_tlog = tmp_path / "close.tlog"
|
||||
fake_tlog.write_bytes(b"")
|
||||
captured: dict[str, _FakeMavlinkSource] = {}
|
||||
|
||||
def _factory(_path: str) -> _FakeMavlinkSource:
|
||||
src = _FakeMavlinkSource([])
|
||||
captured["src"] = src
|
||||
return src
|
||||
|
||||
# Act
|
||||
load_tlog_ground_truth(fake_tlog, source_factory=_factory)
|
||||
|
||||
# Assert
|
||||
assert captured["src"].closed is True
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# DTO surface
|
||||
|
||||
|
||||
def test_tlog_ground_truth_is_frozen() -> None:
|
||||
# Arrange
|
||||
truth = TlogGroundTruth(records=(), source="")
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises((AttributeError, TypeError)):
|
||||
truth.source = "GLOBAL_POSITION_INT" # type: ignore[misc]
|
||||
|
||||
|
||||
def test_tlog_gps_fix_is_frozen() -> None:
|
||||
# Arrange
|
||||
fix = TlogGpsFix(
|
||||
ts_ns=0,
|
||||
lat_deg=0.0,
|
||||
lon_deg=0.0,
|
||||
alt_m=0.0,
|
||||
hdg_deg=0.0,
|
||||
vx_m_s=0.0,
|
||||
vy_m_s=0.0,
|
||||
vz_m_s=0.0,
|
||||
)
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises((AttributeError, TypeError)):
|
||||
fix.lat_deg = 1.0 # type: ignore[misc]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# AC-4: mypy --strict scoped to the new module
|
||||
|
||||
|
||||
def test_ac4_mypy_strict_clean(tmp_path: Path) -> None:
|
||||
"""``mypy --strict`` on the AZ-697 module reports zero errors.
|
||||
|
||||
The project is strict-by-default via ``pyproject.toml [tool.mypy]``;
|
||||
this scoped run catches regressions in CI without waiting for the
|
||||
full-suite mypy pass.
|
||||
"""
|
||||
# Arrange
|
||||
module_path = (
|
||||
Path(__file__).resolve().parents[2].parent
|
||||
/ "src"
|
||||
/ "gps_denied_onboard"
|
||||
/ "replay_input"
|
||||
/ "tlog_ground_truth.py"
|
||||
)
|
||||
|
||||
# Act
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-m", "mypy", "--strict", str(module_path)],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=120,
|
||||
)
|
||||
|
||||
# Assert
|
||||
assert result.returncode == 0, (
|
||||
f"mypy --strict reported errors:\n"
|
||||
f"stdout:\n{result.stdout}\n"
|
||||
f"stderr:\n{result.stderr}"
|
||||
)
|
||||
@@ -0,0 +1,152 @@
|
||||
"""AZ-697 AC-5 — gps_compare helper-move snapshot.
|
||||
|
||||
The ``l2_horizontal_m`` / ``match_percentage`` / ``GroundTruthRow``
|
||||
trio moved from ``tests/e2e/replay/_helpers.py`` into production code
|
||||
at ``src/gps_denied_onboard/helpers/gps_compare.py``. This module
|
||||
pins the post-move numerical behaviour so a future refactor of either
|
||||
the helper or the test re-export can't silently drift.
|
||||
|
||||
The numerical reference values are hand-computed against the WGS84
|
||||
mean Earth radius used by ``helpers/wgs_converter.py`` (AZ-279). The
|
||||
``tests/e2e/replay/test_helpers.py`` module continues to import from
|
||||
``tests/e2e/replay/_helpers`` (which now re-exports from the
|
||||
production location), so both call sites are exercised.
|
||||
|
||||
Style: every test follows the Arrange / Act / Assert pattern.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from gps_denied_onboard.helpers.gps_compare import (
|
||||
GroundTruthRow,
|
||||
l2_horizontal_m,
|
||||
match_percentage,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Snapshot: production location vs prior test-helpers location
|
||||
|
||||
|
||||
def test_l2_zero_at_same_point() -> None:
|
||||
# Act
|
||||
d = l2_horizontal_m(50.08, 36.11, 50.08, 36.11)
|
||||
|
||||
# Assert
|
||||
assert d == pytest.approx(0.0, abs=1e-6)
|
||||
|
||||
|
||||
def test_l2_one_degree_latitude_is_111km() -> None:
|
||||
# Act
|
||||
d = l2_horizontal_m(50.08, 36.11, 51.08, 36.11)
|
||||
|
||||
# Assert — one degree of latitude on a sphere of radius 6_371_008.8 m.
|
||||
assert d == pytest.approx(111_195.0, rel=0.001)
|
||||
|
||||
|
||||
def test_l2_symmetric() -> None:
|
||||
# Arrange
|
||||
a = (49.991, 36.221)
|
||||
b = (50.080, 36.111)
|
||||
|
||||
# Act
|
||||
d_ab = l2_horizontal_m(*a, *b)
|
||||
d_ba = l2_horizontal_m(*b, *a)
|
||||
|
||||
# Assert
|
||||
assert d_ab == pytest.approx(d_ba, rel=1e-12)
|
||||
|
||||
|
||||
def test_l2_kharkiv_to_kyiv_known_pair() -> None:
|
||||
# Arrange — externally known reference distance is ~411 km.
|
||||
kharkiv_lat, kharkiv_lon = 49.9935, 36.2304
|
||||
kyiv_lat, kyiv_lon = 50.4501, 30.5234
|
||||
|
||||
# Act
|
||||
d = l2_horizontal_m(kharkiv_lat, kharkiv_lon, kyiv_lat, kyiv_lon)
|
||||
|
||||
# Assert
|
||||
assert d == pytest.approx(411_000.0, rel=0.005)
|
||||
|
||||
|
||||
def test_match_percentage_all_within_threshold() -> None:
|
||||
# Arrange
|
||||
gt = [GroundTruthRow(t_s=0.0, lat_deg=50.0, lon_deg=36.0, alt_m=100.0)]
|
||||
emissions = [
|
||||
{
|
||||
"emitted_at": 0,
|
||||
"position_wgs84": {"lat_deg": 50.0, "lon_deg": 36.0, "alt_m": 100.0},
|
||||
}
|
||||
]
|
||||
|
||||
# Act
|
||||
pct = match_percentage(emissions, gt, threshold_m=100.0)
|
||||
|
||||
# Assert
|
||||
assert pct == 1.0
|
||||
|
||||
|
||||
def test_match_percentage_none_within_threshold() -> None:
|
||||
# Arrange
|
||||
gt = [GroundTruthRow(t_s=0.0, lat_deg=50.0, lon_deg=36.0, alt_m=100.0)]
|
||||
emissions = [
|
||||
{
|
||||
"emitted_at": 0,
|
||||
# ~111 km north of the GT row.
|
||||
"position_wgs84": {"lat_deg": 51.0, "lon_deg": 36.0, "alt_m": 100.0},
|
||||
}
|
||||
]
|
||||
|
||||
# Act
|
||||
pct = match_percentage(emissions, gt, threshold_m=100.0)
|
||||
|
||||
# Assert
|
||||
assert pct == 0.0
|
||||
|
||||
|
||||
def test_match_percentage_empty_emissions_zero() -> None:
|
||||
# Arrange
|
||||
gt = [GroundTruthRow(t_s=0.0, lat_deg=50.0, lon_deg=36.0, alt_m=100.0)]
|
||||
|
||||
# Act
|
||||
pct = match_percentage([], gt, threshold_m=100.0)
|
||||
|
||||
# Assert
|
||||
assert pct == 0.0
|
||||
|
||||
|
||||
def test_match_percentage_empty_ground_truth_raises() -> None:
|
||||
# Act / Assert
|
||||
with pytest.raises(AssertionError, match="ground_truth must be non-empty"):
|
||||
match_percentage(
|
||||
[{"emitted_at": 0, "position_wgs84": {"lat_deg": 50, "lon_deg": 36}}],
|
||||
[],
|
||||
threshold_m=100.0,
|
||||
)
|
||||
|
||||
|
||||
def test_ground_truth_row_is_frozen() -> None:
|
||||
# Arrange
|
||||
row = GroundTruthRow(t_s=0.0, lat_deg=50.0, lon_deg=36.0, alt_m=100.0)
|
||||
|
||||
# Act / Assert
|
||||
with pytest.raises((AttributeError, TypeError)):
|
||||
row.lat_deg = 51.0 # type: ignore[misc]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------
|
||||
# Snapshot: re-export from prior test-helpers location returns the
|
||||
# same object as the production import. Guarantees there is no second
|
||||
# divergent copy under tests/.
|
||||
|
||||
|
||||
def test_test_helpers_reexport_is_identical() -> None:
|
||||
# Act
|
||||
from tests.e2e.replay import _helpers as test_helpers_module
|
||||
|
||||
# Assert — identity, not just equality.
|
||||
assert test_helpers_module.l2_horizontal_m is l2_horizontal_m
|
||||
assert test_helpers_module.match_percentage is match_percentage
|
||||
assert test_helpers_module.GroundTruthRow is GroundTruthRow
|
||||
Reference in New Issue
Block a user