Files
aimsight-tools/scripts/metrics.py
T

265 lines
12 KiB
Python

"""System metrics of the station (metrics/: temperatures, voltage, battery, link…), as a table and a figure,
against the target median of corr_x, corr_y and the camera pose.
Usage: .venv/bin/python tools/scripts/metrics.py data/PS_DES_COUDRES/
[--names sensor_A0_deg_c,BusVoltage] [--step 1h] [--stat median|min|max] [--against temperature] [--smoothing 24h]
[--metrics-only] [--start ...] [--end ...] [--out FOLDER]
Columns: `temperature` (median of the sensor_*_deg_c components at each hour, as the detectors read it,
data.load_temperature), then each metric of --names (default: the *_deg_c and BusVoltage present), then,
unless --metrics-only, tgt_corr_x, tgt_corr_y (at each measurement, median over the targets of corr minus the target's
own level, its median over the window; accepted measurements only) and the raw camera pose (cam_pan,
cam_tilt, cam_rotation, cam_scale, cam_sigma0). An empty cell of a metrics/ CSV is skipped, not read as zero.
A text metric (BatteryState, link, last_sync…) is given as it is (the last one of the step).
Never an identifier: SIM_CCID and ip* are neither read nor listed.
Prints: the available metrics; the table, one line per step of --step (start of the step, UTC; --stat of the values of the
step; "-": no value), steps where no metric has a value are left out; per column, the number of filled
steps, the minimum and the maximum with their time; the correlation r and the slope (least squares, over the steps where
both have a value) of each numeric column on --against (default temperature; with --smoothing D, on its
moving average centred over D, column <against>_smooth); the dated runs of steps without any metric.
One PNG, path printed: temperatures on one panel, each other numeric metric on its own, then tgt_corr_x,
tgt_corr_y and the pose, at native times (line broken over a gap). Gives values, not a verdict.
"""
import argparse
import csv
import glob
import os
import statistics
import sys
from datetime import date, timedelta
import matplotlib
matplotlib.use("Agg")
import matplotlib.dates as mdates # noqa: E402
import matplotlib.pyplot as plt # noqa: E402
from data import POSE, filter_window, is_reference, iso, load, load_camera, load_temperature, parse_time # noqa: E402
from inventory import METRIC_RE # noqa: E402
from plot import bound # noqa: E402
from summary import STATS, duration # noqa: E402
def is_identifier(name):
"""Metric that carries an identifier (SIM card, IP addresses): never read."""
return name == "SIM_CCID" or name.startswith("ip")
def parse_value(text):
v = text.strip().strip("[]").strip("'\"")
try:
return float(v)
except ValueError:
return v or None
def read_metrics(folder, start, end):
"""metrics/ -> {name: [{'t', 'v'}]} sorted, over the window; v float or text; empty cell skipped; name without a value: []."""
prefix = os.path.basename(os.path.normpath(folder)) + "_"
by_name = {}
for p in glob.glob(os.path.join(folder, "metrics", "*.csv")):
m = METRIC_RE.match(os.path.basename(p))
if not m:
continue
name = m.group(1)
name = name[len(prefix):] if name.startswith(prefix) else name.split("_", 1)[-1]
if is_identifier(name):
continue
day = date(*map(int, m.group(2, 3, 4)))
series = by_name.setdefault(name, {})
if (start and day < start.date() - timedelta(days=1)) or (end and day > end.date()):
continue
with open(p, newline="") as f:
for r in csv.DictReader(f):
if (v := parse_value(r.get("metric") or "")) is not None:
series[parse_time(r["time_utc"])] = v
return {n: filter_window([{"t": t, "v": s[t]} for t in sorted(s)], start, end) for n, s in by_name.items()}
def targets(by_sensor, col, start, end):
"""At each measurement, median over the targets of col minus the target's own level (median over the window)."""
deviations = {}
for s, ms in by_sensor.items():
if is_reference(ms):
continue
ms = [m for m in filter_window(ms, start, end) if m[col] is not None]
if ms:
level = statistics.median(m[col] for m in ms)
for m in ms:
deviations.setdefault(m["t"], []).append(m[col] - level)
return [{"t": t, "v": statistics.median(v)} for t, v in sorted(deviations.items())]
def smooth(series, width):
"""Moving average centred over width (numeric values)."""
pts = [p for p in series if isinstance(p["v"], float)]
# ponytail: O(n²), enough for hourly metrics over a few weeks
return [{"t": p["t"], "v": statistics.fmean(q["v"] for q in pts if abs(q["t"] - p["t"]) <= width / 2)} for p in pts]
def fmt(v):
return "-" if v is None else f"{v:.6g}" if isinstance(v, float) else str(v)
def runs(empty_steps, step):
"""[t] sorted -> [(start, end, n)] of consecutive steps."""
out = []
for t in empty_steps:
if out and t - out[-1][1] == step:
out[-1] = (out[-1][0], t, out[-1][2] + 1)
else:
out.append((t, t, 1))
return out
def cut(series):
"""Times and numeric values, NaN inserted over a gap of more than 1.5 times the usual step (the line breaks)."""
pts = [(p["t"], p["v"]) for p in series if isinstance(p["v"], float)]
if len(pts) < 3:
return [t for t, _ in pts], [v for _, v in pts]
step = statistics.median(b[0] - a[0] for a, b in zip(pts, pts[1:]))
ts, vs = [pts[0][0]], [pts[0][1]]
for (a, _), (b, v) in zip(pts, pts[1:]):
if b - a > 1.5 * step:
ts.append(a + (b - a) / 2)
vs.append(float("nan"))
ts.append(b)
vs.append(v)
return ts, vs
def main():
a = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
a.add_argument("folder")
a.add_argument("--names", help="metrics, comma-separated (default: the *_deg_c and BusVoltage present)")
a.add_argument("--step", default="1h")
a.add_argument("--stat", default="median", choices=STATS)
a.add_argument("--against", default="temperature", help="column on which r and the slope are computed")
a.add_argument("--smoothing", type=duration, help="centred moving average of --against, e.g. 24h")
a.add_argument("--metrics-only", action="store_true", help="without tgt_corr_x, tgt_corr_y or the pose")
a.add_argument("--start", type=parse_time)
a.add_argument("--end", type=parse_time)
a.add_argument("--out", default=os.path.join("out", "diagnostic"), help="folder of the PNG")
o = a.parse_args()
step = duration(o.step)
met = read_metrics(o.folder, o.start, o.end)
if not met:
print(f"No metrics/ in {o.folder}.")
else:
print("Available metrics (values over the window; identifiers SIM_CCID, ip* excluded): "
+ ", ".join(f"{n} ({len(s)})" if s else f"{n} (empty)" for n, s in sorted(met.items())))
if o.names:
names = o.names.split(",")
if forbidden := [n for n in names if is_identifier(n)]:
sys.exit(f"Identifiers, never read: {', '.join(forbidden)}.")
if missing := [n for n in names if n not in met]:
sys.exit(f"Unknown metrics: {', '.join(missing)}. Known: {', '.join(sorted(met))}.")
else:
names = sorted(n for n in met if n.endswith("_deg_c")) + [n for n in ("BusVoltage",) if n in met]
series = {"temperature": filter_window([{"t": t, "v": v} for t, v in load_temperature(o.folder)], o.start, o.end)}
series.update({n: met[n] for n in names})
if o.smoothing:
if o.against not in series:
sys.exit(f"--against {o.against}: not a column ({', '.join(series)}).")
series[o.against + "_smooth"] = smooth(series[o.against], o.smoothing)
metric_cols = list(series)
if not o.metrics_only:
by_sensor = load(o.folder)
for c in ("corr_x", "corr_y"):
series["tgt_" + c] = targets(by_sensor, c, o.start, o.end)
poses = filter_window(load_camera(o.folder), o.start, o.end)
for c in POSE:
series["cam_" + c] = [{"t": p["t"], "v": p[c]} for p in poses if p[c] is not None]
reference = o.against + "_smooth" if o.smoothing else o.against
if reference not in series:
sys.exit(f"--against {o.against}: not a column ({', '.join(series)}).")
times = [p["t"] for s in series.values() for p in s]
if not times:
sys.exit("No value in the requested window.")
t0 = (o.start or min(times)).replace(minute=0, second=0, microsecond=0)
t1 = o.end or max(times)
cells = {} # column -> start of the step -> values
for c, s in series.items():
for p in s:
cells.setdefault(c, {}).setdefault(t0 + ((p["t"] - t0) // step) * step, []).append(p["v"])
stat = STATS[o.stat]
table = {c: {k: (stat(v) if all(isinstance(x, float) for x in v) else v[-1]) for k, v in d.items()}
for c, d in cells.items()}
grid = [t0 + i * step for i in range(int((t1 - t0) / step) + 1)]
columns = list(series)
print(f"\n{o.stat} per step of {o.step} (start of the step, UTC); \"-\": no value; tgt_: deviation from the targets' own level")
print("\t".join(["time"] + columns))
for k in grid:
line = [table.get(c, {}).get(k) for c in columns]
if any(v is not None for v in line):
print("\t".join([iso(k)] + [fmt(v) for v in line]))
print(f"\nPer column ({len(grid)} steps):")
for c in columns:
d = table.get(c, {})
nums = [(v, k) for k, v in d.items() if isinstance(v, float)]
if not d:
print(f" {c}: empty")
elif nums:
(vmin, kmin), (vmax, kmax) = min(nums), max(nums)
print(f" {c}: {len(d)} steps; min {fmt(vmin)} ({iso(kmin)}); max {fmt(vmax)} ({iso(kmax)})")
else:
ks = sorted(d)
print(f" {c}: {len(d)} steps, text; {len(set(d.values()))} distinct values; "
f"first {d[ks[0]]} ({iso(ks[0])}); last {d[ks[-1]]} ({iso(ks[-1])})")
ref = table.get(reference, {})
print(f"\nCorrelation r and slope on {reference} (over the steps where both have a value):")
if not any(isinstance(v, float) for v in ref.values()):
print(f" {reference} empty: none")
for c in columns:
if c == reference or not ref or not any(isinstance(v, float) for v in table.get(c, {}).values()):
continue
pairs = [(ref[k], v) for k, v in table.get(c, {}).items()
if isinstance(v, float) and isinstance(ref.get(k), float)]
try:
r = statistics.correlation(*zip(*pairs))
slope = statistics.linear_regression(*zip(*pairs)).slope
print(f" {c}: r = {r:+.2f}; slope = {slope:.4g} per unit of {reference}; n = {len(pairs)}")
except (statistics.StatisticsError, TypeError, ValueError):
print(f" {c}: - (n = {len(pairs)})")
empty = [k for k in grid if all(k not in table.get(c, {}) for c in metric_cols)]
print(f"\nSteps without any metric ({', '.join(metric_cols)}): {len(empty)} of {len(grid)}")
for d, f, n in runs(empty, step):
print(f" {iso(d)} → {iso(f)} ({n} steps)")
panels = {} # title -> columns
for c in columns:
if any(isinstance(p["v"], float) for p in series[c]):
key = "°C" if c.startswith("temperature") or "_deg_c" in c else c
panels.setdefault(key, []).append(c)
if not panels:
print("\nNothing to plot.")
return
fig, axes = plt.subplots(len(panels), 1, sharex=True, figsize=(14, 2.4 * len(panels)), squeeze=False,
layout="constrained")
for ax, (title, cols) in zip(axes[:, 0], panels.items()):
for c in cols:
black = c in ("temperature", reference)
ax.plot(*cut(series[c]), ".-", ms=2, lw=1.4 if black else 0.7, label=c, zorder=3 if black else 2,
color="k" if c == "temperature" else None)
if len(cols) > 1:
ax.legend(loc="upper left", bbox_to_anchor=(1.0, 1.0), fontsize=8)
ax.set_ylabel(title if title == "°C" else title.replace("_", "_\n", 1), fontsize=8)
ax.grid(alpha=0.3)
axes[-1, 0].xaxis.set_major_formatter(mdates.DateFormatter("%d/%m\n%H:%MZ"))
station = os.path.basename(os.path.normpath(o.folder))
fig.suptitle(f"{station}: metrics, targets and pose (UTC times)")
os.makedirs(o.out, exist_ok=True)
out = os.path.join(o.out, f"{station}_metrics_{bound(o.start, 'start')}_{bound(o.end, 'end')}.png")
fig.savefig(out, dpi=100, bbox_inches="tight")
print(f"\n{out}")
if __name__ == "__main__":
main()