"""
A summary in a few patterns: MDL
================================

Instead of hundreds of frequent sequences, the Minimum Description Length
principle picks the few that compress the data best (Krimp, SQS: Tatti &
Vreeken, 2012). Every sequence is covered greedily by the selected patterns;
the cover plot shows which runs of each day each pattern explains.
"""

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_cover

db = sv.datasets.make_daily_routines(300, random_state=0)
summary = sv.mdl_summary(db, min_support=0.1, max_length=8, max_patterns=12)
print(f"{len(summary.patterns)} patterns, description length {summary.compression:.0%} of the singleton code")
print(summary.to_frame())
plot_cover(db, summary, max_sequences=40, max_length=15)
# %%
# Parameter study: how many patterns
# ----------------------------------
for k in (2, 5, 10, 20):
    s = sv.mdl_summary(db, min_support=0.1, max_length=8, max_patterns=k)
    print(f"{k:2d} patterns allowed: compression {s.compression:.2f}")
plt.show()
