"""
An everyday example: daily routines
===================================

Three hundred days of three kinds of people — early birds, commuters and remote
workers — each a sequence of activities from waking up to going to sleep. The
raw sequences are summarized by a *sunburst* (rings = steps, angle = number of
days), the frequent sequences by a *prefix tree*, and the most typical patterns
by a glyph table. Patterns may skip activities (``max_gap=2`` allows one
activity in between).
"""

# %%
# Look at the data first
# ----------------------
import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_patterns, plot_prefix_tree, plot_sunburst
db = sv.datasets.make_daily_routines(300, random_state=0)
print(db.stats())
print(sv.format_pattern(db[0]))
plot_sunburst(db, max_depth=4, title="how the days begin")
# %%
# Frequent sequences with a small gap
# -----------------------------------
ps = sv.mine(db, min_support=0.3, max_gap=2, max_length=6)
print(ps)
plot_prefix_tree(ps, max_depth=4, max_children=3, title="frequent sequences as a tree (three most frequent continuations per node)")
# %%
# The ten best patterns of length at least three
# ----------------------------------------------
plot_patterns(ps.filter(min_length=3), top=10)

# %%
# Parameter study: the gap
# ------------------------
# Without a gap constraint, patterns may join activities hours apart and their
# number grows; with ``max_gap=1`` only directly consecutive activities count.
for gap in (1, 2, 3, None):
    n = len(sv.mine(db, 0.3, max_gap=gap, max_length=6))
    print(f"max_gap={gap}: {n} patterns")
plt.show()
