"""Draw the page's two charts as inline SVG from our own results and write them into body.html between the markers … and … . python3 -I 10_charts.py Colours are tokens only, set in the page's page.css: blue for the days the note tests, gray for the rest and for the chance band. """ import json import re import sys def f2(v): return ("%.2f" % v).replace("-", "−") def lag_svg(prof): x0, x1, yb, yt = 70.0, 970.0, 330.0, 30.0 lo_v, hi_v = 0.4, 1.6 def X(k): return x0 + (k + 10) * (x1 - x0) / 25.0 def Y(v): return yb - (v - lo_v) / (hi_v - lo_v) * (yb - yt) band_lo = min(p["null_lo"] for p in prof) band_hi = max(p["null_hi"] for p in prof) out = [] a = [] for v in (0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6): out.append('' % (x0, Y(v), x1, Y(v))) out.append('%s×' % (x0 - 8, Y(v) + 4.5, ("%.1f" % v))) out.append('' % (X(0.5), yt, X(3.5) - X(0.5), yb - yt)) out.append('' % (x0, Y(band_hi), x1 - x0, Y(band_lo) - Y(band_hi))) out.append('' % (x0, Y(1.0), x1, Y(1.0))) pts = " ".join("%.1f,%.1f" % (X(p["lag"]), Y(p["OE"])) for p in prof) out.append('' % pts) for p in prof: k = p["lag"] cls = "lag-dot-w" if k in (1, 2, 3) else "lag-dot" t = "Day %s: %d logged, %.1f expected, %s×" % (str(k).replace("-", "−"), p["O"], p["E"], f2(p["OE"])) out.append('%s' % (t, cls, X(k), Y(p["OE"]))) lab = {-4: (0, -14, "middle"), 1: (-4, -14, "end"), 2: (0, 24, "middle"), 3: (4, -14, "start"), 5: (12, 5, "start"), 7: (12, 5, "start")} for p in prof: if p["lag"] in lab: dx, dy, anc = lab[p["lag"]] c = "t-val" if p["lag"] in (1, 2, 3) else "t-gray" out.append('%s' % (c, X(p["lag"]) + dx, Y(p["OE"]) + dy, anc, f2(p["OE"]))) out.append('Days 1–3' % ((X(0.5) + X(3.5)) / 2, yt + 16)) out.append('95%% of shifted storm calendars, for any single day' % (x1 - 6, Y(band_hi) + 20)) out.append('' % (x0, yb, x1, yb)) for k in range(-10, 16): out.append('' % (X(k), yb, X(k), yb + 5)) if k in (-10, -5, 0, 1, 2, 3, 5, 10, 15): out.append('%s' % (X(k), yb + 22, str(k).replace("-", "−"))) d = {p["lag"]: p for p in prof} aria = ("Dot chart of the ratio of logged to expected explosions on each day from 10 days before to 15 days after the " "peak day of 127 strong storms. Days 1, 2 and 3 are %s, %s and %s; the storm day is %s; the highest day is " "day −4, at %s, and the lowest day 5, at %s. A gray band from %s to %s shows where 95%% of shifted storm " "calendars put any single day." % (f2(d[1]["OE"]), f2(d[2]["OE"]), f2(d[3]["OE"]), f2(d[0]["OE"]), f2(d[-4]["OE"]), f2(d[5]["OE"]), f2(band_lo), f2(band_hi))) return ('%s' % (aria, "".join(out))) def dose_svg(cum): rows = [("4.667", "Kp ≥ 5− · 790 storms"), ("5.0", "Kp ≥ 5, G1 · 609 storms"), ("6.0", "Kp ≥ 6, G2 · 240 storms"), ("6.667", "Kp ≥ 7−, the note’s G3+ · 127"), ("7.0", "Kp ≥ 7, strict G3 · 96"), ("7.667", "Kp ≥ 8−, G4+ · 50 storms")] x0, x1 = 330.0, 830.0 lo_v, hi_v = 0.7, 1.9 top, rh = 34.0, 46.0 def X(v): return x0 + (v - lo_v) / (hi_v - lo_v) * (x1 - x0) out = [] yb = top + rh * len(rows) - 8 for v in (0.8, 1.0, 1.2, 1.4, 1.6, 1.8): out.append('' % ("c-ref" if v == 1.0 else "c-grid", X(v), top - 18, X(v), yb)) out.append('%.1f×' % (X(v), yb + 20, v)) aria_parts = [] for i, (key, label) in enumerate(rows): r = cum[key] y = top + rh * i + 10 lo, hi = r["ci"] head = key == "6.667" out.append('%s' % ("t-row-h" if head else "t-row", x0 - 16, y + 5, label)) out.append('' % (" is-h" if head else "", X(lo), y, X(hi), y)) for v in (lo, hi): out.append('' % (" is-h" if head else "", X(v), y - 7, X(v), y + 7)) t = "%s: %d logged, %.1f expected on days 1–3, %s× (95%% interval %s–%s)" % (label, r["O"], r["E"], f2(r["OE"]), f2(lo), f2(hi)) out.append('%s' % (t, "dose-dot-h" if head else "dose-dot", X(r["OE"]), y)) out.append('%s %s–%s' % ("t-val" if head else "t-gray", x1 + 14, y + 5, f2(r["OE"]), f2(lo), f2(hi))) aria_parts.append("%s %s (%s to %s)" % (label.split(" · ")[0], f2(r["OE"]), f2(lo), f2(hi))) aria = ("Dot-and-whisker chart of the ratio of logged to expected explosions on days 1–3 after storms, by the storm " "threshold, with exact 95%% intervals: %s. Every interval overlaps the others, and every one includes 1." % "; ".join(aria_parts)) return ('%s' % (int(yb + 34), aria, "".join(out))) def main(): series = json.load(open(sys.argv[1], encoding="utf-8")) body_path = sys.argv[2] body = open(body_path, encoding="utf-8").read() for name, svg in (("lag", lag_svg(series["lag_profile_A"])), ("dose", dose_svg(series["dose_cumulative"]))): pat = re.compile(r"(\n).*?()" % (name, name), re.S) if not pat.search(body): sys.exit("marker chart:%s not found" % name) body = pat.sub(lambda m: m.group(1) + svg + "\n" + m.group(2), body) with open(body_path, "w", encoding="utf-8", newline="\n") as f: f.write(body) print("charts written") if __name__ == "__main__": main()