r"""
Isomap, Isotop and curvilinear distance analysis
================================================

Three ways to map geodesic distances:

* Isomap embeds the graph distances of a kNN graph by classical MDS, a global
  eigenproblem;
* Isotop lets the nodes self-organize like a SOM whose neighborhood is
  measured along the data graph;
* CCA weights the distance errors by a function of the *map* distance so that
  only short distances are enforced; with geodesic input distances it becomes
  curvilinear distance analysis (CDA).

On the swiss roll Isomap and CDA unroll the sheet, while CCA with Euclidean
input distances tears it into pieces. Isotop unfolds most of the roll but its
stochastic self-organization can leave folds, which is why its schedules
need some care.
"""

import matplotlib.pyplot as plt

from graphclust import CurvilinearComponentAnalysis, Isomap, Isotop
from graphclust.datasets import make_swiss_roll
from graphclust.plotting import plot_embedding

X, t = make_swiss_roll(1500, random_state=0)

# [snippet:geomaps]
iso = Isomap(k=8).fit(X)
itp = Isotop(n_nodes=250, k=6, random_state=0).fit(X)
cca = CurvilinearComponentAnalysis(n_nodes=250, n_epochs=30, random_state=0).fit(X)
cda = CurvilinearComponentAnalysis(geodesic=True, k=6, n_nodes=250, n_epochs=30, random_state=0).fit(X)
# [/snippet:geomaps]

fig, axes = plt.subplots(1, 4, figsize=(14, 3.5))
plot_embedding(iso.embedding_, t, ax=axes[0], s=5, title="Isomap (samples)")
for ax, m, name in [(axes[1], itp, "Isotop"), (axes[2], cca, "CCA, Euclidean"), (axes[3], cda, "CDA, geodesic")]:
    plot_embedding(m.transform(X), t, ax=ax, s=5, title=f"{name} (nodes, samples at nearest node)")
plt.tight_layout()
