.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/09_visualization/plot_02_visibility_graphs.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_09_visualization_plot_02_visibility_graphs.py: Six-sigma visibility graphs =========================== A visibility graph turns a series into a network: two samples are linked if the straight line between them passes above all samples in between (Lacasa et al., 2008); in the horizontal version the line must be horizontal (Luque et al., 2009). Labelling the nodes with the zones of a Shewhart control chart -- C within one sigma, B within two, A within three -- and the Western Electric rule violations gives a network view of process capability: the zone-transition statistics of the edges change when the process drifts. .. GENERATED FROM PYTHON SOURCE LINES 14-26 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd from mvtsa import representation as rp from mvtsa import viz rng = np.random.default_rng(1) in_control = rng.normal(0, 1, 120) drifting = rng.normal(0, 1, 120) + np.linspace(0, 2.5, 120) .. GENERATED FROM PYTHON SOURCE LINES 27-29 The graph of an in-control and a drifting process ------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 29-49 .. code-block:: Python G_ok = rp.six_sigma_visibility_graph(in_control, center=0, sigma=1) G_drift = rp.six_sigma_visibility_graph(drifting, center=0, sigma=1) rows = [] for name, G in [("in control", G_ok), ("drifting", G_drift)]: f = rp.graph_features(G) rules = { k: sum(G.nodes[i][k] for i in G.nodes) for k in ("rule1", "rule2", "rule3", "rule4") } rows.append( { "process": name, **{ k: f[k] for k in ("mean_degree", "degree_entropy", "clustering") }, **rules, } ) print(pd.DataFrame(rows).round(3).to_string(index=False)) .. rst-class:: sphx-glr-script-out .. code-block:: none process mean_degree degree_entropy clustering rule1 rule2 rule3 rule4 in control 3.833 1.823 0.640 0 0 0 0 drifting 3.833 1.836 0.645 5 16 47 52 .. GENERATED FROM PYTHON SOURCE LINES 50-54 The global graph features hardly change -- the mean degree of a horizontal visibility graph of an uncorrelated series is close to four whatever the distribution (Luque et al., 2009) -- but the zone labels and the rule flags reveal the drift at once. .. GENERATED FROM PYTHON SOURCE LINES 54-60 .. code-block:: Python fig, axes = plt.subplots(2, 1, figsize=(7, 5), sharex=True) viz.plot_visibility_graph(in_control, G_ok, ax=axes[0]) viz.plot_visibility_graph(drifting, G_drift, ax=axes[1]) axes[0].set_title("in control", fontsize=9, loc="left") axes[1].set_title("drifting", fontsize=9, loc="left") .. image-sg:: /auto_examples/09_visualization/images/sphx_glr_plot_02_visibility_graphs_001.png :alt: in control, drifting :srcset: /auto_examples/09_visualization/images/sphx_glr_plot_02_visibility_graphs_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.0, 1.0, 'drifting') .. GENERATED FROM PYTHON SOURCE LINES 61-63 Zone transitions ---------------- .. GENERATED FROM PYTHON SOURCE LINES 63-71 .. code-block:: Python for name, G in [("in control", G_ok), ("drifting", G_drift)]: zs = ( pd.Series([G.edges[e]["zones"] for e in G.edges]) .value_counts(normalize=True) .round(3) ) print(name, dict(zs.head(5))) plt.show() .. rst-class:: sphx-glr-script-out .. code-block:: none in control {'C-C': np.float64(0.635), 'B-C': np.float64(0.252), 'B-B': np.float64(0.039), 'A-B': np.float64(0.035), 'A-C': np.float64(0.035)} drifting {'B-C': np.float64(0.2), 'A-B': np.float64(0.2), 'C-C': np.float64(0.174), 'B-B': np.float64(0.135), 'A-A': np.float64(0.096)} .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.145 seconds) .. _sphx_glr_download_auto_examples_09_visualization_plot_02_visibility_graphs.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/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/09_visualization/plot_02_visibility_graphs.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_visibility_graphs.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_visibility_graphs.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_visibility_graphs.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_