#!/usr/bin/env python
"""The numbers behind the page's two charts, from our own analysis of public data. Added for the page: decides nothing.
python -I 16_chart_series.py --store
Writes ../out/chart_series.json
Chart 1, z against chance, on a log axis of period, 50 to 850 d, in COLUMNS equal steps:
z the highest observed z in each column (family N)
p95 95th percentile of z at each frequency under null B', from all 20,000 draws (RULES.txt entry 19), averaged in each column, then a running
mean over 2*SMOOTH+1 columns
p999 the same for the 99.9th percentile
bands where the core windows of the nine lines fall, as merged [short, long] period intervals
Chart 2, the phasors, is read straight from split_N.json by the page's chart code.
"""
import argparse
import os
import sys
import numpy as np
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import rr_core as rr # noqa: E402
COLUMNS = 470
SMOOTH = 8
TMIN, TMAX = 50.0, 850.0
ap = argparse.ArgumentParser()
ap.add_argument("--store", required=True)
a = ap.parse_args()
days, kinds, levels = rr.load_family("N")
masks = rr.set_masks(kinds, levels)
null = np.load(os.path.join(a.store, "later_N_Bp_W7.npz"))
draws = int(null["exact"].shape[0])
k = np.arange(rr.KMAX + 1)
T = np.where(k > 0, rr.NFFT / np.maximum(k, 1), np.inf)
edges = np.geomspace(TMIN, TMAX, COLUMNS + 1)
mids = np.sqrt(edges[:-1] * edges[1:])
col = np.searchsorted(edges, T, side="right") - 1 # column of each bin, -1 or COLUMNS outside the axis
inside = (col >= 0) & (col < COLUMNS)
def column_max(y):
out = np.full(COLUMNS, np.nan)
np.maximum.at(out_fill := np.full(COLUMNS, -np.inf), col[inside], y[inside])
out[np.isfinite(out_fill)] = out_fill[np.isfinite(out_fill)]
return out
def column_mean(y):
s = np.bincount(col[inside], weights=y[inside], minlength=COLUMNS)
n = np.bincount(col[inside], minlength=COLUMNS)
return s / np.maximum(n, 1)
def running(y):
out = np.empty_like(y)
for i in range(y.size):
lo, hi = max(0, i - SMOOTH), min(y.size, i + SMOOTH + 1)
out[i] = y[lo:hi].mean()
return out
core = np.zeros(rr.KMAX + 1, dtype=bool)
central = np.zeros(rr.KMAX + 1, dtype=bool)
nine = np.array([rr.PERIODS[n] for n in rr.NAMES])
for c in rr.line_bins(1 / nine):
central[c - rr.HALF : c + rr.HALF + 1] = True
for o in rr.CORE_BINS:
core[max(c + o - rr.HALF, 0) : c + o + rr.HALF + 1] = True
def intervals(flags):
"""Merged [short, long] period intervals of the flagged bins inside the axis."""
sel = np.where(flags & (T >= TMIN) & (T <= TMAX))[0]
if sel.size == 0:
return []
groups = np.split(sel, np.where(np.diff(sel) > 1)[0] + 1)
return [[round(float(T[g[-1]]), 2), round(float(T[g[0]]), 2)] for g in groups]
panels = {}
for i, env, name in ((0, "env0", "X10+GLE"), (4, "env4", "X1+GLE")):
z = rr.z_grid(days[masks[i]])
k0 = int(null["env_k0"])
p95, p999 = np.zeros(rr.KMAX + 1), np.zeros(rr.KMAX + 1)
p95[k0 : k0 + null[env].shape[1]] = null[env][1]
p999[k0 : k0 + null[env].shape[1]] = null[env][2]
panels[name] = dict(
N=int(masks[i].sum()),
z=np.round(column_max(z), 2).tolist(),
p95=np.round(running(column_mean(p95)), 2).tolist(),
p999=np.round(running(column_mean(p999)), 2).tolist(),
)
rr.save_json(
os.path.join(rr.OUT, "chart_series.json"),
dict(
note="Family N. z: highest observed z in each column. p95, p999: percentiles of z under null B' (bursts moved whole, no overlap), from all the draws, averaged in each column and smoothed.",
null="Bp",
draws=draws,
columns=COLUMNS,
smooth=SMOOTH,
T_mid=np.round(mids, 2).tolist(),
T_range=[TMIN, TMAX],
panels=panels,
bands_core=intervals(core),
bands_central=intervals(central),
nine=[dict(name=n, T=rr.PERIODS[n]) for n in rr.NAMES],
),
)
print("draws", draws, "bands", len(intervals(core)), "central", len(intervals(central)))
for name, p in panels.items():
print(name, "z max", np.nanmax(p["z"]), "p95 max", max(p["p95"]), "p999 max", max(p["p999"]), "p999 min", min(p["p999"]), "empty columns", int(np.isnan(p["z"]).sum()))