Understanding Neighborhood Enrichment Analysis with Squidpy

A beginner-friendly walkthrough of neighborhood enrichment analysis in spatial transcriptomics using Python’s Squidpy library.
Spatial Transcriptomics
Xenium
Author

Axel R

Published

September 11, 2025

Work in progress

This site is stll under development, so you may notice that some parts are not fully polished yet. Thanks for your patience and feel free to check back soon for updates!

1 What is this notebook about?

Spatial transcriptomics is a powerful way to study how cells are organized and interact within tissues. One of the most popular Python libraries for this kind of analysis is Squidpy(see the original paper here). Squidpy comes with many tools for exploring and visualizing spatial omics data.

In this guide, we’ll focus on one of its key features: neighborhood enrichment analysis. This method helps us see which cell types are more likely to be close to each other, revealing interesting patterns and possible cell–cell interactions in tissue.

When I first tried Squidpy, I found neighborhood enrichment a bit confusing to interpret. I found it challenging to interpret the output and connect it to biological meaning. This motivated me to write this short guide, where I try to digest what is being done under the hood and how to interpret the results.

I hope you find it useful, and please feel free to share any suggestions or questions in the comments!

2 Download the data

Following the Squidpy tutorial on analysis of Xenium data, we will be using a Xenium dataset of Human Lung Cancer.

Here’s how you can grab the data yourself:

  1. Go to the dataset’s preview page.
  2. Under Output and supplemental files, download the Xenium Output Bundle (we’ll use the full dataset).
  3. Unzip the downloaded file.
  4. Inside the unziped folder, unzip all the .zip files to get everything ready for analysis.
# Load libraries
import spatialdata as sd
from spatialdata_io import xenium

import matplotlib.pyplot as plt
import seaborn as sns

import scanpy as sc
import squidpy as sq
/home/axel/miniconda3/envs/ne_analysis/lib/python3.12/site-packages/dask/dataframe/__init__.py:31: FutureWarning: The legacy Dask DataFrame implementation is deprecated and will be removed in a future version. Set the configuration option `dataframe.query-planning` to `True` or None to enable the new Dask Dataframe implementation and silence this warning.
  warnings.warn(
/home/axel/miniconda3/envs/ne_analysis/lib/python3.12/site-packages/xarray_schema/__init__.py:1: UserWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html. The pkg_resources package is slated for removal as early as 2025-11-30. Refrain from using this package or pin to Setuptools<81.
  from pkg_resources import DistributionNotFound, get_distribution
/home/axel/miniconda3/envs/ne_analysis/lib/python3.12/site-packages/anndata/__init__.py:44: FutureWarning: Importing read_text from `anndata` is deprecated. Import anndata.io.read_text instead.
  return module_get_attr_redirect(attr_name, deprecated_mapping=_DEPRECATED)
from pathlib import Path
import os
cwd = Path.cwd()
xenium_path = os.path.join(cwd, "data", "Xenium")