Scripts: shared helpers in common.py, no tool imports another; CLI and outputs unchanged

data.py stays the reader (METRIC_RE now also captures the file date, same matches);
common.py holds columns, steps, noise, sensor list, runs, file names. Duplicated
run grouping, pose gaps, step bucketing, --table/--outside parsing removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
maximeandClaude Opus 5.5 committed 2026-11-16 15:48:42 +01:00
1 parent c1719cbd1d
commit 6ed4c420e1
13 files changed
+308 -359

No files matched your search

+12 -21
View File
@@ -46,7 +46,7 @@ import sys
from datetime import timedelta
import data
from summary import QUALITY as METRICS, duration
from common import QUALITY as METRICS, duration, group, per_step, sensors
HIGH_BAD = ("fwhm", "dominance") # for these two, it is the rise that says a degraded image
COLOURS = {"Visibility": "tab:blue", "Obstruction": "tab:orange", "Thermal displacement": "tab:red",
@@ -89,6 +89,11 @@ def affected(r, c, threshold):
return r is not None and ((c in ("flux", "snr") and r < threshold) or (c == "dominance" and r > 1 / threshold))
def hit(ref, s, m, threshold):
"""The measurement says a degraded image: flux or snr below threshold, or dominance above 1/threshold."""
return any(affected(ratio(ref, s, c, m), c, threshold) for c in ("flux", "snr", "dominance"))
def med(v):
return f"{statistics.median(v):.2f}" if v else "-"
@@ -123,14 +128,8 @@ def summary(by_sensor, ref, names, refs, pmax):
for c in METRICS))
def per_step(by_sensor, names, step):
step = duration(step)
start = min(m["t"] for s in names for m in by_sensor[s]).replace(hour=0, minute=0, second=0, microsecond=0)
return lambda t: start + (t - start) // step * step
def table(by_sensor, ref, names, c, step, threshold):
key, cells = per_step(by_sensor, names, step), {}
key, cells = per_step(min(m["t"] for s in names for m in by_sensor[s]), duration(step)), {}
for s in names:
for m in by_sensor[s]:
r = ratio(ref, s, c, m)
@@ -158,7 +157,7 @@ def table(by_sensor, ref, names, c, step, threshold):
def common(by_sensor, ref, names, refs, step):
"""Per step: median of the ratios of the targets, then of the references, for each metric."""
key, cells = per_step(by_sensor, names, step), {}
key, cells = per_step(min(m["t"] for s in names for m in by_sensor[s]), duration(step)), {}
for s in names:
for m in by_sensor[s]:
d = cells.setdefault(key(m["t"]), {c: ([], []) for c in METRICS})
@@ -204,7 +203,7 @@ def labels(a, every, ref, refs, names, start, end):
worst = []
for s in names:
ms = [m for m in win[s] if t0 <= m["t"] <= t1]
if any(affected(ratio(ref, s, c, m), c, a.threshold) for m in ms for c in ("flux", "snr", "dominance")):
if any(hit(ref, s, m, a.threshold) for m in ms):
fl = [x for x in (ratio(ref, s, "flux", m) for m in ms) if x is not None]
worst.append((min(fl) if fl else 1, s + ("r" if s in refs else "")))
worst.sort()
@@ -236,14 +235,9 @@ def outside(a, every, ref, refs, names, start, end):
for m in data.filter_window(every[s], start, end):
if within(m["t"], covered):
continue
if any(affected(ratio(ref, s, c, m), c, a.threshold) for c in ("flux", "snr", "dominance")):
if hit(ref, s, m, a.threshold):
inst.setdefault(m["t"], {})[s + ("r" if s in refs else "")] = ratio(ref, s, "flux", m) or 0
runs = []
for t in sorted(t for t in inst if len(inst[t]) >= a.min):
if runs and t - runs[-1][-1] <= timedelta(minutes=15):
runs[-1].append(t)
else:
runs.append([t])
runs = group(sorted(t for t in inst if len(inst[t]) >= a.min), lambda x, y: y - x <= timedelta(minutes=15))
print(f"Outside the labels (except {a.except_}): times when at least {a.min} sensors are affected; {len(runs)} runs")
for ru in runs:
worst = {}
@@ -369,13 +363,10 @@ def main():
if unknown:
p.error(f"unknown metrics: {', '.join(unknown)}; known: {', '.join(METRICS)}")
every = data.load(a.folder)
if not every:
sys.exit(f"No \"advanced\" export in {a.folder} (see inventory.py).")
every, names = sensors(a.folder, a.sensors)
if not any(m[c] is not None for ms in every.values() for m in ms for c in METRICS):
sys.exit(f"No quality metrics in {a.folder}: peak to snr empty (old camera; "
"tools/DATA.md).")
names = a.sensors.split(",") if a.sensors else sorted(every, key=lambda s: (not data.is_reference(every[s]), s))
missing = [s for s in names if s not in every]
if missing:
sys.exit(f"Missing sensors: {', '.join(missing)}. Present: {', '.join(sorted(every))}.")