"""
Benchmark databases of sequential pattern mining
================================================

The databases distributed with SPMF (Fournier-Viger et al., 2016) are the common
yardstick of the literature: SIGN (sign language), LEVIATHAN and BIBLE (text),
BMS1/BMS2, FIFA and KOSARAK (click streams), BIKE (bike stations), MSNBC (page
categories). :func:`~spmviz.datasets.fetch_spmf` downloads and caches them.
Here SIGN is mined and drawn; the book reports run times on all of them.
"""

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_nbvfs, plot_sunburst

print(sv.datasets.benchmark_table()[["name", "sequences", "items", "avg_length"]])
try:
    db = sv.datasets.fetch_spmf("SIGN")
except OSError as exc:  # no network: fall back to a synthetic database of the same size
    print("download failed:", exc)
    db = sv.datasets.make_quest(730, n_items=267, avg_elements=52, avg_itemset=1.0, random_state=0)
print(db.stats())
ps = sv.mine(db, min_support=0.5, max_length=5)
print(ps)
# %%
# Maps of the frequent sequences
# ------------------------------
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
plot_nbvfs(sv.NBVFS("cm").fit(ps.filter(min_length=2)), ax=axes[0], legend=False, labels=10)
plot_sunburst(db, ax=axes[1], max_depth=3, min_angle=8, title="first three signs")
axes[1].get_legend().remove()
plt.show()
