|
|
import json
|
|
|
import math
|
|
|
from pathlib import Path
|
|
|
import subprocess
|
|
|
import sys
|
|
|
from tempfile import TemporaryDirectory
|
|
|
import unittest
|
|
|
|
|
|
import calibrate_tele_profile as c
|
|
|
|
|
|
|
|
|
def envelope(cam, start, event, at, **fields):
|
|
|
return dict(cam=cam, generation=4, track_start_utc=start, utc=at,
|
|
|
elapsed_ms=round((at - start) * 1000, 1), event=event, dropped_before=0, **fields)
|
|
|
|
|
|
|
|
|
def episode(cam=8, start=1000., *, initial=11., settled=10.4, margin=.40, request=1):
|
|
|
def row(event, elapsed, **fields):
|
|
|
return envelope(cam, start, event, start + elapsed, **fields)
|
|
|
|
|
|
put = start + 1.
|
|
|
stop_fov = settled + (initial - settled) * .5
|
|
|
first_fov = settled + (initial - settled) / 6
|
|
|
rows = [row("first_zoom", 1.04, t_to_first_zoom_ms=1040., t_to_first_zoom_put_ms=1000.,
|
|
|
request=request, native=10)]
|
|
|
for seq, (offset, status_offset, hfov) in enumerate((
|
|
|
(1., .95, initial), (1.10, 1.10, stop_fov), (1.20, 1.20, first_fov),
|
|
|
(1.30, 1.20, first_fov), (1.40, 1.40, settled), (1.50, 1.40, settled),
|
|
|
(1.57, 1.57, settled),
|
|
|
), 1):
|
|
|
rows.append(row("tick", offset, seq=seq, frame_utc=start+offset-.10,
|
|
|
zoom_cmd=.10 if offset <= 1.10 else 0., reason="pulse_active",
|
|
|
stop_requested=False, FOV_actual=hfov, FOV_status_utc=start+status_offset,
|
|
|
FOV_estimated=hfov, FOV_min_estimated=hfov-.05, bbox_px=[18,15],
|
|
|
tracker_score=.80, detector_conf=.71, detector_utc=start+.95,
|
|
|
margin=margin, edge_margin_required=.32, gate="tracking_zoom", ff_deg_s=[0.,0.]))
|
|
|
rows.extend((
|
|
|
row("stop", 1.17, request=request, reason="priority_stop:watchdog", confirmed=True, clean=True),
|
|
|
row(c.PULSE, 1.60, request=request, native=10, put_at=put, ack_at=start+1.04,
|
|
|
put_rtt_ms=40., HFOV_before=initial, before_at=start+.95, stop_issued_at=start+1.12,
|
|
|
stop_ack_at=start+1.17, stop_rtt_ms=50., duration_s=.17, stop_reason="priority_stop:watchdog",
|
|
|
stop_status_at=start+1.10, HFOV_stop_sample=stop_fov, clean_stop=True, outcome="settled",
|
|
|
HFOV_after=settled, after_at=start+1.57, delta_hfov_deg=settled-initial,
|
|
|
first_post_stop_at=start+1.20, HFOV_first_post_stop=first_fov, post_stop_tail_deg=first_fov-settled,
|
|
|
stop_bracket_narrowing_deg=stop_fov-settled, stop_sample_age_ms=20.,
|
|
|
effective_tele_log_rate_s=(c.focal(settled)-c.focal(initial))/.17 if initial < 15 else None),
|
|
|
row("zoom_block_summary", 1.80, final=True, zoom_block_reason={"min_hfov_limit":5},
|
|
|
last_values={"min_hfov_limit":{"hfov":settled,"minimum":10.}}),
|
|
|
))
|
|
|
return rows
|
|
|
|
|
|
|
|
|
def run(rows, observations=None, limits=c.Limits()):
|
|
|
grouped = {}
|
|
|
for r in rows:
|
|
|
key = (r["cam"], r["generation"], r["track_start_utc"])
|
|
|
grouped.setdefault(key, []).append(r)
|
|
|
return c.analyze(grouped, c.load_profiles(None), limits, observations)
|
|
|
|
|
|
|
|
|
def pulse(rows):
|
|
|
return next(r for r in rows if r["event"] == c.PULSE)
|
|
|
|
|
|
|
|
|
def write(path, rows):
|
|
|
path.write_text("".join(json.dumps(row)+"\n" for row in rows),encoding="utf-8")
|
|
|
|
|
|
|
|
|
class CalibrationTests(unittest.TestCase):
|
|
|
def test_poll_cadence_counts_fresh_optics_separately_from_cache(self):
|
|
|
rows = episode()
|
|
|
for i in range(5):
|
|
|
now = 1000.5 + .08 * i
|
|
|
rows.append(envelope(8, 1000., "status_sample", now, request_ms=40.,
|
|
|
status_ok=True, FOV_actual=13.,
|
|
|
FOV_status_utc=now if i != 2 else now-.08, optics_fresh=i != 2))
|
|
|
report, _ = run(rows)
|
|
|
status = report["episodes"][0]["isapi_track_status"]
|
|
|
self.assertEqual(status["success"], 5)
|
|
|
self.assertEqual(status["fresh_optics"]["samples"], 4)
|
|
|
self.assertAlmostEqual(status["successful_status"]["hz"], 12.5)
|
|
|
self.assertAlmostEqual(status["fresh_optics"]["gap_ms"]["max"], 160.)
|
|
|
self.assertEqual(status["request_ms"]["p95"], 40.)
|
|
|
|
|
|
def test_recovers_rate_tail_rtt_and_bounded_lower_profile(self):
|
|
|
report, candidate = run(sum((episode(start=1000.+i*10) for i in range(3)), []))
|
|
|
camera = candidate["cameras"]["19"]
|
|
|
self.assertEqual(camera["qualified_tele_pulses"],3)
|
|
|
result = camera["predictive_zoom_for_review"]
|
|
|
expected = math.log(math.tan(math.radians(11 / 2))/math.tan(math.radians(10.4 / 2))) / .17
|
|
|
self.assertAlmostEqual(result["nominal_gain"]*c.FULL_SWEEP_LOG_RATE,expected)
|
|
|
self.assertGreaterEqual(result["upper_gain"],c.BASELINE[8][1])
|
|
|
self.assertEqual(result["min_allowed_hfov_deg"],9.5)
|
|
|
self.assertFalse(candidate["production_enable"])
|
|
|
bins = report["cameras"]["19"]["pulses_by_native_and_hfov_band"]["10"]["10_to_lt_12"]
|
|
|
self.assertAlmostEqual(bins["effective_log_rate_s"]["p50"],expected)
|
|
|
self.assertAlmostEqual(bins["pre_stop_hfov_narrowing_deg_s"]["p50"],3.)
|
|
|
self.assertAlmostEqual(bins["post_stop_tail_deg"]["p95"],.10)
|
|
|
stop_window = bins["settled_band_0_10deg_from_stop_issue_ms"]
|
|
|
self.assertEqual(stop_window["lower"]["p50"], 0.)
|
|
|
self.assertAlmostEqual(stop_window["upper"]["p50"], 80.)
|
|
|
ack_window = bins["settled_band_0_10deg_from_stop_ack_ms"]
|
|
|
self.assertAlmostEqual(ack_window["upper"]["p50"], 30.)
|
|
|
self.assertAlmostEqual(report["episodes"][0]["control_sampled_frame_age_ms"]["p95"], 100.)
|
|
|
self.assertAlmostEqual(bins["stop_bracket_narrowing_deg"]["max"],.30)
|
|
|
self.assertEqual(bins["put_rtt_ms"]["p95"],40.)
|
|
|
self.assertEqual(bins["stop_rtt_ms"]["p95"],50.)
|
|
|
self.assertAlmostEqual(bins["native10_profile_comparison"]["median_observed_over_nominal"],
|
|
|
expected/(c.BASELINE[8][0]*c.FULL_SWEEP_LOG_RATE))
|
|
|
|
|
|
def test_all_hfov_bands_and_native_are_separate(self):
|
|
|
rows = []
|
|
|
for i,(initial,settled) in enumerate(((48.,47.),(35.,34.),(18.,17.),(12.,11.),(10.,9.5),(9.,8.8))):
|
|
|
rows.extend(episode(start=1000+i*10, initial=initial, settled=settled))
|
|
|
other = episode(start=1100.)
|
|
|
pulse(other)["native"] = 11
|
|
|
report,_=run(rows+other)
|
|
|
bins=report["cameras"]["19"]["pulses_by_native_and_hfov_band"]
|
|
|
self.assertEqual(set(bins["10"]),set(c.BINS))
|
|
|
self.assertEqual(bins["10"]["18_to_35"]["seen"],2)
|
|
|
self.assertEqual(bins["11"]["10_to_lt_12"]["seen"],1)
|
|
|
|
|
|
def test_stale_status_sparse_polling_and_owner_loss_are_excluded(self):
|
|
|
a=episode(start=1000.)
|
|
|
pulse(a)["before_at"]=1000.1
|
|
|
b=episode(start=1010.)
|
|
|
pulse(b)["first_post_stop_at"]=1011.48
|
|
|
d=[r for r in episode(start=1020.) if not (r["event"]=="tick" and r["seq"] in (5,6))]
|
|
|
e=episode(start=1030.)
|
|
|
pulse(e).update(outcome="owner_changed", clean_stop=False, stop_reason="priority_stop:track_lost")
|
|
|
report, candidate = run(a+b+d+e)
|
|
|
observed=[ep["pulses"][0]["excluded"] for ep in report["episodes"]]
|
|
|
self.assertIn("stale_before_status",observed[0])
|
|
|
self.assertIn("late_post_stop_status",observed[1])
|
|
|
self.assertIn("sparse_post_stop_status",observed[2])
|
|
|
self.assertIn("interrupted_or_unclean_stop",observed[3])
|
|
|
self.assertIsNone(candidate["cameras"]["19"]["predictive_zoom_for_review"])
|
|
|
|
|
|
def test_outlier_is_excluded_from_fit_but_retained_as_safety_evidence(self):
|
|
|
normal=sum((episode(start=1000.+i*10) for i in range(5)), [])
|
|
|
extreme=episode(start=1050., initial=11., settled=9.85)
|
|
|
report,candidate=run(normal+extreme)
|
|
|
rejected=report["episodes"][-1]["pulses"][0]
|
|
|
self.assertIn("rate_outlier",rejected["excluded"])
|
|
|
profile=candidate["cameras"]["19"]
|
|
|
self.assertEqual(profile["qualified_tele_pulses"],5)
|
|
|
self.assertIsNone(profile["next_floor_review"]["proposed_next_test_deg"])
|
|
|
self.assertGreaterEqual(profile["predictive_zoom_for_review"]["upper_gain"],
|
|
|
1.25*rejected["effective_log_rate_s"]/c.FULL_SWEEP_LOG_RATE)
|
|
|
|
|
|
def test_no_floor_decrease_on_small_margin_or_data_far_from_floor(self):
|
|
|
for opts,reason in (({"margin":.325},"no_lower_step_with_observed_margin"),
|
|
|
({"initial":14.,"settled":13.},"insufficient_near_floor_status_and_framing")):
|
|
|
with self.subTest(opts=opts):
|
|
|
_,candidate=run(sum((episode(start=1000.+i*10,**opts) for i in range(3)), []))
|
|
|
camera=candidate["cameras"]["19"]
|
|
|
self.assertEqual(camera["next_floor_review"]["reason"],reason)
|
|
|
self.assertEqual(camera["predictive_zoom_for_review"]["min_allowed_hfov_deg"],10.)
|
|
|
|
|
|
def test_floor_rejected_when_tick_coverage_or_episode_missing(self):
|
|
|
a=episode(start=1000.)
|
|
|
b=episode(start=1010.)
|
|
|
d=[r for r in episode(start=1020.) if r["event"]!="zoom_block_summary"]
|
|
|
_,candidate=run(a+b+d)
|
|
|
self.assertIsNone(candidate["cameras"]["19"]["next_floor_review"]["proposed_next_test_deg"])
|
|
|
e=episode(start=1030.)
|
|
|
e[3]["dropped_before"]=1
|
|
|
_,candidate=run(a+b+e)
|
|
|
self.assertIsNone(candidate["cameras"]["19"]["next_floor_review"]["proposed_next_test_deg"])
|
|
|
|
|
|
def test_episode_summary_counts_cumulative_snapshots_and_preserves_unknowns(self):
|
|
|
rows=episode()
|
|
|
rows.extend((
|
|
|
envelope(8,1000., "zoom_block_summary",1001.69, final=False,
|
|
|
zoom_block_reason={"min_hfov_limit":3,"edge_margin":4},
|
|
|
last_values={"edge_margin":{"measured":.22}}),
|
|
|
envelope(10,1020., "zoom_block_summary",1023., final=True,
|
|
|
zoom_block_reason={"bbox_check":3,"angle_tol":5},last_values={}),
|
|
|
))
|
|
|
for row in rows:
|
|
|
if row["event"]=="tick" and row["seq"] in (4,5):
|
|
|
row["zoom_cmd"]=-.10
|
|
|
report,_=run(rows)
|
|
|
ep, no=report["episodes"]
|
|
|
self.assertAlmostEqual(ep["t_to_first_zoom_s"],1.04)
|
|
|
self.assertEqual(ep["min_hfov_deg"],10.4)
|
|
|
self.assertEqual(ep["min_sampled_measured_edge_margin"],.22)
|
|
|
self.assertEqual(ep["negative_zoom_request_runs"],1)
|
|
|
self.assertEqual(ep["zoom_block_reason"],{"min_hfov_limit":5,"edge_margin":4})
|
|
|
self.assertIsNone(ep["tele_center_fraction"])
|
|
|
self.assertIsNone(ep["confirmed_safety_step_back_count"])
|
|
|
self.assertEqual(no["classification"],"no_positive_ack_observed")
|
|
|
self.assertEqual(no["top_block_reasons"],[("angle_tol",5),("bbox_check",3)])
|
|
|
|
|
|
def test_optional_matched_frames_provide_time_weighted_center_and_acks(self):
|
|
|
rows=episode()
|
|
|
ticks=[r for r in rows if r["event"]=="tick"]
|
|
|
observations=[dict(event="frame_observation",seq=r["seq"],frame_utc=r["frame_utc"],
|
|
|
dx_norm=.20 if r["seq"]==5 else .05,dy_norm=0.,measured_edge_margin=.24)
|
|
|
for r in ticks if r["seq"]>=4]
|
|
|
ack=dict(event="safety_step_back_ack",command_seq=30,utc=1001.4)
|
|
|
observed={(8,4,1000.):observations+[ack,ack]}
|
|
|
report,_=run(rows,observed)
|
|
|
ep=report["episodes"][0]
|
|
|
self.assertAlmostEqual(ep["tele_center_covered_s"],.27)
|
|
|
self.assertAlmostEqual(ep["tele_center_fraction"],.17/.27)
|
|
|
self.assertEqual(ep["min_independently_measured_edge_margin"],.24)
|
|
|
self.assertEqual(ep["confirmed_safety_step_back_count"],1)
|
|
|
|
|
|
def test_cli_directory_file_duplicates_and_custom_baseline(self):
|
|
|
with TemporaryDirectory() as work:
|
|
|
root=Path(work)
|
|
|
data=root/"in"
|
|
|
data.mkdir()
|
|
|
rows=sum((episode(start=1000.+i*10) for i in range(3)), [])
|
|
|
write(data/"samples.jsonl",rows)
|
|
|
write(root/"copy.jsonl",rows)
|
|
|
profile=root/"profile.json"
|
|
|
profile.write_text(json.dumps({"cameras":{"19":{"predictive_zoom":
|
|
|
{"nominal_gain":.52,"upper_gain":1.1,"min_allowed_hfov_deg":10.,
|
|
|
"latency_horizon_ms":245.}}}}),encoding="utf-8")
|
|
|
out=root/"out"
|
|
|
command=[sys.executable,str(Path(c.__file__)),str(data),str(root/"copy.jsonl"),
|
|
|
"--output-dir",str(out),"--profile",str(profile)]
|
|
|
subprocess.run(command,check=True,capture_output=True,text=True)
|
|
|
cand=json.loads((out/"native10_calibrated.json").read_bytes())
|
|
|
report=json.loads((out/"field_report.json").read_bytes())
|
|
|
self.assertEqual(report["input"]["duplicate_rows"],len(rows))
|
|
|
self.assertEqual(cand["cameras"]["19"]["baseline_predictive_zoom"]["nominal_gain"],.52)
|
|
|
self.assertEqual(cand["cameras"]["19"]["predictive_zoom_for_review"]["latency_horizon_ms"],245.)
|
|
|
self.assertIn("1,72 с",(out/"field_report.md").read_text(encoding="utf-8"))
|
|
|
|
|
|
def test_bad_input_leaves_no_outputs(self):
|
|
|
with TemporaryDirectory() as work:
|
|
|
path=Path(work)/"malformed.jsonl"
|
|
|
path.write_text('{"event":"tick","cam":8}\n',encoding="utf-8")
|
|
|
out=Path(work)/"out"
|
|
|
with self.assertRaises(ValueError):
|
|
|
c.load_episodes([path])
|
|
|
self.assertFalse(out.exists())
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
unittest.main()
|