#!/usr/bin/env python """Survival table of rule 13 from the JSON results. Run: python -I 14_summary.py [family]""" import json import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import rr_core as rr # noqa: E402 fam = sys.argv[1] if len(sys.argv) > 1 else "N" e = json.load(open(os.path.join(rr.OUT, f"eval_{fam}.json"), encoding="utf-8")) sp_path = os.path.join(rr.OUT, f"split_{fam}.json") sp = json.load(open(sp_path, encoding="utf-8")) if os.path.exists(sp_path) else None names = e["names"] nulls = ("Ap", "Bp", "C") rows = [] surv = [] print(f"Family {fam}. Events {e['N_events']}") for k, nm in enumerate(names): n = e["nulls"] raw_b = [n[x]["clustered"]["best_p"][k] for x in nulls] adj_b = [n[x]["clustered"]["adjusted_p"][k] for x in nulls] within_b = [n[x]["clustered"]["adjusted_within_period"][k] for x in nulls] raw_a = [n[x]["declustered"]["best_p"][k] for x in nulls] adj_a = [n[x]["declustered"]["adjusted_p"][k] for x in nulls] within_a = [n[x]["declustered"]["adjusted_within_period"][k] for x in nulls] rank = e["decoy_line_rank"][nm] c_ok = min(rank["C"]["p_adjusted_for_set"], rank["B"]["p_adjusted_for_set"]) <= 0.05 b_ok = max(adj_b) <= 0.05 a_ok = max(adj_a) <= 0.05 d_ok = sp["split_pass"][nm]["passes"] if sp else None row = dict( period=nm, T=rr.PERIODS[nm], z_exact_max=max(e["z_exact"][i][k] for i in range(5)), z_exact_X10=e["z_exact"][0][k], z_exact_X1=e["z_exact"][4][k], C_best=max(e["scores_observed"]["C"][i][k] for i in range(5)), B_best=max(e["scores_observed"]["B"][i][k] for i in range(5)), z_exact_decl_max=max(e["z_exact"][i][k] for i in range(5, 10)), C_decl_best=max(e["scores_observed"]["C"][i][k] for i in range(5, 10)), B_decl_best=max(e["scores_observed"]["B"][i][k] for i in range(5, 10)), raw_clustered=raw_b, adj_clustered=adj_b, within_clustered=within_b, raw_declustered=raw_a, adj_declustered=adj_a, within_declustered=within_a, decoy_C=rank["C"]["p_adjusted_for_set"], decoy_B=rank["B"]["p_adjusted_for_set"], decoy_C_positions=rank["C"]["share_of_positions_including_neighbours"], decoy_B_positions=rank["B"]["share_of_positions_including_neighbours"], split_exact=sp["split_pass"][nm]["exact"] if sp else None, split_selected=sp["split_pass"][nm]["selected"] if sp else None, a=a_ok, b=b_ok, c=c_ok, d=d_ok, survives=bool(a_ok and b_ok and c_ok and d_ok), ) rows.append(row) print(nm, json.dumps({k_: v for k_, v in row.items() if k_ not in ("period",)})) print("survivors:", [r["period"] for r in rows if r["survives"]]) print("passes (a):", [r["period"] for r in rows if r["a"]], "(b):", [r["period"] for r in rows if r["b"]], "(c):", [r["period"] for r in rows if r["c"]], "(d):", [r["period"] for r in rows if r["d"]]) for x in nulls: print(x, "rates", {k_: v for k_, v in e["nulls"][x]["clustered"]["rates"].items() if "all_nine" in k_ or "at_least" in k_}) print(x, "declustered rates", {k_: v for k_, v in e["nulls"][x]["declustered"]["rates"].items() if "all_nine" in k_ or "at_least" in k_}) print(x, "global", [(g["set"], g["highest_z"], g["T"], g["p_global"], g["null_q95_of_max"]) for g in e["nulls"][x]["clustered"]["global_50_850"]]) print(x, "mean z", e["nulls"][x]["mean_z_50_700"]) print("A' long", e["nulls"]["Ap"]["long_periods"]) rr.save_json(os.path.join(rr.OUT, f"survival_{fam}.json"), rows)