Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,128 +0,0 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from sklearn.manifold import TSNE
|
| 7 |
-
from sklearn.decomposition import PCA
|
| 8 |
-
import plotly.express as px
|
| 9 |
-
|
| 10 |
-
# Load data
|
| 11 |
-
def load_data():
|
| 12 |
-
print("Loading data...")
|
| 13 |
-
data = torch.load("demo_data.pt")
|
| 14 |
-
records = []
|
| 15 |
-
for i, d in enumerate(data):
|
| 16 |
-
records.append({
|
| 17 |
-
"index": i,
|
| 18 |
-
"tech": d['tech'],
|
| 19 |
-
"snr": d['snr'],
|
| 20 |
-
"mod": d['mod'],
|
| 21 |
-
"mob": d['mob'],
|
| 22 |
-
"embedding": d['embedding'].numpy(),
|
| 23 |
-
"spectrogram": d['data'].numpy().flatten()
|
| 24 |
-
})
|
| 25 |
-
df = pd.DataFrame(records)
|
| 26 |
-
print(f"Loaded {len(df)} samples.")
|
| 27 |
-
return df
|
| 28 |
-
|
| 29 |
-
df = load_data()
|
| 30 |
-
|
| 31 |
-
# Get unique values for filters
|
| 32 |
-
tech_choices = sorted(list(df['tech'].unique()))
|
| 33 |
-
snr_choices = sorted(list(df['snr'].unique()))
|
| 34 |
-
mod_choices = sorted(list(df['mod'].unique()))
|
| 35 |
-
mob_choices = sorted(list(df['mob'].unique()))
|
| 36 |
-
|
| 37 |
-
def plot_tsne(tech_filter, snr_filter, mod_filter, mob_filter, representation, color_by, perplexity, n_iter):
|
| 38 |
-
# Filter data
|
| 39 |
-
filtered_df = df.copy()
|
| 40 |
-
|
| 41 |
-
if tech_filter and len(tech_filter) > 0:
|
| 42 |
-
filtered_df = filtered_df[filtered_df['tech'].isin(tech_filter)]
|
| 43 |
-
|
| 44 |
-
if snr_filter and len(snr_filter) > 0:
|
| 45 |
-
filtered_df = filtered_df[filtered_df['snr'].isin(snr_filter)]
|
| 46 |
-
|
| 47 |
-
if mod_filter and len(mod_filter) > 0:
|
| 48 |
-
filtered_df = filtered_df[filtered_df['mod'].isin(mod_filter)]
|
| 49 |
-
|
| 50 |
-
if mob_filter and len(mob_filter) > 0:
|
| 51 |
-
filtered_df = filtered_df[filtered_df['mob'].isin(mob_filter)]
|
| 52 |
-
|
| 53 |
-
if len(filtered_df) < 5:
|
| 54 |
-
return None, f"Not enough data points ({len(filtered_df)}). Need at least 5."
|
| 55 |
-
|
| 56 |
-
# Select features
|
| 57 |
-
if representation == "LWM Embedding":
|
| 58 |
-
features = np.stack(filtered_df['embedding'].values)
|
| 59 |
-
else:
|
| 60 |
-
features = np.stack(filtered_df['spectrogram'].values)
|
| 61 |
-
# PCA for raw spectrograms to speed up t-SNE
|
| 62 |
-
if features.shape[1] > 50:
|
| 63 |
-
pca = PCA(n_components=50, random_state=42)
|
| 64 |
-
features = pca.fit_transform(features)
|
| 65 |
-
|
| 66 |
-
# Run t-SNE
|
| 67 |
-
# Adjust perplexity if N is small
|
| 68 |
-
eff_perplexity = min(perplexity, len(filtered_df) - 1)
|
| 69 |
-
|
| 70 |
-
tsne = TSNE(n_components=2, perplexity=eff_perplexity, n_iter=n_iter, random_state=42, init='pca', learning_rate='auto')
|
| 71 |
-
projections = tsne.fit_transform(features)
|
| 72 |
-
|
| 73 |
-
filtered_df['x'] = projections[:, 0]
|
| 74 |
-
filtered_df['y'] = projections[:, 1]
|
| 75 |
-
|
| 76 |
-
# Plot
|
| 77 |
-
fig = px.scatter(
|
| 78 |
-
filtered_df,
|
| 79 |
-
x='x',
|
| 80 |
-
y='y',
|
| 81 |
-
color=color_by,
|
| 82 |
-
hover_data=['tech', 'snr', 'mod', 'mob'],
|
| 83 |
-
title=f"t-SNE of {representation} ({len(filtered_df)} samples)",
|
| 84 |
-
template="plotly_white"
|
| 85 |
-
)
|
| 86 |
-
fig.update_layout(legend_title_text=color_by.capitalize())
|
| 87 |
-
|
| 88 |
-
return fig, f"Displayed {len(filtered_df)} samples."
|
| 89 |
-
|
| 90 |
-
# UI
|
| 91 |
-
with gr.Blocks(title="LWM-Spectro Demo") as demo:
|
| 92 |
-
gr.Markdown("# 🔬 LWM-Spectro Interactive t-SNE Demo")
|
| 93 |
-
gr.Markdown("""
|
| 94 |
-
Compare **LWM embeddings** vs **Raw Spectrograms** for wireless signal classification.
|
| 95 |
-
|
| 96 |
-
* **LWM Embedding**: 128-dim vector from the pre-trained Large Wireless Model.
|
| 97 |
-
* **Raw Spectrogram**: Flattened 128x128 spectrogram (reduced via PCA before t-SNE).
|
| 98 |
-
""")
|
| 99 |
-
|
| 100 |
-
with gr.Row():
|
| 101 |
-
with gr.Column(scale=1, min_width=300):
|
| 102 |
-
gr.Markdown("### Filters")
|
| 103 |
-
tech_filter = gr.CheckboxGroup(choices=tech_choices, value=tech_choices, label="Technology")
|
| 104 |
-
snr_filter = gr.Dropdown(choices=snr_choices, value=None, multiselect=True, label="SNR (Empty = All)")
|
| 105 |
-
mod_filter = gr.Dropdown(choices=mod_choices, value=None, multiselect=True, label="Modulation (Empty = All)")
|
| 106 |
-
mob_filter = gr.Dropdown(choices=mob_choices, value=None, multiselect=True, label="Mobility (Empty = All)")
|
| 107 |
-
|
| 108 |
-
gr.Markdown("### Visualization Settings")
|
| 109 |
-
representation = gr.Radio(choices=["LWM Embedding", "Raw Spectrogram"], value="LWM Embedding", label="Representation")
|
| 110 |
-
color_by = gr.Dropdown(choices=["tech", "snr", "mod", "mob"], value="tech", label="Color By")
|
| 111 |
-
|
| 112 |
-
with gr.Accordion("Advanced t-SNE Settings", open=False):
|
| 113 |
-
perplexity = gr.Slider(minimum=5, maximum=50, value=30, step=1, label="Perplexity")
|
| 114 |
-
n_iter = gr.Slider(minimum=250, maximum=2000, value=1000, step=50, label="Iterations")
|
| 115 |
-
|
| 116 |
-
btn = gr.Button("Update Plot", variant="primary")
|
| 117 |
-
status = gr.Textbox(label="Status", interactive=False)
|
| 118 |
-
|
| 119 |
-
with gr.Column(scale=3):
|
| 120 |
-
plot = gr.Plot(label="t-SNE Visualization")
|
| 121 |
-
|
| 122 |
-
btn.click(plot_tsne, inputs=[tech_filter, snr_filter, mod_filter, mob_filter, representation, color_by, perplexity, n_iter], outputs=[plot, status])
|
| 123 |
-
|
| 124 |
-
# Initial load
|
| 125 |
-
demo.load(plot_tsne, inputs=[tech_filter, snr_filter, mod_filter, mob_filter, representation, color_by, perplexity, n_iter], outputs=[plot, status])
|
| 126 |
-
|
| 127 |
-
if __name__ == "__main__":
|
| 128 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|