r"""
A first hypergraph: incidence matrix, dual and degrees
======================================================

A hypergraph :math:`H=(V,E)` consists of vertices and hyperedges, where every
hyperedge is an arbitrary subset of the vertices. This script builds the
five-vertex example of the HAT-VIS paper, prints its incidence matrix
:math:`\mathbf{B}`, the dual hypergraph :math:`H^*` (incidence
:math:`\mathbf{B}^\mathsf{T}`) and the basic statistics, and draws the two
views side by side.
"""

# %%
import matplotlib.pyplot as plt

import hatvis as hv
from hatvis.plotting import draw_hypergraph, plot_incidence

# [snippet:build]
H = hv.Hypergraph({"e1": ["v1", "v2", "v3"], "e2": ["v1", "v2", "v4"], "e3": ["v3", "v4", "v5"]})
print(H.to_dataframe().astype(int))
print(H.degrees().to_dict(), H.edge_sizes().to_dict())
D = H.dual()
print(D.edge_dict())
# [/snippet:build]
print(H.summary())

# %%
# The incidence matrix (left) is a complete description; the node-link view
# (middle) draws every hyperedge as a padded convex hull around its members,
# here with the force-directed layout of HyperGodot; the dual (right) swaps
# the roles: the hyperedges become the vertices.
fig, axes = plt.subplots(1, 3, figsize=(13, 3.8), gridspec_kw={"width_ratios": [1, 1.3, 1.3]})
plot_incidence(H, order=None, ax=axes[0], title="incidence matrix")
draw_hypergraph(H, layout="force", ax=axes[1], title="H", legend=False, edge_labels=True, style="smooth")
draw_hypergraph(D, layout="force", ax=axes[2], title="dual H*", legend=False, edge_labels=True, style="smooth")
plt.tight_layout()
