"""Plane heat vs orb distance, thermal video (reproducible test). Input: detections from tools/detect_drone.py. Distinct real-time stretches 20-39.5 s and 44-53.5 s. For every picture with the plane (largest elongated warm blob, area > 1500 px) and at least one orb (round blob, 0.2%-25% of the plane's area, within 4 plane-widths): y = plane's mean palette heat; x = mean orb distance from the plane centre / plane width (bounding box). Slow drift removed within each stretch (quadratic in time) from y, x and the controls (log plane area, plane's vertical screen position). Ordinary least squares y ~ x + controls. Null: the orb-distance series is rolled in time (2000 rolls), keeping its own slow structure. Writes analysis/bubble/plane-cooling.json with per-picture values and the result. Usage: python -I plane_cooling.py """ import sys, json, math, numpy as np R = [json.loads(l) for l in open(sys.argv[1])]; rows = [] for r in R: t = r["t"] if not (20 <= t <= 39.5 or 44 <= t <= 53.5): continue pl = [b for b in r["blobs"] if b["circ"] < 0.5 and b["area"] > 1500] if not pl: continue p = max(pl, key=lambda b: b["area"]); W = max(p["bbox"][2], 20) orbs = [b for b in r["blobs"] if b is not p and b["circ"] >= 0.55 and 0.002 * p["area"] < b["area"] < 0.25 * p["area"]] ds = [d for d in (math.hypot(b["cx"] - p["cx"], b["cy"] - p["cy"]) / W for b in orbs) if d < 4] if ds: rows.append({"frame": r["i"], "t": t, "orb_dist_W": float(np.mean(ds)), "plane_heat": p["heat_mean"], "plane_area": p["area"], "plane_y": p["cy"]}) t = np.array([r["t"] for r in rows]) def detr(v): out = v.copy() for a, b in [(20, 39.5), (44, 53.5)]: m = (t >= a) & (t <= b); c = np.polyfit(t[m] - t[m].mean(), v[m], 2); out[m] = v[m] - np.polyval(c, t[m] - t[m].mean()) return out y = detr(np.array([r["plane_heat"] for r in rows])) X = np.column_stack([detr(np.array([r["orb_dist_W"] for r in rows])), detr(np.log([r["plane_area"] for r in rows])), detr(np.array([r["plane_y"] for r in rows], float)), np.ones(len(rows))]) beta = np.linalg.lstsq(X, y, rcond=None)[0] rng = np.random.default_rng(8); null = [] for _ in range(2000): Xs = X.copy(); Xs[:, 0] = np.roll(X[:, 0], rng.integers(20, len(rows) - 20)); null.append(np.linalg.lstsq(Xs, y, rcond=None)[0][0]) null = np.array(null); p = (np.sum(np.abs(null) >= abs(beta[0])) + 1) / (len(null) + 1) for r, xv, yv in zip(rows, X[:, 0], y): r["orb_dist_detrended"] = float(xv); r["plane_heat_detrended"] = float(yv) res = {"pictures": len(rows), "slope_heat_per_plane_width": float(beta[0]), "null_band95": float(np.percentile(np.abs(null), 95)), "p": float(p)} json.dump({"method": __doc__, "result": res, "rows": rows}, open(sys.argv[2], "w"), indent=1); print(res)