How sbrussee/pathforge works
Purpose and Entry Points
PathForge is a config-driven Python framework for computational-pathology workflows: WSI tiling/feature extraction, H5 artifact production, MIL training/benchmarking/optimization, slide retrieval, and packaged-model inference. The primary CLI surface is the umbrella app in src/pathforge/cli/app.py, with workflow commands under src/pathforge/cli/ and default runnable templates in default_config/.
Configuration, Policies, and Experiment Control
YAML is parsed and validated into the central configuration model, then mode-specific policies coordinate work. Feature extraction, benchmarking, inference, and Optuna optimization are policy boundaries rather than CLI business logic.
• src/pathforge/config/config.py
• src/pathforge/policy/feature_extraction.py
• src/pathforge/policy/benchmarking.py
• src/pathforge/policy/optimization.py
• src/pathforge/policy/inference.py
Dataset and Artifact Boundary
Annotation rows define datasets and slide identity. WSI datasets resolve slide files and metadata; bag datasets load feature bags and labels for MIL. Per-slide H5 artifacts are the durable interchange format between extraction, training, retrieval, reporting, and inference. Their core invariant is row alignment between tile coordinates and feature matrices.
• src/pathforge/core/datasets/wsi_dataset.py
• src/pathforge/core/datasets/bag_dataset.py
• src/pathforge/core/datasets/bag_schema.py
• src/pathforge/core/io/h5/features.py
• src/pathforge/core/io/h5/layout.py
Slide Processing and Feature Extraction
A registry-selected slide processor performs tissue segmentation, tiling, optional tile-overview generation, and feature extraction. The bundled LazySlide implementation is the main WSI backend; feature extractors are independently selected through factories/registries. Processing emits reusable H5 bags keyed by tiling and extractor choices.
• src/pathforge/core/slide_processing/factory.py
• src/pathforge/core/slide_processing/lazyslide/processor.py
• src/pathforge/core/slide_processing/lazyslide/feature_extraction_patch.py
• src/pathforge/core/feature_extractors/factory.py
• src/pathforge/core/feature_extractors/registry.py
MIL Training, Tasks, and Backend Adapters
Task definitions normalize classification, regression, survival, and retrieval behavior. Native models and trainers operate on canonical bag dictionaries; Lightning is the principal deep-learning runtime, with a sklearn path for slide-level models. Registries decouple policies from concrete models, losses, trainers, metrics, and explainers.
• src/pathforge/core/tasks/mil.py
• src/pathforge/core/models/mil_base.py
• src/pathforge/training/lightning.py
• src/pathforge/training/sklearn_trainer.py
• src/pathforge/utils/registries.py
Optional integrations are deliberately isolated: TorchMIL and MIL-Lab model adapters, plus TorchMetrics/TorchSurv metric/loss adapters, prevent optional dependencies from entering core domain and training code.
• src/pathforge/adapters/torchmil/backend.py
• src/pathforge/adapters/mil_lab/backend.py
• src/pathforge/adapters/metrics/classification.py
• src/pathforge/utils/optional/
Evaluation, Optimization, and Distributed Execution
Benchmark and optimization policies enumerate or sample pipeline combinations, train/evaluate them, and produce normalized ranked results. Evaluation and visualization use registries/orchestrators. Optuna is the search/pruning service; distributed planning/execution supports local or cluster-oriented staged workloads, with SLURM scripts supplied.
• src/pathforge/core/evaluation/orchestrator.py
• src/pathforge/core/experiments/combinations.py
• src/pathforge/execution/distributed.py
• scripts/run_distributed_smoke.sbatch
Retrieval, Inference, and Outputs
Slide retrieval consumes existing H5 bags, creates configurable representations, searches reference pools, aggregates rankings, evaluates metrics, and renders results—without training. Packaged-model inference reads H5 bags and can persist normalized per-instance heatmaps in the artifact prediction namespace.
• src/pathforge/core/tasks/slide_retrieval.py
• src/pathforge/slide_retrieval/representation_strategies/registry.py
• src/pathforge/slide_retrieval/search_strategies/registry.py
• src/pathforge/inference/model_package.py
• src/pathforge/inference/heatmaps.py
Main entry points: app.py PathForge CLI · features.py H5 feature bags · combinations.py Experiment combinations · feature_extraction.py Feature extraction policy
Components 22
CLI & Control
- PathForge CLI · Umbrella workflow command surface.
src/pathforge/cli/app.py - Configuration model · Central configuration and defaults contract.
src/pathforge/config/config.py - Feature extraction policy
src/pathforge/policy/feature_extraction.py - Benchmark policy
src/pathforge/policy/benchmarking.py - Optimization policy
src/pathforge/policy/optimization.py - Inference policy
src/pathforge/policy/inference.py
Data & Artifacts
- WSI dataset · Resolves annotation rows, slide files, and metadata.
src/pathforge/core/datasets/wsi_dataset.py - Bag dataset · Loads feature bags and labels.
src/pathforge/core/datasets/bag_dataset.py - H5 feature bags · Coordinates and feature rows remain aligned.
src/pathforge/core/io/h5/features.py - Bag schema · Defines the H5 bag contract.
src/pathforge/core/datasets/bag_schema.py
WSI Processing
- Slide processor factory
src/pathforge/core/slide_processing/factory.py - LazySlide processor · Segments tissue, tiles slides, and orchestrates extraction.
src/pathforge/core/slide_processing/lazyslide/processor.py - Feature extractor registry
src/pathforge/core/feature_extractors/registry.py
MIL & Evaluation
- MIL tasks · Normalizes classification, regression, survival, and retrieval.
src/pathforge/core/tasks/mil.py - MIL model contract
src/pathforge/core/models/mil_base.py - Lightning trainer · Principal deep-learning backend.
src/pathforge/training/lightning.py - Evaluation orchestrator · Coordinates normalized evaluation and visualization.
src/pathforge/core/evaluation/orchestrator.py - Experiment combinations · Enumerates benchmark pipeline variants.
src/pathforge/core/experiments/combinations.py - Distributed execution · Plans local or cluster-oriented workloads.
src/pathforge/execution/distributed.py
Retrieval & Inference
- Slide retrieval · Builds representations, searches reference pools, and ranks slides.
src/pathforge/core/tasks/slide_retrieval.py - Model package inference · Runs trained packages against existing bags.
src/pathforge/inference/model_package.py - Prediction heatmaps · Persists normalized per-instance predictions.
src/pathforge/inference/heatmaps.py
Connections 24
- PathForge CLI → Configuration model: loads
- PathForge CLI → Feature extraction policy: starts
- PathForge CLI → Benchmark policy: starts
- PathForge CLI → Optimization policy: starts
- PathForge CLI → Inference policy: starts
- Feature extraction policy → WSI dataset: selects slides
- Feature extraction policy → Slide processor factory: selects processor
- Slide processor factory → LazySlide processor: creates
- LazySlide processor → Feature extractor registry: selects extractor
- LazySlide processor → H5 feature bags: writes bags
- Bag schema → H5 feature bags: defines layout
- H5 feature bags → Bag dataset: supplies bags
- Benchmark policy → Experiment combinations: enumerates
- Optimization policy → Experiment combinations: samples variants
- Experiment combinations → MIL tasks: configures task
- MIL tasks → MIL model contract: drives
- Bag dataset → Lightning trainer: feeds bags
- MIL model contract → Lightning trainer: trains
- Lightning trainer → Evaluation orchestrator: evaluates
- Experiment combinations → Distributed execution: plans stages
- H5 feature bags → Slide retrieval: searches bags
- Inference policy → Model package inference: runs
- H5 feature bags → Model package inference: reads bags
- Model package inference → Prediction heatmaps: writes predictions