.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_measures/plot_01_network_measures.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_01_network_measures.py: System characterisation: network and node measures ================================================== The second module of NOCAD computes about fifty measures of a system from its state-space matrices: network-level values (size, density, diameter, degree centralization, assortativity, relative degree, the share of self-loops and of symmetric edges), node centralities (degrees, closeness, betweenness, PageRank, control and observe centrality) and the clustering and similarity measures of the drivers, sensors and edges. :func:`nocad.measures.measures_tables` returns them as pandas tables. For the nine-state example of the NOCAD manual the density, diameter, degree variance, Freeman centralization and share of symmetric edges agree with the values printed in the manual. .. GENERATED FROM PYTHON SOURCE LINES 17-19 The measures of the NOCAD example --------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 19-34 .. 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 measures_tables adj = load_nocad_example().adj # [snippet:tables] A, B, C, D = nocad.generate_matrices(adj) tables = measures_tables(A, B, C, D) print(tables.system) print(tables.nodes[["degree", "closeness", "betweenness", "pagerank", "control", "observe"]].round(3)) # [/snippet:tables] .. rst-class:: sphx-glr-script-out .. code-block:: none value controllable True observable True num_nodes 9 num_edges 15 density 0.185185 diameter 4.0 degree_freeman 0.428571 degree_variance 2.666667 degree_relative 4.0 r_in_in -0.363766 r_in_out -0.332531 r_out_out 0.28339 r_out_in 0.201945 percent_loops 0.0 percent_sym 40.0 degree closeness betweenness pagerank control observe x1 3 0.513 1.0 25.420 0 7 x2 5 0.700 9.0 26.463 0 0 x3 2 0.488 0.0 13.740 0 0 x4 6 0.894 9.0 6.075 0 0 x5 4 0.744 0.0 3.784 0 0 x6 4 0.719 7.0 5.904 0 0 x7 4 0.525 4.0 7.366 0 0 x8 1 0.364 0.0 2.493 9 0 x9 1 0.312 0.0 8.754 0 6 .. GENERATED FROM PYTHON SOURCE LINES 35-37 Node centralities side by side ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 37-52 .. code-block:: Python cols = ["degree_scott", "closeness", "betweenness", "pagerank"] vals = tables.nodes[cols].to_numpy(float) vals = vals / np.where(vals.max(axis=0) > 0, vals.max(axis=0), 1) fig, ax = plt.subplots(figsize=(6.4, 3.3)) x = np.arange(len(tables.nodes)) for k, c in enumerate(cols): ax.bar(x + (k - 1.5) * 0.2, vals[:, k], width=0.2, color=nocad.plotting.PALETTE[k], label=c.replace("_", " ")) ax.set_xticks(x) ax.set_xticklabels(tables.nodes.index) ax.set_ylabel("value / maximum") ax.set_title("node centralities of the NOCAD example (normalized)", fontsize=10) ax.legend(frameon=False, fontsize=7, ncol=4, loc="upper center", bbox_to_anchor=(0.5, -0.12)) ax.grid(True, axis="y", color="0.9") fig.tight_layout() .. image-sg:: /auto_examples/05_measures/images/sphx_glr_plot_01_network_measures_001.png :alt: node centralities of the NOCAD example (normalized) :srcset: /auto_examples/05_measures/images/sphx_glr_plot_01_network_measures_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 53-57 Parameter study: measures of growing random networks ---------------------------------------------------- Density, diameter and degree centralization of Erdős--Rényi networks with N = 100 against the mean degree: the diameter peaks where the giant component forms. .. GENERATED FROM PYTHON SOURCE LINES 57-81 .. code-block:: Python from nocad.datasets import make_random_network from nocad.distances import diameter from nocad.measures import degree_freeman, density degrees = np.linspace(0.5, 6, 12) rows = {"density x 10": [], "diameter / 10": [], "Freeman centralization": []} for k in degrees: v = {key: [] for key in rows} for s in range(3): a = make_random_network(100, k, random_state=s).adj v["density x 10"].append(10 * density(a)) v["diameter / 10"].append(diameter(a) / 10) v["Freeman centralization"].append(degree_freeman(a)) for key in rows: rows[key].append(np.mean(v[key])) fig, ax = plt.subplots(figsize=(5.2, 3.4)) for k, (name, y) in enumerate(rows.items()): ax.plot(degrees, y, marker="osD"[k], ms=4, color=nocad.plotting.PALETTE[k], label=name) ax.set_xlabel(r"mean degree $\langle k\rangle$") ax.set_title("network measures of random networks (N = 100)", fontsize=10) ax.grid(True, color="0.9") ax.legend(frameon=False, fontsize=8) fig.tight_layout() .. image-sg:: /auto_examples/05_measures/images/sphx_glr_plot_01_network_measures_002.png :alt: network measures of random networks (N = 100) :srcset: /auto_examples/05_measures/images/sphx_glr_plot_01_network_measures_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 82-85 **Take-away.** One call gives the measures that the NOCAD papers use to relate the structure of a network to the effort of controlling and observing it, in tables that are easy to compare across systems. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.237 seconds) .. _sphx_glr_download_auto_examples_05_measures_plot_01_network_measures.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_01_network_measures.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_network_measures.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_network_measures.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_network_measures.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_