#!/usr/bin/env python3 """Write the page's charts and result tables as HTML snippets, from the results files. python3 -I charts.py Standard library only. Every number in the snippets comes from the two results files. """ import json import os import sys ROWS = [ # (bin key, label, sub-label) ((5.5, 6.4), "5.5–6.4", "from November 2024"), ((6.0, 6.4), "6.0–6.4", "to October 2024"), ((6.5, 6.9), "6.5–6.9", "all years"), ((7.0, None), "7.0 and above", "2022"), ((7.0, 8.4), "7.0–8.4", "January–May 2023"), ((7.0, 7.4), "7.0–7.4", "from May 2023"), ((7.5, 8.4), "7.5–8.4", "from May 2023"), ((8.5, None), "8.5 and above", "from January 2023"), ] def pct(x): return f"{100 * x:.0f}%" def calib(res): by = {(b["bin"][0], b["bin"][1]): b for b in res["analysis1"]["primary"]["by_bin"]} out = ['
    '] for key, lab, sub in ROWS: b = by[key] p, o, q = b["mean_p"], b["observed"] / b["n"], b["mean_q"] out.append( f'
  • M {lab}{sub} · {b["n"]} windows

    ' f'
    ' f'

    {pct(p)}

    ' f'

    {pct(o)}

    ' f'

    {pct(q) if q >= 0.005 else "0.2%" if q > 0.0015 else "<0.2%"}

    ' f'
  • ') out.append("
") return "\n".join(out) def calib_table(res): by = {(b["bin"][0], b["bin"][1]): b for b in res["analysis1"]["primary"]["by_bin"]} out = ['' '' ''] for key, lab, sub in ROWS: b = by[key] out.append(f'' f'' f'') p = res["analysis1"]["primary"] n_obs = sum(b["observed"] for b in p["by_bin"]) out.append(f'' f'' f'' f'') out.append("
Magnitude rowWindowsSSGEOS expectedHappenedBase rate expectedBrier, SSGEOSBrier, base rate
M {lab} ({sub}){b["n"]}{b["expected_ssgeos"]:.1f}{b["observed"]}{b["expected_base_rate"]:.1f}{b["brier_ssgeos"]:.3f}{b["brier_base_rate"]:.3f}
All rows{p["n_pairs"]}{sum(b["expected_ssgeos"] for b in p["by_bin"]):.1f}{n_obs}{sum(b["expected_base_rate"] for b in p["by_bin"]):.1f}{p["brier_ssgeos"]:.3f}{p["brier_base_rate"]:.3f}
") return "\n".join(out) def sens_table(res): names = [("primary", "Primary: 7 days from the table’s stamp"), ("short3", "3 days (SSGEOS’s “next 2-3 days”)"), ("named", "To the end of the window the page names"), ("exceed", "Each row read as “at least this magnitude”"), ("after_archive", "Only earthquakes after the copy was archived"), ("monthly", "Only the first table in each month")] out = ['' '' ''] for k, lab in names: r = res["analysis1"][k] lo, hi = r["bss_95"] cls = ' class="is-primary"' if k == "primary" else "" m = lambda x: f"{x:.2f}".replace("-", "−") # a true minus sign in the numbers only out.append(f'' f'') out.append("
RuleTablesBrier, SSGEOSBrier, base rateSkill score (95% interval)
{lab}{r["n_forecasts"]}{r["brier_ssgeos"]:.3f}{r["brier_base_rate"]:.3f}{m(r["bss"])} ({m(lo)} to {m(hi)})
") return "\n".join(out) def conv(c): rows = [("in_sample", "convergence_1d", "SSGEOS’s years, 2011 to February 2023", "182 earthquakes, SSGEOS’s list; within a day"), ("in_sample", "convergence_2d", "SSGEOS’s years, 2011 to February 2023", "within two days"), ("out_of_sample", "convergence_1d", "New years, March 2023 to October 2026", f'{c["out_of_sample"]["convergence_1d"]["earthquakes"]} earthquakes, USGS; within a day'), ("out_of_sample", "convergence_2d", "New years, March 2023 to October 2026", "within two days")] out = ['
    '] for part, name, lab, sub in rows: r = c[part][name] d, e = r["share_of_days"], r["share_of_earthquakes"] out.append(f'
  • {lab}{sub}

    ' f'

    {100 * d:.1f}% of days

    ' f'

    {100 * e:.1f}% of quake days

    ' f'

    p = {r["p_one_sided"]:.2f}

  • ') out.append("
") return "\n".join(out) def main(): res = json.load(open(sys.argv[1], encoding="utf-8")) c = json.load(open(sys.argv[2], encoding="utf-8")) os.makedirs(sys.argv[3], exist_ok=True) parts = {"calibration-chart.html": calib(res), "calibration-table.html": calib_table(res), "sensitivity-table.html": sens_table(res), "convergence-chart.html": conv(c)} for name, html in parts.items(): with open(os.path.join(sys.argv[3], name), "w", encoding="utf-8", newline="\n") as fh: fh.write(html + "\n") print("wrote", ", ".join(parts)) if __name__ == "__main__": main()