"""
Reports, animations and the burden of a session
===============================================

A finished session can be written to a self-contained HTML report (settings,
summary, galleries, curves, genealogy, map, evaluator diagnostics) and to an
animated GIF of its galleries. Burden measures — images shown, elementary
decisions, the information-theoretic upper bound of what the answers could
convey, human answers, API cost — make sessions with different protocols
comparable.
"""

# %%
# A report and an animation
# -------------------------
import matplotlib.pyplot as plt

import easyiec as ei
from easyiec.problems import CPPNArt
from easyiec.strategies import InteractiveCMAES, InteractiveGA, PreferentialBO, SequentialGallery
from easyiec.validation import burden

problem = CPPNArt()
user = ei.SimulatedUser(problem, target="random", noise=0.04, seed=2)
result = ei.run(problem, InteractiveGA(pop_size=9), user, generations=10, seed=2)
ei.viz.session_report(result, "cppn_report.html", evaluator=user, title="CPPN art with a simulated user")
ei.viz.animate_session(result.log, problem, "cppn_session.gif", max_items=9, tile=96)
print(burden(result.log))
ei.viz.plot_history(result.log, problem, steps=[0, 3, 6, 9], size=0.9)

# %%
# Burden of four protocols
# ------------------------
# The same 150-image budget spent through galleries (GA, CMA-ES), single duels
# (PBO) and 3×3 grids (sequential gallery) on the same problem.
from easyiec.problems import SyntheticPreference  # noqa: E402

p = SyntheticPreference(dim=4, seed=1)
rows = []
for name, strat in {"GA (select)": InteractiveGA(), "CMA-ES (rank)": InteractiveCMAES(protocol="rank"),
                    "PBO (pairs)": PreferentialBO(), "gallery (grid)": SequentialGallery()}.items():
    res = ei.run(p, strat, ei.SimulatedUser(p, seed=3), budget=ei.Budget(max_items=150), seed=3)
    b = burden(res.log)
    rows.append((name, b["queries"], b["decisions"], b["bits"], res.best_true))
fig, axes = plt.subplots(1, 3, figsize=(12, 3))
for ax, k, lab in zip(axes, [1, 2, 3], ["questions asked", "elementary decisions", "information bound (bits)"]):
    ax.bar([r[0] for r in rows], [r[k] for r in rows], color="#285780")
    ax.set_title(lab, fontsize=9)
    ax.tick_params(axis="x", labelsize=7, rotation=20)
fig.tight_layout()
plt.show()
