"""Checks written after the plan was registered (deviations.json says which the plan foresaw).

The registered analysis (code/analyze.py) is unchanged and still writes results/R1.json to
R6.json and S.json. This script reads the same comparisons through it and writes results/X.json:

- which pre-specified kinds of trial and vaccine have intervals inside the primary margins (the
  plan said the paper would say);
- the registry's counts for the 15 largest comparisons beside the counts their publications or
  regulators' reviews give (data/published.json), and the first two primary estimates with the
  usable published counts in the registry's place (planned; the plan said its code would follow);
- exploratory, not planned: the estimates without the trials whose vaccine and placebo groups two
  coders, reading the records without counts (data/follow_up.json), did not code as followed
  equally: without those both coded so, and without those either did;
- exploratory, not planned: infections other than the vaccine's target for live vaccines, by
  target disease; the cardiovascular outcome with a few terms' tags changed
  (data/term_sensitivity.json); and two named outcomes with only the terms that name the
  diagnosis itself (data/narrow_terms.json).

Usage: python3 code/checks.py <bundle root>
"""

import json
import os

import analyze as A

ROOT = A.ROOT
P, O = A.P, A.O


def within(m, margin):
    return m["rr"] is not None and m["lo"] >= margin[0] and m["hi"] <= margin[1]


def strata_margins():
    """Each pre-specified stratum's interval against the margins of P1 (any serious event) and P2
    (death), and the strata whose risk difference's interval excludes zero."""
    margins = {"any_serious": O["primary"][0]["margin"], "death": O["primary"][1]["margin"]}
    out = {}
    for key, margin in margins.items():
        rows = [(r["id"], r[key]) for r in A.load("results/R3.json")]
        out[key] = {
            "margin": margin,
            "estimable": sum(1 for _, m in rows if m["rr"] is not None),
            "inside": sorted(i for i, m in rows if within(m, margin)),
            "above_one": sorted(i for i, m in rows if m["rr"] is not None and m["lo"] > 1),
            "below_one": sorted(i for i, m in rows if m["rr"] is not None and m["hi"] < 1),
            "difference_above_zero": sorted(i for i, m in rows if m.get("rd_lo") is not None and m["rd_lo"] > 0),
            "difference_below_zero": sorted(i for i, m in rows if m.get("rd_hi") is not None and m["rd_hi"] < 0),
        }
        out[key]["inside_count"] = len(out[key]["inside"])
    return out


PUBLISHED = {"any_serious": "serious", "death": "deaths"}


def usable(cell):
    """Whether a published count can stand in for the registry's: data/published.json says so, and
    gives both arms' counts and denominators."""
    return bool(cell["usable"]) and all(cell[k] is not None for k in ("vaccine", "vaccine_n", "placebo", "placebo_n"))


def published(main):
    """Registry counts beside published ones for the largest comparisons, and P1 and P2 with the
    usable published counts in the registry's place: swapped where the registry has the outcome,
    and, apart, also added where it has none."""
    found = {row["id"]: row for row in A.load("data/published.json")}
    out = {"trials": [], "looked_for": len(found)}
    for kind, field in PUBLISHED.items():
        rows = A.tables(main, {"kind": kind})
        swapped = {r[0]["id"]: r for r in rows}
        added = dict(swapped)
        counts = {"swapped": 0, "added": 0, "computed": 0}
        for cid, pub in found.items():
            cell = pub[field]
            if not usable(cell):
                continue
            comp = next(c for c, _ in main if c["id"] == cid)
            row = (comp, cell["vaccine"], cell["vaccine_n"], cell["placebo"], cell["placebo_n"])
            counts["computed"] += cell["basis"] == "computed"
            added[cid] = row
            if cid in swapped:
                swapped[cid] = row
                counts["swapped"] += 1
            else:
                counts["added"] += 1
        out[kind] = {"registry": A.shown(A.mantel_haenszel(rows)), "swapped": A.shown(A.mantel_haenszel(list(swapped.values()))),
                     "swapped_and_added": A.shown(A.mantel_haenszel(list(added.values()))),
                     "usable": counts["swapped"] + counts["added"], "swapped_count": counts["swapped"], "added_count": counts["added"],
                     "computed_count": counts["computed"], "not_usable": sorted(cid for cid, pub in found.items() if not usable(pub[field]))}
    for cid, pub in found.items():
        comp, arm = next((c, a) for c, a in main if c["id"] == cid)
        reg = {}
        for kind in PUBLISHED:
            v, p = A.count(arm, comp, {"kind": kind}), A.count(comp["placebo"], comp, {"kind": kind})
            reg[kind] = None if v is None or p is None else {"vaccine": v[0], "vaccine_n": v[1], "placebo": p[0], "placebo_n": p[1]}
        out["trials"].append({"id": cid, "nct": pub["nct"], "found": bool(pub.get("found")), "source": pub.get("source"), "url": pub.get("url"),
                              "period": pub.get("period"), "note": pub.get("note"), "registry": reg,
                              "published": {field: pub[field] for field in PUBLISHED.values()}})
    out["found"] = sum(1 for t in out["trials"] if t["found"])
    return out


def follow_up(comparisons):
    """The estimates without the flagged trials whose vaccine and placebo groups the coders did not
    code as followed equally: without those both coders coded so (`agreed`), and without those
    either did (`either`); and the trials both coded as following the vaccine groups longer."""
    codes = {row["nct"]: (row["coder_1"], row["coder_2"]) for row in A.load("data/follow_up.json")}
    agreed = {nct for nct, (a, b) in codes.items() if a != "equal" and b != "equal"}
    either = {nct for nct, (a, b) in codes.items() if a != "equal" or b != "equal"}
    longer = {nct for nct, (a, b) in codes.items() if a == b == "vaccine_longer"}
    main = A.select(comparisons, {"set": "main"})
    people = lambda trials: sum(c["size"] for c, _ in main if c["nct"] in trials)
    out = {"flagged_trials": len(codes), "both_equal": len(codes) - len(either), "differed": sum(a != b for a, b in codes.values()),
           "agreed_trials": len(agreed), "either_trials": len(either), "longer_trials": len(longer),
           "placebo_longer_codings": sum("placebo_longer" in pair for pair in codes.values()),
           "agreed_participants": people(agreed), "either_participants": people(either), "longer_participants": people(longer),
           "primary": {}, "strata": {}}

    def versions(rows):
        return {"without_agreed": A.shown(A.mantel_haenszel([r for r in rows if r[0]["nct"] not in agreed])),
                "without_either": A.shown(A.mantel_haenszel([r for r in rows if r[0]["nct"] not in either]))}

    for item in O["primary"]:
        rows = A.tables(A.select(comparisons, item["view"]), item["outcome"])
        out["primary"][item["id"]] = dict(versions(rows), name=item["name"], all=A.shown(A.mantel_haenszel(rows)),
                                          longer_alone=A.shown(A.mantel_haenszel([r for r in rows if r[0]["nct"] in longer])))
    for item in O["strata"]:
        sel = A.select(comparisons, item["view"])
        out["strata"][item["id"]] = {kind: versions(A.tables(sel, {"kind": kind})) for kind in ("any_serious", "death")}
    # Each of the trials both coders coded as following the vaccine groups longer, and the one with the most deaths.
    trials = []
    for nct in sorted(longer):
        cell = {"nct": nct}
        for kind in ("any_serious", "death"):
            rows = [r for r in A.tables(main, {"kind": kind}) if r[0]["nct"] == nct]
            cell[kind] = {"vaccine": sum(r[1] for r in rows), "vaccine_n": sum(r[2] for r in rows),
                          "placebo": sum(r[3] for r in rows), "placebo_n": sum(r[4] for r in rows)} if rows else None
        trials.append(cell)
    out["longer"] = trials
    deaths = lambda t: (t["death"]["vaccine"] + t["death"]["placebo"]) if t["death"] else 0
    serious = lambda t: (t["any_serious"]["vaccine"] + t["any_serious"]["placebo"]) if t["any_serious"] else 0
    lead = max(trials, key=lambda t: (deaths(t), t["nct"]))
    pub = next((row for row in A.load("data/published.json") if row["nct"] == lead["nct"]), None)
    out["lead"] = {"nct": lead["nct"], "any_serious": lead["any_serious"], "death": lead["death"], "deaths": deaths(lead), "serious": serious(lead),
                   "deaths_in_all": sum(deaths(t) for t in trials), "serious_in_all": sum(serious(t) for t in trials),
                   "published": None if pub is None else {field: pub[field] for field in PUBLISHED.values()}}
    return out


def narrow_outcomes(comparisons):
    """Two named outcomes again with only the terms that name the diagnosis itself
    (data/narrow_terms.json). Exploratory, not planned; the registered outcomes are unchanged."""
    spec = A.load("data/narrow_terms.json")
    out = {}
    for item in O["named"]:
        if item["id"] not in spec["narrow"]:
            continue
        terms = set(spec["narrow"][item["id"]]["terms"])
        sel = A.select(comparisons, item["view"])
        rows, seen = [], set()
        for comp, arm in sel:
            pair = []
            for side in (arm, comp["placebo"]):
                reports = A.reports(side, comp, item["outcome"])
                seen.update(term for term, _ in reports)
                pair.append((min(sum(n for (term, _), n in reports.items() if term in terms), side.at_risk), side.at_risk))
            rows.append((comp, pair[0][0], pair[0][1], pair[1][0], pair[1][1]))
        assert terms <= seen, sorted(terms - seen)
        out[item["id"]] = dict(A.shown(A.mantel_haenszel(rows)), events=A.events(rows), label=spec["narrow"][item["id"]]["label"],
                               registered_terms=sorted(seen), narrow_terms=sorted(terms))
    return out


def live_by_target(comparisons):
    """Infections other than the vaccine's target, live vaccines, by target disease."""
    spec = {"kind": "non_target_infection"}
    sel = A.select(comparisons, {"set": "main", "live": True})
    targets = sorted({t for c, _ in sel for t in c["targets"]})
    out = {}
    for t in targets:
        rows = A.tables([(c, a) for c, a in sel if t in c["targets"]], spec)
        if A.events(rows) >= P["min_events"]:
            out[t] = dict(A.shown(A.mantel_haenszel(rows)), events=A.events(rows))
    rows = A.tables([(c, a) for c, a in sel if "rotavirus" not in c["targets"]], spec)
    out["all_but_rotavirus"] = dict(A.shown(A.mantel_haenszel(rows)), events=A.events(rows))
    return out


def tables_csv():
    """The registered estimates as tables a reader can open: results/strata.csv, named.csv, organ_systems.csv."""
    s = A.load("results/S.json")
    cols = ["vaccine_affected", "vaccine_at_risk", "placebo_affected", "placebo_at_risk", "rr", "lo", "hi", "p", "rd", "rd_lo", "rd_hi", "trials"]
    table("strata.csv", ["id", "name"] + ["serious_" + c for c in cols] + ["death_" + c for c in cols],
          [[sid, row["name"]] + [row["any_serious"][c] for c in cols] + [row["death"][c] for c in cols] for sid, row in s["strata"].items()])
    labels = A.load("data/narrow_terms.json")["labels"]  # names that say what an outcome's registered terms cover
    rows = []
    for group in ("primary", "named"):
        for oid, e in s[group].items():
            re = e.get("random_effects", {})
            rows.append([oid, labels.get(oid, e["name"]), e.get("role", "primary"), e["counts"]] + [e["all"][c] for c in cols]
                        + [e.get("q") or e.get("holm_p"), re.get("rr"), re.get("lo"), re.get("hi"), e.get("detectable_lower"), e.get("detectable_higher")])
    table("named.csv", ["id", "name", "role", "counts"] + cols
          + ["q", "random_effects_rr", "random_effects_lo", "random_effects_hi", "detectable_lower", "detectable_higher"], rows)
    table("organ_systems.csv", ["organ_system", "reports"] + cols + ["q_all", "half1_rr", "half1_q", "half2_rr", "half2_p", "replicates"],
          [[name, e["events"]] + [e["all"][c] for c in cols] + [e["q_all"], e["half1"]["rr"], e["q_half1"], e["half2"]["rr"], e["half2"]["p"], e["replicates"]]
           for name, e in s["organ_systems"]["each"].items()])


def records():
    """The records the bundle carries against the digests registered for them (data/ctgov_digests.json, a copy of
    plan/ctgov_digests.json, since a re-run sees only code/, env/ and data/)."""
    import hashlib
    digests = A.load("data/ctgov_digests.json")
    eligible = sorted(nct for nct, row in A.SCREENING.items() if row["eligible"])
    match = 0
    for nct in eligible:
        with open(os.path.join(ROOT, "data", "ctgov", f"{nct}.json"), "rb") as f:
            match += hashlib.sha256(f.read()).hexdigest() == digests[nct]
    return {"registered": len(digests), "carried": len(eligible), "carried_matching_registered_digest": match}


def term_sensitivity(comparisons):
    """The cardiovascular outcome (P3) with the term coding changed as data/term_sensitivity.json lists:
    terms added, terms dropped, and both. Exploratory, not planned."""
    spec = A.load("data/term_sensitivity.json")
    item = next(p for p in O["primary"] if p["id"] == "P3")
    sel = A.select(comparisons, item["view"])
    registered = dict(A.TAGS)

    def estimate(add, drop):
        try:
            for term, tags in (spec["add"].items() if add else ()):
                assert term in registered, term
                A.TAGS[term] = registered[term] | set(tags)
            for term in (spec["drop"] if drop else ()):
                assert term in registered, term
                A.TAGS[term] = registered[term] - set(item["outcome"]["tags"])
            return A.shown(A.mantel_haenszel(A.tables(sel, item["outcome"])))
        finally:
            A.TAGS.clear()
            A.TAGS.update(registered)

    return {"registered": estimate(False, False), "added": estimate(True, False), "dropped": estimate(False, True), "both": estimate(True, True),
            "terms_added": len(spec["add"]), "terms_dropped": len(spec["drop"])}


def n(x):
    """A count with thousands grouped, as the paper prints it."""
    return None if x is None else f"{x:,}"


def est(m, short=False):
    """An estimate's values under the short names the paper cites."""
    out = {"rr": m["rr"], "lo": m["lo"], "hi": m["hi"], "p": m["p"], "rd": m["rd"], "rdlo": m["rd_lo"], "rdhi": m["rd_hi"]}
    if not short:
        out.update({"va": n(m["vaccine_affected"]), "vn": n(m["vaccine_at_risk"]), "pa": n(m["placebo_affected"]), "pn": n(m["placebo_at_risk"]),
                    "vp": m["vaccine_percent"], "pp": m["placebo_percent"], "people": n(m["participants"]), "trials": n(m["trials"])})
    return out


def paper_values(x):
    """results/T.json: the values the paper and the claims cite, formatted, under short names."""
    s = A.load("results/S.json")
    agree, terms = A.load("data/agreement.json"), A.load("data/terms_agreement.json")
    tr = s["trials"]
    t = {"per": n(P["per"]), "alpha": f'{P["alpha"]:.2f}', "older": str(P["older_min_years"]), "n": {
        "found": n(tr["found"]), "screened": n(tr["screened"]), "eligible": n(tr["eligible_trials"]),
        "comps": n(tr["sets"]["main"]["comparisons"]), "trials": n(tr["sets"]["main"]["trials"]), "people": n(tr["sets"]["main"]["participants"]),
        "vacc": n(tr["sets"]["main"]["vaccine"]), "plac": n(tr["sets"]["main"]["placebo"]),
        "inertComps": n(tr["sets"]["inert"]["comparisons"]), "inertPeople": n(tr["sets"]["inert"]["participants"]),
        "unstatedComps": n(tr["sets"]["not_stated"]["comparisons"]), "unstatedPeople": n(tr["sets"]["not_stated"]["participants"]),
        "adjComps": n(tr["sets"]["adjuvant_placebo"]["comparisons"]), "adjPeople": n(tr["sets"]["adjuvant_placebo"]["participants"]),
        "offComps": n(tr["sets"]["offspring"]["comparisons"]), "offPeople": n(tr["sets"]["offspring"]["participants"]),
        "deathComps": n(tr["death_tables"]["comparisons"]), "deathPeople": n(tr["death_tables"]["participants"]),
        "half1": n(tr["half1_trials"]), "half2": n(tr["half2_trials"]),
        "kappa": f'{agree["kappa_eligibility"]:.2f}', "third": n(agree["to_third_reader"]),
        "terms": n(terms["terms"]), "tags": n(terms["tags"]), "termKappa": f'{terms["kappa_term_tag_pairs"]:.2f}', "termThird": n(terms["to_third_reader"]),
        "carried": n(x["records"]["carried"]), "matching": n(x["records"]["carried_matching_registered_digest"]),
    }}
    for pid, e in s["primary"].items():
        cell = est(e["all"])
        re, loo = e["random_effects"], e["leave_one_out"]
        cell.update({"holm": e["holm_p"], "rerr": re["rr"], "relo": re["lo"], "rehi": re["hi"], "rep": re["p"], "i2": re["i2_percent"],
                     "h1": e["half1"]["rr"], "h2": e["half2"]["rr"], "loomin": loo["rr_min"], "loomax": loo["rr_max"],
                     "loolo": loo["lo_min"], "loohi": loo["hi_max"], "dlo": e["detectable_lower"], "dhi": e["detectable_higher"],
                     "mlo": f'{e["margin"][0]:.2f}', "mhi": f'{e["margin"][1]:.2f}', "reading": e["reading"].replace("_", " "), "robust": e["robust"]})
        if "dominant_term" in e:
            cell.update({"term": e["dominant_term"]["term"], "trr": e["dominant_term"]["rr"], "tlo": e["dominant_term"]["lo"], "thi": e["dominant_term"]["hi"]})
        t[pid] = cell
    t["s"] = {}
    for sid, row in s["strata"].items():
        cell = est(row["any_serious"])
        d = row["death"]
        cell.update({"drr": d["rr"], "dlo": d["lo"], "dhi": d["hi"], "dva": n(d["vaccine_affected"]), "dpa": n(d["placebo_affected"])})
        t["s"][sid.replace("_", "")] = cell
    t["H"] = {}
    for hid, e in s["named"].items():
        cell = {"va": n(e["all"]["vaccine_affected"]), "pa": n(e["all"]["placebo_affected"]), "rr": e["all"]["rr"], "lo": e["all"]["lo"], "hi": e["all"]["hi"],
                "p": e["all"]["p"], "q": e.get("q"), "dlo": e.get("detectable_lower"), "dhi": e.get("detectable_higher")}
        t["H"][hid] = cell
    ns = s["named_summary"]
    hyp = [e for e in s["named"].values() if e.get("role") == "hypothesis" and e["testable"]]
    t["H"]["count"], t["H"]["tested"], t["H"]["significant"] = n(ns["hypotheses"]), n(ns["tested"]), n(len(ns["significant"]))
    t["H"]["minq"] = min((e["q"] for e in hyp), key=lambda q: float(q.replace("< ", "")))
    org = s["organ_systems"]
    t["org"] = {"tested": n(org["tested"]), "replicated": n(len(org["replicated"])), "significant": n(len(org["significant_overall"])),
                "minq": min((e["q_all"] for e in org["each"].values()), key=lambda q: float(q.replace("< ", "")))}
    inf = org["each"]["infections and infestations"]
    t["org"]["inf"] = {"rr": inf["all"]["rr"], "lo": inf["all"]["lo"], "hi": inf["all"]["hi"], "q": inf["q_all"]}
    t["live"] = {}
    for key, row in s["live_against_non_live"].items():
        short = "".join(part.capitalize() if i else part for i, part in enumerate(key.split("_")))
        cell = {"lrr": row["live"]["rr"], "llo": row["live"]["lo"], "lhi": row["live"]["hi"], "lpeople": n(row["live"]["participants"]),
                "nrr": row["non_live"]["rr"], "nlo": row["non_live"]["lo"], "nhi": row["non_live"]["hi"], "npeople": n(row["non_live"]["participants"])}
        if "ratio" in row:
            cell.update({"ratio": row["ratio"]["rr"], "rlo": row["ratio"]["lo"], "rhi": row["ratio"]["hi"], "p": row["ratio"]["p"]})
        t["live"][short] = cell
    t["live"]["byTarget"] = {k.replace("_", ""): {"rr": v["rr"], "lo": v["lo"], "hi": v["hi"], "events": n(v["events"])} for k, v in x["live_non_target_by_target"].items()}
    m = x["strata_margins"]
    t["margins"] = {"seriousInside": n(m["any_serious"]["inside_count"]), "seriousOf": n(m["any_serious"]["estimable"]),
                    "deathInside": n(m["death"]["inside_count"]), "deathOf": n(m["death"]["estimable"]),
                    "seriousAbove": n(len(m["any_serious"]["above_one"])), "deathAbove": n(len(m["death"]["above_one"])),
                    "seriousDiffAbove": n(len(m["any_serious"]["difference_above_zero"])), "deathDiffAbove": n(len(m["death"]["difference_above_zero"]))}
    pub = x["published"]
    t["pub"] = {"looked": n(pub["looked_for"]), "found": n(pub["found"])}
    for kind, short in (("any_serious", "sae"), ("death", "death")):
        p = pub[kind]
        t["pub"][short] = {"usable": n(p["usable"]), "swapped": n(p["swapped_count"]), "added": n(p["added_count"]), "computed": n(p["computed_count"]),
                           "unusable": n(len(p["not_usable"])), "reg": est(p["registry"], True), "swap": est(p["swapped"], True),
                           "all": est(p["swapped_and_added"], True)}
    fu = x["follow_up"]
    t["fu"] = {"flagged": n(fu["flagged_trials"]), "bothEqual": n(fu["both_equal"]), "differed": n(fu["differed"]),
               "agreed": n(fu["agreed_trials"]), "either": n(fu["either_trials"]), "longer": n(fu["longer_trials"]),
               "agreedPeople": n(fu["agreed_participants"]), "eitherPeople": n(fu["either_participants"]), "longerPeople": n(fu["longer_participants"]),
               "placeboLonger": n(fu["placebo_longer_codings"])}
    for pid, row in fu["primary"].items():
        t["fu"][pid] = {"agreed": est(row["without_agreed"]), "either": est(row["without_either"]), "longer": est(row["longer_alone"])}

    def both(cell):
        a, d = cell["any_serious"], cell["death"]
        return {v: {"rr": a[k]["rr"], "lo": a[k]["lo"], "hi": a[k]["hi"], "rd": a[k]["rd"], "rdlo": a[k]["rd_lo"], "rdhi": a[k]["rd_hi"],
                    "drr": d[k]["rr"], "dlo": d[k]["lo"], "dhi": d[k]["hi"]} for v, k in (("agreed", "without_agreed"), ("either", "without_either"))}

    t["fu"]["s"] = {sid.replace("_", ""): both(cell) for sid, cell in fu["strata"].items()}
    lead = fu["lead"]
    t["fu"]["lead"] = {"nct": lead["nct"], "sva": n(lead["any_serious"]["vaccine"]), "spa": n(lead["any_serious"]["placebo"]),
                       "dva": n(lead["death"]["vaccine"]), "dpa": n(lead["death"]["placebo"]), "deaths": n(lead["deaths"]),
                       "deathsInAll": n(lead["deaths_in_all"]), "serious": n(lead["serious"]), "seriousInAll": n(lead["serious_in_all"])}
    if lead["published"]:
        ps, pd = lead["published"]["serious"], lead["published"]["deaths"]
        t["fu"]["lead"].update({"psva": n(ps["vaccine"]), "pspa": n(ps["placebo"]), "pdva": n(pd["vaccine"]), "pdpa": n(pd["placebo"])})
    ts = x["p3_term_sensitivity"]
    t["P3terms"] = {"nAdded": n(ts["terms_added"]), "nDropped": n(ts["terms_dropped"]),
                    **{key: {"rr": ts[key]["rr"], "lo": ts[key]["lo"], "hi": ts[key]["hi"]} for key in ("added", "dropped", "both")}}
    t["narrow"] = {hid: {"va": n(e["vaccine_affected"]), "pa": n(e["placebo_affected"]), "rr": e["rr"], "lo": e["lo"], "hi": e["hi"], "p": e["p"]}
                   for hid, e in x["narrow_outcomes"].items()}
    return t


def table(name, head, rows):
    """Write a table under results/, every column under a name of its own."""
    import csv
    assert len(set(head)) == len(head), [h for h in head if head.count(h) > 1]
    with open(os.path.join(A.OUT, name), "w", newline="", encoding="utf-8") as f:
        w = csv.writer(f)
        w.writerow(head)
        for row in rows:
            assert len(row) == len(head), (name, len(row), len(head))
            w.writerow(row)


def published_csv(x):
    """results/published.csv and follow_up.csv: each outcome's counts with its own denominators."""
    head, rows = ["comparison", "source", "url", "period"], []
    for kind in ("serious", "deaths"):
        head += [f"registry_vaccine_{kind}", f"registry_vaccine_{kind}_n", f"registry_placebo_{kind}", f"registry_placebo_{kind}_n",
                 f"published_vaccine_{kind}", f"published_vaccine_{kind}_n", f"published_placebo_{kind}", f"published_placebo_{kind}_n",
                 f"published_{kind}_basis", f"published_{kind}_usable", f"published_{kind}_why_not"]
    for t in x["published"]["trials"]:
        row = [t["id"], t["source"], t["url"], t["period"]]
        for kind, field in PUBLISHED.items():
            reg, pub = t["registry"][kind] or {}, t["published"][field]
            row += [reg.get("vaccine"), reg.get("vaccine_n"), reg.get("placebo"), reg.get("placebo_n"),
                    pub["vaccine"], pub["vaccine_n"], pub["placebo"], pub["placebo_n"], pub["basis"], pub["usable"], pub["why_not"]]
        rows.append(row + [t["note"]])
    table("published.csv", head + ["note"], rows)
    rows = []
    for t in x["follow_up"]["longer"]:
        s, d = t["any_serious"] or {}, t["death"] or {}
        rows.append([t["nct"], s.get("vaccine"), s.get("vaccine_n"), s.get("placebo"), s.get("placebo_n"),
                     d.get("vaccine"), d.get("vaccine_n"), d.get("placebo"), d.get("placebo_n")])
    table("follow_up.csv", ["trial", "vaccine_serious", "vaccine_serious_n", "placebo_serious", "placebo_serious_n",
                            "vaccine_deaths", "vaccine_deaths_n", "placebo_deaths", "placebo_deaths_n"], rows)


def main():
    tables_csv()
    comparisons = A.build_comparisons()
    main_set = A.select(comparisons, {"set": "main"})
    x = {"records": records(), "strata_margins": strata_margins(), "published": published(main_set), "follow_up": follow_up(comparisons),
         "live_non_target_by_target": live_by_target(comparisons), "p3_term_sensitivity": term_sensitivity(comparisons),
         "narrow_outcomes": narrow_outcomes(comparisons)}
    published_csv(x)
    for name, value in (("X.json", x), ("T.json", paper_values(x))):
        with open(os.path.join(A.OUT, name), "w", encoding="utf-8") as f:
            json.dump(value, f, indent=1, sort_keys=True, allow_nan=False, ensure_ascii=False)
            f.write("\n")


if __name__ == "__main__":
    main()
