Spaces:
Running on Zero
Running on Zero
copilot-swe-agent[bot] raylim commited on
Commit ·
71ae2f0
1
Parent(s): e6c73c0
Add comprehensive documentation improvements
Browse files- Fix installation instructions in README (correct repo URL)
- Fix command name inconsistency (mosaic_app -> mosaic)
- Add detailed examples section to README
- Add CSV file format documentation
- Add cancer subtypes reference
- Add troubleshooting section
- Add advanced usage examples
- Create CONTRIBUTING.md with development guidelines
- Add comprehensive docstrings to all modules
- Add module-level docstrings to core modules
Co-authored-by: raylim <3074310+raylim@users.noreply.github.com>
Files changed (9)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267# Contributing to Mosaic
Thank you for your interest in contributing to Mosaic! This document provides guidelines and instructions for contributing to the project.
## Table of Contents
- [Getting Started](#getting-started)- [Development Setup](#development-setup)- [Code Style](#code-style)- [Testing](#testing)- [Submitting Changes](#submitting-changes)- [Reporting Issues](#reporting-issues)
## Getting Started
1. Fork the repository on GitHub2. Clone your fork locally3. Set up the development environment4. Create a new branch for your changes5. Make your changes6. Test your changes7. Submit a pull request
## Development Setup
### Prerequisites
- Python 3.10 or higher- [uv](https://docs.astral.sh/uv/) package manager- NVIDIA GPU with CUDA support (for model inference)
### Installation
1. Clone the repository:
```bashgit clone https://github.com/pathology-data-mining/mosaic.gitcd mosaic```
2. Install dependencies including development tools:
```bashuv sync```
This will install all dependencies, including development tools like pytest, pylint, and black.
### Running Tests
Run all tests:
```bashpytest tests/```
Run tests with coverage report:
```bashpytest tests/ --cov=src/mosaic --cov-report=term-missing```
Run a specific test file:
```bashpytest tests/inference/test_data.py -v```
### Code Quality
#### Linting
We use pylint for code linting. Run it with:
```bashpylint src/mosaic```
#### Code Formatting
We use black for code formatting. Format your code with:
```bashblack src/mosaic tests/```
## Code Style
### Python Style Guide
- Follow [PEP 8](https://pep8.org/) style guidelines- Use meaningful variable and function names- Add docstrings to all public functions, classes, and modules- Keep functions focused and concise- Use type hints where appropriate
### Docstring Format
Use Google-style docstrings:
```pythondef function_name(param1: str, param2: int) -> bool: """Brief description of the function. More detailed description if needed. Args: param1: Description of param1 param2: Description of param2 Returns: Description of return value Raises: ValueError: Description of when this error is raised """ pass```
### Commit Messages
- Use clear and descriptive commit messages- Start with a verb in the imperative mood (e.g., "Add", "Fix", "Update")- Keep the first line under 72 characters- Provide additional context in the commit body if needed
Example:
```Add docstrings to inference module functions
- Added comprehensive docstrings to all public functions- Included type hints for better code clarity- Updated existing docstrings to follow Google style```
## Testing
### Writing Tests
- Write tests for all new features and bug fixes- Place tests in the appropriate directory under `tests/`- Use pytest fixtures for common setup code- Mock external dependencies (e.g., model loading, network requests)- Ensure tests can run without GPU access or large model downloads
### Test Structure
```pythonimport pytestfrom mosaic.module import function_to_test
def test_function_basic_case(): """Test basic functionality of the function.""" result = function_to_test(input_data) assert result == expected_output
def test_function_edge_case(): """Test edge cases.""" with pytest.raises(ValueError): function_to_test(invalid_input)```
## Submitting Changes
### Pull Request Process
1. **Create a feature branch**: ```bash git checkout -b feature/your-feature-name ```
2. **Make your changes**: - Write clear, focused commits - Add tests for new functionality - Update documentation as needed
3. **Ensure code quality**: ```bash black src/mosaic tests/ pylint src/mosaic pytest tests/ ```
4. **Push to your fork**: ```bash git push origin feature/your-feature-name ```
5. **Create a Pull Request**: - Go to the GitHub repository - Click "New Pull Request" - Select your branch - Provide a clear description of your changes - Reference any related issues
### Pull Request Guidelines
- Keep pull requests focused on a single feature or fix- Update documentation for any changed functionality- Add or update tests as appropriate- Ensure all tests pass before submitting- Respond to review feedback promptly
## Reporting Issues
### Bug Reports
When reporting a bug, please include:
- A clear and descriptive title- Steps to reproduce the issue- Expected behavior- Actual behavior- System information (OS, Python version, GPU model)- Relevant log output or error messages- Minimal code example to reproduce the issue
### Feature Requests
When suggesting a feature, please include:
- A clear description of the feature- The use case and benefits- Any alternative solutions you've considered- Examples of how the feature would be used
### Issue Templates
Please use the appropriate issue template when creating a new issue.
## Development Guidelines
### Module Organization
- Keep modules focused on a single responsibility- Place UI-related code in `src/mosaic/ui/`- Place inference code in `src/mosaic/inference/`- Place analysis logic in `src/mosaic/analysis.py`- Avoid circular dependencies
### Adding New Features
When adding new features:
1. Discuss the feature in an issue first2. Follow the existing code structure3. Add comprehensive tests4. Update relevant documentation5. Consider backward compatibility
### Dependencies
- Avoid adding new dependencies unless necessary- Discuss new dependencies in an issue or pull request- Ensure dependencies are compatible with the project's license- Pin dependency versions in `pyproject.toml`
## Questions?
If you have questions about contributing, please:
- Check existing issues and pull requests- Open a new issue with your question- Join our community discussions (if available)
Thank you for contributing to Mosaic!3 unmodified lines456789101113 unmodified lines2526272829303117 unmodified lines49505152535455565758596061626364656667686970718 unmodified lines808182838485861 unmodified line888990913 unmodified lines## Table of Contents- [Installation](#installation)- [Usage](#usage)### System requirements13 unmodified lines## Installation```bashuv pip install git+ssh://git@github.com/pathology-data-mining/paladin_webapp.git@dev```## Usage17 unmodified linesRun the web application with:```bashmosaic_app```It will start a web server on port 7860 by default. You can access the web interface by navigating to `http://localhost:7860` in your web browser.### Command Line InterfaceTo process a WSI, use the following command:```bashmosaic_app --slide-path /path/to/your/wsi.svs --output-dir /path/to/output/directory```To process a batch of WSIs, use:```bashmosaic_app --slide-csv /path/to/your/wsi_list.csv --output-dir /path/to/output/directory```The CSV file should at least contain columns `Slide`, and `Site Type`.8 unmodified linesSee additional options with the help command. This command may take a few seconds to run:```bashmosaic_app --help```If setting port to run in server mode, you may check for available ports using `ss -tuln | grep :PORT` where PORT is the port number you want to check. No output indicates the port may be available. If port is available, set environment variable `export GRADIO_SERVER_PORT="PORT"`1 unmodified line### Notes- The first time you run the application, it will download the necessary models from HuggingFace. This may take some time depending on your internet connection.- The models are downloaded to a directory relative to where you run the application. (A subdirectory named `data`).3 unmodified lines4567891011121314151617181920212223242513 unmodified lines39404142434445464748495051525317 unmodified lines71727374757677787980818283848586878889909192938 unmodified lines1021031041051061071081 unmodified line1101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972983 unmodified lines## Table of Contents- [System Requirements](#system-requirements)- [Pre-requisites](#pre-requisites)- [Installation](#installation)- [Usage](#usage)- [Initial Setup](#initial-setup)- [Web Application](#web-application)- [Command Line Interface](#command-line-interface)- [Notes](#notes)- [Output Files](#output-files)- [Examples](#examples)- [Advanced Usage](#advanced-usage)- [CSV File Format](#csv-file-format)- [Cancer Subtypes](#cancer-subtypes)- [Troubleshooting](#troubleshooting)- [Contributing](#contributing)- [License](#license)### System requirements13 unmodified lines## Installation```bashgit clone https://github.com/pathology-data-mining/mosaic.gitcd mosaicuv sync```Alternatively, install directly from the repository:```bashuv pip install git+https://github.com/pathology-data-mining/mosaic.git```## Usage17 unmodified linesRun the web application with:```bashmosaic```It will start a web server on port 7860 by default. You can access the web interface by navigating to `http://localhost:7860` in your web browser.### Command Line InterfaceTo process a single WSI, use the following command:```bashmosaic --slide-path /path/to/your/wsi.svs --output-dir /path/to/output/directory```To process a batch of WSIs, use:```bashmosaic --slide-csv /path/to/your/wsi_list.csv --output-dir /path/to/output/directory```The CSV file should at least contain columns `Slide`, and `Site Type`.8 unmodified linesSee additional options with the help command. This command may take a few seconds to run:```bashmosaic --help```If setting port to run in server mode, you may check for available ports using `ss -tuln | grep :PORT` where PORT is the port number you want to check. No output indicates the port may be available. If port is available, set environment variable `export GRADIO_SERVER_PORT="PORT"`1 unmodified line### Notes- The first time you run the application, it will download the necessary models from HuggingFace. This may take some time depending on your internet connection.- The models are downloaded to a directory named `data` relative to where you run the application.## Output Files### Single Slide ProcessingWhen processing a single slide, the following files are generated in the output directory:- `{slide_name}_mask.png`: Visualization of the tissue segmentation- `{slide_name}_aeon_results.csv`: Cancer subtype predictions with confidence scores (if cancer subtype was set to "Unknown")- `{slide_name}_paladin_results.csv`: Biomarker predictions for the slide### Batch ProcessingWhen processing multiple slides, in addition to individual slide outputs, combined results are generated:- `combined_aeon_results.csv`: Cancer subtype predictions for all slides in a single file- `combined_paladin_results.csv`: Biomarker predictions for all slides in a single file## Examples### Example 1: Process a single slide with unknown cancer type```bashmosaic --slide-path /data/slides/sample.svs \--output-dir /data/results \--site-type Primary \--cancer-subtype Unknown \--segmentation-config Resection```### Example 2: Process a single breast cancer slide with known IHC subtype```bashmosaic --slide-path /data/slides/breast_sample.svs \--output-dir /data/results \--site-type Primary \--cancer-subtype BRCA \--ihc-subtype "HR+/HER2-" \--segmentation-config Biopsy```### Example 3: Process multiple slides from CSVCreate a CSV file `slides.csv` with the following format:```csvSlide,Site Type,Cancer Subtype,Segmentation Config,IHC Subtype/data/slides/sample1.svs,Primary,Unknown,Resection,/data/slides/sample2.svs,Metastatic,LUAD,Biopsy,/data/slides/sample3.svs,Primary,BRCA,TCGA,HR+/HER2-```Then run:```bashmosaic --slide-csv slides.csv --output-dir /data/results```## Advanced Usage### Adjusting PerformanceYou can control the number of workers for feature extraction to balance between speed and memory usage:```bashmosaic --slide-path /path/to/slide.svs \--output-dir /path/to/output \--num-workers 8```### Running in Server ModeTo run Mosaic as a web server accessible from other machines:```bashexport GRADIO_SERVER_PORT=7860mosaic --server-name 0.0.0.0 --server-port 7860```Check for available ports using:```bashss -tuln | grep :7860```To share the application publicly (use with caution):```bashmosaic --share```### Debug ModeEnable debug logging for troubleshooting:```bashmosaic --debug```This will create a `debug.log` file with detailed information about the processing steps.## CSV File FormatWhen processing multiple slides using the `--slide-csv` option, the CSV file must contain the following columns:### Required Columns- **Slide**: Full path to the WSI file (e.g., `/path/to/slide.svs`)- **Site Type**: Either `Primary` or `Metastatic`### Optional Columns- **Cancer Subtype**: OncoTree code for the cancer subtype (e.g., `LUAD`, `BRCA`, `COAD`). Use `Unknown` to let Aeon infer the cancer type.- **Segmentation Config**: One of `Biopsy`, `Resection`, or `TCGA`. Defaults to `Biopsy` if not specified.- **IHC Subtype**: For breast cancer (BRCA) only. One of:- `HR+/HER2+`- `HR+/HER2-`- `HR-/HER2+`- `HR-/HER2-`### CSV Example```csvSlide,Site Type,Cancer Subtype,Segmentation Config,IHC Subtype/data/slides/lung1.svs,Primary,LUAD,Resection,/data/slides/breast1.svs,Primary,BRCA,Biopsy,HR+/HER2-/data/slides/unknown1.svs,Metastatic,Unknown,TCGA,```## Cancer SubtypesMosaic uses OncoTree codes to identify cancer subtypes. Common examples include:- **LUAD**: Lung Adenocarcinoma- **LUSC**: Lung Squamous Cell Carcinoma- **BRCA**: Breast Invasive Carcinoma- **COAD**: Colon Adenocarcinoma- **READ**: Rectal Adenocarcinoma- **PRAD**: Prostate Adenocarcinoma- **SKCM**: Skin Cutaneous MelanomaFor a complete list of supported cancer subtypes, see the [OncoTree website](http://oncotree.mskcc.org/).When the cancer subtype is set to `Unknown`, Mosaic will use the Aeon model to predict the most likely cancer subtype based on the H&E image features.## Troubleshooting### HuggingFace Authentication ErrorsIf you encounter authentication errors when downloading models:1. Ensure you have access to the PDM-Group on HuggingFace2. Create a HuggingFace access token with appropriate permissions3. Set the `HF_TOKEN` environment variable correctly### Out of Memory ErrorsIf you encounter GPU out-of-memory errors:1. Reduce the number of workers: `--num-workers 2`2. Process slides sequentially instead of in batch3. Consider using a GPU with more memory### Tissue Segmentation IssuesIf tissue is not being detected correctly:1. Try a different segmentation configuration (`Biopsy`, `Resection`, or `TCGA`)2. Check that the slide file is not corrupted3. Verify the slide format is supported (e.g., `.svs`, `.tif`)### Port Already in UseIf the default port 7860 is already in use:1. Check for running processes: `ss -tuln | grep :7860`2. Use a different port: `export GRADIO_SERVER_PORT=7861`3. Or specify the port directly: `mosaic --server-port 7861`## ContributingWe welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines on how to contribute to this project.## LicenseThis project is licensed under the terms specified in the LICENSE file.
12318 unmodified lines222324252627import pickleimport torchimport pandas as pd18 unmodified linesnum_workers=4,progress=gr.Progress(track_tqdm=True),):if slide_path is None:raise gr.Error("Please upload a slide.")# Step 1: Segment tissue12345678918 unmodified lines28293031323334353637383940414243444546474849505152535455565758596061626364"""Core slide analysis module for Mosaic.This module provides the main slide analysis pipeline that integrates tissue segmentation,feature extraction, and model inference for cancer subtype and biomarker prediction."""import pickleimport torchimport pandas as pd18 unmodified linesnum_workers=4,progress=gr.Progress(track_tqdm=True),):"""Analyze a whole slide image for cancer subtype and biomarker prediction.This function performs a complete analysis pipeline including:1. Tissue segmentation2. CTransPath feature extraction3. Feature filtering with marker classifier4. Optimus feature extraction on filtered tiles5. Aeon inference for cancer subtype (if not provided)6. Paladin inference for biomarker predictionArgs:slide_path: Path to the whole slide image fileseg_config: Segmentation configuration, one of "Biopsy", "Resection", or "TCGA"site_type: Site type, either "Primary" or "Metastatic"cancer_subtype: Cancer subtype (OncoTree code or "Unknown" for inference)cancer_subtype_name_map: Dictionary mapping cancer subtype names to codesihc_subtype: IHC subtype for breast cancer (optional)num_workers: Number of worker processes for feature extractionprogress: Gradio progress tracker for UI updatesReturns:tuple: (slide_mask, aeon_results, paladin_results)- slide_mask: PIL Image of tissue segmentation visualization- aeon_results: DataFrame with cancer subtype predictions and confidence scores- paladin_results: DataFrame with biomarker predictionsRaises:gr.Error: If no slide is providedgr.Warning: If no tissue is detected in the slideValueError: If an unknown segmentation configuration is provided"""if slide_path is None:raise gr.Error("Please upload a slide.")# Step 1: Segment tissue
12313 unmodified lines17181920212218 unmodified lines414243444546from argparse import ArgumentParserimport pandas as pdfrom pathlib import Path13 unmodified linesdef download_and_process_models():snapshot_download(repo_id="PDM-Group/paladin-aeon-models", local_dir="data")model_map = pd.read_csv(18 unmodified linesdef main():parser = ArgumentParser()parser.add_argument("--debug", action="store_true", help="Enable debug logging")parser.add_argument(12345678910111213 unmodified lines262728293031323334353637383940414218 unmodified lines61626364656667686970717273747576"""Mosaic command-line interface and entry point.This module provides the main CLI for the Mosaic application, handling:- Model downloading and initialization- Single slide processing- Batch slide processing from CSV- Launching the Gradio web interface"""from argparse import ArgumentParserimport pandas as pdfrom pathlib import Path13 unmodified linesdef download_and_process_models():"""Download models from HuggingFace and initialize cancer subtype mappings.Downloads the Paladin and Aeon models from the PDM-Group HuggingFace repositoryand creates mappings between cancer subtype names and OncoTree codes.Returns:tuple: (cancer_subtype_name_map, reversed_cancer_subtype_name_map, cancer_subtypes)- cancer_subtype_name_map: Dict mapping display names to OncoTree codes- reversed_cancer_subtype_name_map: Dict mapping OncoTree codes to display names- cancer_subtypes: List of all supported cancer subtype codes"""snapshot_download(repo_id="PDM-Group/paladin-aeon-models", local_dir="data")model_map = pd.read_csv(18 unmodified linesdef main():"""Main entry point for the Mosaic application.Parses command-line arguments and routes to the appropriate mode:- Single slide processing (--slide-path)- Batch processing (--slide-csv)- Web interface (default, no slide arguments)Command-line arguments control analysis parameters like site type,cancer subtype, segmentation configuration, and output directory."""parser = ArgumentParser()parser.add_argument("--debug", action="store_true", help="Enable debug logging")parser.add_argument(
12312 unmodified lines16171819202126 unmodified lines484950515253import pickle # nosecimport sysfrom argparse import ArgumentParser12 unmodified linesfrom loguru import loggercancer_types_to_drop = ["UDMN","ADNOS",26 unmodified linesdef run(features, model_path, metastatic=False, batch_size=8, num_workers=8, use_cpu=False):device = torch.device("cuda" if not use_cpu and torch.cuda.is_available() else "cpu")12345678912 unmodified lines2223242526272826 unmodified lines555657585960616263646566676869707172737475"""Aeon model inference module for cancer subtype prediction.This module provides functionality to run the Aeon deep learning modelfor predicting cancer subtypes from H&E whole slide image features."""import pickle # nosecimport sysfrom argparse import ArgumentParser12 unmodified linesfrom loguru import logger# Cancer types excluded from prediction (too broad or ambiguous)cancer_types_to_drop = ["UDMN","ADNOS",26 unmodified linesdef run(features, model_path, metastatic=False, batch_size=8, num_workers=8, use_cpu=False):"""Run Aeon model inference for cancer subtype prediction.Args:features: NumPy array of tile features extracted from the WSImodel_path: Path to the pickled Aeon model filemetastatic: Whether the slide is from a metastatic sitebatch_size: Batch size for inferencenum_workers: Number of workers for data loadinguse_cpu: Force CPU usage instead of GPUReturns:tuple: (results_df, part_embedding)- results_df: DataFrame with cancer subtypes and confidence scores- part_embedding: Torch tensor of the learned part representation"""device = torch.device("cuda" if not use_cpu and torch.cuda.is_available() else "cpu")
123from enum import Enumfrom typing import List1234567891011"""Data structures and utilities for inference modules.This module provides:- Cancer type to integer mappings for model inputs/outputs- SiteType enum for primary vs metastatic classification- TileFeatureTensorDataset for feeding features to PyTorch models"""from enum import Enumfrom typing import List
12323 unmodified lines27282930313233343536377 unmodified lines454647484950515253544 unmodified lines596061626364651 unmodified line676869707172732 unmodified lines767778798081828324 unmodified lines10810911011111211310 unmodified lines124125126127128129130131132133134135136import csvimport pickle # nosecimport sys23 unmodified linesdef load_model_map(model_map_path: str) -> dict[Any, Any]:"""Load the table mapping cancer_subtypes and targets to the paladinmodel (a pickle file) that predicts that target for that cancer subtype.A dict is returned, mapping each cancer_subtype to a table mapping atarget to the pathname for the model that predicts it."""models = defaultdict(dict)with Path(model_map_path).open() as fp:7 unmodified linesdef load_aeon_scores(df: pd.DataFrame) -> dict[str, float]:"""Load the output table from a single-slide Aeon run, listing Oncotreecancer subtypes and their confidence values.A dict is returned, mapping each cancersubtype to its confidence score."""score = {}for _, row in df.iterrows():4 unmodified linesdef select_cancer_subtypes(aeon_scores: dict[str, float], k=1) -> list[str]:"""Return the three top-scoring cancer_subtypes, based on the given Aeon scores."""sorted_cancer_subtypes = list(sorted([(v, k) for k, v in aeon_scores.items()], reverse=True))1 unmodified linedef select_models(cancer_subtypes: list[str], model_map: dict[Any, Any]) -> list[Any]:""" """models = []for cancer_subtype, target, model in model_map.items():if cancer_subtype in cancer_subtypes:2 unmodified linesdef run_model(device, dataset, model_path: str, num_workers, batch_size) -> float:"""Run inference for the given embeddings and model.The point estimate is returned."""logger.debug(f"[loading model {model_path}]")24 unmodified linesdef logits_to_point_estimates(logits):# logits is a tensor of shape (batch_size, 2 * (n_clf_tasks + n_reg_tasks))# need to convert it to a tensor of shape (batch_size, n_clf_tasks + n_reg_tasks)return logits[:, ::2] / (logits[:, ::2] + logits[:, 1::2])10 unmodified linesnum_workers: int = NUM_WORKERS,use_cpu: bool = False,):"""Run Paladin inference on a single slide, using the given embeddingsand either a single model or a table mapping cancer_subtypes and targets to models.If cancer_subtype_codes is given, it is a list of OncoTree codes for the slide.If aeon_predictions_path is given, it is the pathname to a CSV filewith the output of an Aeon run on the slide.If both are given, an error is raised.The output is written to the given output_path (a CSV file)."""if aeon_results is not None:1234567891023 unmodified lines343536373839404142434445464748497 unmodified lines575859606162636465666768694 unmodified lines7475767778798081828384858687881 unmodified line909192939495969798991001011021031042 unmodified lines10710810911011111211311411511611711811912012112212324 unmodified lines14814915015115215315415515615715815916016116216316410 unmodified lines175176177178179180181182183184185186187188189190191192193194195196197198199200201202"""Paladin model inference module for biomarker prediction.This module provides functionality to run the Paladin deep learning modelsfor predicting various biomarkers from H&E whole slide image features, basedon the predicted or known cancer subtype."""import csvimport pickle # nosecimport sys23 unmodified linesdef load_model_map(model_map_path: str) -> dict[Any, Any]:"""Load the table mapping cancer subtypes and targets to Paladin models.A dict is returned, mapping each cancer_subtype to a table mapping atarget to the pathname for the model that predicts it.Args:model_map_path: Path to the CSV file containing the model mapReturns:Dictionary mapping cancer subtypes to their target-specific models"""models = defaultdict(dict)with Path(model_map_path).open() as fp:7 unmodified linesdef load_aeon_scores(df: pd.DataFrame) -> dict[str, float]:"""Load Aeon output table with cancer subtypes and confidence values.Args:df: DataFrame with columns 'Cancer Subtype' and 'Confidence'Returns:Dictionary mapping cancer subtypes to their confidence scores"""score = {}for _, row in df.iterrows():4 unmodified linesdef select_cancer_subtypes(aeon_scores: dict[str, float], k=1) -> list[str]:"""Select the top k cancer subtypes based on Aeon confidence scores.Args:aeon_scores: Dictionary mapping cancer subtypes to confidence scoresk: Number of top subtypes to select (default: 1)Returns:List of cancer subtype codes sorted by confidence (highest first)"""sorted_cancer_subtypes = list(sorted([(v, k) for k, v in aeon_scores.items()], reverse=True))1 unmodified linedef select_models(cancer_subtypes: list[str], model_map: dict[Any, Any]) -> list[Any]:"""Select Paladin models for the given cancer subtypes.Args:cancer_subtypes: List of cancer subtype codesmodel_map: Dictionary mapping cancer subtypes to their modelsReturns:List of tuples (cancer_subtype, target, model_path)"""models = []for cancer_subtype, target, model in model_map.items():if cancer_subtype in cancer_subtypes:2 unmodified linesdef run_model(device, dataset, model_path: str, num_workers, batch_size) -> float:"""Run inference for the given dataset and Paladin model.Args:device: Torch device (CPU or CUDA)dataset: TileFeatureTensorDataset containing the featuresmodel_path: Path to the pickled Paladin modelnum_workers: Number of workers for data loadingbatch_size: Batch size for inferenceReturns:Point estimate (predicted value) from the model"""logger.debug(f"[loading model {model_path}]")24 unmodified linesdef logits_to_point_estimates(logits):"""Convert model logits to point estimates for beta-binomial distribution.The logits tensor contains alpha and beta parameters interleaved.This function computes the mean of the beta-binomial distribution: alpha/(alpha+beta).Args:logits: Tensor of shape (batch_size, 2*(n_tasks)) with alpha/beta parametersReturns:Tensor of shape (batch_size, n_tasks) with point estimates"""# logits is a tensor of shape (batch_size, 2 * (n_clf_tasks + n_reg_tasks))# need to convert it to a tensor of shape (batch_size, n_clf_tasks + n_reg_tasks)return logits[:, ::2] / (logits[:, ::2] + logits[:, 1::2])10 unmodified linesnum_workers: int = NUM_WORKERS,use_cpu: bool = False,):"""Run Paladin inference for biomarker prediction on a single slide.Uses either Aeon predictions or user-provided cancer subtype codes to selectthe appropriate Paladin models for biomarker prediction.Args:features: NumPy array of tile features extracted from the WSIaeon_results: DataFrame with Aeon predictions (Cancer Subtype, Confidence)cancer_subtype_codes: List of OncoTree codes if cancer subtype is knownmodel_map_path: Path to CSV file mapping subtypes/targets to model pathsmodel_path: Path to a single Paladin model (alternative to model_map_path)metastatic: Whether the slide is from a metastatic sitebatch_size: Batch size for inferencenum_workers: Number of workers for data loadinguse_cpu: Force CPU usage instead of GPUReturns:DataFrame with columns: Cancer Subtype, Target, ScoreNote:Either aeon_results or cancer_subtype_codes must be provided, but not both.Either model_map_path or model_path must be provided, but not both."""if aeon_results is not None:
123import gradio as grimport pandas as pdfrom pathlib import Path12345678910111213"""Gradio web interface for Mosaic.This module provides the web-based user interface for analyzing whole slide images.It includes functionality for:- Multi-slide upload and analysis- Settings configuration (site type, cancer subtype, IHC subtype, segmentation)- Results visualization and export- CSV-based batch processing"""import gradio as grimport pandas as pdfrom pathlib import Path
12317 unmodified lines21222324252611 unmodified lines383940414243444 unmodified lines495051525354558 unmodified lines6465666768697039 unmodified lines110111112113114115import tempfilefrom pathlib import Pathimport pandas as pd17 unmodified linesdef get_oncotree_code_name(code):global oncotree_code_mapif code in oncotree_code_map.keys():return oncotree_code_map[code]11 unmodified linesdef create_user_directory(state, request: gr.Request):"""Create a unique directory for each user session."""session_hash = request.session_hashif session_hash is None:return None, None4 unmodified linesdef load_settings(slide_csv_path):"""Load settings from CSV file and validate columns."""settings_df = pd.read_csv(slide_csv_path, na_filter=False)if "Segmentation Config" not in settings_df.columns:settings_df["Segmentation Config"] = "Biopsy"8 unmodified linesdef validate_settings(settings_df, cancer_subtype_name_map, cancer_subtypes, reversed_cancer_subtype_name_map):"""Validate settings DataFrame and provide warnings for invalid entries."""settings_df.columns = SETTINGS_COLUMNSwarnings = []for idx, row in settings_df.iterrows():39 unmodified linesdef export_to_csv(df):if df is None or df.empty:raise gr.Error("No data to export.")csv_path = "paladin_results.csv"12345678910111217 unmodified lines303132333435363738394041424344454611 unmodified lines5859606162636465666768697071724 unmodified lines77787980818283848586878889909192939495968 unmodified lines10510610710810911011111211311411511611711811912012112212312412512612712839 unmodified lines168169170171172173174175176177178179180181182183184"""UI utility functions for the Mosaic Gradio interface.This module provides helper functions for:- OncoTree code lookup and caching- User session directory management- Settings CSV loading and validation- Data export functionality"""import tempfilefrom pathlib import Pathimport pandas as pd17 unmodified linesdef get_oncotree_code_name(code):"""Retrieve the human-readable name for an OncoTree code.Queries the OncoTree API to get the cancer subtype name correspondingto the given code. Results are cached to avoid repeated API calls.Args:code: OncoTree code (e.g., "LUAD", "BRCA")Returns:Human-readable cancer subtype name, or "Unknown" if not found"""global oncotree_code_mapif code in oncotree_code_map.keys():return oncotree_code_map[code]11 unmodified linesdef create_user_directory(state, request: gr.Request):"""Create a unique directory for each user session.Args:state: Gradio state object (unused)request: Gradio request object containing session hashReturns:Path to user's session directory, or None if no session hash available"""session_hash = request.session_hashif session_hash is None:return None, None4 unmodified linesdef load_settings(slide_csv_path):"""Load slide analysis settings from CSV file.Loads the CSV and ensures all required columns are present, adding defaultsfor optional columns if they are missing.Args:slide_csv_path: Path to the CSV file containing slide settingsReturns:DataFrame with columns: Slide, Site Type, Cancer Subtype, IHC Subtype, Segmentation ConfigRaises:ValueError: If required columns are missing from the CSV"""settings_df = pd.read_csv(slide_csv_path, na_filter=False)if "Segmentation Config" not in settings_df.columns:settings_df["Segmentation Config"] = "Biopsy"8 unmodified linesdef validate_settings(settings_df, cancer_subtype_name_map, cancer_subtypes, reversed_cancer_subtype_name_map):"""Validate and normalize slide analysis settings.Checks each row for valid values and normalizes cancer subtype names.Generates warnings for invalid entries and replaces them with defaults.Args:settings_df: DataFrame with slide settings to validatecancer_subtype_name_map: Dict mapping subtype display names to codescancer_subtypes: List of valid cancer subtype codesreversed_cancer_subtype_name_map: Dict mapping codes to display namesReturns:Validated DataFrame with normalized valuesNote:Invalid entries are replaced with defaults and warnings are displayedto the user via Gradio warnings."""settings_df.columns = SETTINGS_COLUMNSwarnings = []for idx, row in settings_df.iterrows():39 unmodified linesdef export_to_csv(df):"""Export a DataFrame to CSV file for download.Args:df: DataFrame to exportReturns:Path to the exported CSV fileRaises:gr.Error: If the DataFrame is None or empty"""if df is None or df.empty:raise gr.Error("No data to export.")csv_path = "paladin_results.csv"
CONTRIBUTING.mdAdded+267
README.md+214-7
src/mosaic/analysis.py+37
src/mosaic/gradio_app.py+30
src/mosaic/inference/aeon.py+22
src/mosaic/inference/data.py+8
src/mosaic/inference/paladin.py+83-17
src/mosaic/ui/app.py+10
src/mosaic/ui/utils.py+72-3