"""POST HOC, descriptive (RULES_posthoc_2015_excess.txt): the 2015 winter excess in U.S. weekly deaths and its fading in mid-March, beside the other years, with the baseline of RULES_weekly_deaths.txt. Run: py -3.12 -I posthoc_2015_excess.py Reads only our own derived file (observed and expected weekly counts written by weekly_deaths.py).""" import json import os import sys src, outdir = sys.argv[1], sys.argv[2] r = json.load(open(src, encoding="utf-8")) out = {"rules": "RULES_posthoc_2015_excess.txt", "note": "post hoc; descriptive only", "outcomes": {}} def X(obs, exp, a, b): o = sum(obs[a - 1:b]) e = sum(exp[a - 1:b]) return round(100 * (o / e - 1), 2) for okey in ("O1_heart", "O2_all_cause"): wk = r["outcomes"][okey]["weekly"] res = {"by_year": {}, "weekly_2015_weeks_1_20_pct": []} for y in sorted(wk): obs, exp = wk[y]["observed"], wk[y]["expected"] a, b, c = X(obs, exp, 1, 10), X(obs, exp, 11, 12), X(obs, exp, 13, 14) res["by_year"][y] = {"weeks_1_10": a, "weeks_11_12": b, "weeks_13_14": c, "step_11_12_minus_1_10": round(b - a, 2)} obs, exp = wk["2015"]["observed"], wk["2015"]["expected"] res["weekly_2015_weeks_1_20_pct"] = [round(100 * (o / e - 1), 2) for o, e in zip(obs[:20], exp[:20])] out["outcomes"][okey] = res os.makedirs(outdir, exist_ok=True) with open(os.path.join(outdir, "posthoc_2015_excess.json"), "w", newline="\n", encoding="utf-8") as f: json.dump(out, f, indent=1) f.write("\n") print(json.dumps(out, indent=1))