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)}")
{}                                            support = 276
{'max_gap': 1}                                support = 191
{'max_gap': 0.5, 'gap_unit': 'time'}          support = 151
{'max_span': 2.0, 'gap_unit': 'time'}         support = 268

Where the occurrences are#

plot_sequence_index(db, max_sequences=60, sort="pattern", highlight=["wake up -> coffee", "commute -> work"], max_length=16)
sequence index plot — marked: wake up → coffee; commute → work
<Axes: title={'left': 'sequence index plot — marked: wake up → coffee; commute → work'}, xlabel='position', ylabel='60 sequences'>

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()
max_span= 4 h: 0 days
max_span= 6 h: 21 days
max_span= 8 h: 242 days
max_span=10 h: 246 days
74 patterns that happen within three hours

Total running time of the script: (0 minutes 0.799 seconds)

Gallery generated by Sphinx-Gallery