r"""
Biclusters of the manufacturing space
=====================================

Spectral co-clustering finds groups of resources that are used together by
groups of activities. Outlined on the seriated incidence matrix (activities in
rows, as in the figure of Nagy et al. 2022), the blocks correspond to the
transport (AGV) activities, the crimping stations and the assembly stations.
Vertex labels are colored by resource type.
"""

# %%
import matplotlib.pyplot as plt

import hatvis as hv
from hatvis.plotting import plot_incidence

H = hv.datasets.load_wire_harness()
# [snippet:blocks]
blocks = hv.seriation.bicluster_blocks(H, n_clusters=6, random_state=0, min_density=0.3)
for b in blocks:
    print(f"{len(b['nodes']):2d} resources x {len(b['edges']):2d} activities, density {b['density']:.2f}:",
          ", ".join(b["nodes"]))
order = hv.seriate(H, "correspondence")
# [/snippet:blocks]

# %%
fig, ax = plt.subplots(figsize=(7, 12))
plot_incidence(H, order=order, ax=ax, transpose=True, biclusters=blocks, node_groups="type", label_size=6)
plt.tight_layout()
