.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_measures/plot_03_similarity.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_05_measures_plot_03_similarity.py: Similarity of drivers, sensors and edges ======================================== The NOCAD toolbox introduced three similarity measures built on reachability. Two driver nodes are similar if they influence the same states after a similar number of derivations (Jaccard index of the controlled sets, weighted by the agreement of the distances); the *endpoint similarity* of an edge compares what its two endpoints control and observe, and is one on cycles; the *edge similarity* of two edges compares the four sets of their endpoints and reveals parts of the network with redundancy. .. GENERATED FROM PYTHON SOURCE LINES 15-17 Driver similarity and edge similarity of the NOCAD example ---------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 17-61 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import nocad from nocad.datasets import load_nocad_example from nocad.measures import driver_similarity, edge_similarity, endpoint_similarity, sensor_similarity adj = load_nocad_example().adj drivers = [3, 5, 7] # x4, x6, x8: a richer configuration than the minimal one sensors = [0, 1, 2, 8] A = nocad.adjacency_to_state_matrix(adj).toarray() B = np.zeros((9, 3)) B[drivers, range(3)] = 1 C = np.zeros((4, 9)) C[range(4), sensors] = 1 # [snippet:similarity] ds = driver_similarity(A, B) ss = sensor_similarity(A, C) S, edges = edge_similarity(adj) es = endpoint_similarity(adj) # [/snippet:similarity] print("driver similarity:\n", ds[np.ix_(drivers, drivers)].round(2)) print("sensor similarity:\n", ss[np.ix_(sensors, sensors)].round(2)) fig, axes = plt.subplots(1, 2, figsize=(9.6, 4.2), gridspec_kw={"width_ratios": [1, 1.6]}) im = axes[0].imshow(ds[np.ix_(drivers, drivers)], vmin=0, vmax=1, cmap="Blues") axes[0].set_xticks(range(3)) axes[0].set_xticklabels([f"x{d + 1}" for d in drivers]) axes[0].set_yticks(range(3)) axes[0].set_yticklabels([f"x{d + 1}" for d in drivers]) axes[0].set_title("driver similarity", fontsize=10) for a in range(3): for b in range(3): axes[0].text(b, a, f"{ds[drivers[a], drivers[b]]:.2f}", ha="center", va="center", fontsize=8, color="white" if ds[drivers[a], drivers[b]] > 0.6 else "black") lab = [f"{j + 1}{i + 1}" for j, i in edges] im = axes[1].imshow(S, vmin=0, vmax=1, cmap="Greens") axes[1].set_xticks(range(len(lab))) axes[1].set_xticklabels(lab, fontsize=6.5, rotation=90) axes[1].set_yticks(range(len(lab))) axes[1].set_yticklabels(lab, fontsize=6.5) axes[1].set_title("edge similarity (label: start, end)", fontsize=10) fig.colorbar(im, ax=axes[1], fraction=0.04) fig.tight_layout() .. image-sg:: /auto_examples/05_measures/images/sphx_glr_plot_03_similarity_001.png :alt: driver similarity, edge similarity (label: start, end) :srcset: /auto_examples/05_measures/images/sphx_glr_plot_03_similarity_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none driver similarity: [[1. 0.81 0.62] [0.81 1. 0.71] [0.62 0.71 1. ]] sensor similarity: [[1. 0.8 0.91 0.4 ] [0.8 1. 0.77 0.39] [0.91 0.77 1. 0.4 ] [0.4 0.39 0.4 1. ]] .. GENERATED FROM PYTHON SOURCE LINES 62-68 Parameter study: endpoint similarity on the network --------------------------------------------------- Edges inside the strongly connected components have endpoint similarity one (their endpoints reach and are reached by the same nodes). An edge between components is low when its endpoints differ much in what they reach or in what reaches them: the edge from the source x8 and the edges into x7 and x2 are the weakest links. .. GENERATED FROM PYTHON SOURCE LINES 68-81 .. code-block:: Python from nocad.plotting import ROLE_COLORS, default_layout, draw_network pos = default_layout(adj) hl = {} cmap = plt.get_cmap("viridis") for j, i in edges: hl[(int(j), int(i))] = {"color": cmap(es[j, i]), "lw": 1.0 + 2.0 * es[j, i]} fig, ax = plt.subplots(figsize=(5.6, 4.2)) draw_network(adj, pos, ax=ax, highlight=hl, node_color=ROLE_COLORS["state"], label_color="#1F2A36") sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(0, 1)) fig.colorbar(sm, ax=ax, fraction=0.04, label="endpoint similarity") ax.set_title("endpoint similarity of the edges", fontsize=10) .. image-sg:: /auto_examples/05_measures/images/sphx_glr_plot_03_similarity_002.png :alt: endpoint similarity of the edges :srcset: /auto_examples/05_measures/images/sphx_glr_plot_03_similarity_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Text(0.5, 1.0, 'endpoint similarity of the edges') .. GENERATED FROM PYTHON SOURCE LINES 82-85 **Take-away.** Similar drivers (sensors) duplicate each other's work; similar edges mark redundant parts of the network, dissimilar ones the bridges that carry a unique share of the influence. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.210 seconds) .. _sphx_glr_download_auto_examples_05_measures_plot_03_similarity.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/nocad/main?urlpath=lab/tree/notebooks/auto_examples/05_measures/plot_03_similarity.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_03_similarity.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_03_similarity.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_03_similarity.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_