.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_basics/plot_04_constraints.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_basics_plot_04_constraints.py: 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). .. GENERATED FROM PYTHON SOURCE LINES 11-19 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 20-22 The same pattern under different constraints -------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python 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)}") .. rst-class:: sphx-glr-script-out .. code-block:: none {} 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 .. GENERATED FROM PYTHON SOURCE LINES 26-28 Where the occurrences are ------------------------- .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python plot_sequence_index(db, max_sequences=60, sort="pattern", highlight=["wake up -> coffee", "commute -> work"], max_length=16) .. image-sg:: /auto_examples/01_basics/images/sphx_glr_plot_04_constraints_001.png :alt: sequence index plot — marked: wake up → coffee; commute → work :srcset: /auto_examples/01_basics/images/sphx_glr_plot_04_constraints_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 31-34 Parameter study: a time window on the whole pattern --------------------------------------------------- How many sequences contain "lunch → … → dinner" within w hours? .. GENERATED FROM PYTHON SOURCE LINES 34-39 .. code-block:: Python 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() .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.621 seconds) .. _sphx_glr_download_auto_examples_01_basics_plot_04_constraints.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/spmviz/main?urlpath=lab/tree/notebooks/auto_examples/01_basics/plot_04_constraints.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_04_constraints.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_04_constraints.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_04_constraints.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_