diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md index dfd4324..032d382 100644 --- a/.github/copilot-instructions.md +++ b/.github/copilot-instructions.md @@ -1,6 +1,7 @@ # Copilot Instructions for CellAnnotator Canonical repo guidance lives in: + - `AGENTS.md` — architecture, invariants, and validation commands - `REVIEW_GUIDE.md` — PR review workflow, risk areas, and changed-path test lookup diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 969e3ec..caa2852 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -107,6 +107,6 @@ jobs: - test runs-on: ubuntu-latest steps: - - uses: re-actors/alls-green@release/v1 - with: - jobs: ${{ toJSON(needs) }} + - uses: re-actors/alls-green@release/v1 + with: + jobs: ${{ toJSON(needs) }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c7982e6..5268af8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,16 +7,16 @@ default_stages: minimum_pre_commit_version: 2.16.0 repos: - repo: https://github.com/biomejs/pre-commit - rev: v2.5.2 + rev: v2.6.8 hooks: - id: biome-format exclude: ^\.cruft\.json$ # inconsistent indentation with cruft - file never to be modified manually. - repo: https://github.com/tox-dev/pyproject-fmt - rev: v2.25.1 + rev: v2.31.2 hooks: - id: pyproject-fmt - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.15.20 + rev: v0.17.6 hooks: - id: ruff-check types_or: [python, pyi, jupyter] diff --git a/AGENTS.md b/AGENTS.md index aa1b06c..9348ed3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -6,6 +6,7 @@ Key frameworks: scanpy/AnnData, Pydantic, python-dotenv, rich, and the provider ## Trust Order When sources disagree: + 1. PR description and changed code 2. This file (`AGENTS.md`) 3. `REVIEW_GUIDE.md` @@ -16,18 +17,18 @@ Every fact should have one owner. This file owns invariants and the reference ta ## Where To Find What -| Topic | Source of truth | -|-------|----------------| -| User-facing overview, installation, quickstart | `README.md` | -| Contributor setup, environments, docs build, release workflow | `docs/contributing.md` | -| Public API reference | `docs/api.md` and autosummary under `docs/generated/` | -| Tutorials and example notebooks | `docs/notebooks/tutorials/` | -| PR review workflow and risk areas | `REVIEW_GUIDE.md` | -| Test fixtures (providers, example AnnData) | `tests/conftest.py`, `tests/utils.py` | -| LLM prompt templates | `src/cell_annotator/_prompts.py` | -| Structured LLM response schemas | `src/cell_annotator/_response_formats.py` | -| Provider wiring (OpenAI / Anthropic / Gemini) | `src/cell_annotator/model/_providers.py`, `llm_interface.py`, `base_annotator.py` | -| Release notes | GitHub Releases () | +| Topic | Source of truth | +| ------------------------------------------------------------- | --------------------------------------------------------------------------------- | +| User-facing overview, installation, quickstart | `README.md` | +| Contributor setup, environments, docs build, release workflow | `docs/contributing.md` | +| Public API reference | `docs/api.md` and autosummary under `docs/generated/` | +| Tutorials and example notebooks | `docs/notebooks/tutorials/` | +| PR review workflow and risk areas | `REVIEW_GUIDE.md` | +| Test fixtures (providers, example AnnData) | `tests/conftest.py`, `tests/utils.py` | +| LLM prompt templates | `src/cell_annotator/_prompts.py` | +| Structured LLM response schemas | `src/cell_annotator/_response_formats.py` | +| Provider wiring (OpenAI / Anthropic / Gemini) | `src/cell_annotator/model/_providers.py`, `llm_interface.py`, `base_annotator.py` | +| Release notes | GitHub Releases () | ## Review Guidelines @@ -37,7 +38,7 @@ This file only owns the project invariants and source-of-truth map below. ## Critical Invariants -- **Data privacy.** Only cluster marker gene *names*, plus `species`, `tissue`, and (optionally) `stage`, are sent to LLM providers. Raw expression values, cell-level data, and user metadata must never cross the provider boundary. Any PR that broadens what is sent is a hard red flag. +- **Data privacy.** Only cluster marker gene _names_, plus `species`, `tissue`, and (optionally) `stage`, are sent to LLM providers. Raw expression values, cell-level data, and user metadata must never cross the provider boundary. Any PR that broadens what is sent is a hard red flag. - **Structured outputs only.** All LLM calls round-trip through Pydantic models in `_response_formats.py` (e.g. `CellTypeListOutput`, `ExpectedMarkerGeneOutput`, `CellTypeMappingOutput`). Never parse free-form LLM text in production paths; renaming or removing fields breaks user-visible contracts. - **Optional provider extras.** `openai`, `anthropic`, and `google-genai` are optional extras. Provider-specific imports must be guarded and must fail with a clear message when the extra is missing; they must not leak into import-time code paths of the top-level package. - **API keys stay in the environment.** Keys are loaded from env vars or a local `.env` via python-dotenv (`APIKeyManager`). They must never be logged, serialized, or included in error messages / exceptions. @@ -60,6 +61,7 @@ pre-commit run --all-files # lint and format ``` Focused runs (with `uv`): + ```bash uv run pytest tests/model uv run pytest tests/test_utils.py diff --git a/README.md b/README.md index 293135d..90e57fc 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,8 @@ [badge-downloads]: https://static.pepy.tech/badge/cell-annotator [badge-zenodo]: https://zenodo.org/badge/899554552.svg - đŸ§Ŧ CellAnnotator is an [scverse ecosystem package](https://scverse.org/packages/#ecosystem), designed to annotate cell types in scRNA-seq data based on marker genes using large language models (LLMs). It supports OpenAI, Google Gemini, Anthropic Claude, and OpenRouter models out of the box. - ## ✨ Key Features - 🤖 **LLM-agnostic backend**: Seamlessly use models from OpenAI, Anthropic (Claude), Gemini (Google), or OpenRouter — just set your provider and API key. @@ -32,29 +30,29 @@ > â„šī¸ **Note:** This package is based on output generated by large language models and might **sometimes make mistakes**. We use some safeguards, like anchoring the tool in a multi-step process, and using structured output predictions, but mistakes are still possible. We recommend using this tool as a first step in an annotation workflow to generate an initial, coarse set of annotations that must be further refined. - ## đŸ“Ļ Installation + You need to have 🐍 Python 3.11 or newer installed on your system. If you don't have Python installed, we recommend installing [Mambaforge][]. - ### 🚀 PyPI + Install by running: ```bash pip install cell-annotator ``` - ### đŸ› ī¸ Development version + To install the latest development version from [GitHub](https://github.com/quadbio/cell-annotator), run ```bash pip install git+https://github.com/quadbio/cell-annotator.git@main ``` - ## 🏁 Getting started + After installation, head over to the LLM provider of your choice to generate an API key 🔑. For example: - OpenAI: [API key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key) @@ -62,10 +60,8 @@ After installation, head over to the LLM provider of your choice to generate an - Anthropic (Claude): [API key](https://docs.anthropic.com/en/docs/get-started) - OpenRouter: [API key](https://openrouter.ai/settings/keys) - 🔒 Keep this key private and don't share it with anyone. `CellAnnotator` will try to read the key as an environmental variable - either expose it to the environment yourself, or store it as an `.env` file anywhere within the repository where you conduct your analysis and plan to run `CellAnnotator`. The package will then use [dotenv](https://pypi.org/project/python-dotenv/) to export the key from the `env` file as an environmental variable. - Here's the simplest way to annotate your data: ```python @@ -76,7 +72,6 @@ cell_ann = CellAnnotator( ).annotate_clusters() ``` - By default, this will store annotations in `adata.obs['cell_type_predicted']`. Head over to our 📚 [tutorials](https://cell-annotator.readthedocs.io/en/latest/notebooks/tutorials/index.html) to see more advanced use cases, and learn how to adapt this to your own data. You can run `CellAnnotator` for just a single sample of data, or across multiple samples. In the latter case, it will attempt to harmonize annotations across samples. ### Advanced provider options @@ -104,9 +99,8 @@ cell_ann.get_cluster_markers() cell_ann.annotate_clusters(key_added="cell_type_predicted") ``` - - ## 💸 Costs and models + CellAnnotator is LLM-agnostic and works with multiple providers: - **OpenAI (GPT models):** The default model is currently `gpt-4o-mini`, which is included in [OpenAI's Free Usage Tier](https://platform.openai.com/docs/guides/rate-limits). You can get started for free and experiment with our 📚 [tutorials](https://cell-annotator.readthedocs.io/en/latest/notebooks/tutorials/index.html) and your own data. For more accurate cell type labels in complex tissues, we recommend more powerful models like `gpt-4o`, `gpt-4.1`, or reasoning models like `o3-mini` (these may incur a small fee; e.g., running both tutorials with `o3-mini` costs around 1 USD). See the [OpenAI API docs](https://platform.openai.com/docs/models) for details. @@ -119,25 +113,24 @@ CellAnnotator is LLM-agnostic and works with multiple providers: You can select your provider and model by setting the appropriate parameters. More providers may be supported in the future as the LLM ecosystem evolves. - - ## 🔐 Data privacy + This package sends cluster marker genes, and the `species` and `tissue` you define, to the selected LLM provider (e.g., OpenAI, Google, Anthropic, or OpenRouter routes). **No actual gene expression values are sent.** Please ensure your usage of this package aligns with your institution's guidelines on data privacy and the use of external AI models. Each provider has its own privacy policy and terms of service. Review these carefully before using CellAnnotator with sensitive or regulated data. When using OpenRouter, requests are forwarded to the upstream provider implied by your model slug (e.g. `openai/...`, `anthropic/...`). Review both [OpenRouter's privacy policy](https://openrouter.ai/privacy) and the upstream provider's. Some OpenRouter model tiers may log prompts by default; users who need privacy guarantees should configure this via their OpenRouter account settings. - ## 🙏 Credits -This tool was inspired by [Hou et al., Nature Methods 2024](https://www.nature.com/articles/s41592-024-02235-4) and [https://github.com/VPetukhov/GPTCellAnnotator](https://github.com/VPetukhov/GPTCellAnnotator). +This tool was inspired by [Hou et al., Nature Methods 2024](https://www.nature.com/articles/s41592-024-02235-4) and [https://github.com/VPetukhov/GPTCellAnnotator](https://github.com/VPetukhov/GPTCellAnnotator). ## đŸ“Ŧ Contact -If you found a bug, please use the [issue tracker][]. +If you found a bug, please use the [issue tracker][]. ## 📖 Citation + Please use our [zenodo][] entry to cite this software. [mambaforge]: https://github.com/conda-forge/miniforge#mambaforge diff --git a/REVIEW_GUIDE.md b/REVIEW_GUIDE.md index ae19a24..c54d677 100644 --- a/REVIEW_GUIDE.md +++ b/REVIEW_GUIDE.md @@ -18,7 +18,7 @@ Use `AGENTS.md` for architecture, invariants, and commands. Use this guide for r ## High-Risk Areas -- **Data privacy boundary.** Any change that broadens what is sent to an LLM provider beyond marker gene *names* + `species` / `tissue` / `stage` is a hard red flag. Audit new prompt construction code in `_prompts.py` and anything that builds payloads in `base_annotator.py` / `llm_interface.py`. +- **Data privacy boundary.** Any change that broadens what is sent to an LLM provider beyond marker gene _names_ + `species` / `tissue` / `stage` is a hard red flag. Audit new prompt construction code in `_prompts.py` and anything that builds payloads in `base_annotator.py` / `llm_interface.py`. - **Prompt template wording** (`src/cell_annotator/_prompts.py`): silent wording changes measurably shift annotation outputs. Treat these like behavior changes; they warrant a GitHub-release note. - **Pydantic response schemas** (`src/cell_annotator/_response_formats.py`): renaming, removing, or retyping fields breaks parsing of structured LLM outputs and is a serialized-contract change. - **Provider SDK integration** (`src/cell_annotator/model/_providers.py`, `llm_interface.py`, `base_annotator.py`): auth, retry, structured-output parsing, and error surfacing differ per provider; regressions are easy to miss with only one provider mocked. @@ -32,34 +32,38 @@ Use `AGENTS.md` for architecture, invariants, and commands. Use this guide for r Test directories mirror `src/cell_annotator/` — changes to `src/cell_annotator/X/` have matching tests under `tests/`. -| Changed path | Look at | -|---|---| -| `src/cell_annotator/model/` | `tests/model/` (per-class files: `test_cell_annotator.py`, `test_sample_annotator.py`, `test_base_annotator.py`, `test_llm_interface.py`, `test_obs_beautifier.py`) | -| `src/cell_annotator/utils.py` | `tests/test_utils.py` | -| `src/cell_annotator/check.py` | `tests/test_check.py` | -| `src/cell_annotator/_response_formats.py` or `_prompts.py` | downstream: `tests/model/test_base_annotator.py`, `tests/model/test_llm_interface.py` | -| Cross-cutting fixture changes | `tests/conftest.py`, `tests/utils.py` | +| Changed path | Look at | +| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `src/cell_annotator/model/` | `tests/model/` (per-class files: `test_cell_annotator.py`, `test_sample_annotator.py`, `test_base_annotator.py`, `test_llm_interface.py`, `test_obs_beautifier.py`) | +| `src/cell_annotator/utils.py` | `tests/test_utils.py` | +| `src/cell_annotator/check.py` | `tests/test_check.py` | +| `src/cell_annotator/_response_formats.py` or `_prompts.py` | downstream: `tests/model/test_base_annotator.py`, `tests/model/test_llm_interface.py` | +| Cross-cutting fixture changes | `tests/conftest.py`, `tests/utils.py` | ## Testing Apply these checks whenever the PR touches code or tests. **New code.** Confirm that new behavior is covered by tests. + - Reuse fixtures from `tests/conftest.py` and `tests/utils.py` rather than creating parallel ones. - Prefer `pytest.mark.parametrize` over many near-identical tests; the existing `provider_name` fixture already parametrizes across available providers. - Favor few meaningful tests over many redundant ones; flag low-value tests that only duplicate existing coverage. **Real-LLM tests.** Keep live-provider usage minimal. + - Live tests must be marked `@pytest.mark.real_llm_query` and must be skipped by default in CI. - Prefer mocked provider fixtures for unit tests. - Anthropic is the most expensive provider — do not add Anthropic-only real-LLM tests without justification. **Failing tests.** If CI is red, do not wave it through. + - Inspect which tests fail and why (`gh pr checks`, `gh run view --log-failed`). - Distinguish critical regressions (data-privacy, provider-contract, AnnData-key, structured-output) from trivial or flaky failures. - Surface critical failures back to the author and ask them to fix before merge. -**Modified tests.** Scrutinize *how* existing tests were changed. +**Modified tests.** Scrutinize _how_ existing tests were changed. + - PRs that only relax thresholds, remove assertions, delete cases, or loosen `parametrize` matrices are a red flag — tests-working-around-tests defeats the purpose. - Require an explicit justification in the PR body for any weakened assertion; do not accept silently. @@ -68,12 +72,14 @@ Apply these checks whenever the PR touches code or tests. A single behavioral or API change often touches docs in multiple places. Check both audiences and ask the author to update what is stale. Point to the **owning file** for each topic rather than duplicating content in your review. **Human-facing docs (`docs/`, Sphinx/RTD).** + - Public API signature or symbol changes → `docs/api.md` and autosummary entries under `docs/generated/`; any prose referencing the symbol. - Contributor workflow, environment, or release changes → `docs/contributing.md`. - Installation / quickstart changes → `README.md`. - Tutorials under `docs/notebooks/tutorials/` → flag stale imports, outputs, or API usage (e.g. renamed kwargs, changed AnnData keys). **Agent-facing docs (repo root and `.github/`).** + - Invariants or development commands changed → `AGENTS.md` (Critical Invariants, Development Commands). - Review workflow, risk areas, or testing conventions changed → `REVIEW_GUIDE.md` (this file). - Repo structure, new top-level docs, or moved pointers → `AGENTS.md` "Where To Find What" table, `CLAUDE.md`, `.github/copilot-instructions.md`. @@ -98,6 +104,7 @@ If behavior changes but the relevant docs do not, call it out explicitly in the This repo uses a structured PR template. Reviewers and agents should treat these sections as the preferred summary surface: + - summary - behavior or invariants changed - tests run diff --git a/docs/contributing.md b/docs/contributing.md index 153c69f..496c2ce 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -11,7 +11,7 @@ the [scientific Python tutorials][], or the [scanpy developer guide][]. [scientific Python tutorials]: https://learn.scientific-python.org/development/tutorials/ [scanpy developer guide]: https://scanpy.readthedocs.io/en/latest/dev/index.html -:::{tip} The *hatch* project manager +:::{tip} The _hatch_ project manager We highly recommend to familiarize yourself with [`hatch`][hatch]. Hatch is a Python project manager that diff --git a/pyproject.toml b/pyproject.toml index 3fefbfe..2d927d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,7 @@ classifiers = [ "Programming Language :: Python :: 3.12", "Programming Language :: Python :: 3.13", "Programming Language :: Python :: 3.14", + "Programming Language :: Python :: 3.15", ] dynamic = [ "version" ] dependencies = [ @@ -174,11 +175,11 @@ ini_options.markers = [ ini_options.xfail_strict = true [tool.coverage] +run.source = [ "cell_annotator" ] run.omit = [ "**/test_*.py", ] run.patch = [ "subprocess" ] -run.source = [ "cell_annotator" ] [tool.cruft] skip = [