# scverse/scanpy architecture

> Architecture diagram of the GitHub repository scverse/scanpy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/scverse/scanpy
- Repository: https://github.com/scverse/scanpy

## Overview

1. **Repository purpose and top-level shape**
   This is a Python library for single-cell analysis, not an application server. The runtime package lives under `src/scanpy`, with a small CLI entrypoint (`__main__.py`, `cli.py`) and a large surrounding ecosystem for docs, tests, benchmarks, and release automation.

2. **Core architectural boundary: AnnData-centric API**
   Scanpy is built around the `anndata.AnnData` data structure. Most functions accept an `AnnData` object, read from standardized slots like `X`, `obs`, `var`, `obsm`, `obsp`, and write results back into those same slots. This is the main internal/external boundary of the project.

3. **Configuration, logging, and compatibility**
   Global behavior is controlled by `src/scanpy/_settings` and `src/scanpy/logging.py`, with shared constants and compatibility helpers in `_keys.py`, `_compat.py`, and `_utils`. These modules provide cross-cutting concerns such as verbosity, presets, feature flags, and backward-compatibility shims.

4. **Data ingestion and persistence**
   `readwrite.py`, `datasets/`, and `queries/` cover loading and accessing data from on-disk formats and remote/curated sources. The repo includes bundled example datasets and simulator inputs, plus support for common single-cell formats like `h5ad`, `zarr`, and 10x Genomics matrices. This layer is the ingress/egress boundary between files or services and in-memory `AnnData`.

5. **Preprocessing subsystem**
   `src/scanpy/preprocessing` contains the data-cleaning and feature-engineering pipeline: QC metrics, filtering, normalization, scaling, highly variable gene selection, combat batch correction, PCA, Scrublet, Harmony integration, and recipe-style pipelines. There are also distributed/Dask-aware paths and deprecated compatibility modules, showing that preprocessing is both a core path and a migration-sensitive area.

6. **Neighborhood graph construction**
   `src/scanpy/neighbors` builds k-nearest-neighbor graphs and connectivities from embeddings or expression matrices. The resulting graph structures are stored back into `AnnData` and reused by downstream clustering, manifold learning, and trajectory methods. The presence of a RAPIDS backend indicates an alternate GPU-accelerated execution path.

7. **Analysis and “tools” layer**
   `src/scanpy/tools` is the main algorithmic analysis layer. It contains clustering (`leiden`, `louvain`), trajectory and manifold methods (`dpt`, `diffmap`, `paga`, `draw_graph`, `umap`, `tsne`), gene ranking and scoring (`rank_genes_groups`, `score_genes`), ingestion/integration (`ingest`), and simulation helpers. This layer consumes preprocessed data and neighbor graphs and produces embeddings, clusters, pseudotime, scores, and differential-expression summaries.

8. **Visualization subsystem**
   `src/scanpy/plotting` turns analysis results into publication-style plots. It is tightly coupled to `AnnData` annotations and to outputs from preprocessing/tools, with specialized plot types such as dot plots, matrix plots, stacked violins, embedding/scatter plots, PAGA figures, QC plots, and spatial plots. Matplotlib is the main rendering backend, and the module structure reflects a clear separation between plot primitives, embedded plots, and plot-specific utilities.

9. **Metrics and derived accessors**
   `src/scanpy/metrics` provides graph/spatial statistics like Moran’s I and Geary’s C. `src/scanpy/get` exposes convenience accessors for aggregated values and kernels. These modules are small but important because they expose reusable derived computations without forcing callers into the higher-level tools pipeline.

10. **Experimental and external integration layers**
   `src/scanpy/experimental` contains newer preprocessing functionality, while `src/scanpy/external` wraps or delegates to third-party methods such as BBKNN, MAGIC, MNN correction, Scanorama, PHATE, Palantir, Phenograph, SAM, TriMap, and Wishbone. These folders are explicit integration boundaries: Scanpy provides a unified API while the actual algorithms may live in external packages.

11. **Testing architecture**
   The test suite is extensive and artifact-driven. `tests/` includes unit and integration tests, notebook-based regression checks, and many image baselines under `tests/_images`, indicating that plot stability is part of the contract. `src/testing/scanpy` contains reusable fixtures/helpers for the test framework.

12. **Documentation, tutorials, and release process**
   `docs/` is a first-class subsystem with API docs, tutorials, how-to guides, and custom Sphinx extensions. `benchmarks/` uses ASV for performance tracking, and `.github/workflows` plus `ci/` automate linting, test runs, docs, benchmarking, and publishing. This repo is maintained as a mature scientific library with strong emphasis on reproducibility, backward compatibility, and visual regression testing.

13. **Typical data flow through the library**
   A common flow is: load data into `AnnData` → preprocess and normalize → compute PCA/neighbors → cluster or infer trajectories → rank marker genes / compute metrics → visualize results. Most modules either transform `AnnData` in place or read from its stored annotations, which makes `AnnData` the central integration point for the whole architecture.

Main entry points: [`src/scanpy/__init__.py`](https://github.com/scverse/scanpy/blob/main/src/scanpy/__init__.py) (AnnData API), [`src/scanpy/preprocessing/__init__.py`](https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/__init__.py) (Preprocess), [`src/scanpy/tools/__init__.py`](https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/__init__.py) (Tools), [`src/scanpy/plotting/__init__.py`](https://github.com/scverse/scanpy/blob/main/src/scanpy/plotting/__init__.py) (Plotting)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core API"]
  node_ann_data["AnnData API<br/>core package<br/>[__init__.py]"]
  node_cli["CLI<br/>entrypoint<br/>[cli.py]"]
  node_runtime_cfg["Settings<br/>runtime config<br/>[__init__.py]"]
  node_logging["Logging<br/>diagnostics<br/>[logging.py]"]
  node_compat_utils["Compat utils<br/>compat helpers<br/>[_compat.py]"]
end

subgraph group_data["Data IO"]
  node_readwrite["Read/write<br/>io layer<br/>[readwrite.py]"]
  node_datasets[("Datasets<br/>bundled data<br/>[__init__.py]")]
  node_queries["Queries<br/>data access<br/>[__init__.py]"]
end

subgraph group_prep["Preprocessing"]
  node_preprocess_core["Preprocess<br/>pipeline<br/>[__init__.py]"]
  node_hvg["HVG selection<br/>feature selection"]
  node_pca["PCA<br/>reduction<br/>[__init__.py]"]
  node_scrublet["Scrublet<br/>doublet detection<br/>[__init__.py]"]
end

subgraph group_analysis["Analysis"]
  node_neighbors["Neighbors<br/>graph builder<br/>[__init__.py]"]
  node_tools["Tools<br/>analysis layer<br/>[__init__.py]"]
  node_paga["PAGA<br/>graph analysis<br/>[_paga.py]"]
  node_umap["UMAP<br/>embedding<br/>[_umap.py]"]
end

subgraph group_viz["Visualization"]
  node_plotting["Plotting<br/>visualization<br/>[__init__.py]"]
  node_plot_tools["Plot tools<br/>plot submodule<br/>[__init__.py]"]
end

subgraph group_ext["Extensions"]
  node_experimental["Experimental<br/>new methods<br/>[__init__.py]"]
  node_external["External<br/>integrations<br/>[__init__.py]"]
end

subgraph group_support["Support"]
  node_metrics["Metrics<br/>derived stats<br/>[__init__.py]"]
  node_get["Getters<br/>accessors<br/>[__init__.py]"]
  node_testing["Testing<br/>test support<br/>[__init__.py]"]
end

subgraph group_docs["Docs & Ops"]
  node_docs["Docs<br/>documentation<br/>[index.md]"]
  node_benchmarks["Benchmarks<br/>performance suite<br/>[asv.conf.json]"]
end

node_ann_data -->|"load/save"| node_readwrite
node_ann_data -->|"examples"| node_datasets
node_ann_data -->|"fetch"| node_queries
node_runtime_cfg -->|"controls"| node_logging
node_compat_utils -->|"supports"| node_preprocess_core
node_readwrite -->|"returns"| node_ann_data
node_datasets -->|"seed data"| node_ann_data
node_queries -->|"materialize"| node_ann_data
node_preprocess_core -->|"includes"| node_hvg
node_preprocess_core -->|"includes"| node_pca
node_preprocess_core -->|"includes"| node_scrublet
node_preprocess_core -->|"mutates"| node_ann_data
node_pca -->|"feeds"| node_neighbors
node_neighbors -->|"feeds"| node_tools
node_tools -->|"contains"| node_paga
node_tools -->|"contains"| node_umap
node_tools -->|"writes"| node_ann_data
node_neighbors -->|"stores graph"| node_ann_data
node_tools -->|"visualizes"| node_plotting
node_plotting -->|"uses"| node_plot_tools
node_plotting -->|"reads"| node_ann_data
node_metrics -->|"derives from"| node_ann_data
node_get -->|"reads"| node_ann_data
node_experimental -->|"extends"| node_preprocess_core
node_external -->|"wraps"| node_tools
node_external -->|"wraps"| node_preprocess_core
node_testing -.->|"regresses"| node_plotting
node_docs -->|"documents"| node_plotting
node_benchmarks -.->|"profiles"| node_tools

click node_ann_data "https://github.com/scverse/scanpy/blob/main/src/scanpy/__init__.py"
click node_cli "https://github.com/scverse/scanpy/blob/main/src/scanpy/cli.py"
click node_runtime_cfg "https://github.com/scverse/scanpy/blob/main/src/scanpy/_settings/__init__.py"
click node_logging "https://github.com/scverse/scanpy/blob/main/src/scanpy/logging.py"
click node_compat_utils "https://github.com/scverse/scanpy/blob/main/src/scanpy/_compat.py"
click node_readwrite "https://github.com/scverse/scanpy/blob/main/src/scanpy/readwrite.py"
click node_datasets "https://github.com/scverse/scanpy/blob/main/src/scanpy/datasets/__init__.py"
click node_queries "https://github.com/scverse/scanpy/blob/main/src/scanpy/queries/__init__.py"
click node_preprocess_core "https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/__init__.py"
click node_hvg "https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/_highly_variable_genes.py"
click node_pca "https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/_pca/__init__.py"
click node_scrublet "https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/_scrublet/__init__.py"
click node_neighbors "https://github.com/scverse/scanpy/blob/main/src/scanpy/neighbors/__init__.py"
click node_tools "https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/__init__.py"
click node_paga "https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/_paga.py"
click node_umap "https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/_umap.py"
click node_plotting "https://github.com/scverse/scanpy/blob/main/src/scanpy/plotting/__init__.py"
click node_plot_tools "https://github.com/scverse/scanpy/blob/main/src/scanpy/plotting/_tools/__init__.py"
click node_metrics "https://github.com/scverse/scanpy/blob/main/src/scanpy/metrics/__init__.py"
click node_get "https://github.com/scverse/scanpy/blob/main/src/scanpy/get/__init__.py"
click node_experimental "https://github.com/scverse/scanpy/blob/main/src/scanpy/experimental/__init__.py"
click node_external "https://github.com/scverse/scanpy/blob/main/src/scanpy/external/__init__.py"
click node_testing "https://github.com/scverse/scanpy/blob/main/src/testing/scanpy/__init__.py"
click node_docs "https://github.com/scverse/scanpy/blob/main/docs/index.md"
click node_benchmarks "https://github.com/scverse/scanpy/blob/main/benchmarks/asv.conf.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_ann_data,node_cli,node_runtime_cfg,node_logging,node_compat_utils,node_metrics,node_get,node_testing toneBlue
class node_readwrite,node_datasets,node_queries,node_docs,node_benchmarks toneAmber
class node_preprocess_core,node_hvg,node_pca,node_scrublet toneMint
class node_neighbors,node_tools,node_paga,node_umap toneRose
class node_plotting,node_plot_tools toneIndigo
class node_experimental,node_external toneTeal
```

## Components

### Core API

Public package surface centered on AnnData and shared runtime behavior.

- [AnnData API](https://github.com/scverse/scanpy/blob/main/src/scanpy/__init__.py): core package · Primary boundary for all read/transform/write operations on scanpy data. · `src/scanpy/__init__.py`
- [CLI](https://github.com/scverse/scanpy/blob/main/src/scanpy/cli.py): entrypoint · Small command-line entrypoint for invoking package functionality. · `src/scanpy/cli.py`
- [Settings](https://github.com/scverse/scanpy/blob/main/src/scanpy/_settings/__init__.py): runtime config · Global configuration, presets, and verbosity controls. · `src/scanpy/_settings/__init__.py`
- [Logging](https://github.com/scverse/scanpy/blob/main/src/scanpy/logging.py): diagnostics · Shared logging and message formatting used across the library. · `src/scanpy/logging.py`
- [Compat utils](https://github.com/scverse/scanpy/blob/main/src/scanpy/_compat.py): compat helpers · Version shims, shared keys, and low-level utility helpers. · `src/scanpy/_compat.py`

### Data IO

Loading, curated datasets, and remote queries that bridge files/services to AnnData.

- [Read/write](https://github.com/scverse/scanpy/blob/main/src/scanpy/readwrite.py): io layer · Parsing and persisting common single-cell file formats and backed data. · `src/scanpy/readwrite.py`
- [Datasets](https://github.com/scverse/scanpy/blob/main/src/scanpy/datasets/__init__.py): bundled data · Packaged example datasets and dataset-loading helpers. · `src/scanpy/datasets/__init__.py`
- [Queries](https://github.com/scverse/scanpy/blob/main/src/scanpy/queries/__init__.py): data access · Access helpers for curated or remote data sources. · `src/scanpy/queries/__init__.py`

### Preprocessing

Cleaning, normalization, dimensionality reduction, and batch-correction routines.

- [Preprocess](https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/__init__.py): pipeline · Main preprocessing pipeline for QC, filtering, normalization, and scaling. · `src/scanpy/preprocessing/__init__.py`
- [HVG selection](https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/_highly_variable_genes.py): feature selection · Highly variable gene selection and related filtering logic. · `src/scanpy/preprocessing/_highly_variable_genes.py`
- [PCA](https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/_pca/__init__.py): reduction · Principal-component analysis including dask and compatibility paths. · `src/scanpy/preprocessing/_pca/__init__.py`
- [Scrublet](https://github.com/scverse/scanpy/blob/main/src/scanpy/preprocessing/_scrublet/__init__.py): doublet detection · Doublet scoring pipeline and sparse helpers. · `src/scanpy/preprocessing/_scrublet/__init__.py`

### Analysis

Graph construction and higher-level analytical methods built on preprocessed AnnData.

- [Neighbors](https://github.com/scverse/scanpy/blob/main/src/scanpy/neighbors/__init__.py): graph builder · kNN graph and connectivity construction with CPU and GPU backends. · `src/scanpy/neighbors/__init__.py`
- [Tools](https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/__init__.py): analysis layer · Clustering, manifold learning, pseudotime, gene ranking, and simulation. · `src/scanpy/tools/__init__.py`
- [PAGA](https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/_paga.py): graph analysis · Graph abstraction and trajectory-aware analysis over neighbor graphs. · `src/scanpy/tools/_paga.py`
- [UMAP](https://github.com/scverse/scanpy/blob/main/src/scanpy/tools/_umap.py): embedding · Nonlinear embedding generation from neighbor structure. · `src/scanpy/tools/_umap.py`

### Visualization

Plotting and figure generation for analysis results and annotations.

- [Plotting](https://github.com/scverse/scanpy/blob/main/src/scanpy/plotting/__init__.py): visualization · Figure generation from AnnData annotations and analysis outputs. · `src/scanpy/plotting/__init__.py`
- [Plot tools](https://github.com/scverse/scanpy/blob/main/src/scanpy/plotting/_tools/__init__.py): plot submodule · Embedding, scatter, and PAGA-specific plotting primitives. · `src/scanpy/plotting/_tools/__init__.py`

### Extensions

Experimental features and wrappers around external algorithms.

- [Experimental](https://github.com/scverse/scanpy/blob/main/src/scanpy/experimental/__init__.py): new methods · Newer preprocessing functionality under active evolution. · `src/scanpy/experimental/__init__.py`
- [External](https://github.com/scverse/scanpy/blob/main/src/scanpy/external/__init__.py): integrations · Wrappers around third-party methods exposed through the Scanpy API. · `src/scanpy/external/__init__.py`

### Support

Metrics, accessors, packaging helpers, and test infrastructure.

- [Metrics](https://github.com/scverse/scanpy/blob/main/src/scanpy/metrics/__init__.py): derived stats · Graph and spatial statistics such as Moran's I and Geary's C. · `src/scanpy/metrics/__init__.py`
- [Getters](https://github.com/scverse/scanpy/blob/main/src/scanpy/get/__init__.py): accessors · Convenience accessors for aggregated values and kernels. · `src/scanpy/get/__init__.py`
- [Testing](https://github.com/scverse/scanpy/blob/main/src/testing/scanpy/__init__.py): test support · Reusable fixtures and helpers for the package test suite. · `src/testing/scanpy/__init__.py`

### Docs & Ops

Documentation, tutorials, benchmarks, CI, and release automation.

- [Docs](https://github.com/scverse/scanpy/blob/main/docs/index.md): documentation · User guides, API docs, tutorials, and Sphinx extensions. · `docs/index.md`
- [Benchmarks](https://github.com/scverse/scanpy/blob/main/benchmarks/asv.conf.json): performance suite · ASV-based performance tracking and benchmark workloads. · `benchmarks/asv.conf.json`

## Connections

- AnnData API → Read/write: load/save · Convert between files and in-memory AnnData.
- AnnData API → Datasets: examples · Provide packaged datasets and loaders.
- AnnData API → Queries: fetch · Access curated or remote sources into AnnData-oriented workflows.
- Settings → Logging: controls · Configuration influences verbosity and diagnostics.
- Compat utils → Preprocess: supports · Shared compatibility and helper code used across preprocessing.
- Read/write → AnnData API: returns · Populate standard AnnData slots from input formats.
- Datasets → AnnData API: seed data · Ship example AnnData inputs for tutorials and tests.
- Queries → AnnData API: materialize · Materialize queried data into AnnData-centric workflows.
- Preprocess → HVG selection: includes · Feature selection is a core preprocessing stage.
- Preprocess → PCA: includes · Dimensionality reduction is part of preprocessing.
- Preprocess → Scrublet: includes · Doublet detection is exposed within preprocessing.
- Preprocess → AnnData API: mutates · Read and write standardized slots on AnnData.
- PCA → Neighbors: feeds · Low-dimensional coordinates feed graph construction.
- Neighbors → Tools: feeds · Neighbor graphs are consumed by analysis methods.
- Tools → PAGA: contains · Trajectory and graph abstraction methods live in tools.
- Tools → UMAP: contains · Embedding methods are part of the analysis layer.
- Tools → AnnData API: writes · Store clusters, embeddings, scores, and results back into AnnData.
- Neighbors → AnnData API: stores graph · Persist kNN/connectivity structures in AnnData.
- Tools → Plotting: visualizes · Downstream analysis results are rendered by plotting.
- Plotting → Plot tools: uses · High-level plotting delegates to specialized plot primitives.
- Plotting → AnnData API: reads · Plots are driven by AnnData annotations and embeddings.
- Metrics → AnnData API: derives from · Compute statistics from stored graphs or spatial structure.
- Getters → AnnData API: reads · Convenience accessors extract derived values from AnnData.
- Experimental → Preprocess: extends · Experimental preprocessing adds newer pipeline variants.
- External → Tools: wraps · Expose third-party analysis methods behind Scanpy APIs.
- External → Preprocess: wraps · Some third-party integrations act as preprocessing helpers.
- Testing → Plotting: regresses · Reusable test infrastructure supports image and notebook checks.
- Docs → Plotting: documents · Docs cover plotting APIs and tutorials.
- Benchmarks → Tools: profiles · Performance suite measures core preprocessing and analysis paths.
