r"""
What the clique expansion forgets
=================================

Two different hypergraphs can have the same clique expansion: a single
three-vertex hyperedge and a triangle of three pairwise hyperedges both
project to the same triangle graph. The star expansion (bipartite graph) and
the incidence matrix keep the difference; conformality tells whether a
hypergraph can be recovered from its clique expansion.
"""

# %%
import matplotlib.pyplot as plt
import networkx as nx

import hatvis as hv
from hatvis.plotting import draw_bipartite, draw_clique, draw_hypergraph

# [snippet:compare]
H1 = hv.Hypergraph({"group": ["a", "b", "c"]}, name="one 3-way interaction")
H2 = hv.Hypergraph({"ab": ["a", "b"], "bc": ["b", "c"], "ac": ["a", "c"]}, name="three pairwise interactions")
G1, G2 = hv.clique_expansion(H1), hv.clique_expansion(H2)
print("same clique expansion:", set(map(frozenset, G1.edges())) == set(map(frozenset, G2.edges())))
print("same star expansion:  ", nx.is_isomorphic(hv.star_expansion(H1), hv.star_expansion(H2)))
print("conformal:", hv.problems.is_conformal(H1), hv.problems.is_conformal(H2))
# [/snippet:compare]

# %%
pos = {"a": (0, 0), "b": (1, 0), "c": (0.5, 0.85)}
fig, axes = plt.subplots(2, 3, figsize=(11, 6.5))
for row, H in enumerate((H1, H2)):
    draw_hypergraph(H, pos=pos, ax=axes[row, 0], legend=False, title=H.name, pad=0.06)
    draw_clique(H, pos=pos, ax=axes[row, 1], title="clique expansion")
    draw_bipartite(H, ax=axes[row, 2], title="star expansion", random_state=1)
plt.tight_layout()
