|
|
"""Analyze Chameleon-C PR #10 episode JSONL offline (standard library only)."""
|
|
|
from __future__ import annotations
|
|
|
|
|
|
import argparse
|
|
|
from bisect import bisect_right
|
|
|
from collections import Counter, defaultdict
|
|
|
from dataclasses import dataclass
|
|
|
import json
|
|
|
import math
|
|
|
from pathlib import Path
|
|
|
from statistics import median
|
|
|
|
|
|
|
|
|
EVENTS = {"tick", "first_zoom", "stop", "native10_pulse_calibration", "zoom_block_summary", "status_sample"}
|
|
|
PULSE = "native10_pulse_calibration"
|
|
|
FULL_SWEEP_LOG_RATE = math.log(252 / 6) / 5.5
|
|
|
# Published PR8 replay coefficients carried by PR10. Keys are internal camera IDs.
|
|
|
BASELINE = {8: (0.44648, 0.95109), 10: (0.73468, 1.09732)}
|
|
|
BINS = ("gt_35", "18_to_35", "12_to_lt_18", "10_to_lt_12", "lt_10")
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
class Limits:
|
|
|
max_before_age_s: float = .65
|
|
|
max_pre_stop_age_s: float = .18
|
|
|
max_first_post_stop_age_s: float = .25
|
|
|
max_status_gap_s: float = .35
|
|
|
max_tick_gap_s: float = .20
|
|
|
max_settle_s: float = 1.2
|
|
|
minimum_pulses: int = 3
|
|
|
minimum_tracks: int = 2
|
|
|
edge_buffer: float = .32
|
|
|
floor_step_deg: float = .5
|
|
|
field_floor_deg: float = 10.
|
|
|
|
|
|
|
|
|
def number(value):
|
|
|
if isinstance(value, (int, float)) and not isinstance(value, bool) and math.isfinite(value):
|
|
|
return float(value)
|
|
|
return None
|
|
|
|
|
|
|
|
|
def val(row, key):
|
|
|
return number(row.get(key))
|
|
|
|
|
|
|
|
|
def fov(row, key):
|
|
|
value = val(row, key)
|
|
|
return value if value is not None and 0 < value < 180 else None
|
|
|
|
|
|
|
|
|
def focal(angle):
|
|
|
return -math.log(math.tan(math.radians(angle / 2)))
|
|
|
|
|
|
|
|
|
def angle(log_focal):
|
|
|
return math.degrees(2 * math.atan(math.exp(-log_focal)))
|
|
|
|
|
|
|
|
|
def band(hfov):
|
|
|
return next((label for lower, label in zip((35, 18, 12, 10), BINS)
|
|
|
if hfov >= lower and (lower != 35 or hfov > 35)), BINS[-1])
|
|
|
|
|
|
|
|
|
def percentile(values, q):
|
|
|
i = (len(values) - 1) * q
|
|
|
lo, hi = math.floor(i), math.ceil(i)
|
|
|
return values[lo] + (i - lo) * (values[hi] - values[lo])
|
|
|
|
|
|
|
|
|
def stats(values):
|
|
|
values = sorted(v for v in values if v is not None)
|
|
|
if not values:
|
|
|
return {"n": 0, "min": None, "p50": None, "p90": None, "p95": None, "max": None}
|
|
|
return {"n": len(values), "min": values[0], "p50": median(values),
|
|
|
"p90": percentile(values, .90), "p95": percentile(values, .95), "max": values[-1]}
|
|
|
|
|
|
|
|
|
def input_files(paths):
|
|
|
files = set()
|
|
|
for path in paths:
|
|
|
if path.is_dir():
|
|
|
files.update(p.resolve() for p in path.rglob("*.jsonl") if p.is_file())
|
|
|
elif path.is_file():
|
|
|
files.add(path.resolve())
|
|
|
else:
|
|
|
raise ValueError(f"Input does not exist: {path}")
|
|
|
if not files:
|
|
|
raise ValueError("No JSONL files found")
|
|
|
return sorted(files)
|
|
|
|
|
|
|
|
|
def load_episodes(files):
|
|
|
episodes = defaultdict(list)
|
|
|
ignored = duplicates = 0
|
|
|
seen = set()
|
|
|
for path in files:
|
|
|
with path.open(encoding="utf-8-sig") as stream:
|
|
|
for lineno, line in enumerate(stream, 1):
|
|
|
if not line.strip():
|
|
|
continue
|
|
|
try:
|
|
|
row = json.loads(line)
|
|
|
if not isinstance(row, dict):
|
|
|
raise ValueError("expected a JSON object")
|
|
|
if not isinstance(row.get("event"), str) or row["event"] not in EVENTS:
|
|
|
ignored += 1
|
|
|
continue
|
|
|
cam, gen, start, utc = (val(row, k) for k in
|
|
|
("cam", "generation", "track_start_utc", "utc"))
|
|
|
if (cam is None or gen is None or start is None or utc is None
|
|
|
or not cam.is_integer() or cam < 0 or not gen.is_integer()
|
|
|
or start <= 0 or utc < start):
|
|
|
raise ValueError("invalid PR10 event envelope")
|
|
|
if row["event"] in (PULSE, "stop") and type(row.get("request")) is not int:
|
|
|
raise ValueError("request must be an integer for pulse and stop rows")
|
|
|
if row["event"] == PULSE and type(row.get("native")) is not int:
|
|
|
raise ValueError("native must be an integer for pulse rows")
|
|
|
identity = json.dumps(row, sort_keys=True, allow_nan=False)
|
|
|
if identity in seen:
|
|
|
duplicates += 1
|
|
|
continue
|
|
|
seen.add(identity)
|
|
|
episodes[(int(cam), int(gen), start)].append(row)
|
|
|
except (TypeError, ValueError) as exc:
|
|
|
raise ValueError(f"{path}:{lineno}: {exc}") from exc
|
|
|
return episodes, {"files": [str(p) for p in files], "duplicate_rows": duplicates, "ignored_rows": ignored}
|
|
|
|
|
|
|
|
|
def status_samples(ticks):
|
|
|
samples = {}
|
|
|
for tick in ticks:
|
|
|
at, hfov = val(tick, "FOV_status_utc"), fov(tick, "FOV_actual")
|
|
|
if at is not None and hfov is not None and 0 < at <= tick["utc"]:
|
|
|
samples.setdefault(at, set()).add(hfov)
|
|
|
return samples
|
|
|
|
|
|
|
|
|
def cadence(times):
|
|
|
ordered = sorted(set(times))
|
|
|
gaps = [(b - a) * 1000 for a, b in zip(ordered, ordered[1:])]
|
|
|
return {"samples": len(ordered), "hz": 1000 * len(gaps) / sum(gaps) if gaps else None,
|
|
|
"gap_ms": stats(gaps)}
|
|
|
|
|
|
|
|
|
def analyze_pulse(row, stops, samples, limits):
|
|
|
before, at_stop, first, after = (fov(row, k) for k in
|
|
|
("HFOV_before", "HFOV_stop_sample", "HFOV_first_post_stop", "HFOV_after"))
|
|
|
start_status, put, ack, stop_sample, stop, stop_ack, first_post, after_at = (val(row, k) for k in
|
|
|
("before_at", "put_at", "ack_at", "stop_status_at", "stop_issued_at", "stop_ack_at",
|
|
|
"first_post_stop_at", "after_at"))
|
|
|
duration = val(row, "duration_s")
|
|
|
put_rtt, stop_rtt = val(row, "put_rtt_ms"), val(row, "stop_rtt_ms")
|
|
|
stop_reason = str(row.get("stop_reason") or "").lower()
|
|
|
finished = all(v is not None for v in (put, ack, stop, stop_ack, duration))
|
|
|
finished = bool(finished and put <= ack <= stop <= stop_ack and duration > 0)
|
|
|
reasons = []
|
|
|
if not finished:
|
|
|
reasons.append("missing_or_invalid_completion")
|
|
|
if finished and abs(duration - (stop_ack - put)) > .075:
|
|
|
reasons.append("duration_timestamp_mismatch")
|
|
|
if row.get("outcome") != "settled":
|
|
|
reasons.append("outcome_" + str(row.get("outcome", "unknown")))
|
|
|
if (row.get("clean_stop") is not True or any(s.get("clean") is False for s in stops)
|
|
|
or stop_reason not in {"physical_stop", "priority_stop:watchdog"}):
|
|
|
reasons.append("interrupted_or_unclean_stop")
|
|
|
if put_rtt is None or stop_rtt is None or min(put_rtt, stop_rtt) < 0:
|
|
|
reasons.append("invalid_rtt")
|
|
|
elif finished and (put_rtt / 1000 > ack - put + .05 or abs(stop_rtt / 1000 - (stop_ack - stop)) > .05):
|
|
|
reasons.append("rtt_timestamp_mismatch")
|
|
|
# The settled delta alone cannot separate commanded motion and lens tail.
|
|
|
timing = (start_status, put, ack, stop_sample, stop, stop_ack, first_post, after_at)
|
|
|
ordered = all(t is not None for t in timing) and (
|
|
|
0 < start_status <= put <= ack < stop_sample <= stop <= stop_ack <= first_post < after_at <= row["utc"]
|
|
|
)
|
|
|
if not ordered:
|
|
|
reasons.append("missing_or_unordered_status_brackets")
|
|
|
else:
|
|
|
if stop_sample - put < .04:
|
|
|
reasons.append("insufficient_in_pulse_interval")
|
|
|
if put - start_status > limits.max_before_age_s:
|
|
|
reasons.append("stale_before_status")
|
|
|
if stop - stop_sample > limits.max_pre_stop_age_s:
|
|
|
reasons.append("sparse_pre_stop_status")
|
|
|
if first_post - stop_ack > limits.max_first_post_stop_age_s:
|
|
|
reasons.append("late_post_stop_status")
|
|
|
if first_post - stop_sample > limits.max_status_gap_s:
|
|
|
reasons.append("sparse_status_across_stop")
|
|
|
if after_at - stop_ack > limits.max_settle_s:
|
|
|
reasons.append("settled_status_timeout")
|
|
|
times = [first_post, *(t for t in samples if first_post < t < after_at), after_at]
|
|
|
times.sort()
|
|
|
if max(b - a for a, b in zip(times, times[1:])) > limits.max_status_gap_s:
|
|
|
reasons.append("sparse_post_stop_status")
|
|
|
observations = ((start_status, before), (stop_sample, at_stop), (first_post, first), (after_at, after))
|
|
|
if any(h is not None and any(abs(observed - h) > .1 for observed in samples.get(t, ()))
|
|
|
for t, h in observations):
|
|
|
reasons.append("conflicting_status_samples")
|
|
|
if None in (before, at_stop, first, after):
|
|
|
reasons.append("missing_hfov_bracket")
|
|
|
elif not (after < before and at_stop < before and first <= at_stop + .1 and after <= first + .1):
|
|
|
reasons.append("no_narrowing_or_nonmonotone_hfov")
|
|
|
result = {"request": row.get("request"), "native": row.get("native"), "band": band(before) if before else "unknown",
|
|
|
"outcome": row.get("outcome"), "stop_reason": row.get("stop_reason"),
|
|
|
"completed": finished, "excluded": reasons,
|
|
|
"HFOV_before": before, "HFOV_after": after, "put_at": put, "ack_at": ack,
|
|
|
"stop_issued_at": stop, "stop_ack_at": stop_ack, "after_at": after_at,
|
|
|
"duration_s": duration, "put_rtt_ms": put_rtt, "stop_rtt_ms": stop_rtt,
|
|
|
"effective_log_rate_s": None, "effective_hfov_narrowing_deg_s": None,
|
|
|
"pre_stop_log_rate_s": None, "pre_stop_hfov_narrowing_deg_s": None,
|
|
|
"post_stop_tail_deg": None, "stop_bracket_narrowing_deg": None,
|
|
|
"stop_bracket_log_narrowing": None}
|
|
|
if reasons:
|
|
|
return result
|
|
|
rise = focal(at_stop) - focal(before)
|
|
|
result.update(
|
|
|
effective_log_rate_s=(focal(after) - focal(before)) / duration,
|
|
|
effective_hfov_narrowing_deg_s=(before - after) / duration,
|
|
|
pre_stop_log_rate_s=rise / (stop_sample - put),
|
|
|
pre_stop_hfov_narrowing_deg_s=(before - at_stop) / (stop_sample - put),
|
|
|
post_stop_tail_deg=max(0., first - after),
|
|
|
stop_bracket_narrowing_deg=max(0., at_stop - after),
|
|
|
stop_bracket_log_narrowing=max(0., focal(after) - focal(at_stop)),
|
|
|
)
|
|
|
return result
|
|
|
|
|
|
|
|
|
def mark_rate_outliers(pulses):
|
|
|
eligible = [p for p in pulses if not p["excluded"]]
|
|
|
if len(eligible) < 5:
|
|
|
return
|
|
|
rates = [p["effective_log_rate_s"] for p in eligible]
|
|
|
middle = median(rates)
|
|
|
mad = median(abs(x - middle) for x in rates)
|
|
|
threshold = max(.35 * middle, 5 * 1.4826 * mad)
|
|
|
for pulse in eligible:
|
|
|
if abs(pulse["effective_log_rate_s"] - middle) > threshold:
|
|
|
pulse["excluded"].append("rate_outlier")
|
|
|
|
|
|
|
|
|
def summaries(rows):
|
|
|
result, latest = {}, {}
|
|
|
for row in sorted(rows, key=lambda r: r["utc"]):
|
|
|
counts = row.get("zoom_block_reason")
|
|
|
if not isinstance(counts, dict):
|
|
|
continue
|
|
|
for key, value in counts.items():
|
|
|
if type(value) is int and value >= result.get(key, 0):
|
|
|
result[key] = value
|
|
|
values = row.get("last_values", {})
|
|
|
if isinstance(values, dict) and key in values:
|
|
|
latest[key] = values[key]
|
|
|
return result, latest
|
|
|
|
|
|
|
|
|
def episode_report(owner, rows, limits):
|
|
|
camera, generation, start = owner
|
|
|
by_event = defaultdict(list)
|
|
|
for row in rows:
|
|
|
by_event[row["event"]].append(row)
|
|
|
ticks = sorted(by_event["tick"], key=lambda row: row["utc"])
|
|
|
poll_rows = by_event["status_sample"]
|
|
|
fresh_rows = [row for row in poll_rows if row.get("optics_fresh") is True
|
|
|
and fov(row, "FOV_actual") is not None and val(row, "FOV_status_utc") is not None]
|
|
|
statuses = status_samples([*ticks, *fresh_rows])
|
|
|
frame_times = [at for row in ticks if (at := val(row, "frame_utc")) is not None
|
|
|
and 0 < at <= row["utc"]]
|
|
|
stops = defaultdict(list)
|
|
|
for row in by_event["stop"]:
|
|
|
stops[row.get("request")].append(row)
|
|
|
pulses = [analyze_pulse(row, stops[row.get("request")], statuses, limits) for row in by_event[PULSE]]
|
|
|
requests = Counter(p["request"] for p in pulses)
|
|
|
for pulse in pulses:
|
|
|
if type(pulse["request"]) is not int or pulse["request"] <= 0:
|
|
|
pulse["excluded"].append("invalid_request")
|
|
|
elif requests[pulse["request"]] != 1:
|
|
|
pulse["excluded"].append("conflicting_request_records")
|
|
|
first_events = [r for r in by_event["first_zoom"] if val(r, "t_to_first_zoom_ms") is not None
|
|
|
and r["t_to_first_zoom_ms"] >= 0 and val(r, "native") and r["native"] > 0
|
|
|
and abs(r["t_to_first_zoom_ms"] / 1000 - (r["utc"] - start)) <= .005]
|
|
|
first = min((r["t_to_first_zoom_ms"] for r in first_events), default=None)
|
|
|
ack = min((p["ack_at"] for p in pulses if p["ack_at"] is not None and p["ack_at"] >= start
|
|
|
and number(p["native"]) and p["native"] > 0), default=None)
|
|
|
early = {p["request"] for p in pulses if "interrupted_or_unclean_stop" in p["excluded"]
|
|
|
and p["completed"]}
|
|
|
early.update(key for key, values in stops.items()
|
|
|
if key is not None and any(r.get("clean") is False for r in values))
|
|
|
counts, values = summaries(by_event["zoom_block_summary"])
|
|
|
negative_starts = 0
|
|
|
prev_negative = False
|
|
|
previous_utc = None
|
|
|
predicted_margins = []
|
|
|
tele_s = 0.
|
|
|
prev_tele = False
|
|
|
for row in ticks:
|
|
|
if previous_utc is None or row["utc"] - previous_utc > limits.max_tick_gap_s:
|
|
|
prev_negative, prev_tele = False, False
|
|
|
zoom = val(row, "zoom_cmd")
|
|
|
negative = zoom is not None and zoom < 0
|
|
|
negative_starts += int(negative and not prev_negative)
|
|
|
measured_at, hfov = val(row, "FOV_status_utc"), fov(row, "FOV_actual")
|
|
|
tele = bool(measured_at is not None and hfov is not None and hfov < 18
|
|
|
and 0 <= row["utc"] - measured_at <= limits.max_status_gap_s)
|
|
|
if tele and prev_tele:
|
|
|
tele_s += row["utc"] - previous_utc
|
|
|
prev_negative, prev_tele, previous_utc = negative, tele, row["utc"]
|
|
|
if val(row, "margin") is not None:
|
|
|
predicted_margins.append(row["margin"])
|
|
|
observed_hfov = [h for vals in statuses.values() for h in vals]
|
|
|
for row in by_event[PULSE]:
|
|
|
for key, timestamp in (("HFOV_before", "before_at"), ("HFOV_stop_sample", "stop_status_at"),
|
|
|
("HFOV_first_post_stop", "first_post_stop_at"), ("HFOV_after", "after_at")):
|
|
|
hfov, at = fov(row, key), val(row, timestamp)
|
|
|
if hfov is not None and at is not None and 0 < at <= row["utc"]:
|
|
|
observed_hfov.append(hfov)
|
|
|
edge_samples = []
|
|
|
for row in by_event["zoom_block_summary"]:
|
|
|
edge = row.get("last_values", {})
|
|
|
edge = edge.get("edge_margin", {}) if isinstance(edge, dict) else {}
|
|
|
m = val(edge, "measured") if isinstance(edge, dict) else None
|
|
|
if m is not None and -.5 <= m <= .5:
|
|
|
edge_samples.append(m)
|
|
|
final = any(r.get("final") is True for r in by_event["zoom_block_summary"])
|
|
|
reported_start = first is not None or ack is not None
|
|
|
category = ("early_stop" if early else "positive_ack_observed" if reported_start
|
|
|
else "no_positive_ack_observed" if final else "incomplete_observation")
|
|
|
return {
|
|
|
"cam": camera, "camera": camera + 11, "generation": generation, "track_start_utc": start,
|
|
|
"final_summary_seen": final, "classification": category,
|
|
|
"t_to_first_zoom_s": first / 1000 if first is not None else None,
|
|
|
"t_to_first_observed_pulse_s": first / 1000 if first is not None else ack - start if ack is not None else None,
|
|
|
"min_hfov_deg": min(observed_hfov, default=None),
|
|
|
"isapi_track_status": {
|
|
|
"published": len(poll_rows),
|
|
|
"success": sum(row.get("status_ok") is True for row in poll_rows),
|
|
|
"request_ms": stats([val(row, "request_ms") for row in poll_rows]),
|
|
|
"successful_status": cadence(row["utc"] for row in poll_rows if row.get("status_ok") is True),
|
|
|
"fresh_optics": cadence(row["FOV_status_utc"] for row in fresh_rows),
|
|
|
},
|
|
|
"control_sampled_frame_cadence": cadence(frame_times),
|
|
|
"tele_observed_s": tele_s, "tele_center_fraction": None, "tele_center_covered_s": 0.,
|
|
|
"min_independently_measured_edge_margin": None,
|
|
|
"tele_center_availability": "PR10 ticks omit target center coordinates",
|
|
|
"min_projected_margin": min(predicted_margins, default=None),
|
|
|
"min_sampled_measured_edge_margin": min(edge_samples, default=None),
|
|
|
"negative_zoom_request_runs": negative_starts,
|
|
|
"confirmed_safety_step_back_count": None,
|
|
|
"safety_step_back_availability": "PR10 does not mark delivered negative commands or their purpose",
|
|
|
"early_stop_requests": len(early),
|
|
|
"zoom_block_reason": counts, "block_last_values": values,
|
|
|
"top_block_reasons": sorted(counts.items(), key=lambda p: (-p[1], p[0]))[:5],
|
|
|
"pulses": pulses,
|
|
|
}, ticks
|
|
|
|
|
|
|
|
|
def load_observations(paths):
|
|
|
"""Optional independent observations indexed by PR10 episode owner."""
|
|
|
records = defaultdict(list)
|
|
|
for path in paths:
|
|
|
with path.open(encoding="utf-8-sig") as source:
|
|
|
for line_no, line in enumerate(source, 1):
|
|
|
if not line.strip():
|
|
|
continue
|
|
|
try:
|
|
|
row = json.loads(line)
|
|
|
cam, generation, start = (val(row, key) for key in ("cam", "generation", "track_start_utc"))
|
|
|
if (cam is None or generation is None or start is None or not cam.is_integer()
|
|
|
or not generation.is_integer() or start <= 0 or not row.get("source")):
|
|
|
raise ValueError("observation needs owner and source")
|
|
|
event = row.get("event")
|
|
|
if event == "frame_observation":
|
|
|
at, x, y = (val(row, key) for key in ("frame_utc", "dx_norm", "dy_norm"))
|
|
|
margin = val(row, "measured_edge_margin")
|
|
|
if (type(row.get("seq")) is not int or at is None or x is None or y is None
|
|
|
or ("measured_edge_margin" in row and
|
|
|
(margin is None or not -.5 <= margin <= .5))):
|
|
|
raise ValueError("frame observation needs seq, frame_utc and normalized offsets")
|
|
|
elif event == "safety_step_back_ack":
|
|
|
if (type(row.get("command_seq")) is not int or val(row, "utc") is None
|
|
|
or row["utc"] < start or row.get("native", 0) >= 0
|
|
|
or row.get("reason") != "tele_step_back"):
|
|
|
raise ValueError("step-back observation needs a delivered negative command and reason")
|
|
|
else:
|
|
|
raise ValueError("unsupported observation event")
|
|
|
records[(int(cam), int(generation), start)].append(row)
|
|
|
except (AttributeError, TypeError, ValueError) as exc:
|
|
|
raise ValueError(f"{path}:{line_no}: {exc}") from exc
|
|
|
return records
|
|
|
|
|
|
|
|
|
def apply_observations(ep, ticks, rows, limits):
|
|
|
if not rows:
|
|
|
return
|
|
|
logged_frames = {r.get("seq"): r for r in ticks if type(r.get("seq")) is int}
|
|
|
statuses = status_samples(ticks)
|
|
|
times = sorted(statuses)
|
|
|
samples = {}
|
|
|
confirmed = set()
|
|
|
for row in rows:
|
|
|
if row["event"] == "safety_step_back_ack":
|
|
|
if ticks and row["utc"] <= ticks[-1]["utc"] + limits.max_tick_gap_s:
|
|
|
confirmed.add(row["command_seq"])
|
|
|
continue
|
|
|
recorded = logged_frames.get(row["seq"])
|
|
|
recorded_at = val(recorded, "frame_utc") if recorded else None
|
|
|
if recorded_at is None or abs(recorded_at - row["frame_utc"]) > .01:
|
|
|
continue
|
|
|
at = row["frame_utc"]
|
|
|
i = bisect_right(times, at) - 1
|
|
|
tele = (i >= 0 and len(statuses[times[i]]) == 1 and max(statuses[times[i]]) < 18
|
|
|
and at - times[i] <= limits.max_status_gap_s)
|
|
|
value = (at, tele, max(abs(row["dx_norm"]), abs(row["dy_norm"])) <= .1,
|
|
|
row.get("measured_edge_margin"))
|
|
|
if row["seq"] in samples and samples[row["seq"]] != value:
|
|
|
raise ValueError(f"Conflicting frame observation for sequence {row['seq']}")
|
|
|
samples[row["seq"]] = value
|
|
|
frames = sorted(samples.values(), key=lambda r: r[0])
|
|
|
exposure = centered = 0.
|
|
|
for a, b in zip(frames, frames[1:]):
|
|
|
dt = b[0] - a[0]
|
|
|
if a[1] and b[1] and 0 < dt <= limits.max_tick_gap_s:
|
|
|
exposure += dt
|
|
|
centered += dt * a[2]
|
|
|
ep["tele_center_covered_s"] = exposure
|
|
|
ep["tele_center_fraction"] = centered / exposure if exposure else None
|
|
|
if exposure:
|
|
|
ep["tele_center_availability"] = "time-weighted over paired independent frames with fresh ISAPI"
|
|
|
ep["min_independently_measured_edge_margin"] = min((r[3] for r in frames if r[3] is not None), default=None)
|
|
|
if confirmed:
|
|
|
ep["confirmed_safety_step_back_count"] = len(confirmed)
|
|
|
ep["safety_step_back_availability"] = "observed independently confirmed acknowledgements; completeness unknown"
|
|
|
|
|
|
|
|
|
def load_profiles(path):
|
|
|
if path is None:
|
|
|
return {cam: {"nominal_gain": n, "upper_gain": u, "min_allowed_hfov_deg": 10.}
|
|
|
for cam, (n, u) in BASELINE.items()}
|
|
|
source = json.loads(path.read_text(encoding="utf-8-sig"))
|
|
|
result = {}
|
|
|
for external, row in source["cameras"].items():
|
|
|
cfg = row.get("predictive_zoom", row.get("replay_settings", row))
|
|
|
nominal, upper = (val(cfg, key) for key in ("nominal_gain", "upper_gain"))
|
|
|
floor = val(cfg, "min_allowed_hfov_deg") if "min_allowed_hfov_deg" in cfg else 10.
|
|
|
edge = val(cfg, "tele_edge_margin_ratio") if "tele_edge_margin_ratio" in cfg else .32
|
|
|
if (nominal is None or upper is None or not 0 < nominal <= upper
|
|
|
or floor is None or not 2 <= floor <= 15 or edge is None or not 0 < edge < .5):
|
|
|
raise ValueError(f"Invalid profile for camera {external}")
|
|
|
result[int(external) - 11] = cfg.copy()
|
|
|
return result
|
|
|
|
|
|
|
|
|
def range_report(pulses, profile):
|
|
|
completed = [p for p in pulses if p["completed"]]
|
|
|
accepted = [p for p in pulses if not p["excluded"]]
|
|
|
excluded = Counter(r for p in pulses for r in p["excluded"])
|
|
|
result = {"seen": len(pulses), "completed": len(completed),
|
|
|
"qualified": len(accepted), "exclusions": dict(sorted(excluded.items())),
|
|
|
"completed_put_rtt_ms": stats(p["put_rtt_ms"] for p in completed if (p["put_rtt_ms"] or 0) >= 0),
|
|
|
"completed_stop_rtt_ms": stats(p["stop_rtt_ms"] for p in completed if (p["stop_rtt_ms"] or 0) >= 0)}
|
|
|
for key in ("effective_log_rate_s", "effective_hfov_narrowing_deg_s", "pre_stop_log_rate_s",
|
|
|
"pre_stop_hfov_narrowing_deg_s", "post_stop_tail_deg", "stop_bracket_narrowing_deg",
|
|
|
"stop_bracket_log_narrowing", "put_rtt_ms", "stop_rtt_ms"):
|
|
|
result[key] = stats(p[key] for p in accepted)
|
|
|
if profile is not None:
|
|
|
nominal, upper = (profile[key] * FULL_SWEEP_LOG_RATE for key in ("nominal_gain", "upper_gain"))
|
|
|
median_observed = result["effective_log_rate_s"]["p50"]
|
|
|
result["native10_profile_comparison"] = {
|
|
|
"nominal_log_rate_s": nominal, "upper_log_rate_s": upper,
|
|
|
"median_observed_over_nominal": None if median_observed is None else median_observed / nominal,
|
|
|
"qualified_effective_rates_above_upper": sum(p["effective_log_rate_s"] > upper for p in accepted),
|
|
|
}
|
|
|
return result
|
|
|
|
|
|
|
|
|
def margin_during_pulse(pulse, ticks, limits):
|
|
|
lo, hi = pulse["put_at"], pulse["after_at"]
|
|
|
window = [r for r in ticks if lo <= r["utc"] <= hi]
|
|
|
if len(window) < 2:
|
|
|
return None
|
|
|
times = [lo, *(r["utc"] for r in window), hi]
|
|
|
if max(b - a for a, b in zip(times, times[1:])) > limits.max_tick_gap_s:
|
|
|
return None
|
|
|
dropped = [val(r, "dropped_before") for r in window]
|
|
|
if None in dropped or min(dropped) != max(dropped):
|
|
|
return None
|
|
|
margins, requirements = [], []
|
|
|
for row in window:
|
|
|
margin, required, measured_at = (val(row, k) for k in
|
|
|
("margin", "edge_margin_required", "FOV_status_utc"))
|
|
|
if (margin is None or not -.5 <= margin < .5 or measured_at is None
|
|
|
or not 0 <= row["utc"] - measured_at <= limits.max_status_gap_s
|
|
|
or fov(row, "FOV_actual") is None or row.get("stop_requested") is True):
|
|
|
return None
|
|
|
margins.append(margin)
|
|
|
if required is not None and 0 < required < .5:
|
|
|
requirements.append(required)
|
|
|
return min(margins), max(requirements, default=limits.edge_buffer)
|
|
|
|
|
|
|
|
|
def next_floor(profile, pulses_and_episodes, limits):
|
|
|
current = profile.get("min_allowed_hfov_deg", limits.field_floor_deg)
|
|
|
near = [(p, ep, ticks) for p, ep, ticks in pulses_and_episodes
|
|
|
if p["HFOV_before"] is not None and current <= p["HFOV_before"] < current + 2
|
|
|
and (p["HFOV_after"] is not None and p["HFOV_after"] <= current + .5
|
|
|
or p["HFOV_before"] <= current + .5 and p["HFOV_after"] is None)]
|
|
|
paired = [(p, ep, margin_during_pulse(p, ticks, limits)) for p, ep, ticks in near
|
|
|
if not p["excluded"] and ep["final_summary_seen"]]
|
|
|
usable = [(p, ep, frame) for p, ep, frame in paired if frame is not None]
|
|
|
result = {"current_deg": current, "proposed_next_test_deg": None, "near_floor_windows": len(near),
|
|
|
"qualified_margin_windows": len(usable), "scope": "bounded_review_only"}
|
|
|
if len(usable) < limits.minimum_pulses or len({ep["track_start_utc"] for _, ep, _ in usable}) < limits.minimum_tracks:
|
|
|
return result | {"reason": "insufficient_near_floor_status_and_framing"}
|
|
|
if len(usable) != len(near):
|
|
|
return result | {"reason": "unqualified_near_floor_windows_present"}
|
|
|
if any(p["HFOV_after"] is not None and p["HFOV_after"] < current - .05
|
|
|
for p, _, _ in pulses_and_episodes):
|
|
|
return result | {"reason": "observed_hfov_below_current_floor"}
|
|
|
worst_tail = max(p["stop_bracket_log_narrowing"] for p, _, _ in usable)
|
|
|
# Bound a small local step by the worst projected extent and observed
|
|
|
# STOP bracket. The +.02 log reserve keeps estimation/rounding conservative.
|
|
|
floors = []
|
|
|
for pulse, _, (margin, required) in usable:
|
|
|
required = max(limits.edge_buffer, required, profile.get("tele_edge_margin_ratio", 0.))
|
|
|
room = (.5 - required) / (.5 - margin)
|
|
|
log_budget = math.log(room) - worst_tail - .02
|
|
|
floors.append(angle(focal(pulse["HFOV_after"]) + log_budget))
|
|
|
geometric_floor = max(floors)
|
|
|
proposed = math.ceil(max(current - limits.floor_step_deg, geometric_floor, 2.) * 10 - 1e-9) / 10
|
|
|
result.update(worst_stop_bracket_log_narrowing=worst_tail, geometry_floor_deg=geometric_floor,
|
|
|
minimum_projected_margin=min(frame[0] for _, _, frame in usable))
|
|
|
if proposed >= current:
|
|
|
return result | {"reason": "no_lower_step_with_observed_margin"}
|
|
|
return result | {"proposed_next_test_deg": proposed, "reason": "limited_step_supported_by_observed_brackets"}
|
|
|
|
|
|
|
|
|
def analyze(episodes, profiles, limits, observations=None):
|
|
|
reports = []
|
|
|
pulses_by_camera = defaultdict(list)
|
|
|
grouped = defaultdict(list)
|
|
|
for owner, rows in sorted(episodes.items()):
|
|
|
ep, ticks = episode_report(owner, rows, limits)
|
|
|
apply_observations(ep, ticks, observations.get(owner, []) if observations else [], limits)
|
|
|
reports.append(ep)
|
|
|
for pulse in ep["pulses"]:
|
|
|
pulses_by_camera[owner[0]].append((pulse, ep, ticks))
|
|
|
grouped[(owner[0], pulse["native"], pulse["band"])].append(pulse)
|
|
|
ranges = defaultdict(dict)
|
|
|
for (cam, native, fov_band), pulses in sorted(grouped.items(), key=lambda p: repr(p[0])):
|
|
|
mark_rate_outliers(pulses)
|
|
|
ranges[cam].setdefault(str(native), {})[fov_band] = range_report(
|
|
|
pulses, profiles.get(cam) if native == 10 else None)
|
|
|
camera_reports = {}
|
|
|
candidates = {}
|
|
|
for cam in sorted({ep["cam"] for ep in reports}):
|
|
|
profile = profiles.get(cam)
|
|
|
native10 = [(p, ep, ticks) for p, ep, ticks in pulses_by_camera[cam] if p["native"] == 10]
|
|
|
accepted = [(p, ep) for p, ep, _ in native10 if not p["excluded"] and p["HFOV_before"] < 18]
|
|
|
count_tracks = len({ep["track_start_utc"] for _, ep in accepted})
|
|
|
floor = next_floor(profile, native10, limits) if profile is not None else {
|
|
|
"current_deg": None, "proposed_next_test_deg": None, "reason": "camera_profile_missing"}
|
|
|
ready = (profile is not None and len(accepted) >= limits.minimum_pulses
|
|
|
and count_tracks >= limits.minimum_tracks)
|
|
|
candidate = None
|
|
|
if ready:
|
|
|
nominal = median(p["effective_log_rate_s"] for p, _ in accepted) / FULL_SWEEP_LOG_RATE
|
|
|
# A rejected rate spike must not quietly reduce the upper envelope.
|
|
|
high = max(max(p["effective_log_rate_s"], p["pre_stop_log_rate_s"])
|
|
|
for p, _, _ in native10 if p["effective_log_rate_s"] is not None
|
|
|
and p["HFOV_before"] < 18)
|
|
|
candidate = profile | {
|
|
|
"nominal_gain": nominal,
|
|
|
"upper_gain": max(profile["upper_gain"], nominal, 1.25 * high / FULL_SWEEP_LOG_RATE),
|
|
|
"min_allowed_hfov_deg": (floor["proposed_next_test_deg"] if floor["proposed_next_test_deg"] is not None
|
|
|
else profile.get("min_allowed_hfov_deg", limits.field_floor_deg)),
|
|
|
}
|
|
|
candidates[str(cam + 11)] = {
|
|
|
"internal_camera": cam, "qualified_tele_pulses": len(accepted), "qualified_tracks": count_tracks,
|
|
|
"baseline_predictive_zoom": profile, "predictive_zoom_for_review": candidate,
|
|
|
"next_floor_review": floor,
|
|
|
"observed_start_hfov_deg": stats(p["HFOV_before"] for p, _ in accepted),
|
|
|
}
|
|
|
camera_reports[str(cam + 11)] = {
|
|
|
"internal_camera": cam, "pulses_by_native_and_hfov_band": ranges[cam],
|
|
|
"episodes": sum(ep["cam"] == cam for ep in reports),
|
|
|
"t_to_first_zoom_s": stats(ep["t_to_first_zoom_s"] for ep in reports if ep["cam"] == cam),
|
|
|
}
|
|
|
return {"cameras": camera_reports, "episodes": reports}, {
|
|
|
"production_enable": False, "scope": "offline_native10_profile_for_review", "cameras": candidates}
|
|
|
|
|
|
|
|
|
def md_number(value, scale=1):
|
|
|
return "—" if value is None else f"{value * scale:.3f}"
|
|
|
|
|
|
|
|
|
def markdown(report, candidate):
|
|
|
lines = [
|
|
|
"# Полевой отчёт Chameleon-C · PR №10", "",
|
|
|
"Время первого зума отсчитывается от TRACK до подтверждения первого положительного PUT. "
|
|
|
"Минимальный HFOV берётся из фактических замеров ISAPI.", "",
|
|
|
"| Камера | TRACK UTC (Unix s) | Статус | Первый зум, с | HFOV min, ° | Секунды с HFOV <18° | "
|
|
|
"В центре, % | Прогноз запаса, % | Измеренный запас, % | Запросы / подтверждённые шаги |",
|
|
|
"|---|---:|---|---:|---:|---:|---:|---:|---:|---:|",
|
|
|
]
|
|
|
for ep in report["episodes"]:
|
|
|
lines.append(
|
|
|
f'| PTZ{ep["camera"]} | {ep["track_start_utc"]:.3f} | {ep["classification"]} | '
|
|
|
f'{md_number(ep["t_to_first_zoom_s"])} | {md_number(ep["min_hfov_deg"])} | '
|
|
|
f'{md_number(ep["tele_observed_s"])} | {md_number(ep["tele_center_fraction"], 100)} | '
|
|
|
f'{md_number(ep["min_projected_margin"], 100)} | '
|
|
|
f'{md_number(ep["min_independently_measured_edge_margin"] if ep["min_independently_measured_edge_margin"] is not None else ep["min_sampled_measured_edge_margin"], 100)} | '
|
|
|
f'{ep["negative_zoom_request_runs"]} / {ep["confirmed_safety_step_back_count"] if ep["confirmed_safety_step_back_count"] is not None else "—"} |'
|
|
|
)
|
|
|
lines += ["", "Координаты центра и подтверждения защитных расширений берутся из дополнительных "
|
|
|
"наблюдений, когда они переданы. Без них эти метрики остаются неизвестными. "
|
|
|
"Запросы расширения считаются по наблюдаемым группам отрицательного zoom_cmd. "
|
|
|
"Измеренный запас из одного PR10 JSONL ограничен выборкой блокировок у края.", "",
|
|
|
"Ориентиры стенда PR №10 — 1,72 с до первого импульса на восстановленном PTZ19 при "
|
|
|
"220/50 мс, 100% и 99,92% у центра для PTZ19 и PTZ21 на общих отсчётах ниже 18°. "
|
|
|
"Сопоставимость с полевым пролётом зависит от траектории, задержки и покрытия наблюдений.", "",
|
|
|
"## Причины блокировки", ""]
|
|
|
for ep in report["episodes"]:
|
|
|
if ep["classification"] in ("no_positive_ack_observed", "early_stop", "incomplete_observation"):
|
|
|
reasons = ", ".join(f"{reason} ({count})" for reason, count in ep["top_block_reasons"]) or "сводки нет"
|
|
|
lines.append(f'- PTZ{ep["camera"]}, TRACK {ep["track_start_utc"]:.3f} · {reasons}')
|
|
|
lines += ["", "## Частота статуса во время TRACK", "",
|
|
|
"| Камера | TRACK UTC (Unix s) | Свежий HFOV, Гц | Интервал HFOV p95 / max, мс | "
|
|
|
"Ответ статуса p95, мс | Интервал кадров в TRACK p95, мс |",
|
|
|
"|---|---:|---:|---:|---:|---:|"]
|
|
|
for ep in report["episodes"]:
|
|
|
status = ep["isapi_track_status"]
|
|
|
optics = status["fresh_optics"]
|
|
|
lines.append(f'| PTZ{ep["camera"]} | {ep["track_start_utc"]:.3f} | '
|
|
|
f'{md_number(optics["hz"])} | {md_number(optics["gap_ms"]["p95"])} / '
|
|
|
f'{md_number(optics["gap_ms"]["max"])} | {md_number(status["request_ms"]["p95"])} | '
|
|
|
f'{md_number(ep["control_sampled_frame_cadence"]["gap_ms"]["p95"])} |')
|
|
|
lines += ["", "Интервал кадров рассчитан по кадрам, дошедшим до контроллера. "
|
|
|
"Для оценки джиттера всего видеопотока нужны метки времени исходных кадров.", "",
|
|
|
"## Отклик оптики и RTT", "",
|
|
|
"| Камера | Native | Диапазон HFOV | Принято / завершено | До STOP, °/с (медиана) | "
|
|
|
"Хвост после STOP, ° (макс.) | Верхняя граница скобки STOP, ° (макс.) | RTT PUT / STOP p95, мс |",
|
|
|
"|---|---:|---|---:|---:|---:|---:|---:|"]
|
|
|
for camera, rows in report["cameras"].items():
|
|
|
for native, bins in rows["pulses_by_native_and_hfov_band"].items():
|
|
|
for name, value in sorted(bins.items(), key=lambda x: (BINS+("unknown",)).index(x[0])):
|
|
|
lines.append(f'| PTZ{camera} | {native} | {name} | {value["qualified"]} / {value["completed"]} | '
|
|
|
f'{md_number(value["pre_stop_hfov_narrowing_deg_s"]["p50"])} | '
|
|
|
f'{md_number(value["post_stop_tail_deg"]["max"])} | '
|
|
|
f'{md_number(value["stop_bracket_narrowing_deg"]["max"])} | '
|
|
|
f'{md_number(value["completed_put_rtt_ms"]["p95"])} / '
|
|
|
f'{md_number(value["completed_stop_rtt_ms"]["p95"])} |')
|
|
|
lines += ["", "Скорость до STOP оценивается по статусу от PUT до последнего замера перед STOP. "
|
|
|
"Остаточное сужение от первого замера после STOP даёт нижнюю оценку. "
|
|
|
"Интервал от последнего замера до STOP даёт верхнюю наблюдаемую скобку при монотонном движении.", "",
|
|
|
"## Следующий предел HFOV", ""]
|
|
|
for camera, row in candidate["cameras"].items():
|
|
|
f = row["next_floor_review"]
|
|
|
lines.append(f'- PTZ{camera} · текущий {md_number(f["current_deg"])}°, '
|
|
|
f'следующий на проверку {md_number(f["proposed_next_test_deg"])}° · {f["reason"]}')
|
|
|
lines += ["", "Предложение ограничено наблюдавшимся диапазоном и малым шагом. "
|
|
|
"Полный набор исключений, RTT и оценки по импульсам находятся в field_report.json.", ""]
|
|
|
return "\n".join(lines)
|
|
|
|
|
|
|
|
|
def main(argv=None):
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
parser.add_argument("inputs", nargs="+", type=Path, help="episode JSONL files or directories")
|
|
|
parser.add_argument("--output-dir", type=Path, required=True)
|
|
|
parser.add_argument("--profile", type=Path, help="JSON cameras keyed by external camera number")
|
|
|
parser.add_argument("--observations", nargs="+", type=Path,
|
|
|
help="optional independent frame and step-back observations, separate from PR10 JSONL")
|
|
|
parser.add_argument("--max-before-age-ms", type=float, default=650.)
|
|
|
parser.add_argument("--max-pre-stop-age-ms", type=float, default=180.)
|
|
|
parser.add_argument("--max-first-post-stop-age-ms", type=float, default=250.)
|
|
|
parser.add_argument("--max-status-gap-ms", type=float, default=350.)
|
|
|
args = parser.parse_args(argv)
|
|
|
delays = (args.max_before_age_ms, args.max_pre_stop_age_ms,
|
|
|
args.max_first_post_stop_age_ms, args.max_status_gap_ms)
|
|
|
if not all(math.isfinite(x) and x > 0 for x in delays):
|
|
|
parser.error("Status thresholds must be finite and positive")
|
|
|
limits = Limits(*(x / 1000 for x in delays))
|
|
|
try:
|
|
|
inputs = input_files(args.inputs)
|
|
|
episodes, metadata = load_episodes(inputs)
|
|
|
if not episodes:
|
|
|
raise ValueError("No recognized PR10 events")
|
|
|
profiles = load_profiles(args.profile)
|
|
|
observations = load_observations(args.observations) if args.observations else {}
|
|
|
report, candidate = analyze(episodes, profiles, limits, observations)
|
|
|
metadata["independent_observation_files"] = [str(p) for p in (args.observations or [])]
|
|
|
report.update(input=metadata, limits=vars(limits),
|
|
|
baseline_profile_source=str(args.profile) if args.profile else "PR10 published replay coefficients")
|
|
|
args.output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
files = {"native10_calibrated.json": json.dumps(candidate, indent=2, allow_nan=False) + "\n",
|
|
|
"field_report.json": json.dumps(report, indent=2, allow_nan=False) + "\n",
|
|
|
"field_report.md": markdown(report, candidate)}
|
|
|
protected = set(inputs) | {p.resolve() for p in (args.observations or [])}
|
|
|
if args.profile:
|
|
|
protected.add(args.profile.resolve())
|
|
|
if any((args.output_dir / name).resolve() in protected for name in files):
|
|
|
raise ValueError("Output path would overwrite an input")
|
|
|
for name, content in files.items():
|
|
|
(args.output_dir / name).write_text(content, encoding="utf-8")
|
|
|
except (OSError, KeyError, TypeError, ValueError) as exc:
|
|
|
parser.error(str(exc))
|
|
|
return 0
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
raise SystemExit(main())
|