r"""
Critical elements of the manufacturing space
============================================

Nagy et al. (2022) ranked the elements of the intelligent collaborative
manufacturing space by the :math:`s`-betweenness and :math:`s`-closeness of
the vertices, averaged over several :math:`s`. The same analysis on the
reconstructed benchmark identifies the sensors and the crimping competence as
the most central elements, and shows how the ranking changes from
:math:`s=1` to :math:`s=2`.
"""

# %%
import matplotlib.pyplot as plt

import hatvis as hv
from hatvis.plotting import plot_ranking

H = hv.datasets.load_wire_harness()
# [snippet:table]
table = hv.s_centrality_table(H, s_values=(1, 2, 3), edges=False)
avg_bc = hv.average_s_centrality(H, hv.s_betweenness, s_max=3, edges=False)
avg_hc = hv.average_s_centrality(H, hv.s_harmonic_closeness, s_max=3, edges=False)
# [/snippet:table]
print(table.sort_values("BC s=1", ascending=False).head(8).round(3))
print("rank of C7 by BC: s=1 ->", int(table["BC s=1"].rank(ascending=False)["C7"]),
      " s=2 ->", int(table["BC s=2"].rank(ascending=False)["C7"]))

# %%
fig, axes = plt.subplots(1, 3, figsize=(14, 4.6))
plot_ranking(table["BC s=1"], top=10, ax=axes[0], title="s-betweenness, s = 1")
plot_ranking(table["BC s=2"], top=10, ax=axes[1], title="s-betweenness, s = 2", color=hv.plotting.PALETTE[1])
plot_ranking(avg_hc, top=10, ax=axes[2], title="harmonic s-closeness, mean over s = 1..3",
             color=hv.plotting.PALETTE[2])
plt.tight_layout()
