File size: 20,901 Bytes
8e0cd11 da08b3c 8e0cd11 da08b3c 8e0cd11 da08b3c 8e0cd11 da08b3c 8e0cd11 da08b3c 8e0cd11 211e2f6 8e0cd11 da08b3c 8e0cd11 |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 |
# phase 4: gradio / spaces app
## purpose
Build a minimal but clean Gradio 5 app that allows interactive case selection, segmentation, and visualization. At the end of this phase, we have a deployable Hugging Face Space.
## deliverables
- [ ] `src/stroke_deepisles_demo/ui/app.py` - Main Gradio application
- [ ] `src/stroke_deepisles_demo/ui/viewer.py` - NiiVue integration
- [ ] `src/stroke_deepisles_demo/ui/components.py` - Reusable UI components
- [ ] `app.py` at repo root - HF Spaces entry point
- [ ] Unit tests for UI logic (not Gradio itself)
- [ ] Smoke test for app import
## vertical slice outcome
After this phase, you can run locally:
```bash
uv run gradio src/stroke_deepisles_demo/ui/app.py
# or
uv run python -m stroke_deepisles_demo.ui.app
```
And deploy to Hugging Face Spaces with the standard Gradio SDK.
## module structure
```
src/stroke_deepisles_demo/ui/
βββ __init__.py # Public API
βββ app.py # Main Gradio application
βββ viewer.py # NiiVue integration
βββ components.py # Reusable UI components
# Root level for HF Spaces
app.py # Entry point: from stroke_deepisles_demo.ui.app import demo
```
## gradio 5 considerations
Based on [Gradio 5 documentation](https://huggingface.co/blog/gradio-5):
- Server-side rendering (SSR) for fast initial load
- Improved components (Buttons, Tabs, Sliders)
- WebRTC support for real-time streaming
- New built-in themes
Key patterns:
```python
import gradio as gr
# Gradio 5 app pattern
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Title")
with gr.Row():
with gr.Column():
# Inputs
...
with gr.Column():
# Outputs
...
demo.launch()
```
## niivue integration strategy
[NiiVue](https://github.com/niivue/niivue) is a WebGL2-based neuroimaging viewer.
### proven implementation: tobias's bids-neuroimaging space
**Reference**: [TobiasPitters/bids-neuroimaging](https://huggingface.co/spaces/TobiasPitters/bids-neuroimaging) - A working HF Space with NiiVue multiplanar + 3D rendering.
Key patterns from Tobias's implementation:
1. **FastAPI + raw HTML** (not Gradio) - Cleaner for single-page viewer
2. **NiiVue via unpkg CDN**: `https://unpkg.com/@niivue/niivue@0.57.0/dist/index.js`
3. **Base64 data URLs** for NIfTI data (no file serving needed):
```python
import base64
nifti_bytes = nifti_image.to_bytes()
nifti_b64 = base64.b64encode(nifti_bytes).decode("utf-8")
data_url = f"data:application/octet-stream;base64,{nifti_b64}"
```
4. **NiiVue configuration for multiplanar + 3D**:
```javascript
nv.setSliceType(nv.sliceTypeMultiplanar);
nv.setMultiplanarLayout(2); // 2x2 grid with 3D render
nv.opts.show3Dcrosshair = true;
```
### implementation approach: gradio + direct base64 injection
For our demo, we use:
- **Gradio** for case selection dropdown and "Run Segmentation" button
- **Direct Base64 data URLs** injected into HTML (no separate API endpoints)
- **NiiVue via `gr.HTML`** for interactive 3D visualization
This gives us:
- Gradio's nice UI components for inputs
- Proven NiiVue rendering pattern from Tobias's implementation
- No iframe complexity, no proxy issues in HF Spaces
### concrete implementation
```python
import base64
from pathlib import Path
import nibabel as nib
def nifti_to_data_url(nifti_path: Path) -> str:
"""Convert NIfTI file to base64 data URL for NiiVue."""
img = nib.load(nifti_path)
nifti_bytes = img.to_bytes()
nifti_b64 = base64.b64encode(nifti_bytes).decode("utf-8")
return f"data:application/octet-stream;base64,{nifti_b64}"
def create_niivue_viewer_html(
volume_data_url: str,
mask_data_url: str | None = None,
height: int = 600,
) -> str:
"""Create NiiVue HTML viewer with optional mask overlay."""
mask_loading = ""
if mask_data_url:
mask_loading = f"""
volumes.push({{
url: '{mask_data_url}',
colorMap: 'red',
opacity: 0.5
}});
"""
return f"""
<div style="width:100%; height:{height}px; background:#000; border-radius:8px;">
<canvas id="niivue-canvas" style="width:100%; height:100%;"></canvas>
</div>
<script type="module">
const niivueModule = await import('https://unpkg.com/@niivue/niivue@0.57.0/dist/index.js');
const Niivue = niivueModule.Niivue;
const nv = new Niivue({{
logging: false,
show3Dcrosshair: true,
textHeight: 0.04
}});
await nv.attachTo('niivue-canvas');
const volumes = [{{
url: '{volume_data_url}',
name: 'dwi.nii.gz'
}}];
{mask_loading}
await nv.loadVolumes(volumes);
// Multiplanar + 3D view
nv.setSliceType(nv.sliceTypeMultiplanar);
if (nv.setMultiplanarLayout) {{
nv.setMultiplanarLayout(2);
}}
nv.opts.show3Dcrosshair = true;
nv.setRenderAzimuthElevation(120, 10);
nv.drawScene();
</script>
"""
```
## interfaces and types
### `ui/app.py`
```python
"""Main Gradio application for stroke-deepisles-demo."""
from __future__ import annotations
import gradio as gr
from stroke_deepisles_demo.pipeline import run_pipeline_on_case
from stroke_deepisles_demo.ui.components import create_case_selector, create_results_display
from stroke_deepisles_demo.ui.viewer import render_comparison_view
def create_app() -> gr.Blocks:
"""
Create the Gradio application.
Returns:
Configured gr.Blocks application
"""
with gr.Blocks(
title="Stroke Lesion Segmentation Demo",
theme=gr.themes.Soft(),
) as demo:
# Header
gr.Markdown("""
# Stroke Lesion Segmentation Demo
This demo runs [DeepISLES](https://github.com/ezequieldlrosa/DeepIsles)
stroke segmentation on cases from
[ISLES24-MR-Lite](https://huggingface.co/datasets/YongchengYAO/ISLES24-MR-Lite).
> **Disclaimer**: This is for research/demonstration only. Not for clinical use.
""")
with gr.Row():
# Left column: Controls
with gr.Column(scale=1):
case_selector = create_case_selector()
run_btn = gr.Button("Run Segmentation", variant="primary")
status = gr.Textbox(label="Status", interactive=False)
# Right column: Results
with gr.Column(scale=2):
results_display = create_results_display()
# Event handlers
run_btn.click(
fn=run_segmentation,
inputs=[case_selector],
outputs=[results_display, status],
)
return demo
def run_segmentation(case_id: str) -> tuple[dict, str]:
"""
Run segmentation and return results for display.
Args:
case_id: Selected case identifier
Returns:
Tuple of (results_dict, status_message)
"""
...
# Module-level app instance for Gradio CLI
demo = create_app()
if __name__ == "__main__":
demo.launch()
```
### `ui/viewer.py`
```python
"""Neuroimaging visualization for Gradio."""
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING
import numpy as np
if TYPE_CHECKING:
from matplotlib.figure import Figure
from numpy.typing import NDArray
def render_slice_comparison(
dwi_path: Path,
prediction_path: Path,
ground_truth_path: Path | None = None,
*,
slice_idx: int | None = None,
orientation: str = "axial",
) -> Figure:
"""
Render side-by-side comparison of DWI, prediction, and ground truth.
Args:
dwi_path: Path to DWI NIfTI
prediction_path: Path to predicted mask NIfTI
ground_truth_path: Optional path to ground truth mask
slice_idx: Slice index (default: middle slice)
orientation: One of "axial", "coronal", "sagittal"
Returns:
Matplotlib figure with comparison view
"""
...
def render_3panel_view(
nifti_path: Path,
mask_path: Path | None = None,
*,
mask_alpha: float = 0.5,
mask_color: str = "red",
) -> Figure:
"""
Render axial/coronal/sagittal slices with optional mask overlay.
Args:
nifti_path: Path to base NIfTI volume
mask_path: Optional path to mask for overlay
mask_alpha: Transparency of mask overlay
mask_color: Color for mask overlay
Returns:
Matplotlib figure with 3-panel view
"""
...
def create_niivue_html(
volume_url: str,
mask_url: str | None = None,
*,
height: int = 400,
) -> str:
"""
Create HTML/JS for NiiVue viewer.
Args:
volume_url: URL to volume NIfTI file
mask_url: Optional URL to mask NIfTI file
height: Viewer height in pixels
Returns:
HTML string with embedded NiiVue viewer
"""
template = f"""
<div id="gl" style="width:100%; height:{height}px;"></div>
<script type="module">
const niivueModule = await import('https://unpkg.com/@niivue/niivue@0.57.0/dist/index.js');
const Niivue = niivueModule.Niivue;
const nv = new Niivue({{ show3Dcrosshair: true }});
nv.attachToCanvas(document.getElementById('gl'));
const volumes = [{{ url: '{volume_url}' }}];
{'volumes.push({ url: "' + mask_url + '", colorMap: "red", opacity: 0.5 });' if mask_url else ''}
await nv.loadVolumes(volumes);
</script>
"""
return template
def get_slice_at_max_lesion(
mask_path: Path,
orientation: str = "axial",
) -> int:
"""
Find slice index with maximum lesion area.
Useful for displaying the most informative slice.
Args:
mask_path: Path to lesion mask NIfTI
orientation: Slice orientation
Returns:
Slice index with maximum lesion area
"""
...
```
### `ui/components.py`
```python
"""Reusable UI components."""
from __future__ import annotations
import gradio as gr
from stroke_deepisles_demo.data import list_case_ids
def create_case_selector() -> gr.Dropdown:
"""
Create a dropdown for selecting cases.
Returns:
Configured gr.Dropdown component
"""
try:
case_ids = list_case_ids()
except Exception:
case_ids = ["Error loading cases"]
return gr.Dropdown(
choices=case_ids,
value=case_ids[0] if case_ids else None,
label="Select Case",
info="Choose a case from ISLES24-MR-Lite",
)
def create_results_display() -> dict[str, gr.components.Component]:
"""
Create results display components.
Returns:
Dictionary of component name -> gr.Component
"""
with gr.Group():
viewer = gr.Image(label="Segmentation Result", type="filepath")
metrics = gr.JSON(label="Metrics")
download = gr.File(label="Download Prediction")
return {
"viewer": viewer,
"metrics": metrics,
"download": download,
}
def create_settings_accordion() -> dict[str, gr.components.Component]:
"""
Create expandable settings section.
Returns:
Dictionary of setting name -> gr.Component
"""
with gr.Accordion("Advanced Settings", open=False):
fast_mode = gr.Checkbox(
value=True,
label="Fast Mode (SEALS)",
info="Run SEALS only (ISLES'22 winner, requires DWI+ADC). Disable for full ensemble (requires FLAIR).",
)
show_ground_truth = gr.Checkbox(
value=True,
label="Show Ground Truth",
info="Display ground truth mask if available",
)
return {
"fast_mode": fast_mode,
"show_ground_truth": show_ground_truth,
}
```
### Root `app.py` for HF Spaces
```python
"""Entry point for Hugging Face Spaces deployment."""
from stroke_deepisles_demo.ui.app import demo
if __name__ == "__main__":
demo.launch()
```
## hugging face spaces configuration
### `README.md` header for Spaces
```yaml
---
title: Stroke DeepISLES Demo
emoji: π§
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: mit
---
```
### `requirements.txt` for Spaces
```
# Note: HF Spaces uses requirements.txt, not pyproject.toml
git+https://github.com/CloseChoice/datasets.git@feat/bids-loader-streaming-upload-fix
huggingface-hub>=0.25.0
nibabel>=5.2.0
numpy>=1.26.0
pydantic>=2.5.0
pydantic-settings>=2.1.0
gradio>=5.0.0
matplotlib>=3.8.0
```
## tdd plan
### test file structure
```
tests/
βββ ui/
β βββ __init__.py
β βββ test_viewer.py # Tests for visualization
β βββ test_components.py # Tests for UI components
β βββ test_app.py # Smoke tests for app
```
### tests to write first (TDD order)
#### 1. `tests/ui/test_viewer.py` - Pure visualization functions
```python
"""Tests for viewer module."""
from __future__ import annotations
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pytest
matplotlib.use("Agg") # Non-interactive backend for tests
from stroke_deepisles_demo.ui.viewer import (
create_niivue_html,
get_slice_at_max_lesion,
render_3panel_view,
render_slice_comparison,
)
class TestRender3PanelView:
"""Tests for render_3panel_view."""
def test_returns_matplotlib_figure(self, synthetic_nifti_3d: Path) -> None:
"""Returns a matplotlib Figure object."""
fig = render_3panel_view(synthetic_nifti_3d)
assert isinstance(fig, plt.Figure)
plt.close(fig)
def test_has_three_axes(self, synthetic_nifti_3d: Path) -> None:
"""Figure has 3 subplots (axial, coronal, sagittal)."""
fig = render_3panel_view(synthetic_nifti_3d)
assert len(fig.axes) == 3
plt.close(fig)
def test_overlay_mask_when_provided(
self, synthetic_nifti_3d: Path, temp_dir: Path
) -> None:
"""Overlays mask when mask_path provided."""
# Create a simple mask
import nibabel as nib
mask_data = np.zeros((10, 10, 10), dtype=np.uint8)
mask_data[4:6, 4:6, 4:6] = 1
mask_img = nib.Nifti1Image(mask_data, np.eye(4))
mask_path = temp_dir / "mask.nii.gz"
nib.save(mask_img, mask_path)
fig = render_3panel_view(synthetic_nifti_3d, mask_path=mask_path)
# Should not raise
assert fig is not None
plt.close(fig)
class TestRenderSliceComparison:
"""Tests for render_slice_comparison."""
def test_comparison_without_ground_truth(
self, synthetic_nifti_3d: Path
) -> None:
"""Works when ground truth is None."""
fig = render_slice_comparison(
synthetic_nifti_3d,
synthetic_nifti_3d, # Use same as prediction for test
ground_truth_path=None,
)
assert isinstance(fig, plt.Figure)
plt.close(fig)
def test_comparison_with_ground_truth(
self, synthetic_nifti_3d: Path
) -> None:
"""Works when ground truth is provided."""
fig = render_slice_comparison(
synthetic_nifti_3d,
synthetic_nifti_3d,
ground_truth_path=synthetic_nifti_3d,
)
assert isinstance(fig, plt.Figure)
plt.close(fig)
class TestGetSliceAtMaxLesion:
"""Tests for get_slice_at_max_lesion."""
def test_finds_slice_with_lesion(self, temp_dir: Path) -> None:
"""Returns slice index where lesion is largest."""
import nibabel as nib
# Create mask with lesion at slice 7
mask_data = np.zeros((10, 10, 10), dtype=np.uint8)
mask_data[:, :, 7] = 1 # Full slice 7 is lesion
mask_img = nib.Nifti1Image(mask_data, np.eye(4))
mask_path = temp_dir / "mask.nii.gz"
nib.save(mask_img, mask_path)
slice_idx = get_slice_at_max_lesion(mask_path, orientation="axial")
assert slice_idx == 7
def test_returns_middle_for_empty_mask(self, temp_dir: Path) -> None:
"""Returns middle slice when mask is empty."""
import nibabel as nib
mask_data = np.zeros((10, 10, 20), dtype=np.uint8)
mask_img = nib.Nifti1Image(mask_data, np.eye(4))
mask_path = temp_dir / "mask.nii.gz"
nib.save(mask_img, mask_path)
slice_idx = get_slice_at_max_lesion(mask_path, orientation="axial")
assert slice_idx == 10 # Middle of 20
class TestCreateNiivueHtml:
"""Tests for create_niivue_html."""
def test_includes_volume_url(self) -> None:
"""Generated HTML includes the volume URL."""
html = create_niivue_html("http://example.com/brain.nii.gz")
assert "http://example.com/brain.nii.gz" in html
def test_includes_mask_when_provided(self) -> None:
"""Generated HTML includes mask URL when provided."""
html = create_niivue_html(
"http://example.com/brain.nii.gz",
mask_url="http://example.com/mask.nii.gz",
)
assert "http://example.com/mask.nii.gz" in html
def test_sets_height(self) -> None:
"""Generated HTML respects height parameter."""
html = create_niivue_html(
"http://example.com/brain.nii.gz",
height=600,
)
assert "height:600px" in html
```
#### 2. `tests/ui/test_app.py` - Smoke tests
```python
"""Smoke tests for Gradio app."""
from __future__ import annotations
def test_app_module_imports() -> None:
"""App module imports without side effects."""
# This should not launch the app or make network calls
from stroke_deepisles_demo.ui import app
assert hasattr(app, "create_app")
assert hasattr(app, "demo")
def test_create_app_returns_blocks() -> None:
"""create_app returns a gr.Blocks instance."""
import gradio as gr
from stroke_deepisles_demo.ui.app import create_app
app = create_app()
assert isinstance(app, gr.Blocks)
def test_viewer_module_imports() -> None:
"""Viewer module imports without errors."""
from stroke_deepisles_demo.ui import viewer
assert hasattr(viewer, "render_3panel_view")
assert hasattr(viewer, "create_niivue_html")
def test_components_module_imports() -> None:
"""Components module imports without errors."""
from stroke_deepisles_demo.ui import components
assert hasattr(components, "create_case_selector")
assert hasattr(components, "create_results_display")
```
### what to mock
- `list_case_ids()` in components - Avoid network during import
- Any data loading in app initialization
### what to test for real
- Matplotlib figure generation
- NiiVue HTML string generation
- Slice finding algorithms
- Module imports (no network side effects)
## "done" criteria
Phase 4 is complete when:
1. All unit tests pass: `uv run pytest tests/ui/ -v`
2. App launches locally: `uv run python -m stroke_deepisles_demo.ui.app`
3. Can select a case, click "Run", see visualization
4. Visualization shows DWI with predicted mask overlay
5. Metrics (Dice score) displayed
6. Type checking passes: `uv run mypy src/stroke_deepisles_demo/ui/`
7. Ready for HF Spaces deployment (README header, requirements.txt)
## implementation notes
- **NiiVue is primary** - Proven working in Tobias's Space, not "fragile"
- **Base64 data URLs** - Avoids file serving complexity, works in all environments
- **Lazy initialization** - Do NOT call `list_case_ids()` at module import time (causes network calls)
- **Test on HF Spaces early** - Verify WebGL works in their environment
- **Keep UI simple** - This is a demo, not a full application
- **Cache case list** - Avoid repeated HF Hub calls
### avoiding import-time side effects
The reviewer correctly noted that `demo = create_app()` at module level triggers network calls. Fix:
```python
# BAD - triggers network call on import
demo = create_app()
# GOOD - lazy initialization
_demo: gr.Blocks | None = None
def get_demo() -> gr.Blocks:
global _demo
if _demo is None:
_demo = create_app()
return _demo
# For Gradio CLI compatibility
demo = None # Set lazily
if __name__ == "__main__":
get_demo().launch()
```
Or use a factory pattern in the root `app.py`:
```python
# app.py (HF Spaces entry point)
from stroke_deepisles_demo.ui.app import create_app
demo = create_app() # Only called when this file is executed
if __name__ == "__main__":
demo.launch()
```
## dependencies to add
```toml
# Add to pyproject.toml dependencies
"matplotlib>=3.8.0", # For static slice rendering in viewer.py
```
## reference implementation
Clone Tobias's working Space for reference:
```
_reference_repos/bids-neuroimaging-space/
```
Key file: `main.py` - Complete NiiVue + FastAPI implementation.
|