"""Every 3 h over the range: median of BJ_D corr_x - BJ_G corr_x, median error_value of BJ_C/BJ_D/BJ_G and of BL_D/PBL_D/PBJ_C; then per day the correlation, measurement by measurement, of the spread with the BJ error_value.""" import sys, statistics as st from collections import defaultdict sys.path.insert(0, "tools/scripts") import data m = data.load("data/JUNGFRAU") a, b = data.parse_time("2026-10-10T16:15Z"), data.parse_time("2026-10-21T10:30Z") by = defaultdict(dict) for s, ms in m.items(): for x in data.filter_window(ms, a, b): by[x["t"].replace(second=0, microsecond=0)][s] = x bins = defaultdict(lambda: defaultdict(list)) days = defaultdict(list) for t, d in sorted(by.items()): k = t.replace(hour=t.hour // 3 * 3, minute=0) ev = st.mean(d[s]["error_value"] for s in ("BJ_C", "BJ_D", "BJ_G")) if d["BJ_D"]["corr_x"] is not None and d["BJ_G"]["corr_x"] is not None: sp = d["BJ_D"]["corr_x"] - d["BJ_G"]["corr_x"] bins[k]["spread"].append(sp) days[t.date()].append((ev, sp)) bins[k]["evBJ"].append(ev) bins[k]["evO"].append(st.mean(d[s]["error_value"] for s in ("BL_D", "PBL_D", "PBJ_C"))) for k in sorted(bins): v = bins[k] print(data.iso(k), "spread %.2f" % st.median(v["spread"]) if v["spread"] else "spread -", "evBJ %.3f" % st.median(v["evBJ"]), "evOther %.3f" % st.median(v["evO"])) for d, p in sorted(days.items()): if len(p) > 10: print(d, "n", len(p), "r(evBJ, spread) %.2f" % st.correlation([x for x, _ in p], [y for _, y in p])) # step to step: does the spread jump with the BJ error_value from one measurement to the next? ts = sorted(by) dev, dsp = [], [] for t0, t1 in zip(ts, ts[1:]): if (t1 - t0).seconds != 900: continue p = [by[t][s] for t in (t0, t1) for s in ("BJ_D", "BJ_G")] if any(x["corr_x"] is None for x in p): continue e = [st.mean(by[t][s]["error_value"] for s in ("BJ_C", "BJ_D", "BJ_G")) for t in (t0, t1)] dev.append(e[1] - e[0]) dsp.append((p[2]["corr_x"] - p[3]["corr_x"]) - (p[0]["corr_x"] - p[1]["corr_x"])) print("steps", len(dev), "r(d evBJ, d spread) %.2f" % st.correlation(dev, dsp), "slope %.1f" % st.linear_regression(dev, dsp).slope)