r"""
A family of hypergraph centralities
===================================

Degree, clique-expansion eigenvector (CEC), Z- and H-eigenvector (ZEC, HEC)
centralities (Benson 2019), the nonlinear node–edge centralities of Tudisco
and Higham (2021), PageRank and hyper-coreness (Mancastroppa et al. 2023)
rank the vertices of a 3-uniform hypergraph. Their Spearman correlations show
where they agree and where the higher-order definitions differ.
"""

# %%
import matplotlib.pyplot as plt
import pandas as pd

import hatvis as hv

H = hv.models.chung_lu({f"v{i}": max(1, 30 // (i + 1)) for i in range(40)}, [3] * 70, random_state=0)
H = H.remove_isolated()
# [snippet:family]
C = pd.DataFrame({
    "degree": hv.degree_centrality(H),
    "CEC": hv.clique_eigenvector(H),
    "ZEC": hv.z_eigenvector(H),
    "HEC": hv.h_eigenvector(H),
    "linear NEC": hv.node_edge_eigenvector(H, "linear")[0],
    "max NEC": hv.node_edge_eigenvector(H, "max")[0],
    "PageRank": hv.pagerank(H),
    "hyper-coreness": hv.hypercoreness(H),
})
corr = C.corr(method="spearman")
# [/snippet:family]
print(corr.round(2).to_string())

# %%
fig, ax = plt.subplots(figsize=(6.5, 5.5))
im = ax.imshow(corr, cmap="RdBu_r", vmin=-1, vmax=1)
ax.set_xticks(range(len(corr)), corr.columns, rotation=60, ha="right")
ax.set_yticks(range(len(corr)), corr.columns)
for i in range(len(corr)):
    for j in range(len(corr)):
        ax.text(j, i, f"{corr.iloc[i, j]:.2f}", ha="center", va="center", fontsize=7)
plt.colorbar(im, ax=ax, fraction=0.046, label="Spearman correlation")
plt.tight_layout()
