"""Figure for report.html, from data/ only.
python reproduce/figures.py # writes report/lower_by.png
"""
import csv
from pathlib import Path
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt # noqa: E402
HERE = Path(__file__).resolve().parent.parent
rows = [r for r in csv.DictReader(open(HERE / "data" / "scores.csv", newline="")) if r["outcome"] == "lower"]
fig, axes = plt.subplots(1, 2, figsize=(7.2, 3.0), dpi=150, sharex=True)
for ax, (s, title) in zip(axes, (("reaction_qm", "Reaction-QM"), ("geodesic", "Geodesic set"))):
one = [float(r["lower_by_kcal"]) for r in rows if r["set"] == s and r["best_kind"] == "channel"]
multi = [float(r["lower_by_kcal"]) for r in rows if r["set"] == s and r["best_kind"] == "alternate"]
ax.hist([one, multi], bins=range(0, 52, 3), stacked=True, color=["#2a5db0", "#9bb5e0"],
edgecolor="white", linewidth=0.4, label=["single step", "through intermediates"])
ax.set_title(f"{title} ({len(one) + len(multi)} reactions)", fontsize=10)
ax.set_xlabel("found route lower by (kcal/mol)")
ax.spines[["top", "right"]].set_visible(False)
for ax in axes:
ax.yaxis.get_major_locator().set_params(integer=True)
axes[0].set_ylabel("reactions")
axes[0].legend(frameon=False, fontsize=8)
fig.tight_layout()
(HERE / "report").mkdir(exist_ok=True)
fig.savefig(HERE / "report" / "lower_by.png", metadata={"Software": None})