"""
Gaps, spans and time windows
============================

A pattern ⟨wake up, coffee⟩ is contained in a day even if hours pass between the
two. Constraints restrict which occurrences count: ``max_gap`` bounds the
distance of consecutive matched elements (``max_gap=1`` = directly
consecutive), ``max_span`` the length of the whole occurrence, and with
``gap_unit="time"`` both are measured on the timestamps (here hours).
"""

import matplotlib.pyplot as plt

import spmviz as sv
from spmviz.viz import plot_sequence_index

db = sv.datasets.make_daily_routines(300, random_state=0)

# %%
# The same pattern under different constraints
# --------------------------------------------
p = "wake up -> coffee"            # arrows separate the elements; items may contain spaces
for kw in [{}, {"max_gap": 1}, {"max_gap": 0.5, "gap_unit": "time"}, {"max_span": 2.0, "gap_unit": "time"}]:
    print(f"{str(kw):45s} support = {db.support(p, **kw)}")
# %%
# Where the occurrences are
# -------------------------
plot_sequence_index(db, max_sequences=60, sort="pattern", highlight=["wake up -> coffee", "commute -> work"], max_length=16)

# %%
# Parameter study: a time window on the whole pattern
# ---------------------------------------------------
# How many sequences contain "lunch → … → dinner" within w hours?
for w in (4, 6, 8, 10):
    print(f"max_span={w:2d} h: {db.support('lunch dinner', max_span=w, gap_unit='time')} days")
ps = sv.mine(db, 0.25, max_span=3.0, gap_unit="time", max_length=5)
print(len(ps), "patterns that happen within three hours")
plt.show()
