# (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})

R = json.load(open("recover.json"))
P = R["power_logshift_ln2"]
ns = [5, 10, 20, 30, 50]
meth = [("log_t", "log-scale t-test", "#1f4e79", "o"),
        ("MW", "Mann–Whitney", "#dd8452", "s"),
        ("raw_t", "raw-scale t-test", "#c44e52", "^")]

fig, axes = plt.subplots(1, 2, figsize=(10, 4), sharey=True)
for ax, cv in zip(axes, [1.0, 2.0]):
    for key, lab, col, mk in meth:
        y = [P[str(cv)][str(n)][key] for n in ns]
        ax.plot(ns, y, "-", marker=mk, color=col, ms=5, label=lab)
    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 (median ratio 2)")
axes[0].legend(fontsize=9, loc="lower right")
fig.suptitle("Screen-guided log-scale t-test recovers power lost by the raw t-test and exceeds Mann–Whitney", y=1.02, fontsize=11)
fig.tight_layout()
fig.savefig("fig7_recover.png", bbox_inches="tight", metadata={"Author": "William J. Dwyer (ORCID 0009-0004-0855-7222)", "Title": "fig7_recover", "Subject": "Generated by fig_recover.py for the Comparable-Strata Designer; MIT License; cite Zenodo DOI 10.5281/zenodo.20709963"})
print("saved")
