r"""
The HAT-VIS pipeline step by step
=================================

HAT-VIS computes (1) the weighted Jaccard similarity of the vertices, (2) its
max-product transitive closure, (3) the dissimilarity
:math:`\boldsymbol{\Delta}=\mathbf{1}-\mathbf{S}` and (4) a classical MDS
configuration; hyperedges are drawn as minimum spanning trees of their
members. On the five-vertex example the transitive similarity of
:math:`v_1` and :math:`v_5` is :math:`1/3\cdot1/2=0.167`, as in the paper.
"""

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

import hatvis as hv
from hatvis.plotting import draw_hatvis

H = hv.datasets.hatvis_example()
# [snippet:steps]
S = hv.vertex_similarity(H)                         # weighted Jaccard
ST = hv.transitive_closure(S, "max-product")        # strongest chains
Delta = 1 - ST
Y = hv.layout.classical_mds(Delta)                  # 2-D configuration
# [/snippet:steps]
print(pd.DataFrame(S, H.nodes, H.nodes).round(3))
print(pd.DataFrame(ST, H.nodes, H.nodes).round(3))
print(np.round(Y, 3))

# %%
fig, axes = plt.subplots(1, 3, figsize=(13, 4))
for ax, M, t in ((axes[0], S, "Jaccard similarity"), (axes[1], ST, "transitive similarity")):
    im = ax.imshow(M, cmap=hv.plotting.matrix.BRAND_CMAP, vmin=0, vmax=1)
    ax.set_xticks(range(5), H.nodes)
    ax.set_yticks(range(5), H.nodes)
    for i in range(5):
        for j in range(5):
            ax.text(j, i, f"{M[i, j]:.2f}", ha="center", va="center", fontsize=8,
                    color="white" if M[i, j] > 0.6 else "black")
    ax.set_title(t)
draw_hatvis(H, ax=axes[2], title="HAT-VIS drawing", node_names=None)
plt.tight_layout()
