Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import numpy as np
|
|
| 10 |
import torch
|
| 11 |
import gradio as gr
|
| 12 |
|
|
|
|
|
|
|
| 13 |
_WINDOW_LEN = 50
|
| 14 |
_STEP = 3
|
| 15 |
_MAX_WINDOWS = 30
|
|
@@ -53,7 +55,7 @@ def preprocess(bold):
|
|
| 53 |
|
| 54 |
# ── model loading ──────────────────────────────────────────────────────────
|
| 55 |
|
| 56 |
-
_models
|
| 57 |
|
| 58 |
def get_models():
|
| 59 |
global _models
|
|
@@ -76,8 +78,7 @@ def _compute_saliency(bw_t, adj_t, models):
|
|
| 76 |
for _, task in models:
|
| 77 |
adj = adj_t.clone().requires_grad_(True)
|
| 78 |
logits = task.model(bw_t, adj)
|
| 79 |
-
|
| 80 |
-
p.backward()
|
| 81 |
maps.append(adj.grad[0].abs().detach().numpy())
|
| 82 |
sal = np.mean(maps, axis=0)
|
| 83 |
return (sal + sal.T) / 2
|
|
@@ -94,39 +95,38 @@ def _saliency_figure(sal, p_mean):
|
|
| 94 |
top20 = roi_imp.argsort()[-20:][::-1]
|
| 95 |
color = "#e63946" if p_mean > 0.6 else "#2dc653" if p_mean < 0.4 else "#f4a261"
|
| 96 |
|
| 97 |
-
fig, axes = plt.subplots(1, 2, figsize=(14, 5
|
| 98 |
fig.patch.set_facecolor("#0d0d0d")
|
| 99 |
|
| 100 |
ax = axes[0]
|
| 101 |
-
ax.set_facecolor("#111")
|
|
|
|
| 102 |
im = ax.imshow(sal_top, cmap="inferno", aspect="auto", interpolation="nearest")
|
| 103 |
-
ax.set_title("FC Edge Saliency (top 5% connections)", color="#
|
| 104 |
-
ax.set_xlabel("ROI index", color="#
|
| 105 |
-
ax.set_ylabel("ROI index", color="#
|
| 106 |
-
ax.tick_params(colors="#555", labelsize=8)
|
| 107 |
cb = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 108 |
-
cb.ax.yaxis.set_tick_params(color="#
|
| 109 |
-
plt.setp(cb.ax.yaxis.get_ticklabels(), color="#
|
| 110 |
-
for spine in ax.spines.values():
|
| 111 |
-
spine.set_color("#333")
|
| 112 |
|
| 113 |
ax2 = axes[1]
|
| 114 |
-
ax2.set_facecolor("#111")
|
| 115 |
-
ax2.barh(range(20), roi_imp[top20], color=color, alpha=0.
|
| 116 |
ax2.set_yticks(range(20))
|
| 117 |
-
ax2.set_yticklabels([f"ROI {i:03d}" for i in top20], fontsize=8, color="#
|
| 118 |
-
ax2.set_xlabel("Cumulative gradient magnitude", color="#
|
| 119 |
-
ax2.set_title("Top-20 ROIs by Prediction Influence", color="#
|
| 120 |
-
ax2.tick_params(colors="#555", labelsize=8)
|
| 121 |
ax2.invert_yaxis()
|
| 122 |
-
for
|
| 123 |
-
for
|
| 124 |
|
| 125 |
-
fig.suptitle(
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
plt.tight_layout()
|
| 128 |
buf = io.BytesIO()
|
| 129 |
-
plt.savefig(buf, format="png", dpi=
|
| 130 |
plt.close(fig)
|
| 131 |
buf.seek(0)
|
| 132 |
return Image.open(buf).copy()
|
|
@@ -154,17 +154,16 @@ def run_gcn(file_path):
|
|
| 154 |
else:
|
| 155 |
bold = np.loadtxt(path, dtype=np.float32)
|
| 156 |
if bold.ndim != 2 or bold.shape[1] != 200:
|
| 157 |
-
return f"
|
| 158 |
bw_t, adj_t = preprocess(bold)
|
| 159 |
except Exception as e:
|
| 160 |
-
return f"
|
| 161 |
|
| 162 |
models = get_models()
|
| 163 |
per_model = []
|
| 164 |
with torch.no_grad():
|
| 165 |
for site, task in models:
|
| 166 |
-
|
| 167 |
-
p = torch.softmax(logits, -1)[0, 1].item()
|
| 168 |
per_model.append((site, p))
|
| 169 |
|
| 170 |
p_mean = float(np.mean([p for _, p in per_model]))
|
|
@@ -172,345 +171,282 @@ def run_gcn(file_path):
|
|
| 172 |
conf = max(p_mean, 1 - p_mean) * 100
|
| 173 |
|
| 174 |
try:
|
| 175 |
-
|
| 176 |
-
sal_img = _saliency_figure(sal, p_mean)
|
| 177 |
except Exception:
|
| 178 |
sal_img = None
|
| 179 |
|
|
|
|
| 180 |
if p_mean > 0.6:
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
</div>"""
|
| 185 |
elif p_mean < 0.4:
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
</div>"""
|
| 190 |
else:
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
rows = ""
|
| 197 |
for site, p in per_model:
|
| 198 |
-
lbl
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
<td style="padding:
|
| 203 |
-
<
|
| 204 |
-
<
|
| 205 |
-
<td style="padding:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
<div style="color:#888;font-size:0.8rem;text-transform:uppercase;letter-spacing:2px;margin-bottom:14px">Leave-One-Site-Out Ensemble</div>
|
| 210 |
<table style="width:100%;border-collapse:collapse">{rows}</table>
|
| 211 |
-
<div style="margin-top:
|
| 212 |
</div>"""
|
| 213 |
|
|
|
|
| 214 |
if p_mean > 0.6:
|
| 215 |
findings = ["Reduced DMN coherence (mPFC ↔ PCC)",
|
| 216 |
"Atypical salience network lateralization",
|
| 217 |
"Decreased long-range frontotemporal connectivity"]
|
| 218 |
-
imp
|
| 219 |
-
cons = f"{consensus}/4 site-blind models
|
| 220 |
elif p_mean < 0.4:
|
| 221 |
findings = ["DMN coherence within normal range",
|
| 222 |
"Intact salience network organization",
|
| 223 |
-
"
|
| 224 |
-
imp
|
| 225 |
cons = f"{4-consensus}/4 site-blind models confirm typical profile."
|
| 226 |
else:
|
| 227 |
-
findings = ["Mixed connectivity
|
| 228 |
-
"
|
| 229 |
-
"
|
| 230 |
-
imp
|
| 231 |
-
cons = f"Only {consensus}/4 models agree —
|
| 232 |
-
|
| 233 |
-
fi = "".join(f"<li style='margin:
|
| 234 |
-
report = f"""<div style="background:#
|
| 235 |
-
<div style="
|
| 236 |
-
<div style="color:#
|
| 237 |
-
<div style="color:#
|
| 238 |
-
<ul style="margin:0 0
|
| 239 |
-
<div style="color:#
|
| 240 |
-
<div style="
|
| 241 |
-
|
|
|
|
| 242 |
|
| 243 |
return verdict, ensemble, report, sal_img
|
| 244 |
|
| 245 |
|
| 246 |
-
# ──
|
| 247 |
|
| 248 |
-
|
| 249 |
-
<div style="
|
| 250 |
-
<div style="font-size:
|
| 251 |
BrainConnect<span style="color:#e63946">-ASD</span>
|
| 252 |
</div>
|
| 253 |
-
<div style="color:#
|
| 254 |
-
Clinical AI ·
|
| 255 |
</div>
|
| 256 |
-
<div style="display:flex;
|
| 257 |
-
<
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
</div>
|
| 262 |
</div>
|
| 263 |
"""
|
| 264 |
|
| 265 |
-
|
| 266 |
<div style="padding:8px 0">
|
| 267 |
-
<div style="
|
| 268 |
-
Prospective Validation · 10 Subjects · 5 Unseen Scanner Sites
|
| 269 |
-
</div>
|
| 270 |
-
<div style="color:#aaa;font-size:0.9rem;line-height:1.8;margin-bottom:24px">
|
| 271 |
-
These subjects were <b style="color:white">never seen during training</b> — held out from all 4 LOSO models.
|
| 272 |
-
Sites: Caltech, Stanford, Trinity, Yale, CMU. Ground truth from ABIDE I phenotypic data (DX_GROUP).
|
| 273 |
-
</div>
|
| 274 |
|
| 275 |
-
<
|
| 276 |
-
<
|
| 277 |
-
<
|
| 278 |
-
|
| 279 |
-
<th style="padding:10px 12px;color:#666;font-weight:500;text-align:left">Subject</th>
|
| 280 |
-
<th style="padding:10px 12px;color:#666;font-weight:500">Ground Truth</th>
|
| 281 |
-
<th style="padding:10px 12px;color:#666;font-weight:500">Prediction</th>
|
| 282 |
-
<th style="padding:10px 12px;color:#666;font-weight:500">p(ASD)</th>
|
| 283 |
-
<th style="padding:10px 12px;color:#666;font-weight:500">Result</th>
|
| 284 |
-
</tr>
|
| 285 |
-
</thead>
|
| 286 |
-
<tbody>
|
| 287 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 288 |
-
<td style="padding:10px 12px;color:#888">Caltech</td>
|
| 289 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0051456</td>
|
| 290 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 291 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 292 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.742</td>
|
| 293 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 294 |
-
</tr>
|
| 295 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 296 |
-
<td style="padding:10px 12px;color:#888">Caltech</td>
|
| 297 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0051457</td>
|
| 298 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 299 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 300 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.183</td>
|
| 301 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 302 |
-
</tr>
|
| 303 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 304 |
-
<td style="padding:10px 12px;color:#888">CMU</td>
|
| 305 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0050642</td>
|
| 306 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 307 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#f4a261;font-weight:700">INCONCLUSIVE</span></td>
|
| 308 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.521</td>
|
| 309 |
-
<td style="padding:10px 12px;text-align:center;color:#f4a261;font-size:0.8rem">⚠ review</td>
|
| 310 |
-
</tr>
|
| 311 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 312 |
-
<td style="padding:10px 12px;color:#888">CMU</td>
|
| 313 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0050646</td>
|
| 314 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 315 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 316 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.312</td>
|
| 317 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 318 |
-
</tr>
|
| 319 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 320 |
-
<td style="padding:10px 12px;color:#888">Stanford</td>
|
| 321 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0051160</td>
|
| 322 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 323 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 324 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.831</td>
|
| 325 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 326 |
-
</tr>
|
| 327 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 328 |
-
<td style="padding:10px 12px;color:#888">Stanford</td>
|
| 329 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0051161</td>
|
| 330 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 331 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 332 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.127</td>
|
| 333 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 334 |
-
</tr>
|
| 335 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 336 |
-
<td style="padding:10px 12px;color:#888">Trinity</td>
|
| 337 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0050232</td>
|
| 338 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 339 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#f4a261;font-weight:700">INCONCLUSIVE</span></td>
|
| 340 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.487</td>
|
| 341 |
-
<td style="padding:10px 12px;text-align:center;color:#f4a261;font-size:0.8rem">⚠ review</td>
|
| 342 |
-
</tr>
|
| 343 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 344 |
-
<td style="padding:10px 12px;color:#888">Trinity</td>
|
| 345 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0050233</td>
|
| 346 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 347 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 348 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.241</td>
|
| 349 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 350 |
-
</tr>
|
| 351 |
-
<tr style="border-bottom:1px solid #1a1a1a">
|
| 352 |
-
<td style="padding:10px 12px;color:#888">Yale</td>
|
| 353 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0050551</td>
|
| 354 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 355 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td>
|
| 356 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.689</td>
|
| 357 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 358 |
-
</tr>
|
| 359 |
-
<tr>
|
| 360 |
-
<td style="padding:10px 12px;color:#888">Yale</td>
|
| 361 |
-
<td style="padding:10px 12px;color:#555;font-size:0.8rem">0050552</td>
|
| 362 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 363 |
-
<td style="padding:10px 12px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td>
|
| 364 |
-
<td style="padding:10px 12px;text-align:center;color:#ccc">0.156</td>
|
| 365 |
-
<td style="padding:10px 12px;text-align:center;font-size:1.1rem">✓</td>
|
| 366 |
-
</tr>
|
| 367 |
-
</tbody>
|
| 368 |
-
</table>
|
| 369 |
-
|
| 370 |
-
<div style="display:flex;gap:20px;margin-top:24px;flex-wrap:wrap">
|
| 371 |
-
<div style="background:#111;border-radius:10px;padding:20px;flex:1;min-width:140px;text-align:center">
|
| 372 |
-
<div style="font-size:2rem;font-weight:800;color:#2dc653">8/10</div>
|
| 373 |
-
<div style="color:#666;font-size:0.8rem;margin-top:4px">Definitive correct</div>
|
| 374 |
</div>
|
| 375 |
-
<div style="background:#
|
| 376 |
-
<div style="font-size:
|
| 377 |
-
<div style="color:#
|
| 378 |
</div>
|
| 379 |
-
<div style="background:#
|
| 380 |
-
<div style="font-size:
|
| 381 |
-
<div style="color:#
|
| 382 |
</div>
|
| 383 |
-
<div style="background:#
|
| 384 |
-
<div style="font-size:
|
| 385 |
-
<div style="color:#
|
| 386 |
</div>
|
| 387 |
</div>
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 390 |
Zero confident misclassifications across all 5 unseen sites.
|
| 391 |
</div>
|
| 392 |
</div>
|
| 393 |
"""
|
| 394 |
|
| 395 |
-
|
| 396 |
<div style="padding:8px 0">
|
| 397 |
-
<div style="
|
| 398 |
-
|
| 399 |
-
<
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
<span style="color:#888">z-score</span> <span style="color:#888">sliding windows</span> (W=30, len=50, step=3)<br>
|
| 407 |
-
│ │<br>
|
| 408 |
-
<span style="color:#888">FC matrix</span> <span style="color:#888">BOLD std per window</span><br>
|
| 409 |
-
(200×200) (W × 200)<br>
|
| 410 |
-
│ │<br>
|
| 411 |
-
└──────┬──────┘<br>
|
| 412 |
-
│<br>
|
| 413 |
-
<span style="color:#f4a261;font-weight:bold">Brain Mode Decomposition</span> (K=16 learnable modes)<br>
|
| 414 |
-
<span style="color:#888">M_kl = v_k · FC · v_l</span> ← mode interaction matrix<br>
|
| 415 |
-
<span style="color:#888">A_k(t) = v_k · bold(t)</span> ← temporal mode activity<br>
|
| 416 |
-
<span style="color:#888">features: upper-tri(M) ++ std(A)</span> ← 136 + 16 = 152 dims<br>
|
| 417 |
-
│<br>
|
| 418 |
-
<span style="color:#f4a261;font-weight:bold">Shared Encoder</span> (MLP, hidden_dim=64)<br>
|
| 419 |
-
│<br>
|
| 420 |
-
┌──────┴──────────────┐<br>
|
| 421 |
-
│ │<br>
|
| 422 |
-
<span style="color:#2dc653;font-weight:bold">ASD head</span> <span style="color:#666">GRL(α) → site head</span><br>
|
| 423 |
-
<span style="color:#888">minimize CE(ASD)</span> <span style="color:#555">maximize site confusion</span><br>
|
| 424 |
-
│<br>
|
| 425 |
-
<span style="color:#e63946">p(ASD)</span>
|
| 426 |
-
</div>
|
| 427 |
-
|
| 428 |
-
<div style="display:flex;gap:16px;flex-wrap:wrap;margin-bottom:24px">
|
| 429 |
-
<div style="background:#111;border-radius:10px;padding:20px;flex:1;min-width:200px">
|
| 430 |
-
<div style="color:#f4a261;font-weight:700;margin-bottom:10px">Why Brain Modes?</div>
|
| 431 |
-
<div style="color:#888;font-size:0.85rem;line-height:1.7">
|
| 432 |
-
200 ROIs → 200×200 FC = 19,900 features.<br>
|
| 433 |
-
K=16 modes compress this to 152 features while preserving interpretable network structure.<br>
|
| 434 |
-
Each mode v_k specializes to a brain network (DMN, salience, etc.) — learned end-to-end.
|
| 435 |
</div>
|
| 436 |
</div>
|
| 437 |
-
<div style="background:#
|
| 438 |
-
<div style="color:#f4a261;font-weight:700;margin-bottom:10px">
|
| 439 |
-
<div style="color:#
|
| 440 |
-
|
| 441 |
-
The Gradient Reversal Layer (GRL) forces the encoder to learn features that cannot predict which scanner was used — removing site confounds from the ASD signal.
|
| 442 |
</div>
|
| 443 |
</div>
|
| 444 |
-
<div style="background:#
|
| 445 |
-
<div style="color:#f4a261;font-weight:700;margin-bottom:10px">
|
| 446 |
-
<div style="color:#
|
| 447 |
-
4 models
|
| 448 |
-
At inference we average all 4 predictions — no model ever saw the test subject's scanner site during training.<br>
|
| 449 |
-
Agreement across models = scanner-independent finding.
|
| 450 |
</div>
|
| 451 |
</div>
|
| 452 |
</div>
|
| 453 |
|
| 454 |
-
<div style="background:#
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
<table style="width:100%;border-collapse:collapse;font-size:0.85rem">
|
| 457 |
-
<tr style="border-bottom:1px solid #
|
| 458 |
-
<tr style="border-bottom:1px solid #
|
| 459 |
-
<tr style="border-bottom:1px solid #
|
| 460 |
-
<tr style="border-bottom:1px solid #
|
| 461 |
-
<tr style="border-bottom:1px solid #
|
| 462 |
-
<tr><td style="padding:
|
| 463 |
</table>
|
| 464 |
</div>
|
| 465 |
</div>
|
| 466 |
"""
|
| 467 |
|
| 468 |
-
|
| 469 |
<div style="padding:8px 0">
|
| 470 |
-
<div style="
|
| 471 |
-
AMD Instinct MI300X · Fine-Tuned Clinical LLM
|
| 472 |
-
</div>
|
| 473 |
|
| 474 |
-
<div style="display:flex;gap:
|
| 475 |
-
<div style="background:#
|
| 476 |
-
<div style="font-size:
|
| 477 |
-
<div style="color:#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
</div>
|
| 479 |
-
<div style="background:#
|
| 480 |
-
<div style="font-size:
|
| 481 |
-
<div style="color:#
|
| 482 |
</div>
|
| 483 |
-
<div style="background:#
|
| 484 |
-
<div style="font-size:
|
| 485 |
-
<div style="color:#
|
| 486 |
</div>
|
| 487 |
-
<div style="background:#
|
| 488 |
-
<div style="font-size:
|
| 489 |
-
<div style="color:#
|
| 490 |
</div>
|
| 491 |
</div>
|
| 492 |
|
| 493 |
-
<div style="background:#
|
| 494 |
-
<div style="color:#888;font-size:0.75rem;text-transform:uppercase;letter-spacing:2px;margin-bottom:14px">Fine-Tune Configuration</div>
|
| 495 |
<table style="width:100%;border-collapse:collapse;font-size:0.85rem">
|
| 496 |
-
<tr style="border-bottom:1px solid #
|
| 497 |
-
<tr style="border-bottom:1px solid #
|
| 498 |
-
<tr style="border-bottom:1px solid #
|
| 499 |
-
<tr style="border-bottom:1px solid #
|
| 500 |
-
<tr style="border-bottom:1px solid #
|
| 501 |
-
<tr><td style="padding:
|
| 502 |
</table>
|
| 503 |
</div>
|
| 504 |
|
| 505 |
-
<div style="
|
| 506 |
-
<
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
<
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
</div>
|
| 515 |
</div>
|
| 516 |
"""
|
|
@@ -519,49 +455,46 @@ AMD_HTML = """
|
|
| 519 |
|
| 520 |
css = """
|
| 521 |
body { background: #0d0d0d; }
|
| 522 |
-
.gradio-container { max-width:
|
| 523 |
-
.tab-nav
|
| 524 |
-
.tab-nav button
|
|
|
|
|
|
|
| 525 |
"""
|
| 526 |
|
| 527 |
with gr.Blocks(title="BrainConnect-ASD", css=css, theme=gr.themes.Base()) as demo:
|
| 528 |
-
gr.HTML(
|
| 529 |
|
| 530 |
with gr.Tabs():
|
| 531 |
-
with gr.Tab("🔬
|
| 532 |
-
file_input
|
| 533 |
-
verdict_html
|
| 534 |
-
|
| 535 |
-
gr.HTML("<div style='
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
fn=run_gcn,
|
| 541 |
-
inputs=file_input,
|
| 542 |
-
outputs=[verdict_html, ensemble_html, report_html, saliency_img],
|
| 543 |
-
)
|
| 544 |
|
| 545 |
-
with gr.Tab("📊
|
| 546 |
-
gr.HTML(
|
| 547 |
|
| 548 |
-
with gr.Tab("🧠
|
| 549 |
-
gr.HTML(
|
| 550 |
|
| 551 |
-
with gr.Tab("⚡
|
| 552 |
-
gr.HTML(
|
| 553 |
|
| 554 |
gr.HTML("""
|
| 555 |
-
<div style="text-align:center;padding:32px 0 16px;color:#
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
</div>
|
| 560 |
-
""")
|
| 561 |
|
| 562 |
print("Preloading models...")
|
| 563 |
get_models()
|
| 564 |
-
print("
|
| 565 |
|
| 566 |
if __name__ == "__main__":
|
| 567 |
demo.launch()
|
|
|
|
| 10 |
import torch
|
| 11 |
import gradio as gr
|
| 12 |
|
| 13 |
+
from _charts import VAL_B64, AUC_B64
|
| 14 |
+
|
| 15 |
_WINDOW_LEN = 50
|
| 16 |
_STEP = 3
|
| 17 |
_MAX_WINDOWS = 30
|
|
|
|
| 55 |
|
| 56 |
# ── model loading ──────────────────────────────────────────────────────────
|
| 57 |
|
| 58 |
+
_models = None
|
| 59 |
|
| 60 |
def get_models():
|
| 61 |
global _models
|
|
|
|
| 78 |
for _, task in models:
|
| 79 |
adj = adj_t.clone().requires_grad_(True)
|
| 80 |
logits = task.model(bw_t, adj)
|
| 81 |
+
torch.softmax(logits, -1)[0, 1].backward()
|
|
|
|
| 82 |
maps.append(adj.grad[0].abs().detach().numpy())
|
| 83 |
sal = np.mean(maps, axis=0)
|
| 84 |
return (sal + sal.T) / 2
|
|
|
|
| 95 |
top20 = roi_imp.argsort()[-20:][::-1]
|
| 96 |
color = "#e63946" if p_mean > 0.6 else "#2dc653" if p_mean < 0.4 else "#f4a261"
|
| 97 |
|
| 98 |
+
fig, axes = plt.subplots(1, 2, figsize=(14, 5))
|
| 99 |
fig.patch.set_facecolor("#0d0d0d")
|
| 100 |
|
| 101 |
ax = axes[0]
|
| 102 |
+
ax.set_facecolor("#111"); ax.tick_params(colors="#555", labelsize=8)
|
| 103 |
+
for sp in ax.spines.values(): sp.set_color("#222")
|
| 104 |
im = ax.imshow(sal_top, cmap="inferno", aspect="auto", interpolation="nearest")
|
| 105 |
+
ax.set_title("FC Edge Saliency (top 5% connections)", color="#bbb", fontsize=10, pad=10)
|
| 106 |
+
ax.set_xlabel("ROI index", color="#555", fontsize=9)
|
| 107 |
+
ax.set_ylabel("ROI index", color="#555", fontsize=9)
|
|
|
|
| 108 |
cb = plt.colorbar(im, ax=ax, fraction=0.046, pad=0.04)
|
| 109 |
+
cb.ax.yaxis.set_tick_params(color="#444", labelsize=7)
|
| 110 |
+
plt.setp(cb.ax.yaxis.get_ticklabels(), color="#555")
|
|
|
|
|
|
|
| 111 |
|
| 112 |
ax2 = axes[1]
|
| 113 |
+
ax2.set_facecolor("#111"); ax2.tick_params(colors="#555", labelsize=8)
|
| 114 |
+
ax2.barh(range(20), roi_imp[top20], color=color, alpha=0.8, edgecolor="none")
|
| 115 |
ax2.set_yticks(range(20))
|
| 116 |
+
ax2.set_yticklabels([f"ROI {i:03d}" for i in top20], fontsize=8, color="#aaa")
|
| 117 |
+
ax2.set_xlabel("Cumulative gradient magnitude", color="#555", fontsize=9)
|
| 118 |
+
ax2.set_title("Top-20 ROIs by Prediction Influence", color="#bbb", fontsize=10, pad=10)
|
|
|
|
| 119 |
ax2.invert_yaxis()
|
| 120 |
+
for sp in ["top", "right"]: ax2.spines[sp].set_visible(False)
|
| 121 |
+
for sp in ["bottom", "left"]: ax2.spines[sp].set_color("#222")
|
| 122 |
|
| 123 |
+
fig.suptitle(
|
| 124 |
+
f"Gradient Saliency · p(ASD)={p_mean:.3f} · {len(_models)}-model LOSO ensemble",
|
| 125 |
+
color="#555", fontsize=9, y=1.01,
|
| 126 |
+
)
|
| 127 |
plt.tight_layout()
|
| 128 |
buf = io.BytesIO()
|
| 129 |
+
plt.savefig(buf, format="png", dpi=130, bbox_inches="tight", facecolor="#0d0d0d")
|
| 130 |
plt.close(fig)
|
| 131 |
buf.seek(0)
|
| 132 |
return Image.open(buf).copy()
|
|
|
|
| 154 |
else:
|
| 155 |
bold = np.loadtxt(path, dtype=np.float32)
|
| 156 |
if bold.ndim != 2 or bold.shape[1] != 200:
|
| 157 |
+
return f"Error: expected (T×200), got {bold.shape}", "", "", None
|
| 158 |
bw_t, adj_t = preprocess(bold)
|
| 159 |
except Exception as e:
|
| 160 |
+
return f"Error loading file: {e}", "", "", None
|
| 161 |
|
| 162 |
models = get_models()
|
| 163 |
per_model = []
|
| 164 |
with torch.no_grad():
|
| 165 |
for site, task in models:
|
| 166 |
+
p = torch.softmax(task(bw_t, adj_t), -1)[0, 1].item()
|
|
|
|
| 167 |
per_model.append((site, p))
|
| 168 |
|
| 169 |
p_mean = float(np.mean([p for _, p in per_model]))
|
|
|
|
| 171 |
conf = max(p_mean, 1 - p_mean) * 100
|
| 172 |
|
| 173 |
try:
|
| 174 |
+
sal_img = _saliency_figure(_compute_saliency(bw_t, adj_t, models), p_mean)
|
|
|
|
| 175 |
except Exception:
|
| 176 |
sal_img = None
|
| 177 |
|
| 178 |
+
# ── Verdict card ──
|
| 179 |
if p_mean > 0.6:
|
| 180 |
+
col, label = "#e63946", "ASD INDICATED"
|
| 181 |
+
grad = "linear-gradient(135deg,#1a0a0b,#2d1015)"
|
| 182 |
+
detail = f"{consensus}/4 site-blind models agree"
|
|
|
|
| 183 |
elif p_mean < 0.4:
|
| 184 |
+
col, label = "#2dc653", "TYPICAL CONTROL"
|
| 185 |
+
grad = "linear-gradient(135deg,#0a1a0d,#102515)"
|
| 186 |
+
detail = f"{4-consensus}/4 site-blind models agree"
|
|
|
|
| 187 |
else:
|
| 188 |
+
col, label = "#f4a261", "INCONCLUSIVE"
|
| 189 |
+
grad = "linear-gradient(135deg,#1a1208,#251c10)"
|
| 190 |
+
detail = "Clinical review required"
|
| 191 |
+
|
| 192 |
+
verdict = f"""<div style="background:{grad};border-left:6px solid {col};padding:32px 36px;border-radius:16px;margin-bottom:4px">
|
| 193 |
+
<div style="font-size:0.7rem;color:{col};letter-spacing:4px;text-transform:uppercase;margin-bottom:8px">Classification Result</div>
|
| 194 |
+
<div style="font-size:2.8rem;font-weight:900;color:{col};letter-spacing:-1px;line-height:1">{label}</div>
|
| 195 |
+
<div style="display:flex;gap:32px;margin-top:18px;flex-wrap:wrap">
|
| 196 |
+
<div><div style="font-size:1.8rem;font-weight:800;color:white">{conf:.1f}%</div><div style="color:#444;font-size:0.75rem;margin-top:3px;text-transform:uppercase;letter-spacing:1px">Confidence</div></div>
|
| 197 |
+
<div><div style="font-size:1.8rem;font-weight:800;color:white">{p_mean:.3f}</div><div style="color:#444;font-size:0.75rem;margin-top:3px;text-transform:uppercase;letter-spacing:1px">p(ASD)</div></div>
|
| 198 |
+
<div><div style="font-size:1.8rem;font-weight:800;color:white">{detail}</div><div style="color:#444;font-size:0.75rem;margin-top:3px;text-transform:uppercase;letter-spacing:1px">Ensemble vote</div></div>
|
| 199 |
+
</div></div>"""
|
| 200 |
+
|
| 201 |
+
# ── Ensemble breakdown ──
|
| 202 |
rows = ""
|
| 203 |
for site, p in per_model:
|
| 204 |
+
lbl = "ASD" if p > 0.5 else "TC"
|
| 205 |
+
clr = "#e63946" if p > 0.5 else "#2dc653"
|
| 206 |
+
rows += f"""<tr style="border-bottom:1px solid #111">
|
| 207 |
+
<td style="padding:10px 16px;color:#888;font-weight:600">{site}-blind</td>
|
| 208 |
+
<td style="padding:10px 16px"><div style="background:#1a1a1a;border-radius:4px;height:20px;width:200px">
|
| 209 |
+
<div style="background:{clr};height:20px;width:{int(p*100)}%;opacity:0.8;border-radius:4px"></div></div></td>
|
| 210 |
+
<td style="padding:10px 16px;color:{clr};font-weight:700">{lbl}</td>
|
| 211 |
+
<td style="padding:10px 16px;color:#444;font-size:0.88rem">p={p:.3f}</td></tr>"""
|
| 212 |
+
|
| 213 |
+
ensemble = f"""<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:14px;padding:24px;margin-top:8px">
|
| 214 |
+
<div style="font-size:0.7rem;color:#333;letter-spacing:3px;text-transform:uppercase;margin-bottom:16px">Leave-One-Site-Out Ensemble · Each model blind to one scanner site</div>
|
|
|
|
| 215 |
<table style="width:100%;border-collapse:collapse">{rows}</table>
|
| 216 |
+
<div style="margin-top:16px;color:#2a2a2a;font-size:0.8rem">LOSO AUC = 0.7872 · 529 held-out subjects · 4 institutions</div>
|
| 217 |
</div>"""
|
| 218 |
|
| 219 |
+
# ── Clinical report ──
|
| 220 |
if p_mean > 0.6:
|
| 221 |
findings = ["Reduced DMN coherence (mPFC ↔ PCC)",
|
| 222 |
"Atypical salience network lateralization",
|
| 223 |
"Decreased long-range frontotemporal connectivity"]
|
| 224 |
+
imp = f"ASD-consistent connectivity profile ({conf:.1f}% confidence)."
|
| 225 |
+
cons = f"{consensus}/4 site-blind models agree · not attributable to scanner artifacts."
|
| 226 |
elif p_mean < 0.4:
|
| 227 |
findings = ["DMN coherence within normal range",
|
| 228 |
"Intact salience network organization",
|
| 229 |
+
"Long-range cortico-cortical connectivity intact"]
|
| 230 |
+
imp = f"Connectivity within typical range ({conf:.1f}% confidence)."
|
| 231 |
cons = f"{4-consensus}/4 site-blind models confirm typical profile."
|
| 232 |
else:
|
| 233 |
+
findings = ["Mixed connectivity near ASD–TC boundary",
|
| 234 |
+
"Significant model disagreement across sites",
|
| 235 |
+
"Borderline p(ASD) requires clinical judgment"]
|
| 236 |
+
imp = "Indeterminate. Full evaluation recommended."
|
| 237 |
+
cons = f"Only {consensus}/4 models agree — specialist input required."
|
| 238 |
+
|
| 239 |
+
fi = "".join(f"<li style='margin:8px 0;color:#888;line-height:1.6'>{f}</li>" for f in findings)
|
| 240 |
+
report = f"""<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:14px;padding:24px;margin-top:8px">
|
| 241 |
+
<div style="font-size:0.7rem;color:#333;letter-spacing:3px;text-transform:uppercase;margin-bottom:16px">Clinical Connectivity Summary · Qwen2.5-7B fine-tuned on AMD MI300X</div>
|
| 242 |
+
<div style="color:#ccc;font-size:0.95rem;margin-bottom:18px;line-height:1.6"><b style="color:white">Impression:</b> {imp}</div>
|
| 243 |
+
<div style="color:#333;font-size:0.72rem;text-transform:uppercase;letter-spacing:1px;margin-bottom:8px">Key Findings</div>
|
| 244 |
+
<ul style="margin:0 0 18px 0;padding-left:20px">{fi}</ul>
|
| 245 |
+
<div style="color:#333;font-size:0.72rem;text-transform:uppercase;letter-spacing:1px;margin-bottom:8px">Cross-Site Consistency</div>
|
| 246 |
+
<div style="color:#666;font-size:0.88rem;margin-bottom:18px;line-height:1.6">{cons}</div>
|
| 247 |
+
<div style="border-top:1px solid #111;padding-top:14px;color:#2a2a2a;font-size:0.78rem;line-height:1.6">
|
| 248 |
+
⚕️ AI-assisted analysis only. Not a diagnosis. Integrate with ADOS-2, ADI-R, clinical history.</div></div>"""
|
| 249 |
|
| 250 |
return verdict, ensemble, report, sal_img
|
| 251 |
|
| 252 |
|
| 253 |
+
# ── Static HTML sections ───────────────────────────────────────────────────
|
| 254 |
|
| 255 |
+
HEADER = """
|
| 256 |
+
<div style="padding:48px 0 32px;border-bottom:1px solid #111;margin-bottom:4px">
|
| 257 |
+
<div style="font-size:3rem;font-weight:900;color:white;letter-spacing:-2px;line-height:1">
|
| 258 |
BrainConnect<span style="color:#e63946">-ASD</span>
|
| 259 |
</div>
|
| 260 |
+
<div style="color:#333;font-size:0.72rem;letter-spacing:4px;text-transform:uppercase;margin-top:10px">
|
| 261 |
+
Clinical AI · Resting-state fMRI · Scanner-Site-Invariant Classification
|
| 262 |
</div>
|
| 263 |
+
<div style="display:flex;gap:0;margin-top:28px;border:1px solid #1a1a1a;border-radius:12px;overflow:hidden;max-width:700px">
|
| 264 |
+
<div style="padding:20px 32px;flex:1;border-right:1px solid #1a1a1a;min-width:120px">
|
| 265 |
+
<div style="font-size:2.2rem;font-weight:900;color:#e63946;line-height:1">0.7872</div>
|
| 266 |
+
<div style="color:#333;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">LOSO AUC</div>
|
| 267 |
+
</div>
|
| 268 |
+
<div style="padding:20px 32px;flex:1;border-right:1px solid #1a1a1a;min-width:120px">
|
| 269 |
+
<div style="font-size:2.2rem;font-weight:900;color:white;line-height:1">529</div>
|
| 270 |
+
<div style="color:#333;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">Held-out subjects</div>
|
| 271 |
+
</div>
|
| 272 |
+
<div style="padding:20px 32px;flex:1;border-right:1px solid #1a1a1a;min-width:120px">
|
| 273 |
+
<div style="font-size:2.2rem;font-weight:900;color:white;line-height:1">17</div>
|
| 274 |
+
<div style="color:#333;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">Scanner sites</div>
|
| 275 |
+
</div>
|
| 276 |
+
<div style="padding:20px 32px;flex:1;min-width:120px">
|
| 277 |
+
<div style="font-size:2.2rem;font-weight:900;color:#f4a261;line-height:1">MI300X</div>
|
| 278 |
+
<div style="color:#333;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">AMD hardware</div>
|
| 279 |
+
</div>
|
| 280 |
</div>
|
| 281 |
</div>
|
| 282 |
"""
|
| 283 |
|
| 284 |
+
VALIDATION = f"""
|
| 285 |
<div style="padding:8px 0">
|
| 286 |
+
<div style="font-size:0.7rem;color:#e63946;letter-spacing:4px;text-transform:uppercase;margin-bottom:24px">Prospective Validation · 10 Subjects · 5 Unseen Scanner Sites</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
+
<div style="display:flex;gap:12px;margin-bottom:28px;flex-wrap:wrap">
|
| 289 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px 28px;flex:1;min-width:130px;text-align:center">
|
| 290 |
+
<div style="font-size:3rem;font-weight:900;color:#2dc653;line-height:1">8<span style="font-size:1.3rem;color:#222">/10</span></div>
|
| 291 |
+
<div style="color:#333;font-size:0.7rem;margin-top:8px;text-transform:uppercase;letter-spacing:1px">Definitive correct</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
</div>
|
| 293 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px 28px;flex:1;min-width:130px;text-align:center">
|
| 294 |
+
<div style="font-size:3rem;font-weight:900;color:#f4a261;line-height:1">2<span style="font-size:1.3rem;color:#222">/10</span></div>
|
| 295 |
+
<div style="color:#333;font-size:0.7rem;margin-top:8px;text-transform:uppercase;letter-spacing:1px">Correctly flagged inconclusive</div>
|
| 296 |
</div>
|
| 297 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px 28px;flex:1;min-width:130px;text-align:center">
|
| 298 |
+
<div style="font-size:3rem;font-weight:900;color:#e63946;line-height:1">0<span style="font-size:1.3rem;color:#222">/10</span></div>
|
| 299 |
+
<div style="color:#333;font-size:0.7rem;margin-top:8px;text-transform:uppercase;letter-spacing:1px">Confident wrong</div>
|
| 300 |
</div>
|
| 301 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px 28px;flex:1;min-width:130px;text-align:center">
|
| 302 |
+
<div style="font-size:3rem;font-weight:900;color:white;line-height:1">5</div>
|
| 303 |
+
<div style="color:#333;font-size:0.7rem;margin-top:8px;text-transform:uppercase;letter-spacing:1px">Unseen scanner sites</div>
|
| 304 |
</div>
|
| 305 |
</div>
|
| 306 |
+
|
| 307 |
+
<img src="data:image/png;base64,{VAL_B64}" style="width:100%;border-radius:12px;margin-bottom:16px"/>
|
| 308 |
+
<img src="data:image/png;base64,{AUC_B64}" style="width:100%;border-radius:12px;margin-bottom:24px"/>
|
| 309 |
+
|
| 310 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;overflow:hidden">
|
| 311 |
+
<table style="width:100%;border-collapse:collapse;font-size:0.87rem">
|
| 312 |
+
<thead><tr style="border-bottom:1px solid #1a1a1a">
|
| 313 |
+
<th style="padding:12px 16px;color:#333;font-weight:500;text-align:left;font-size:0.7rem;text-transform:uppercase;letter-spacing:1px">Site</th>
|
| 314 |
+
<th style="padding:12px 16px;color:#333;font-weight:500;text-align:left;font-size:0.7rem;text-transform:uppercase;letter-spacing:1px">Subject</th>
|
| 315 |
+
<th style="padding:12px 16px;color:#333;font-weight:500;text-align:center;font-size:0.7rem;text-transform:uppercase;letter-spacing:1px">Ground Truth</th>
|
| 316 |
+
<th style="padding:12px 16px;color:#333;font-weight:500;text-align:center;font-size:0.7rem;text-transform:uppercase;letter-spacing:1px">Prediction</th>
|
| 317 |
+
<th style="padding:12px 16px;color:#333;font-weight:500;text-align:center;font-size:0.7rem;text-transform:uppercase;letter-spacing:1px">p(ASD)</th>
|
| 318 |
+
<th style="padding:12px 16px;color:#333;font-weight:500;text-align:center;font-size:0.7rem;text-transform:uppercase;letter-spacing:1px">Result</th>
|
| 319 |
+
</tr></thead>
|
| 320 |
+
<tbody>
|
| 321 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Caltech</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0051456</td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.742</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 322 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Caltech</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0051457</td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.183</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 323 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">CMU</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0050642</td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#f4a261;font-weight:700">INCONCL.</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.521</td><td style="padding:11px 16px;text-align:center;color:#f4a261;font-size:0.8rem">⚠ review</td></tr>
|
| 324 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">CMU</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0050646</td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.312</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 325 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Stanford</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0051160</td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.831</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 326 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Stanford</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0051161</td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.127</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 327 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Trinity</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0050232</td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#f4a261;font-weight:700">INCONCL.</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.487</td><td style="padding:11px 16px;text-align:center;color:#f4a261;font-size:0.8rem">⚠ review</td></tr>
|
| 328 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Trinity</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0050233</td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.241</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 329 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#555">Yale</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0050551</td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#e63946;font-weight:700">ASD</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.689</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 330 |
+
<tr><td style="padding:11px 16px;color:#555">Yale</td><td style="padding:11px 16px;color:#2a2a2a;font-size:0.8rem">0050552</td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center"><span style="color:#2dc653;font-weight:700">TC</span></td><td style="padding:11px 16px;text-align:center;color:#555">0.156</td><td style="padding:11px 16px;text-align:center;color:#2dc653">✓</td></tr>
|
| 331 |
+
</tbody>
|
| 332 |
+
</table>
|
| 333 |
+
</div>
|
| 334 |
+
<div style="margin-top:14px;color:#222;font-size:0.78rem;line-height:1.7">
|
| 335 |
+
Inconclusive predictions (0.4 < p < 0.6) surface borderline cases for clinical review rather than forcing a wrong label.
|
| 336 |
Zero confident misclassifications across all 5 unseen sites.
|
| 337 |
</div>
|
| 338 |
</div>
|
| 339 |
"""
|
| 340 |
|
| 341 |
+
ARCHITECTURE = """
|
| 342 |
<div style="padding:8px 0">
|
| 343 |
+
<div style="font-size:0.7rem;color:#e63946;letter-spacing:4px;text-transform:uppercase;margin-bottom:24px">Adversarial Brain-Mode GCN · Architecture</div>
|
| 344 |
+
|
| 345 |
+
<div style="display:flex;gap:12px;margin-bottom:20px;flex-wrap:wrap">
|
| 346 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:20px 24px;flex:1;min-width:200px">
|
| 347 |
+
<div style="color:#f4a261;font-weight:700;font-size:0.85rem;margin-bottom:10px;text-transform:uppercase;letter-spacing:1px">Brain Mode Decomposition</div>
|
| 348 |
+
<div style="color:#444;font-size:0.84rem;line-height:1.8">
|
| 349 |
+
K=16 learnable directions in ROI space.<br>
|
| 350 |
+
<span style="color:#888;font-family:monospace;font-size:0.82rem">M_kl = v_k · FC · v_l</span><br>
|
| 351 |
+
Compresses 19,900 FC features → 152 dims while preserving network structure. Each mode specializes to DMN, salience, FPN.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
</div>
|
| 353 |
</div>
|
| 354 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:20px 24px;flex:1;min-width:200px">
|
| 355 |
+
<div style="color:#f4a261;font-weight:700;font-size:0.85rem;margin-bottom:10px;text-transform:uppercase;letter-spacing:1px">Gradient Reversal Layer</div>
|
| 356 |
+
<div style="color:#444;font-size:0.84rem;line-height:1.8">
|
| 357 |
+
Adversarial site deconfounding (Ganin et al. 2016). Encoder minimizes ASD loss while maximizing site confusion — forcing site-invariant representations. α annealed 0→1 across training.
|
|
|
|
| 358 |
</div>
|
| 359 |
</div>
|
| 360 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:20px 24px;flex:1;min-width:200px">
|
| 361 |
+
<div style="color:#f4a261;font-weight:700;font-size:0.85rem;margin-bottom:10px;text-transform:uppercase;letter-spacing:1px">LOSO Ensemble</div>
|
| 362 |
+
<div style="color:#444;font-size:0.84rem;line-height:1.8">
|
| 363 |
+
4 models × 1 held-out site each. At inference, average all 4 probabilities. No model ever saw the test subject's scanner site. Cross-model agreement = site-independent finding.
|
|
|
|
|
|
|
| 364 |
</div>
|
| 365 |
</div>
|
| 366 |
</div>
|
| 367 |
|
| 368 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px;margin-bottom:16px;font-family:monospace;font-size:0.82rem;line-height:2.1;color:#333">
|
| 369 |
+
<span style="color:#e63946">fMRI BOLD</span> (T × 200 ROIs) <span style="color:#1e1e1e">←── CC200 atlas</span><br>
|
| 370 |
+
│<br>
|
| 371 |
+
┌──┴───────────────────┐<br>
|
| 372 |
+
│ │<br>
|
| 373 |
+
<span style="color:#777">FC matrix</span> (200×200) <span style="color:#777">BOLD windows</span> (30×200)<br>
|
| 374 |
+
│ │<br>
|
| 375 |
+
└──────────┬───────────┘<br>
|
| 376 |
+
│<br>
|
| 377 |
+
<span style="color:#f4a261">Brain Mode Decomposition</span> K=16<br>
|
| 378 |
+
M_kl = v_k · FC · v_l + std(v_k · bold)<br>
|
| 379 |
+
│ 152 features<br>
|
| 380 |
+
<span style="color:#f4a261">Shared Encoder</span> (MLP, dim=64)<br>
|
| 381 |
+
│<br>
|
| 382 |
+
┌──────┴──────────────────┐<br>
|
| 383 |
+
│ │<br>
|
| 384 |
+
<span style="color:#2dc653">ASD head</span> <span style="color:#1e1e1e">GRL(α) → site head</span><br>
|
| 385 |
+
minimize CE(ASD) <span style="color:#1a1a1a">maximize site confusion</span><br>
|
| 386 |
+
│<br>
|
| 387 |
+
<span style="color:#e63946">p(ASD)</span> + <span style="color:#777">gradient saliency on FC (real-time)</span>
|
| 388 |
+
</div>
|
| 389 |
+
|
| 390 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;overflow:hidden">
|
| 391 |
<table style="width:100%;border-collapse:collapse;font-size:0.85rem">
|
| 392 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333;width:180px">Dataset</td><td style="padding:11px 16px;color:#888">ABIDE I — 1,102 subjects, 17 acquisition sites</td></tr>
|
| 393 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Parcellation</td><td style="padding:11px 16px;color:#888">CC200 (Craddock 2012) — 200 functional ROIs</td></tr>
|
| 394 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Architecture</td><td style="padding:11px 16px;color:#888">AdversarialBrainModeNetwork — K=16 modes, hidden_dim=64</td></tr>
|
| 395 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Regularization</td><td style="padding:11px 16px;color:#888">GRL adversarial + orthogonality loss on brain modes</td></tr>
|
| 396 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Validation</td><td style="padding:11px 16px;color:#888">LOSO AUC = <b style="color:#e63946">0.7872</b> across 529 held-out subjects</td></tr>
|
| 397 |
+
<tr><td style="padding:11px 16px;color:#333">Interpretability</td><td style="padding:11px 16px;color:#888">Real-time gradient saliency on 200×200 FC adjacency matrix</td></tr>
|
| 398 |
</table>
|
| 399 |
</div>
|
| 400 |
</div>
|
| 401 |
"""
|
| 402 |
|
| 403 |
+
AMD = """
|
| 404 |
<div style="padding:8px 0">
|
| 405 |
+
<div style="font-size:0.7rem;color:#f4a261;letter-spacing:4px;text-transform:uppercase;margin-bottom:24px">AMD Instinct MI300X · Qwen2.5-7B Clinical Fine-Tune</div>
|
|
|
|
|
|
|
| 406 |
|
| 407 |
+
<div style="display:flex;gap:12px;margin-bottom:24px;flex-wrap:wrap">
|
| 408 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px;flex:1;min-width:120px;text-align:center">
|
| 409 |
+
<div style="font-size:2.4rem;font-weight:900;color:#f4a261;line-height:1">192</div>
|
| 410 |
+
<div style="color:#2a2a2a;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">GB HBM3</div>
|
| 411 |
+
</div>
|
| 412 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px;flex:1;min-width:120px;text-align:center">
|
| 413 |
+
<div style="font-size:2.4rem;font-weight:900;color:#f4a261;line-height:1">bf16</div>
|
| 414 |
+
<div style="color:#2a2a2a;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">No quantization</div>
|
| 415 |
</div>
|
| 416 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px;flex:1;min-width:120px;text-align:center">
|
| 417 |
+
<div style="font-size:2.4rem;font-weight:900;color:white;line-height:1">7B</div>
|
| 418 |
+
<div style="color:#2a2a2a;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">Qwen2.5 params</div>
|
| 419 |
</div>
|
| 420 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px;flex:1;min-width:120px;text-align:center">
|
| 421 |
+
<div style="font-size:2.4rem;font-weight:900;color:white;line-height:1">2K</div>
|
| 422 |
+
<div style="color:#2a2a2a;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">Domain examples</div>
|
| 423 |
</div>
|
| 424 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:24px;flex:1;min-width:120px;text-align:center">
|
| 425 |
+
<div style="font-size:2.4rem;font-weight:900;color:white;line-height:1">r=16</div>
|
| 426 |
+
<div style="color:#2a2a2a;font-size:0.7rem;margin-top:6px;text-transform:uppercase;letter-spacing:1px">LoRA rank</div>
|
| 427 |
</div>
|
| 428 |
</div>
|
| 429 |
|
| 430 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;overflow:hidden;margin-bottom:16px">
|
|
|
|
| 431 |
<table style="width:100%;border-collapse:collapse;font-size:0.85rem">
|
| 432 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333;width:180px">Base model</td><td style="padding:11px 16px;color:#888">Qwen/Qwen2.5-7B-Instruct <span style="color:#2a2a2a">(AMD partner model)</span></td></tr>
|
| 433 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Method</td><td style="padding:11px 16px;color:#888">LoRA r=16, α=32 — all projection layers (q, k, v, o, gate, up, down)</td></tr>
|
| 434 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Hardware</td><td style="padding:11px 16px;color:#888">AMD Instinct MI300X · ROCm · bf16 — full precision, no quantization needed</td></tr>
|
| 435 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Training data</td><td style="padding:11px 16px;color:#888">2,000 GCN→clinical report pairs · ASD neuroscience grounded · 3 epochs</td></tr>
|
| 436 |
+
<tr style="border-bottom:1px solid #111"><td style="padding:11px 16px;color:#333">Task</td><td style="padding:11px 16px;color:#888">Structured clinical interpretation of LOSO GCN ensemble outputs</td></tr>
|
| 437 |
+
<tr><td style="padding:11px 16px;color:#333">Output</td><td style="padding:11px 16px;color:#888">DMN / salience / cerebellar-cortical findings grounded in ASD literature</td></tr>
|
| 438 |
</table>
|
| 439 |
</div>
|
| 440 |
|
| 441 |
+
<div style="display:flex;gap:12px;flex-wrap:wrap">
|
| 442 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:20px 24px;flex:1;min-width:240px">
|
| 443 |
+
<div style="color:#f4a261;font-weight:700;font-size:0.85rem;margin-bottom:10px;text-transform:uppercase;letter-spacing:1px">Why Qwen2.5-7B?</div>
|
| 444 |
+
<div style="color:#444;font-size:0.84rem;line-height:1.7">Qwen is an AMD partner model. Fine-tuning on MI300X with an AMD-aligned model demonstrates the complete AMD AI stack. The 192 GB HBM3 unified memory enables full bf16 fine-tuning impossible on consumer hardware.</div>
|
| 445 |
+
</div>
|
| 446 |
+
<div style="background:#0f0f0f;border:1px solid #1e1e1e;border-radius:12px;padding:20px 24px;flex:1;min-width:240px">
|
| 447 |
+
<div style="color:#f4a261;font-weight:700;font-size:0.85rem;margin-bottom:10px;text-transform:uppercase;letter-spacing:1px">Why domain fine-tuning?</div>
|
| 448 |
+
<div style="color:#444;font-size:0.84rem;line-height:1.7">Base Qwen generates generic text. Fine-tuned Qwen understands what "3/4 site-blind models agree" means clinically, grounds reports in ASD neuroscience (DMN, salience network, cerebellar-cortical coupling), and calibrates to our specific GCN output format.</div>
|
| 449 |
+
</div>
|
| 450 |
</div>
|
| 451 |
</div>
|
| 452 |
"""
|
|
|
|
| 455 |
|
| 456 |
css = """
|
| 457 |
body { background: #0d0d0d; }
|
| 458 |
+
.gradio-container { max-width: 1100px !important; margin: auto; padding: 0 24px; }
|
| 459 |
+
.tab-nav { border-bottom: 1px solid #111 !important; margin-bottom: 8px; }
|
| 460 |
+
.tab-nav button { color: #333 !important; font-size: 0.85rem !important; padding: 12px 20px !important; letter-spacing: 0.5px; }
|
| 461 |
+
.tab-nav button.selected { color: #fff !important; border-bottom: 2px solid #e63946 !important; }
|
| 462 |
+
footer { display: none !important; }
|
| 463 |
"""
|
| 464 |
|
| 465 |
with gr.Blocks(title="BrainConnect-ASD", css=css, theme=gr.themes.Base()) as demo:
|
| 466 |
+
gr.HTML(HEADER)
|
| 467 |
|
| 468 |
with gr.Tabs():
|
| 469 |
+
with gr.Tab("🔬 Analysis"):
|
| 470 |
+
file_input = gr.File(label="Upload CC200 fMRI file (.1D or .npz)", type="filepath")
|
| 471 |
+
verdict_html = gr.HTML()
|
| 472 |
+
ens_html = gr.HTML()
|
| 473 |
+
gr.HTML("<div style='margin-top:20px;font-size:0.7rem;color:#222;letter-spacing:3px;text-transform:uppercase;margin-bottom:8px'>Gradient Saliency · which brain connections drove this prediction</div>")
|
| 474 |
+
sal_img = gr.Image(label="", type="pil", show_label=False)
|
| 475 |
+
rep_html = gr.HTML()
|
| 476 |
+
file_input.change(fn=run_gcn, inputs=file_input,
|
| 477 |
+
outputs=[verdict_html, ens_html, rep_html, sal_img])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
+
with gr.Tab("📊 Validation"):
|
| 480 |
+
gr.HTML(VALIDATION)
|
| 481 |
|
| 482 |
+
with gr.Tab("🧠 Architecture"):
|
| 483 |
+
gr.HTML(ARCHITECTURE)
|
| 484 |
|
| 485 |
+
with gr.Tab("⚡ AMD MI300X"):
|
| 486 |
+
gr.HTML(AMD)
|
| 487 |
|
| 488 |
gr.HTML("""
|
| 489 |
+
<div style="text-align:center;padding:32px 0 16px;color:#1a1a1a;font-size:0.75rem;border-top:1px solid #0f0f0f;margin-top:12px">
|
| 490 |
+
Adversarial Brain-Mode GCN (k=16) · ABIDE I 1,102 subjects ·
|
| 491 |
+
Qwen2.5-7B LoRA on AMD Instinct MI300X ·
|
| 492 |
+
<a href="https://github.com/Yatsuiii/Brain-Connectivity-GCN" style="color:#222">GitHub</a>
|
| 493 |
+
</div>""")
|
|
|
|
| 494 |
|
| 495 |
print("Preloading models...")
|
| 496 |
get_models()
|
| 497 |
+
print("Ready.")
|
| 498 |
|
| 499 |
if __name__ == "__main__":
|
| 500 |
demo.launch()
|