#!/usr/bin/env python """Working charts for the report, from our own analysis of public data. The page's charts come in wave 2. python -I 10_charts.py --store Writes ../out/chart_*.png and ../out/chart_data_*.json """ import argparse import json import os import sys import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import numpy as np sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import rr_core as rr # noqa: E402 ap = argparse.ArgumentParser() ap.add_argument("--store", required=True) a = ap.parse_args() BLUE, ORANGE, AQUA, VIOLET = "#2a78d6", "#eb6834", "#1baf7a", "#6250d6" INK, INK2, MUTED, GRID, SURF = "#0b0b0b", "#52514e", "#8a8984", "#e4e3df", "#fcfcfb" plt.rcParams.update( { "font.family": "DejaVu Sans", "font.size": 10, "axes.edgecolor": MUTED, "axes.labelcolor": INK2, "xtick.color": INK2, "ytick.color": INK2, "axes.facecolor": SURF, "figure.facecolor": SURF, "axes.spines.top": False, "axes.spines.right": False, "text.color": INK, } ) days, kinds, levels = rr.load_family("N") masks = rr.set_masks(kinds, levels) nine = np.array([rr.PERIODS[n] for n in rr.NAMES]) d = np.load(os.path.join(a.store, "null_N_Bp_W7.npz")) k = np.arange(rr.KMAX + 1) T = np.where(k > 0, rr.NFFT / np.maximum(k, 1), np.inf) # ---- A: z against what clustering alone gives ---------------------------------------------------------- sel = (T >= 50) & (T <= 850) fig, axes = plt.subplots(2, 1, figsize=(10, 7.2), sharex=True) core = np.zeros(rr.KMAX + 1, dtype=bool) for c in rr.line_bins(1 / nine): for o in rr.CORE_BINS: core[c + o - rr.HALF : c + o + rr.HALF + 1] = True chart = {} for ax, i, env, name in ((axes[0], 0, "env0", "X10+GLE, 109 events"), (axes[1], 4, "env4", "X1+GLE, 673 events")): z = rr.z_grid(days[masks[i]]) p50, p95, p999 = d[env] ax.fill_between(T[sel], 0, 1, where=core[sel], transform=ax.get_xaxis_transform(), color=GRID, lw=0, label="Near a predicted line or its side bands") ax.plot(T[sel], z[sel], color=BLUE, lw=0.8, label="Observed z") # the percentile curves are noisy at this resolution: draw them smoothed kern = np.ones(61) / 61 p95s = np.convolve(p95, kern, mode="same") p999s = np.convolve(p999, kern, mode="same") ax.plot(T[sel], p95s[sel], color=INK2, lw=1.2, ls=(0, (4, 3)), label="95th percentile by chance, clusters moved whole (null B')") ax.plot(T[sel], p999s[sel], color=INK, lw=1.2, label="99.9th percentile by chance") ax.axhline(6.91, color=ORANGE, lw=1.2, label="The paper's p = 0.001 line, z = 6.91") ax.set_xscale("log") ax.set_xlim(50, 850) ax.set_ylim(0, 42 if i == 4 else 24) ax.set_ylabel("Rayleigh z") ax.text(0.012, 0.92, name, transform=ax.transAxes, fontsize=11, color=INK, va="top", ha="left", bbox=dict(facecolor=SURF, edgecolor="none", pad=2)) for p, nm in zip(nine, rr.NAMES): ax.plot([p, p], [ax.get_ylim()[1] * 0.965, ax.get_ylim()[1]], color=INK, lw=1.5) if ax is axes[0]: ax.text(p, ax.get_ylim()[1] * 1.012, nm[1:], ha="center", va="bottom", fontsize=8.5, color=INK2) chart[name] = dict(T=np.round(T[sel][::4], 2).tolist(), z=np.round(z[sel][::4], 2).tolist(), p95=np.round(p95[sel][::4], 2).tolist(), p999=np.round(p999[sel][::4], 2).tolist()) axes[1].set_xlabel("Period, days (log scale)") axes[1].set_xticks([50, 75, 100, 150, 200, 300, 400, 600, 800]) axes[1].set_xticklabels(["50", "75", "100", "150", "200", "300", "400", "600", "800"]) h, l = axes[0].get_legend_handles_labels() fig.legend(h, l, loc="upper left", frameon=False, fontsize=8.5, ncol=3, bbox_to_anchor=(0.06, 0.955)) fig.suptitle("The paper's p = 0.001 line sits below what clustered flares reach by chance", x=0.07, ha="left", fontsize=13, y=0.985) fig.text(0.07, 0.008, "Family N: NOAA flare reports, S-list, Oulu GLE list. Our analysis. Grey bands: core side-band windows, tolerance 1/25,000 per day. Ticks: the nine predicted periods.", fontsize=8, color=INK2) fig.tight_layout(rect=(0, 0.025, 0.985, 0.90)) fig.savefig(os.path.join(rr.OUT, "chart_z_vs_chance.png"), dpi=160) plt.close(fig) rr.save_json(os.path.join(rr.OUT, "chart_data_z_vs_chance.json"), chart) # ---- B: head-to-tail phasors by solar cycle ------------------------------------------------------------- split = json.load(open(os.path.join(rr.OUT, "split_N.json"), encoding="utf-8")) ph = split["phasors"]["X1+GLE"] cyc = ["SC19", "SC20", "SC21", "SC22", "SC23", "SC24", "SC25"] fig, axes = plt.subplots(3, 3, figsize=(10, 10)) chart2 = {} for ax, nm in zip(axes.ravel(), rr.NAMES): x, y = [0.0], [0.0] labs = [] for c in cyc: r = ph[nm][c] if r.get("N", 0) == 0: continue x.append(x[-1] + r["Rx"]) y.append(y[-1] + r["Ry"]) labs.append(c[2:]) ax.plot(x, y, color=BLUE, lw=1.5, marker="o", ms=4) ax.plot([0, x[-1]], [0, y[-1]], color=ORANGE, lw=1.5) for xi, yi, l in zip(x[1:], y[1:], labs): if l in ("19", "20"): continue ax.text(xi, yi, " " + l, fontsize=8, color=INK2) lim = 150 ax.set_xlim(-lim, lim) ax.set_ylim(-lim, lim) ax.set_aspect("equal") ax.axhline(0, color=GRID, lw=0.8) ax.axvline(0, color=GRID, lw=0.8) ax.set_xticks([]) ax.set_yticks([]) for s in ax.spines.values(): s.set_visible(False) title_a = f"{rr.PERIODS[nm]} d: z = {ph[nm]['z_total']:.1f}" title_b = f"random phases {ph[nm]['z_mean_random_phases']:.1f}, all lined up {ph[nm]['z_if_all_aligned']:.1f}" ax.set_title(chr(10).join((title_a, title_b)), fontsize=8.8, loc="left", color=INK) chart2[nm] = dict(x=np.round(x, 2).tolist(), y=np.round(y, 2).tolist(), labels=labs) fig.suptitle("A phase-stable period would walk in a straight line from cycle to cycle", x=0.05, ha="left", fontsize=13) fig.text(0.05, 0.012, "X1+GLE, family N. Blue: each cycle's resultant, head to tail, labelled by cycle. Orange: the total. Same scale throughout. Our analysis.", fontsize=8, color=INK2) fig.tight_layout(rect=(0, 0.03, 1, 0.965)) fig.savefig(os.path.join(rr.OUT, "chart_phasors.png"), dpi=150) plt.close(fig) rr.save_json(os.path.join(rr.OUT, "chart_data_phasors.json"), chart2) print("charts written")