Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files
app.py
CHANGED
|
@@ -114,11 +114,11 @@ def build_biplot_plotly(df, Z, loadings, feats, color_key, arrow_scale=2.0):
|
|
| 114 |
fig.add_annotation(x=x*1.05, y=y*1.05, text=FEATURE_LABELS.get(key,key),
|
| 115 |
showarrow=False, font=dict(size=10))
|
| 116 |
fig.update_layout(title="PCA-biplot (2D, hover)", xaxis_title="PC1", yaxis_title="PC2",
|
| 117 |
-
|
| 118 |
return fig
|
| 119 |
|
| 120 |
def build_biplot_matplotlib(df, Z, loadings, feats, color_key, arrow_scale=2.0, point_size=32, alpha=0.85):
|
| 121 |
-
fig = plt.figure(
|
| 122 |
ax = fig.add_subplot(111)
|
| 123 |
sc = ax.scatter(Z[:,0], Z[:,1], c=df[color_key].values, s=point_size, alpha=alpha)
|
| 124 |
cbar = plt.colorbar(sc, ax=ax, pad=0.02); cbar.set_label(f"Kleur: {FEATURE_LABELS.get(color_key,color_key)}")
|
|
@@ -136,11 +136,11 @@ def build_pca3d(Z3, color_vals):
|
|
| 136 |
marker=dict(size=4, color=color_vals, opacity=0.85))])
|
| 137 |
fig.update_layout(title="PCA 3D — PC1·PC2·PC3 (sleep om te draaien)",
|
| 138 |
scene=dict(xaxis_title="PC1", yaxis_title="PC2", zaxis_title="PC3"),
|
| 139 |
-
|
| 140 |
return fig
|
| 141 |
|
| 142 |
def build_variance_plot(expl):
|
| 143 |
-
fig = plt.figure(
|
| 144 |
ax = fig.add_subplot(111)
|
| 145 |
xs = np.arange(1, len(expl)+1)
|
| 146 |
ax.bar(xs, expl, width=0.8, align="center")
|
|
@@ -152,9 +152,9 @@ def build_variance_plot(expl):
|
|
| 152 |
|
| 153 |
def build_hist_box(df: pd.DataFrame, color_key: str):
|
| 154 |
series = df[color_key].dropna()
|
| 155 |
-
fig_hist = px.histogram(series, nbins=30, title=f"Histogram — {FEATURE_LABELS.get(color_key,color_key)}"
|
| 156 |
fig_hist.update_layout(margin=dict(l=10, r=10, t=40, b=10))
|
| 157 |
-
fig_box = px.box(series, points="outliers", title=f"Boxplot — {FEATURE_LABELS.get(color_key,color_key)}"
|
| 158 |
fig_box.update_layout(margin=dict(l=10, r=10, t=40, b=10))
|
| 159 |
return fig_hist, fig_box
|
| 160 |
|
|
@@ -212,7 +212,7 @@ def animate_pca(color_label="BMI (Body Mass Index)", point_size=32, alpha=0.85,
|
|
| 212 |
t = i / max(1, frames-1)
|
| 213 |
w1 = min(1.0, t * 2.0); w2 = max(0.0, (t - 0.5) * 2.0)
|
| 214 |
coords = np.column_stack([Z[:, 0] * w1, Z[:, 1] * w2])
|
| 215 |
-
fig = plt.figure(
|
| 216 |
ax = fig.add_subplot(111)
|
| 217 |
ax.scatter(coords[:, 0], coords[:, 1], c=color_vals, s=point_size, alpha=alpha)
|
| 218 |
ax.set_xlabel("PC1 (opbouw)"); ax.set_ylabel("PC2 (opbouw)")
|
|
@@ -269,7 +269,7 @@ with gr.Blocks(title="PCA Dashboard — Diabetes (netjes & compleet)") as demo:
|
|
| 269 |
gr.Markdown("### Animatie")
|
| 270 |
animate_btn = gr.Button("▶ Animate PCA (PC1 → PC2)")
|
| 271 |
gr.HTML('<div class="cta"><b>Klik op: "▶ Animate PCA"</b> om de projectie stap-voor-stap te zien.</div>')
|
| 272 |
-
anim_plot = gr.Plot(label="Animatie van projectie"
|
| 273 |
with gr.Group():
|
| 274 |
gr.Markdown("### Downloads")
|
| 275 |
dl_biplot = gr.DownloadButton("Download biplot (PNG)")
|
|
@@ -279,24 +279,24 @@ with gr.Blocks(title="PCA Dashboard — Diabetes (netjes & compleet)") as demo:
|
|
| 279 |
with gr.Row():
|
| 280 |
with gr.Column():
|
| 281 |
gr.Markdown("### Biplot (2D, hover)")
|
| 282 |
-
plot_biplot = gr.Plot(
|
| 283 |
with gr.Column():
|
| 284 |
gr.Markdown("### 3D PCA (PC1–PC3)")
|
| 285 |
-
plot3d = gr.Plot(
|
| 286 |
with gr.Row():
|
| 287 |
with gr.Column():
|
| 288 |
gr.Markdown("### Uitlegvariantie")
|
| 289 |
-
plot_expl = gr.Plot(
|
| 290 |
with gr.Column():
|
| 291 |
gr.Markdown("### Top-features (PC1 / PC2)")
|
| 292 |
-
table = gr.Dataframe(headers=["Feature (PC1)", "Loading PC1", "Feature (PC2)", "Loading PC2"], row_count=6
|
| 293 |
with gr.Row():
|
| 294 |
with gr.Column():
|
| 295 |
gr.Markdown("### Histogram")
|
| 296 |
-
plot_hist = gr.Plot(
|
| 297 |
with gr.Column():
|
| 298 |
gr.Markdown("### Boxplot")
|
| 299 |
-
plot_box = gr.Plot(
|
| 300 |
with gr.Row():
|
| 301 |
with gr.Column():
|
| 302 |
gr.Markdown("### Overzicht (gemiddelden & verdeling)")
|
|
@@ -304,12 +304,13 @@ with gr.Blocks(title="PCA Dashboard — Diabetes (netjes & compleet)") as demo:
|
|
| 304 |
with gr.Column():
|
| 305 |
gr.Markdown("### Samenvatting")
|
| 306 |
summary = gr.Markdown()
|
|
|
|
| 307 |
|
| 308 |
inputs = [color_feat, n_components, standardize, arrow_scale]
|
| 309 |
run_btn.click(fn=controller, inputs=inputs,
|
| 310 |
-
outputs=[plot_biplot, plot3d, plot_expl, table, overview_tbl,
|
| 311 |
demo.load(fn=controller, inputs=inputs,
|
| 312 |
-
outputs=[plot_biplot, plot3d, plot_expl, table, overview_tbl,
|
| 313 |
|
| 314 |
animate_btn.click(fn=animate_pca, inputs=[color_feat], outputs=anim_plot)
|
| 315 |
|
|
|
|
| 114 |
fig.add_annotation(x=x*1.05, y=y*1.05, text=FEATURE_LABELS.get(key,key),
|
| 115 |
showarrow=False, font=dict(size=10))
|
| 116 |
fig.update_layout(title="PCA-biplot (2D, hover)", xaxis_title="PC1", yaxis_title="PC2",
|
| 117 |
+
margin=dict(l=10, r=10, t=40, b=10))
|
| 118 |
return fig
|
| 119 |
|
| 120 |
def build_biplot_matplotlib(df, Z, loadings, feats, color_key, arrow_scale=2.0, point_size=32, alpha=0.85):
|
| 121 |
+
fig = plt.figure()
|
| 122 |
ax = fig.add_subplot(111)
|
| 123 |
sc = ax.scatter(Z[:,0], Z[:,1], c=df[color_key].values, s=point_size, alpha=alpha)
|
| 124 |
cbar = plt.colorbar(sc, ax=ax, pad=0.02); cbar.set_label(f"Kleur: {FEATURE_LABELS.get(color_key,color_key)}")
|
|
|
|
| 136 |
marker=dict(size=4, color=color_vals, opacity=0.85))])
|
| 137 |
fig.update_layout(title="PCA 3D — PC1·PC2·PC3 (sleep om te draaien)",
|
| 138 |
scene=dict(xaxis_title="PC1", yaxis_title="PC2", zaxis_title="PC3"),
|
| 139 |
+
margin=dict(l=10, r=10, t=40, b=10))
|
| 140 |
return fig
|
| 141 |
|
| 142 |
def build_variance_plot(expl):
|
| 143 |
+
fig = plt.figure()
|
| 144 |
ax = fig.add_subplot(111)
|
| 145 |
xs = np.arange(1, len(expl)+1)
|
| 146 |
ax.bar(xs, expl, width=0.8, align="center")
|
|
|
|
| 152 |
|
| 153 |
def build_hist_box(df: pd.DataFrame, color_key: str):
|
| 154 |
series = df[color_key].dropna()
|
| 155 |
+
fig_hist = px.histogram(series, nbins=30, title=f"Histogram — {FEATURE_LABELS.get(color_key,color_key)}")
|
| 156 |
fig_hist.update_layout(margin=dict(l=10, r=10, t=40, b=10))
|
| 157 |
+
fig_box = px.box(series, points="outliers", title=f"Boxplot — {FEATURE_LABELS.get(color_key,color_key)}")
|
| 158 |
fig_box.update_layout(margin=dict(l=10, r=10, t=40, b=10))
|
| 159 |
return fig_hist, fig_box
|
| 160 |
|
|
|
|
| 212 |
t = i / max(1, frames-1)
|
| 213 |
w1 = min(1.0, t * 2.0); w2 = max(0.0, (t - 0.5) * 2.0)
|
| 214 |
coords = np.column_stack([Z[:, 0] * w1, Z[:, 1] * w2])
|
| 215 |
+
fig = plt.figure()
|
| 216 |
ax = fig.add_subplot(111)
|
| 217 |
ax.scatter(coords[:, 0], coords[:, 1], c=color_vals, s=point_size, alpha=alpha)
|
| 218 |
ax.set_xlabel("PC1 (opbouw)"); ax.set_ylabel("PC2 (opbouw)")
|
|
|
|
| 269 |
gr.Markdown("### Animatie")
|
| 270 |
animate_btn = gr.Button("▶ Animate PCA (PC1 → PC2)")
|
| 271 |
gr.HTML('<div class="cta"><b>Klik op: "▶ Animate PCA"</b> om de projectie stap-voor-stap te zien.</div>')
|
| 272 |
+
anim_plot = gr.Plot(label="Animatie van projectie")
|
| 273 |
with gr.Group():
|
| 274 |
gr.Markdown("### Downloads")
|
| 275 |
dl_biplot = gr.DownloadButton("Download biplot (PNG)")
|
|
|
|
| 279 |
with gr.Row():
|
| 280 |
with gr.Column():
|
| 281 |
gr.Markdown("### Biplot (2D, hover)")
|
| 282 |
+
plot_biplot = gr.Plot()
|
| 283 |
with gr.Column():
|
| 284 |
gr.Markdown("### 3D PCA (PC1–PC3)")
|
| 285 |
+
plot3d = gr.Plot()
|
| 286 |
with gr.Row():
|
| 287 |
with gr.Column():
|
| 288 |
gr.Markdown("### Uitlegvariantie")
|
| 289 |
+
plot_expl = gr.Plot()
|
| 290 |
with gr.Column():
|
| 291 |
gr.Markdown("### Top-features (PC1 / PC2)")
|
| 292 |
+
table = gr.Dataframe(headers=["Feature (PC1)", "Loading PC1", "Feature (PC2)", "Loading PC2"], row_count=6)
|
| 293 |
with gr.Row():
|
| 294 |
with gr.Column():
|
| 295 |
gr.Markdown("### Histogram")
|
| 296 |
+
plot_hist = gr.Plot()
|
| 297 |
with gr.Column():
|
| 298 |
gr.Markdown("### Boxplot")
|
| 299 |
+
plot_box = gr.Plot()
|
| 300 |
with gr.Row():
|
| 301 |
with gr.Column():
|
| 302 |
gr.Markdown("### Overzicht (gemiddelden & verdeling)")
|
|
|
|
| 304 |
with gr.Column():
|
| 305 |
gr.Markdown("### Samenvatting")
|
| 306 |
summary = gr.Markdown()
|
| 307 |
+
overview_note_md = gr.Markdown()
|
| 308 |
|
| 309 |
inputs = [color_feat, n_components, standardize, arrow_scale]
|
| 310 |
run_btn.click(fn=controller, inputs=inputs,
|
| 311 |
+
outputs=[plot_biplot, plot3d, plot_expl, table, overview_tbl, overview_note_md, summary])
|
| 312 |
demo.load(fn=controller, inputs=inputs,
|
| 313 |
+
outputs=[plot_biplot, plot3d, plot_expl, table, overview_tbl, overview_note_md, summary])
|
| 314 |
|
| 315 |
animate_btn.click(fn=animate_pca, inputs=[color_feat], outputs=anim_plot)
|
| 316 |
|