# (c) 2026 William J. Dwyer. Free and open-source software under the MIT License (see LICENSE).
# If you use or build upon this software, please retain this notice and cite the accompanying
# methodology paper together with the archived software (Zenodo DOI: 10.5281/zenodo.20709963).
import json, numpy as np
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
plt.rcParams.update({"font.size": 11, "figure.dpi": 150, "axes.grid": True, "grid.alpha": 0.3})

D = json.load(open("normtests.json"))
ns = D["n_grid"]; cvs = D["cv_grid"]
tests = ["SW", "AD", "DP", "LF", "JB"]
labels = {"SW": "Shapiro–Wilk", "AD": "Anderson–Darling", "DP": "D'Agostino–Pearson",
          "LF": "Lilliefors (KS)", "JB": "Jarque–Bera"}
colors = {"SW": "#1f4e79", "AD": "#4c72b0", "DP": "#55a868", "LF": "#dd8452", "JB": "#c44e52"}
markers = {"SW": "o", "AD": "s", "DP": "^", "LF": "D", "JB": "v"}

fig, axes = plt.subplots(1, 3, figsize=(12, 4), sharey=True)
for ax, cv in zip(axes, cvs):
    ax.axvspan(min(ns), 30, color="gold", alpha=0.12)
    for t in tests:
        y = [D["power"][str(cv)][str(n)][t] for n in ns]
        ax.plot(ns, y, "-", marker=markers[t], color=colors[t], ms=4, label=labels[t])
    ax.axhline(0.8, ls="--", c="grey", lw=1)
    ax.set_xscale("log"); ax.set_xticks(ns); ax.set_xticklabels(ns)
    ax.set_xlabel("sample size per group, n")
    ax.set_title(f"CV = {cv}")
axes[0].set_ylabel("power to reject normality (α = 0.05)")
axes[0].legend(fontsize=8, loc="lower right")
fig.suptitle("All normality tests are blind to log-normal data at small n (shaded n ≤ 30)", y=1.02)
fig.tight_layout()
fig.savefig("fig6_normtests.png", bbox_inches="tight", metadata={"Author": "William J. Dwyer (ORCID 0009-0004-0855-7222)", "Title": "fig6_normtests", "Subject": "Generated by fig_normtests.py for the Comparable-Strata Designer; MIT License; cite Zenodo DOI 10.5281/zenodo.20709963"})
print("saved")
