.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_applications/plot_03_alarm_early_warning.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_07_applications_plot_03_alarm_early_warning.py: Case study: early warning in alarm management ============================================= Operators of a chemical plant face alarm floods. Frequent sequences of alarms and operator actions (Bántay et al., 2023; Bántay & Abonyi, 2024) give two aids: a map of the typical scenarios, and an online prediction of the next event from the longest frequent sequence that matches the alarms seen so far. .. GENERATED FROM PYTHON SOURCE LINES 10-19 .. code-block:: Python import matplotlib.pyplot as plt import spmviz as sv from spmviz.viz import plot_prediction, plot_prefix_tree db = sv.datasets.load_alarm_log() ps = sv.mine(db, 0.01, algorithm="cmspam", max_gap=2, max_length=10) .. GENERATED FROM PYTHON SOURCE LINES 20-22 Scenario trees of the most frequent alarm ----------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python top_alarm = ps.filter(max_length=1).sort("support").patterns[0][0][0] plot_prefix_tree(ps.filter(starts_with=top_alarm), max_depth=4, max_children=3, title=f"frequent scenarios starting with {db.label(top_alarm)}") .. image-sg:: /auto_examples/07_applications/images/sphx_glr_plot_03_alarm_early_warning_001.png :alt: frequent scenarios starting with O1084 :srcset: /auto_examples/07_applications/images/sphx_glr_plot_03_alarm_early_warning_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 26-28 Online prediction ----------------- .. GENERATED FROM PYTHON SOURCE LINES 28-33 .. code-block:: Python long = ps.filter(min_length=3).sort("support").patterns[0] observed = [e[0] for e in long[:2]] print("observed:", [db.label(i) for i in observed]) print(sv.predict_next(ps, observed)) plot_prediction(ps, observed) .. image-sg:: /auto_examples/07_applications/images/sphx_glr_plot_03_alarm_early_warning_002.png :alt: what comes next? (confidence of the extensions of the longest matching frequent sequence) :srcset: /auto_examples/07_applications/images/sphx_glr_plot_03_alarm_early_warning_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none observed: ['O1084', 'O1085'] next probability support context 0 O1084 0.518784 290 O1084 → O1085 1 O1085 0.250447 140 O1084 → O1085 2 O1133 0.239714 134 O1084 → O1085 3 O1080 0.216458 121 O1084 → O1085 4 O1081 0.148479 83 O1084 → O1085 .. GENERATED FROM PYTHON SOURCE LINES 34-38 How often is the top prediction right? -------------------------------------- Leave-one-out on the traces would be the honest test; here, a quick check on the traces that contain the observed prefix. .. GENERATED FROM PYTHON SOURCE LINES 38-49 .. code-block:: Python hits = total = 0 for s in db: items = [e[0] for e in s] for j in range(len(items) - 2): if items[j:j + 2] == observed: pred = sv.predict_next(ps, observed, k=1) total += 1 hits += int(len(pred) and pred.iloc[0]["next"] == db.label(items[j + 2])) break print(f"top-1 accuracy on {total} traces: {hits / max(total, 1):.2f}") plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none top-1 accuracy on 421 traces: 0.38 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.885 seconds) .. _sphx_glr_download_auto_examples_07_applications_plot_03_alarm_early_warning.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/07_applications/plot_03_alarm_early_warning.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_alarm_early_warning.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_alarm_early_warning.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_alarm_early_warning.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_