"""
A spectacular sunburst: chess openings
=======================================

Two thousand games drawn from a small opening book of 33 main lines. Every game
is the sequence of its first moves (plies). The sunburst stacks the moves ring
by ring: the angle of a wedge is the share of games that start with that move
sequence, so the familiar families — 1.e4 e5 open games, the Sicilian 1.e4 c5,
the closed 1.d4 d5 and the Indian 1.d4 Nf6 systems — appear as large slices
with their branches. Frequent sequences then summarize the book.
"""

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_icicle, plot_sunburst
db = sv.datasets.load_chess_openings(2000, random_state=0)
plot_sunburst(db, max_depth=5, min_angle=7, title="the first five plies of 2,000 games")
# %%
# The same tree as an icicle
# --------------------------
plot_icicle(db, max_depth=6, min_count=20, title="icicle of the opening tree")

# %%
# Frequent (contiguous) move sequences
# ------------------------------------
ps = sv.mine(db, min_support=0.05, max_gap=1)
print(ps.filter(min_length=4).sort("support").to_frame()[["pattern", "support", "confidence"]].head(10))
# %%
# Parameter study: depth
# ----------------------
fig, axes = plt.subplots(1, 3, figsize=(15, 5))
for ax, d in zip(axes, (2, 4, 6)):
    plot_sunburst(db, ax=ax, max_depth=d, min_angle=10, title=f"depth {d}")
    ax.get_legend().remove()
plt.show()
