Skip to main content

CytoCommunity2

Contents

Overview

To enhance CytoCommunity (https://github.com/huBioinfo/CytoCommunity), we present CytoCommunity2, a unified weakly-supervised framework for identifying and comparing tissue cellular neighborhoods (TCNs or CNs) across large-scale spatial omics samples with single or multiple biological conditions.

Inspired by histopathology workflows, CytoCommunity2 first hierarchically partitions the large single-cell spatial map into small patches, performs graph construction and weakly supervised CN learning for each patch, and finally merges results through KNN-based CN reassignment at segmentation boundaries to ensure CN spatial continuity. This strategy divides the original sample into patches for CN learning, achieving memory efficiency (typical 24G graphics memory is enough) and also increased sample throughput. These optimizations significantly enhance the robustness of CNs and cross-sample alignment performance.

Furthermore, to make CytoCommunity2 a unified framework that is also applicable for single-condition spatial omics datasets, pseudo-samples with artificial labels are generated, enabling automatic CN alignment across real samples via contrastive learning.

In summary, the advantages of CytoCommunity2 include:

(1) Using significantly less memory for large-scale spatial omics samples with millions of cells.

(2) A unified weakly-supervised model applicable for both multi-condition and single-condition datasets.

(3) High CN alignment performance makes it well-suited for comparative analysis in large cohort studies.

Installation

Hardware requirement

Graphics memory: 24G

Storage: 10GB or more

Install CytoCommunity2

CytoCommunity2 requires Python 3.10 or newer. We recommend creating and activating a Conda environment first:

conda create -n cytocommunity2 python=3.10
conda activate cytocommunity2

For an NVIDIA GPU on Windows or Linux, first install the appropriate CUDA-enabled PyTorch build from the official PyTorch installation page.

Install the package via pip:

pip install cytocommunity2

Usage

1. Prepare input data

The input data to CytoCommunity2 includes four types of files :

(1) An image (sample) name list file, named as "ImageNameList.txt".

(2) A cell type label file for each image (sample), named as "[image name]_CellTypeLabel.txt". Note that [image_name] should be consistent with your customized image names listed in the "ImageNameList.txt". This file lists cell type names of all cells in an image (sample).

(3) A cell spatial coordinate file for each image (sample), named as "[image name]_Coordinates.txt". Note that [image_name] should be consistent with your customized image names listed in the "ImageNameList.txt". This file lists cell coordinates (tab-delimited x/y) of all cells in an image (sample). The cell orders should be exactly the same with "[image name]_CellTypeLabel.txt".

(4) A graph label file for each image (sample), named as "[image name]_GraphLabel.txt". For multi-condition datasets, this file must be provided for every image (sample) and contain one integer indicating its biological condition. For single-condition datasets, graph label files can be omitted for all images (samples), and CytoCommunity2 will generate pseudo-samples for weakly-supervised learning.

2. Configure CytoCommunity2

Before running CytoCommunity2, set input_dir and review all model and downstream parameters in hyperparameters.json. The Python workflow will use this configuration file.

3. Run CytoCommunity2

With hyperparameters.json in the working directory, run the core workflow:

from pathlib import Path
import cytocommunity2 as cc2

CONFIG_FILE = Path("hyperparameters.json").resolve()

config = cc2.CytoCommunityConfig.from_json(CONFIG_FILE)
dataset = cc2.build_spatial_graphs(config)
learning_result = cc2.run_learning(dataset, config)
cc2.visualize_results(learning_result, config)

4.Tutorial

The tutorial uses a TNBC dataset as a worked example and demonstrates the entire CytoCommunity2 workflow, from model training and CN identification to downstream analysis:

jupyter notebook tutorial/TNBC_workflow.ipynb

5. Output files

The main output directories are:

  • CNLearning_Output/: CN-number screening, trained models, and final result tables.
  • CNVisualization_Output/: CN and cell-type spatial plots.
  • data/: downstream analysis results.
  • plot/: figures generated by downstream analyses.

Maintainers

Citation

Yuxuan Hu, Jiazhen Rong, Yafei Xu, Runzhi Xie, Jacqueline Peng, Lin Gao, and Kai Tan. “Unsupervised and supervised discovery of tissue cellular neighborhoods from cell phenotypes.” Nature Methods 21 (2024): 267–278. https://doi.org/10.1038/s41592-023-02124-2

Metadata

Release files for cytocommunity2 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cytocommunity2 1.0.2
File Size Uploaded
cytocommunity2-1.0.2.tar.gz 51.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cytocommunity2 1.0.2
File Interpreter ABI Platform
cytocommunity2-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 115.6 kB

Release files / cytocommunity2-1.0.2.tar.gz

Download URL cytocommunity2-1.0.2.tar.gz
Size 51.1 kB
Tags Source
SHA-256 checksum
How to use checksums
7e79c3aa1d46c65878fc1f66c07bce68ea96b9431831f5d297d9ee16a9499b4d
BLAKE2b-256 checksum
How to use checksums
b87654f240cd18ad6d542a15cecabe5072084c698bfc714887c031677df6aac7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.

Transparency log

Release files / cytocommunity2-1.0.2-py3-none-any.whl

Download URL cytocommunity2-1.0.2-py3-none-any.whl
Size 64.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
569a0693e34e2ac4801b4e6393f60ef079f0df37d8eddbd78e824fcc4865191f
BLAKE2b-256 checksum
How to use checksums
da3d64972172e2a0f8ff4b76c778ed0e9e4d39385787575a7ccd376ca1725c83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.

Transparency log

Release history Release notifications | RSS feed

1.0.3

2 release files

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page