#!/usr/bin/env python3 """Exploratory checks, NOT pre-registered (labelled as such in analysis_log.md and the report). 1. Quiet-day count: placebos have fewer quiet days (mean 6.6 vs 8.0); fewer days make the band narrower. KM for storms and placebos stratified by number of quiet days (5-7, >= 8). 2. 2-hourly run length 7 instead of 6 for the 3 placebos matched to IPK001-IPK003. 3. Bootstrap 95% intervals of the storm-minus-placebo difference in median dTEC % (NS50) at chosen hours. 4. Placebos: still 'disturbed' at 96 h with the median |dTEC| over 72-96 h, i.e. what the long tail looks like. Usage: python3 -I exploratory_checks.py """ import csv import datetime as dt import json import os import random import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import stats_lib as st # noqa: E402 import tec_pipeline as tp # noqa: E402 P = lambda s: dt.datetime.strptime(s, "%Y-%m-%dT%H:%MZ") # noqa: E731 def main(): base = sys.argv[1] res = os.path.join(base, "results") dl = os.path.join(base, "downloads") out = {} srows = [r for r in csv.DictReader(open(os.path.join(res, "storms_recomputed.csv"), encoding="utf-8")) if r["band"] == "NS50"] prows = [r for r in csv.DictReader(open(os.path.join(res, "placebo_trec.csv"), encoding="utf-8")) if r["band"] == "NS50"] qsel = {} for r in csv.DictReader(open(os.path.join(dl, "s0-note-002-drive", "quiet_days_selected.csv"), encoding="utf-8")): qsel.setdefault(r["storm_id"], []).append(dt.date.fromisoformat(r["quiet_date"])) nq_s = {k: len(v) for k, v in qsel.items()} strata = {} for lab, lo, hi in (("q5to7", 5, 7), ("q8plus", 8, 10)): ts = [float(r["T_rec_h_ours"]) for r in srows if lo <= nq_s[r["storm_id"]] <= hi] es = [r["recovered_ours"] == "True" for r in srows if lo <= nq_s[r["storm_id"]] <= hi] tq = [float(r["T_rec_h"]) for r in prows if lo <= int(r["n_quiet"]) <= hi] eq = [r["recovered"] == "True" for r in prows if lo <= int(r["n_quiet"]) <= hi] cs, cp = st.km(ts, es), st.km(tq, eq) chi2, p, _, _ = st.logrank(ts, es, tq, eq) strata[lab] = {"storms": {"n": len(ts), "median": st.km_median(cs), "S24": round(st.surv_at(cs, 24), 3), "S48": round(st.surv_at(cs, 48), 3), "S96": round(st.surv_at(cs, 96), 3)}, "placebo": {"n": len(tq), "median": st.km_median(cp), "S24": round(st.surv_at(cp, 24), 3), "S48": round(st.surv_at(cp, 48), 3), "S96": round(st.surv_at(cp, 96), 3)}, "logrank_p": round(p, 3)} out["by_quiet_day_count"] = strata # 2. run length 7 for the 2-hourly placebos opts = tp.Opts(dipole="map", lon73=False, pct="linear", midnight="prev24", run2h=6) store = tp.BandStore(os.path.join(dl, "code-gim"), os.path.join(base, "work"), os.path.join(dl, "igrf14", "igrf14coeffs.txt"), opts) plac = list(csv.DictReader(open(os.path.join(res, "placebos.csv"), encoding="utf-8"))) two = [] for p in plac: if p["placebo_t0_utc"] and P(p["placebo_t0_utc"]) < tp.TWO_HOURLY_END: qd = [dt.date.fromisoformat(x) for x in p["quiet_days"].split(";") if x] env = tp.quiet_envelope(store, qd, "NS50", "linear") a = tp.recovery(store, env, P(p["placebo_t0_utc"]), 168, "NS50", run2h=6) b = tp.recovery(store, env, P(p["placebo_t0_utc"]), 168, "NS50", run2h=7) two.append((p["storm_id"], p["placebo_t0_utc"], a["T_rec_h"], a["recovered"], b["T_rec_h"], b["recovered"])) out["two_hourly_placebos_run6_vs_run7"] = two # 3. bootstrap of median dTEC difference (NS50) inv = list(csv.DictReader(open(os.path.join(dl, "s0-note-002-drive", "storm_inventory.csv"), encoding="utf-8"))) hours = (5, 12, 24, 36, 48, 72, 96) def series(t0, qd, F): env = tp.quiet_envelope(store, qd, "NS50", "linear") d = {} for h in hours: if h >= F: continue tt = t0 + dt.timedelta(hours=h) v = store.value(tt, "NS50") if v is not None: d[h] = 100.0 * (v - env[tt.hour][2]) / env[tt.hour][2] return d s_ser = [series(P(r["t0_utc"]), qsel[r["storm_id"]], float(r["window_h"])) for r in inv] p_ser = [series(P(p["placebo_t0_utc"]), [dt.date.fromisoformat(x) for x in p["quiet_days"].split(";") if x], 168.0) for p in plac if p["placebo_t0_utc"] and p["quiet_days"]] rng = random.Random(20261106) boot = {} for h in hours: a = [x[h] for x in s_ser if h in x] b = [x[h] for x in p_ser if h in x] diffs = [] for _ in range(2000): aa = [rng.choice(a) for _ in a] bb = [rng.choice(b) for _ in b] diffs.append(st.median(aa) - st.median(bb)) diffs.sort() boot[h] = {"storm_median": round(st.median(a), 2), "placebo_median": round(st.median(b), 2), "diff": round(st.median(a) - st.median(b), 2), "ci95": (round(diffs[50], 2), round(diffs[1949], 2)), "n": (len(a), len(b))} out["median_dTEC_diff_NS50_bootstrap"] = boot # 4. what the placebo tail looks like tail = [r for r in prows if float(r["T_rec_h"]) > 96] out["placebo_still_out_after_96h"] = {"n": len(tail), "never_within_168h": sum(1 for r in tail if r["recovered"] != "True"), "n_quiet": sorted(int(r["n_quiet"]) for r in tail), "placebo_t0": sorted(r["placebo_t0_utc"] for r in tail)} with open(os.path.join(res, "exploratory_checks.json"), "w", encoding="utf-8", newline="\n") as f: json.dump(out, f, indent=1, default=str) print(json.dumps(out, indent=1, default=str)) if __name__ == "__main__": main()