Squidpy: Spatial Single Cell Analysis in Python

repository·main·Indexed 20 days ago

https://github.com/scverse/squidpy

A scalable toolkit for the analysis and visualization of spatial molecular data, integrating with the scverse ecosystem (scanpy, anndata). Core capabilities include building spatial neighbor graphs, computing spatial statistics (neighborhood enrichment, co-occurrence, Moran's I), image processing for high-resolution tissue microscopy via scikit-image, and interactive exploration using napari-spatialdata.

Tokens
14.6K
Snippets
46
Records
68
Agent score
68%

What's inside squidpy

  1. Overview of Squidpy

    main
    Squidpy is a Python tool designed for the analysis and visualization of spatial molecular data. It is built on top of scanpy and anndata, inheriting their modularity and scalability. Squidpy provides specialized analysis tools that utilize spatial coordinates and tissue images to extract biological insights from spatial datasets.
  2. Overview of Squidpy capabilities

    main

    Squidpy is a toolkit for scalable analysis and visualization of spatial molecular data, built on top of scanpy and anndata. Its core capabilities include:

    • Spatial Neighbor Graphs: Building and analyzing graphs from assays like Visium, Slide-seq, and Xenium.
    • Spatial Statistics: Computing neighborhood enrichment, co-occurrence, and Moran's I for cell types and genes.
    • Image Processing: Efficiently storing, featurizing, and visualizing high-resolution tissue microscopy images using scikit-image.
    • Interactive Exploration: Using napari-spatialdata to explore annotated datasets.
  3. Key applications of Squidpy

    main

    Squidpy is used for several core spatial biology tasks:

    • Neighborhood Graph Analysis: Building and analyzing graphs based on spatial coordinates.
    • Spatial Statistics: Computing statistics for cell-types and genes across spatial domains.
    • Image Processing: Efficiently storing, analyzing, and visualizing large tissue images using scikit-image.
    • Interactive Exploration: Using napari-spatialdata for interactive spatial data exploration.
  4. Extending Squidpy with custom graph builders

    main

    Squidpy allows for extensibility through custom graph builders. You can implement or use various builder and postprocessor classes located in squidpy.gr.neighbors to customize how spatial graphs are constructed.

    Key Extensibility Components:

    • GraphBuilder / GraphBuilderCSR: Base classes for building graphs.
    • KNNBuilder, RadiusBuilder, DelaunayBuilder, GridBuilder: Specific builder implementations.
    • GraphPostprocessor: Interface for applying transformations to a graph after construction.
    • DistanceIntervalPostprocessor, PercentilePostprocessor, TransformPostprocessor: Specific postprocessing implementations.
  5. Extend Squidpy with custom graph builders

    main

    Squidpy allows you to implement custom graph construction strategies by subclassing its graph builder base classes. This is useful when you need to use a custom coordinate type, a different sparse-matrix backend, or a specialized nearest-neighbor search algorithm.

    There are two primary base classes in squidpy.gr.neighbors:

    1. GraphBuilder: The generic pipeline. Use this for custom coordinate types or non-standard sparse-matrix backends.
    2. GraphBuilderCSR: A CSR-specialized builder. Use this if your builder returns scipy.sparse.csr_matrix objects. It allows you to reuse Squidpy's CSR-specific postprocessors, multi-library combination logic (via library_key), and automatic suppression of scipy.sparse.SparseEfficiencyWarning.

    Both types of builders return two square sparse matrices of shape (n_obs, n_obs):

    • adj: The connectivity/adjacency matrix (non-zero entries mark edges).
    • dst: The distance matrix for those edges (e.g., Euclidean distance or graph-distance).

    When subclassing GraphBuilderCSR, both adj and dst must be returned as scipy.sparse.csr_matrix objects.

    from squidpy.gr.neighbors import GraphBuilder, GraphBuilderCSR
  6. Install Squidpy

    main

    Squidpy is recommended for recent Linux or macOS systems with Python ≥3.12. It is also compatible with Windows via WSL. You can install it using pip or conda.

    pip install squidpy

    Or via conda-forge

    conda install -c conda-forge squidpy
  7. Configure pre-commit for code-style enforcement

    main

    Squidpy uses pre-commit to enforce consistent code styles. It is recommended to install it locally to catch issues before pushing to GitHub.

    To enable pre-commit locally, run:

    pre-commit install

    If pre-commit.ci (the GitHub service) makes automatic fixes on your branch, integrate them using:

    git pull --rebase
    pre-commit install
    git pull --rebase
  8. Write documentation for Squidpy

    main

    Squidpy uses Sphinx for documentation. When contributing new features or use-cases, follow these standards:

    • Format: Use Markdown/Markedly Structured Text via the myst extension.
    • Docstrings: Use Numpy-style docstrings (supported via the napoleon extension).
    • Type Hints: The project uses sphinx-autodoc-typehints to automatically reference annotated input and output types.
    • Citations: Use sphinxcontrib-bibtex for citations (e.g., {cite:p}palla:22`).
    • External Links: If referencing objects from other packages, add an entry to intersphinx_mapping in docs/conf.py to enable automatic linking.
    • Ignoring Errors: If a build fails due to a missing external link you cannot control, add that link to the nitpick_ignore list in docs/conf.py.
  9. Install development dependencies using Hatch

    main

    Squidpy recommends using hatch to manage separate virtual environments for testing and documentation. This prevents dependency conflicts between development tasks.

    To interact with environments:

    1. List available environments: hatch env show -i
    2. Create a specific environment: hatch env create <env_name> (e.g., hatch env create hatch-test.py3.14-stable)
    3. Find the environment path: hatch env find <env_name>

    IDE Setup (VS Code): After finding the path with hatch env find, open the Command Palette (Ctrl+Shift+P), select Python: Select Interpreter, choose Enter Interpreter Path, and paste the path.

    Running Tasks:

    • Run tests: hatch test (highest supported Python version) or hatch test --all (all supported versions).
    • Build documentation: hatch run docs:build.
    hatch env show -i
    hatch env create hatch-test.py3.14-stable
    hatch env find hatch-test.py3.14-stable
    hatch test
    hatch test --all
    hatch run docs:build
  10. Build the documentation locally

    main

    You can build the Squidpy documentation locally using one of the following package managers. After building, you can open the generated HTML in your browser.

    ### Using Hatch
    ```bash
    hatch run docs:build
    hatch run docs:open

    Using uv

    cd docs
    uv run sphinx-build -M html . _build -W
    (xdg-)open _build/html/index.html

    Using Pip

    source .venv/bin/activate
    cd docs
    sphinx-build -M html . _build -W
    (xdg-)open _build/html/index.html
  11. Set up interactive visualization with napari-spatialdata

    main

    The original Squidpy napari plugin is deprecated. For interactive visualization within napari, use napari-spatialdata, which provides improved support for the SpatialData ecosystem.

    pip install napari-spatialdata