r"""
UMAP and t-SNE on handwritten digits
====================================

Neighbor-embedding methods build a weighted kNN graph and lay it out so that
graph neighbors attract and random pairs repel. t-SNE uses Gaussian
neighborhoods of fixed perplexity and heavy-tailed Student-t similarities in
the map; UMAP uses a fuzzy union of local neighborhoods and a tunable curve.
Both separate the ten digit classes far better than PCA or Isomap, at the
price of distorting large distances (the arrangement of the clusters relative
to each other is not reliable).
"""

import matplotlib.pyplot as plt
from sklearn.decomposition import PCA

from graphclust import Isomap, UMAPEmbedding, metrics, tsne
from graphclust.datasets import load_digits
from graphclust.plotting import plot_embedding

X, y = load_digits()

# [snippet:umap]
umap = UMAPEmbedding(k=15, min_dist=0.1, random_state=0).fit(X)
Y_tsne = tsne(X, perplexity=30, random_state=0)
# [/snippet:umap]
maps = {"PCA": PCA(2).fit_transform(X), "Isomap": Isomap(k=10).fit_transform(X), "t-SNE": Y_tsne, "UMAP": umap.embedding_}
fig, axes = plt.subplots(1, 4, figsize=(14, 3.6))
for ax, (name, Y) in zip(axes, maps.items()):
    T, C = metrics.trustworthiness_continuity(X, Y, 10)
    plot_embedding(Y, y, ax=ax, s=3, title=f"{name}: T(10) = {T:.3f}, C(10) = {C:.3f}")
    print(f"{name:7s} trust {T:.3f} cont {C:.3f} AUC {metrics.auc_rnx(X, Y):.3f}")
plt.tight_layout()
