|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance for Claude Code when working with the Mussel codebase. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +Mussel is a computational pathology toolkit for processing whole-slide images (WSI). It provides CLI tools for tiling, feature extraction, annotation, and aggregation using various foundation models (OpenCLIP, ResNet-50, TransPath, Virchow, Virchow2, Prov-GigaPath, H-Optimus-0, GooglePath, CONCH). |
| 8 | + |
| 9 | +- **Language**: Python 3.10-3.11 |
| 10 | +- **Package Manager**: `uv` |
| 11 | +- **Build System**: setuptools (via `pyproject.toml`) |
| 12 | +- **License**: GPL-3.0 |
| 13 | + |
| 14 | +## Repository Structure |
| 15 | + |
| 16 | +``` |
| 17 | +mussel/ |
| 18 | + cli/ # CLI entry points (tessellate, extract_features, annotate, etc.) |
| 19 | + models/ # Foundation model implementations and factory |
| 20 | + datasets/ # Data loading (HDF5, tile coords, flat images) |
| 21 | + utils/ # Feature extraction, segmentation, file I/O, ML utilities |
| 22 | +tests/ |
| 23 | + testdata/ # Test slide images and fixtures |
| 24 | + mussel/ # Mirrors main package structure |
| 25 | +presets/ # Preset configurations (biopsy, resection, TCGA) |
| 26 | +``` |
| 27 | + |
| 28 | +## Common Commands |
| 29 | + |
| 30 | +### Install dependencies |
| 31 | +```bash |
| 32 | +uv sync --extra torch-gpu # PyTorch with CUDA |
| 33 | +uv sync --extra torch-cpu # PyTorch CPU-only |
| 34 | +uv sync --extra tensorflow-gpu # TensorFlow with CUDA |
| 35 | +``` |
| 36 | + |
| 37 | +### Run tests |
| 38 | +```bash |
| 39 | +uv run pytest tests |
| 40 | +``` |
| 41 | + |
| 42 | +### Run a specific test file |
| 43 | +```bash |
| 44 | +uv run pytest tests/mussel/cli/test_tessellate.py |
| 45 | +``` |
| 46 | + |
| 47 | +### Run a specific test |
| 48 | +```bash |
| 49 | +uv run pytest tests/mussel/cli/test_tessellate.py::test_function_name |
| 50 | +``` |
| 51 | + |
| 52 | +## Code Style & Conventions |
| 53 | + |
| 54 | +- **Formatter**: `black` |
| 55 | +- **Import sorting**: `isort` |
| 56 | +- **Type checking**: `mypy` |
| 57 | +- **Logging**: Standard `logging` module (not loguru) |
| 58 | +- Type hints are used throughout; follow existing patterns |
| 59 | +- Models use the factory pattern (`ModelFactory.create()`) |
| 60 | +- Dataset processing uses the strategy pattern (`get_dataset_processor()`) |
| 61 | + |
| 62 | +### Import style |
| 63 | + |
| 64 | +All imports belong at the top of the file. **Do not place imports inside functions or methods** unless one of these specific exceptions applies: |
| 65 | + |
| 66 | +1. **Optional / guarded dependency** — the import is inside a `try/except ImportError` block because the package may not be installed (e.g. `fsspec`, `flash_attn`, `tensorflow`, `gigapath`). |
| 67 | +2. **Platform-conditional import** — the import only makes sense on certain OSes (e.g. `fcntl` on Linux, `msvcrt` on Windows). |
| 68 | +3. **Circular-import workaround** — moving the import to the top would create a circular dependency. |
| 69 | + |
| 70 | +Everything else — stdlib modules (`os`, `tempfile`, `warnings`, `traceback`, `collections`, `multiprocessing`, `functools`), third-party packages that are always installed (`numpy`, `torch`, `omegaconf`), and local modules — must be at the top. |
| 71 | + |
| 72 | +## Hydra Configuration System |
| 73 | + |
| 74 | +All 13 CLI commands use Hydra with **structured configs only** (no YAML files). The pattern is: |
| 75 | + |
| 76 | +1. Define a `@dataclass` for the command's config with typed fields and defaults |
| 77 | +2. Register it with `ConfigStore` |
| 78 | +3. Decorate `main()` with `@hydra.main(version_base=None, config_path=".", config_name="...")` |
| 79 | + |
| 80 | +```python |
| 81 | +@dataclass |
| 82 | +class ExtractFeaturesConfig: |
| 83 | + slide_path: Optional[str] = None |
| 84 | + batch_size: int = 64 |
| 85 | + model_type: ModelType = ModelType.CLIP |
| 86 | + |
| 87 | +cs = ConfigStore.instance() |
| 88 | +cs.store(name="extract_features_config", node=ExtractFeaturesConfig) |
| 89 | + |
| 90 | +@hydra.main(version_base=None, config_path=".", config_name="extract_features_config") |
| 91 | +def main(cfg: ExtractFeaturesConfig): |
| 92 | + ... |
| 93 | +``` |
| 94 | + |
| 95 | +**Users pass config via Hydra command-line overrides** (not `--flag` style): |
| 96 | +```bash |
| 97 | +extract_features slide_path=slide.svs model_type=VIRCHOW batch_size=128 |
| 98 | +tessellate slide_path=slide.svs seg_config=biopsy # config group preset |
| 99 | +``` |
| 100 | + |
| 101 | +**Nested config groups** are used for segmentation presets (`seg_config=default|biopsy|resection|tcga`), defined as dataclass inheritance (e.g., `BiopsySegConfig(SegConfig)`). |
| 102 | + |
| 103 | +**Common OmegaConf patterns in the codebase**: |
| 104 | +- `OmegaConf.to_container(cfg.nested)` to unpack nested configs as dicts for `**kwargs` |
| 105 | +- `OmegaConf.structured(cfg)` to copy configs |
| 106 | +- `OmegaConf.create(cfg)` in tests to create configs programmatically |
| 107 | + |
| 108 | +## Key Architecture Patterns |
| 109 | + |
| 110 | +- **CLI modules** in `mussel/cli/` each expose a `main()` function registered as a console script in `pyproject.toml` |
| 111 | +- **Model loading** goes through `mussel/models/model_factory.py` which handles all supported foundation models |
| 112 | +- **Feature extraction** core logic lives in `mussel/utils/feature_extract.py` |
| 113 | +- **Tissue segmentation** is in `mussel/utils/segment.py` |
| 114 | +- **File I/O** with remote/cloud support is in `mussel/utils/file.py` (fsspec-based) |
| 115 | +- **Model caching** and downloading is handled by `mussel/utils/model_cache.py` |
| 116 | + |
| 117 | +## Dependencies Notes |
| 118 | + |
| 119 | +- PyTorch and TensorFlow are mutually exclusive install extras (see `[tool.uv]` conflicts in `pyproject.toml`) |
| 120 | +- Custom packages `transpath` and `timm_ctranspath` come from MSK Mind GitHub repos |
| 121 | +- The `fastattn` extra pins specific torch/xformers/flash-attn versions for GigaPath support |
| 122 | +- `numpy<2` is required for compatibility |
| 123 | +- `transformers<4.46` is required for model compatibility |
0 commit comments