r"""
Topology representing networks
==============================

A topology representing network adds competitive Hebbian learning to neural
gas: whenever a sample is drawn, its two nearest nodes are linked. Links that
are not refreshed for a while die. The graph that survives connects nodes
whose Voronoi cells meet *inside* the data, so it captures the topology of the
manifold, here a 2-D sheet bent into an S and a rolled-up swiss roll. More
nodes give a finer approximation.
"""

import matplotlib.pyplot as plt

from graphclust import TopologyRepresentingNetwork
from graphclust.datasets import make_s_curve, make_swiss_roll
from graphclust.plotting import plot_graph

S, _ = make_s_curve(2000, random_state=0)
R, _ = make_swiss_roll(2000, random_state=0)

fig = plt.figure(figsize=(12, 4))
for i, (X, n, name) in enumerate([(S, 100, "S curve"), (S, 200, "S curve"), (R, 200, "swiss roll")]):
    # [snippet:trn]
    trn = TopologyRepresentingNetwork(n_nodes=n, random_state=0).fit(X)
    # [/snippet:trn]
    ax = fig.add_subplot(1, 3, i + 1, projection="3d")
    plot_graph(trn.nodes_, trn.adjacency_, ax=ax, node_size=6, lw=0.6)
    ax.set_title(f"TRN of the {name}, n = {n} ({trn.n_edges_} edges)", fontsize=10)
    ax.view_init(10, -70)
    ax.set_xticks([])
    ax.set_yticks([])
    ax.set_zticks([])
plt.tight_layout()
