r"""
h-motifs and their significance
===============================

The 26 h-motifs (Lee et al. 2020) classify how three connected hyperedges
overlap. Counting them in the wire harness space and comparing with
degree- and size-preserving randomizations (configuration model) gives a
significance profile: patterns of nested and chained activities are
over-represented with respect to chance.
"""

# %%
import matplotlib.pyplot as plt

import hatvis as hv
from hatvis.plotting import plot_motif_profile

H = hv.datasets.load_wire_harness()
# [snippet:motifs]
cat = hv.motifs.h_motif_catalog()
z = hv.motifs.motif_zscores(H, hv.motifs.h_motifs, n_samples=30, random_state=0)
# [/snippet:motifs]
print(cat.head(3))
print(f"{(cat['closed']).sum()} closed and {(~cat['closed']).sum()} open h-motifs")
print(z[z["observed"] > 0].sort_values("z", ascending=False).head(6).round(2))

# %%
fig, ax = plt.subplots(figsize=(9, 3.4))
plot_motif_profile(z, ax=ax, title="h-motif z-scores, wire harness vs. configuration model")
plt.tight_layout()
