File size: 7,496 Bytes
3ea658b
 
4a851e9
3ea658b
 
 
 
4a851e9
 
 
 
3ea658b
4a851e9
3ea658b
4a851e9
 
 
3ea658b
4a851e9
 
 
 
3ea658b
4a851e9
 
 
3ea658b
4a851e9
 
 
 
 
 
 
 
 
 
 
 
3ea658b
4a851e9
 
 
 
 
6988d0c
4a851e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea658b
4a851e9
 
 
 
 
 
 
 
 
 
 
3ea658b
 
4a851e9
3ea658b
4a851e9
 
3ea658b
4a851e9
3ea658b
4a851e9
 
3ea658b
 
4a851e9
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea658b
4a851e9
 
3ea658b
4a851e9
 
 
 
 
 
3ea658b
4a851e9
 
 
 
 
 
 
 
6988d0c
 
 
 
 
 
 
4a851e9
6988d0c
4a851e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ea658b
 
6988d0c
 
 
 
 
4a851e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import io
import textwrap
import itertools

import numpy as np
import pandas as pd
import streamlit as st
from sklearn.manifold import TSNE, trustworthiness
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans, DBSCAN
import umap.umap_ as umap
import plotly.express as px
from sklearn.datasets import make_swiss_roll

# --- Example shapes (some generated on demand) --------------------------------
def generate_hypercube(n=4):
    return np.array(list(itertools.product([0, 1], repeat=n)), dtype=float)

def generate_simplex(n=3):
    eye = np.eye(n, dtype=float)
    origin = np.zeros((1, n), dtype=float)
    return np.vstack([eye, origin])

def generate_swiss_roll(n_samples=500, noise=0.05):
    X, _ = make_swiss_roll(n_samples=n_samples, noise=noise)
    return X

EXAMPLE_SHAPES = {
    "Cube (3-D, 8 pts)": np.array([
        [0,0,0],[0,0,1],[0,1,0],[0,1,1],
        [1,0,0],[1,0,1],[1,1,0],[1,1,1]
    ], dtype=float),
    "Square pyramid (3-D, 5 pts)": np.array([
        [-1,-1,0],[1,-1,0],[1,1,0],[-1,1,0],[0,0,1]
    ], dtype=float),
    "4-D hypercube (16 pts)": generate_hypercube(4),
    "3-simplex (4 pts in 3-D)": generate_simplex(3),
    "Swiss roll (500 pts, 3-D)": generate_swiss_roll,
}

# --- Parsing & embedding -----------------------------------------------------
def parse_text_points(text: str) -> np.ndarray:
    txt = textwrap.dedent(text.strip())
    rows = [r for r in txt.splitlines() if r.strip()]
    data = [list(map(float, r.replace(",", " ").split())) for r in rows]
    return np.array(data, dtype=float)

def run_tsne(data, perp, seed):
    ts = TSNE(n_components=2, perplexity=perp, random_state=seed, init="pca")
    emb = ts.fit_transform(data)
    return emb, ts.kl_divergence_

def run_pca(data):
    pca = PCA(n_components=2)
    return pca.fit_transform(data), None

def run_umap(data, n_neighbors, min_dist, seed):
    um = umap.UMAP(n_components=2, n_neighbors=n_neighbors,
                  min_dist=min_dist, random_state=seed)
    return um.fit_transform(data), None

# --- Streamlit App -----------------------------------------------------------
st.set_page_config(layout="wide")
st.title("πŸŒ€ Dimensionality Reduction Explorer")
st.write("""
Upload or paste your n-D points, pick an algorithm (t-SNE/PCA/UMAP),
optionally cluster, and see the 2-D embedding.  
""")

# Sidebar ─────────────────────────────────────────────────────────────────────
with st.sidebar:
    st.header("1️⃣ Data Input")
    mode = st.radio("Source", ["Example shape","Upload CSV/TXT","Paste text"])
    if mode == "Example shape":
        key = st.selectbox("Choose example", list(EXAMPLE_SHAPES.keys()))
        src = EXAMPLE_SHAPES[key]
        data_raw = src() if callable(src) else src
    elif mode == "Upload CSV/TXT":
        up = st.file_uploader("Upload file", type=["csv","txt"])
        if up:
            txt = io.StringIO(up.getvalue().decode("utf-8")).read()
            data_raw = parse_text_points(txt)
        else:
            st.stop()
    else:
        placeholder = "e.g.\n0,0,0\n0,0,1\n0,1,0\n..."
        txt = st.text_area("Paste coordinates", height=200, placeholder=placeholder)
        if not txt.strip():
            st.stop()
        data_raw = parse_text_points(txt)

    st.header("2️⃣ Algorithm & Params")
    algo = st.selectbox("Method", ["t-SNE","PCA","UMAP"])
    seed = st.number_input("Random seed", value=42, step=1)

    if algo == "t-SNE":
        perp = st.slider("Perplexity", 5.0, 50.0, 30.0, 1.0)
    elif algo == "UMAP":
        neighbors = st.slider("n_neighbors", 5, 200, 15, 5)
        min_dist = st.slider("min_dist", 0.0, 0.99, 0.1, 0.01)

    st.header("3️⃣ Clustering (optional)")
    do_cluster = st.checkbox("Cluster embedding")
    if do_cluster:
        cluster_algo = st.selectbox("Algorithm", ["KMeans","DBSCAN"])
        if cluster_algo == "KMeans":
            n_clusters = st.slider("n_clusters", 2, 10, 3, 1)
        else:
            eps = st.slider("DBSCAN eps", 0.1, 5.0, 0.5, 0.1)

    st.markdown("---")
    run = st.button("Run & Visualize πŸš€")

# Main ────────────────────────────────────────────────────────────────────────
if run:
    pts = data_raw
    if pts.ndim != 2 or pts.shape[0] < 2:
        st.error("Need at least two points in an (n_pts Γ— n_dims) array.")
        st.stop()

    # run chosen reducer
    if algo == "t-SNE":
        emb, kl = run_tsne(pts, perp, seed)
    elif algo == "PCA":
        emb, kl = run_pca(pts)
    else:
        emb, kl = run_umap(pts, neighbors, min_dist, seed)

    # dynamic trustworthiness
    n_samples = pts.shape[0]
    k_max = (n_samples - 1) // 2
    if k_max >= 1:
        tw = trustworthiness(pts, emb, n_neighbors=k_max)
    else:
        tw = None

    # clustering & plotting
    df = pd.DataFrame(emb, columns=["x","y"])
    if do_cluster:
        if cluster_algo == "KMeans":
            labels = KMeans(n_clusters=n_clusters, random_state=seed).fit_predict(emb)
        else:
            labels = DBSCAN(eps=eps).fit_predict(emb)
        df["cluster"] = labels.astype(str)
        fig = px.scatter(df, x="x", y="y", color="cluster",
                         title=f"{algo} embedding with {cluster_algo}", width=700, height=500)
    else:
        fig = px.scatter(df, x="x", y="y",
                         title=f"{algo} embedding", width=700, height=500)

    fig.update_traces(marker=dict(size=8))
    fig.update_layout(margin=dict(l=20, r=20, t=40, b=20))

    # display
    st.subheader("2-D Embedding")
    st.plotly_chart(fig, use_container_width=True)

    if tw is not None:
        st.markdown(f"**Trustworthiness (k={k_max}):** {tw:.3f}")
    else:
        st.markdown("**Trustworthiness:** Not enough samples to compute (need β‰₯3 points).")

    if kl is not None:
        st.markdown(f"**t-SNE KL divergence:** {kl:.3f}")

    # download CSV
    csv = df.to_csv(index=False).encode("utf-8")
    st.download_button(
        "Download embedding as CSV",
        data=csv,
        file_name="embedding.csv",
        mime="text/csv"
    )

    with st.expander("Show original data"):
        st.write(pts)

    if algo == "t-SNE":
        with st.expander("🧠 How t-SNE works"):
            st.markdown(r"""
1. **High-D similarities**  
   Convert pairwise distances \(d_{ij}\) into conditional probabilities  
   \[
     p_{j|i} = \frac{\exp\!\bigl(-\|x_i - x_j\|^2 / 2\sigma_i^2\bigr)}
                     {\sum_{k\neq i}\exp\!\bigl(-\|x_i - x_k\|^2 / 2\sigma_i^2\bigr)}
   \]
   then symmetrize to \(p_{ij}=(p_{j|i}+p_{i|j})/2n\).

2. **Low-D affinities**  
   In 2-D we use a Student-t kernel:
   \[
     q_{ij} = \frac{\bigl(1 + \|y_i - y_j\|^2\bigr)^{-1}}
                  {\sum_{k\neq l}\bigl(1 + \|y_k - y_l\|^2\bigr)^{-1}}
   \]

3. **Minimize KL divergence**  
   Find \(\{y_i\}\) to minimize
   \[
     KL(P\|Q)
     = \sum_{i\neq j} p_{ij}\,\log\frac{p_{ij}}{q_{ij}}
   \]
   via gradient descentβ€”preserving local structure while pushing dissimilar points apart.

**Key parameter – perplexity**  
Controls each \(\sigma_i\) by solving  
\(\mathrm{Perp}(p_{i\cdot})=2^{-\sum_j p_{j|i}\log_2 p_{j|i}}\),  
intuitively setting an β€œeffective # neighbors” (5–50 typical).
            """)