File size: 16,204 Bytes
8e0cd11 211e2f6 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 |
# phase 5: polish, observability, and docs
## purpose
Add production-quality polish: structured logging, environment-driven configuration, comprehensive documentation, and CI readiness. At the end of this phase, the codebase is maintainable, debuggable, and ready for others to contribute.
## deliverables
- [ ] Structured logging throughout all modules
- [ ] Environment-driven configuration via pydantic-settings
- [ ] Developer documentation (CONTRIBUTING.md, architecture)
- [ ] API documentation (docstrings, optional Sphinx/mkdocs)
- [ ] CI configuration (GitHub Actions)
- [ ] Final cleanup and code review checklist
## logging strategy
### centralized logging setup
```python
# src/stroke_deepisles_demo/core/logging.py
"""Centralized logging configuration."""
from __future__ import annotations
import logging
import sys
from typing import Literal
LogLevel = Literal["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
def setup_logging(
level: LogLevel = "INFO",
*,
format_style: Literal["simple", "detailed", "json"] = "simple",
) -> None:
"""
Configure logging for the application.
Args:
level: Minimum log level
format_style: Output format style
Example:
>>> setup_logging("DEBUG", format_style="detailed")
"""
formats = {
"simple": "%(levelname)s: %(message)s",
"detailed": "%(asctime)s | %(name)s | %(levelname)s | %(message)s",
"json": '{"time": "%(asctime)s", "name": "%(name)s", "level": "%(levelname)s", "message": "%(message)s"}',
}
logging.basicConfig(
level=getattr(logging, level),
format=formats[format_style],
stream=sys.stderr,
force=True,
)
# Reduce noise from libraries
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("datasets").setLevel(logging.WARNING)
def get_logger(name: str) -> logging.Logger:
"""
Get a logger for a module.
Args:
name: Logger name (typically __name__)
Returns:
Configured logger instance
"""
return logging.getLogger(f"stroke_demo.{name}")
```
### logging usage pattern
```python
# In each module
from stroke_deepisles_demo.core.logging import get_logger
logger = get_logger(__name__)
def run_deepisles_on_folder(input_dir: Path, *, fast: bool = True) -> DeepISLESResult:
logger.info("Starting DeepISLES inference", extra={"input_dir": str(input_dir), "fast": fast})
try:
result = _run_docker(...)
logger.info("Inference complete", extra={"elapsed": result.elapsed_seconds})
return result
except Exception as e:
logger.error("Inference failed", extra={"error": str(e)}, exc_info=True)
raise
```
## enhanced configuration
### `src/stroke_deepisles_demo/core/config.py`
```python
"""Application configuration using pydantic-settings."""
from __future__ import annotations
from pathlib import Path
from typing import Literal
from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""
Application settings loaded from environment variables.
All settings can be overridden via environment variables with
the STROKE_DEMO_ prefix.
Example:
export STROKE_DEMO_LOG_LEVEL=DEBUG
export STROKE_DEMO_HF_DATASET_ID=my/dataset
"""
model_config = SettingsConfigDict(
env_prefix="STROKE_DEMO_",
env_file=".env",
env_file_encoding="utf-8",
extra="ignore",
)
# Logging
log_level: Literal["DEBUG", "INFO", "WARNING", "ERROR"] = "INFO"
log_format: Literal["simple", "detailed", "json"] = "simple"
# HuggingFace
hf_dataset_id: str = "YongchengYAO/ISLES24-MR-Lite"
hf_cache_dir: Path | None = None
hf_token: str | None = Field(default=None, repr=False) # Hidden from logs
# DeepISLES
deepisles_docker_image: str = "isleschallenge/deepisles"
deepisles_fast_mode: bool = True # SEALS-only (ISLES'22 winner, no FLAIR needed)
deepisles_timeout_seconds: int = 1800 # 30 minutes
deepisles_use_gpu: bool = True
# Paths
temp_dir: Path | None = None
results_dir: Path = Path("./results")
# UI
gradio_server_name: str = "0.0.0.0"
gradio_server_port: int = 7860
gradio_share: bool = False
@field_validator("results_dir", mode="before")
@classmethod
def ensure_results_dir_exists(cls, v: Path | str) -> Path:
"""Create results directory if it doesn't exist."""
path = Path(v)
path.mkdir(parents=True, exist_ok=True)
return path
# Global settings instance
settings = Settings()
def get_settings() -> Settings:
"""Get the current settings instance."""
return settings
def reload_settings() -> Settings:
"""Reload settings from environment (useful for testing)."""
global settings
settings = Settings()
return settings
```
## documentation structure
```
docs/
βββ specs/ # Design specs (these documents)
β βββ 00-context.md
β βββ 01-phase-0-repo-bootstrap.md
β βββ ...
β βββ 06-phase-5-polish.md
β
βββ guides/ # User guides
β βββ quickstart.md # Getting started
β βββ configuration.md # Environment variables
β βββ deployment.md # HF Spaces deployment
β
βββ reference/ # API reference (auto-generated)
βββ api.md
# Root level
README.md # Project overview
CONTRIBUTING.md # Contribution guidelines
CHANGELOG.md # Version history
```
### `CONTRIBUTING.md`
```markdown
# Contributing to stroke-deepisles-demo
Thank you for your interest in contributing!
## Development Setup
1. **Clone the repository**
```bash
git clone https://github.com/The-Obstacle-Is-The-Way/stroke-deepisles-demo.git
cd stroke-deepisles-demo
```
2. **Install uv** (if not already installed)
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```
3. **Install dependencies**
```bash
uv sync
```
4. **Install pre-commit hooks**
```bash
uv run pre-commit install
```
## Running Tests
```bash
# All tests (excluding integration)
uv run pytest
# With coverage
uv run pytest --cov
# Integration tests (requires Docker)
uv run pytest -m integration
# Slow tests (requires Docker + DeepISLES image)
uv run pytest -m "integration and slow"
```
## Code Quality
```bash
# Lint
uv run ruff check .
# Format
uv run ruff format .
# Type check
uv run mypy src/
```
## Project Structure
```
src/stroke_deepisles_demo/
βββ core/ # Shared utilities (config, types, exceptions)
βββ data/ # HF dataset loading and case management
βββ inference/ # DeepISLES Docker integration
βββ ui/ # Gradio application
βββ pipeline.py # End-to-end orchestration
βββ metrics.py # Evaluation metrics
```
## Pull Request Process
1. Create a feature branch from `main`
2. Write tests for new functionality
3. Ensure all tests pass and code quality checks pass
4. Update documentation if needed
5. Submit PR with clear description
## Code Style
- Type hints on all functions
- Docstrings in Google style
- Keep functions focused and small
- Prefer explicit over implicit
```
### `docs/guides/quickstart.md`
```markdown
# Quickstart
Get started with stroke-deepisles-demo in 5 minutes.
## Prerequisites
- Python 3.11+
- Docker (for DeepISLES inference)
- ~10GB disk space (for Docker image and datasets)
## Installation
```bash
# Clone
git clone https://github.com/The-Obstacle-Is-The-Way/stroke-deepisles-demo.git
cd stroke-deepisles-demo
# Install
uv sync
```
## Pull DeepISLES Docker Image
```bash
docker pull isleschallenge/deepisles
```
## Run Locally
### Option 1: Gradio UI
```bash
uv run python -m stroke_deepisles_demo.ui.app
# Open http://localhost:7860
```
### Option 2: CLI
```bash
# List available cases
uv run stroke-demo list
# Run on a specific case
uv run stroke-demo run --case sub-001 --fast
```
### Option 3: Python API
```python
from stroke_deepisles_demo.pipeline import run_pipeline_on_case
result = run_pipeline_on_case("sub-001", fast=True)
print(f"Dice score: {result.dice_score:.3f}")
print(f"Prediction: {result.prediction_mask}")
```
## Configuration
Set environment variables or create a `.env` file:
```bash
# .env
STROKE_DEMO_LOG_LEVEL=DEBUG
STROKE_DEMO_DEEPISLES_USE_GPU=false # If no GPU available
```
See [Configuration Guide](configuration.md) for all options.
```
### `docs/guides/configuration.md`
```markdown
# Configuration
All settings can be configured via environment variables.
## Environment Variables
| Variable | Default | Description |
|----------|---------|-------------|
| `STROKE_DEMO_LOG_LEVEL` | `INFO` | Logging level (DEBUG, INFO, WARNING, ERROR) |
| `STROKE_DEMO_LOG_FORMAT` | `simple` | Log format (simple, detailed, json) |
| `STROKE_DEMO_HF_DATASET_ID` | `YongchengYAO/ISLES24-MR-Lite` | HuggingFace dataset ID |
| `STROKE_DEMO_HF_CACHE_DIR` | `None` | Custom HF cache directory |
| `STROKE_DEMO_HF_TOKEN` | `None` | HuggingFace API token (for private datasets) |
| `STROKE_DEMO_DEEPISLES_DOCKER_IMAGE` | `isleschallenge/deepisles` | DeepISLES Docker image |
| `STROKE_DEMO_DEEPISLES_FAST_MODE` | `true` | Use single-model mode |
| `STROKE_DEMO_DEEPISLES_TIMEOUT_SECONDS` | `1800` | Inference timeout |
| `STROKE_DEMO_DEEPISLES_USE_GPU` | `true` | Use GPU acceleration |
| `STROKE_DEMO_RESULTS_DIR` | `./results` | Directory for output files |
## Using .env File
Create a `.env` file in the project root:
```bash
STROKE_DEMO_LOG_LEVEL=DEBUG
STROKE_DEMO_DEEPISLES_USE_GPU=false
STROKE_DEMO_RESULTS_DIR=/data/results
```
## Programmatic Configuration
```python
from stroke_deepisles_demo.core.config import settings, reload_settings
import os
# Check current settings
print(settings.log_level)
# Override via environment
os.environ["STROKE_DEMO_LOG_LEVEL"] = "DEBUG"
reload_settings()
print(settings.log_level) # DEBUG
```
```
## ci configuration
### `.github/workflows/ci.yml`
```yaml
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync
- name: Lint with ruff
run: uv run ruff check .
- name: Check formatting
run: uv run ruff format --check .
typecheck:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync
- name: Type check with mypy
run: uv run mypy src/
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync
- name: Run tests
run: uv run pytest --cov --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
files: ./coverage.xml
integration:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v4
- name: Set up Python
run: uv python install 3.12
- name: Install dependencies
run: uv sync
- name: Run integration tests
run: uv run pytest -m integration --timeout=600
```
## final code review checklist
### code quality
- [ ] All functions have type hints
- [ ] All public functions have docstrings
- [ ] No unused imports or variables
- [ ] No hardcoded paths or secrets
- [ ] Error messages are helpful
### testing
- [ ] Unit test coverage > 80%
- [ ] Edge cases covered
- [ ] Integration tests for critical paths
- [ ] Tests are deterministic (no flakiness)
### documentation
- [ ] README is clear and accurate
- [ ] CONTRIBUTING.md is complete
- [ ] All configuration options documented
- [ ] Example usage in docstrings
### security
- [ ] No secrets in code
- [ ] HF_TOKEN is optional and hidden from logs
- [ ] Docker commands are properly escaped
- [ ] No arbitrary code execution vulnerabilities
### production readiness
- [ ] Logging is consistent and useful
- [ ] Errors are handled gracefully
- [ ] Configuration is environment-driven
- [ ] CI passes on all checks
## tdd plan
### tests for logging
```python
"""Tests for logging configuration."""
from __future__ import annotations
import logging
from stroke_deepisles_demo.core.logging import get_logger, setup_logging
class TestSetupLogging:
"""Tests for setup_logging."""
def test_sets_log_level(self) -> None:
"""Sets the root logger level."""
setup_logging("DEBUG")
assert logging.getLogger().level == logging.DEBUG
def test_format_styles(self) -> None:
"""Different format styles work."""
for style in ["simple", "detailed", "json"]:
setup_logging("INFO", format_style=style)
# Should not raise
class TestGetLogger:
"""Tests for get_logger."""
def test_returns_namespaced_logger(self) -> None:
"""Returns logger with stroke_demo prefix."""
logger = get_logger("my_module")
assert logger.name == "stroke_demo.my_module"
```
### tests for configuration
```python
"""Tests for configuration."""
from __future__ import annotations
import os
from pathlib import Path
import pytest
from stroke_deepisles_demo.core.config import Settings, reload_settings
class TestSettings:
"""Tests for Settings."""
def test_default_values(self) -> None:
"""Has sensible defaults."""
settings = Settings()
assert settings.log_level == "INFO"
assert settings.hf_dataset_id == "YongchengYAO/ISLES24-MR-Lite"
def test_env_override(self, monkeypatch) -> None:
"""Environment variables override defaults."""
monkeypatch.setenv("STROKE_DEMO_LOG_LEVEL", "DEBUG")
settings = Settings()
assert settings.log_level == "DEBUG"
def test_hf_token_hidden_from_repr(self) -> None:
"""HF token is not visible in repr."""
settings = Settings(hf_token="secret123")
assert "secret123" not in repr(settings)
def test_results_dir_created(self, tmp_path: Path) -> None:
"""Results directory is created if it doesn't exist."""
new_dir = tmp_path / "new_results"
settings = Settings(results_dir=new_dir)
assert new_dir.exists()
```
## "done" criteria
Phase 5 is complete when:
1. Structured logging is in place throughout
2. All settings are configurable via environment
3. README.md and CONTRIBUTING.md are complete
4. Developer guides are written
5. CI workflow passes on GitHub Actions
6. Code coverage > 80% overall
7. All code review checklist items pass
8. Repository is ready for others to contribute
## final deliverables
At the end of all phases, the repository contains:
```
stroke-deepisles-demo/
βββ .github/
β βββ workflows/
β βββ ci.yml
βββ docs/
β βββ specs/
β βββ guides/
β βββ reference/
βββ src/
β βββ stroke_deepisles_demo/
β βββ core/
β βββ data/
β βββ inference/
β βββ ui/
β βββ pipeline.py
β βββ metrics.py
β βββ cli.py
βββ tests/
βββ pyproject.toml
βββ uv.lock
βββ README.md
βββ CONTRIBUTING.md
βββ CHANGELOG.md
βββ .pre-commit-config.yaml
βββ .gitignore
βββ .env.example
βββ app.py # HF Spaces entry point
```
|