Skip to main content

Python port of cellAdmix — evaluating and correcting cell admixtures in imaging-based spatial transcriptomics data

Project description

py-cellAdmix

PyPI version PyPI downloads Python versions License: MIT Tests

Python port of cellAdmix — evaluating and correcting cell admixtures in imaging-based spatial transcriptomics data.

Install

pip install pycelladmix

Quickstart

import celladmix
import pandas as pd

# Load your spatial transcriptomics data
df = pd.read_csv('molecules.csv')  # x, y, z, gene, cell, celltype, mol_id

# Run the pipeline
ca = celladmix.CellAdmix(molecule_df=df)
ca.run_knn_nmf(k=5, h=20)
ca.run_crf_all(num_nn=10)

# Get results
crf_factors = ca.crf_results
nmf_results = ca.nmf_results

Functional API (R one-to-one mirror)

from celladmix import knn_adjacency_matrix, run_knn_nmf, run_crf_all

# KNN adjacency
adj = knn_adjacency_matrix(df, k=20)

# NMF
nmf_res = run_knn_nmf(df, k=5, h=20)

# CRF segmentation
crf_res = run_crf_all(df, nmf_res, num_nn=10)

Performance Benchmarks

Correlation with R Reference

Function Metric Value Gate
Gene Probabilities Pearson r 1.00000000 PASS
Sparse Correlation Pearson r 1.00000000 PASS
Normalize Images Max error 0.00e+00 PASS

Speed Comparison (vs R)

Function Python R Speedup
KNN Count Matrix 0.03s ~3.4s ~100x
NMF Factorization 1.24s ~21s ~17x
Gene Probabilities 0.002s - -
Sparse Correlation instant - -

Key Optimizations

  1. KNN Batch Processing: Vectorized KNN computation instead of per-cell loops (100x faster)
  2. sklearn NMF: Optimized NMF implementation with multiple seeds (17x faster than R)
  3. Sparse Operations: Efficient scipy.sparse operations throughout

What's included

Python function R function
knn_adjacency_matrix knn.adjacency.matrix
knn_count_matrix knn.count.matrix
get_knn_counts_all get.knn.counts.all
run_knn_nmf run.knn.nmf
run_crf_all run.crf.all
estimate_cell_adjacency estimate.cell.adjacency
estimate_cell_type_adjacency estimate.cell.type.adjacency
estimate_gene_prob_per_type estimate.gene.prob.per.type
estimate_contamination_scores estimate.contamination.scores
sparse_cor sparse.cor
estimate_correlation_preservation estimate.correlation.preservation
run_bridge_test run.bridge.test
extract_bridge_res extract.bridge.res
run_memb_test run.memb.test
extract_memb_res extract.memb.res
get_enr get.enr
check_f_rm check.f.rm
check_f_rm_per_ct check.f.rm.per.ct
samp_ct_equal samp.ct.equal
subset_genes subset.genes
get_counts_matrix get.counts.meta.seurat
normalize_images normalize.images
plot_nmf_loadings plot.nmf.loadings
plot_cell_score_ratios plot.cell.score.ratios
plot_annot_hmap plot.annot.hmap
plot_correlation_preservation plot.correlation.preservation
plot_expression_comparison plot.expression.comparison

Reproducing R results exactly

# Run the parity test
import subprocess
subprocess.run(['pytest', 'tests/test_parity.py', '-v'])

Test Results

22 passed, 4 skipped, 0 failed
├── 9 smoke tests (all pass)
├── 13 parity tests (all pass)
└── 4 exact_match tests (skipped — CRF unavailable on R 4.5)

Relationship to omicverse

py-cellAdmix is designed for use with the omicverse ecosystem.

Citation

If you use this package, please cite the original cellAdmix paper:

Mitchel et al. "Evaluating and correcting cell admixtures in imaging-based spatial transcriptomics data."

License

MIT (same as upstream cellAdmix)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycelladmix-0.1.1.tar.gz (34.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycelladmix-0.1.1-py3-none-any.whl (32.5 kB view details)

Uploaded Python 3

File details

Details for the file pycelladmix-0.1.1.tar.gz.

File metadata

  • Download URL: pycelladmix-0.1.1.tar.gz
  • Upload date:
  • Size: 34.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for pycelladmix-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aafff296ce71959730c58d2c91af69869064cc257a1c55d32901772add3067d0
MD5 bb0d0034549c2b8f91cf7635361a0367
BLAKE2b-256 51909e903cdbb0d61134126f5fc5d5e9125577c255a7875c30524f34119cc470

See more details on using hashes here.

File details

Details for the file pycelladmix-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pycelladmix-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 32.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for pycelladmix-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3eab578ce12f9f19384ce0b3d6840e29f4be0e46e078c8894bd9b8e81c84ca83
MD5 a35f4ef95f287bde52cfd6aae950c00d
BLAKE2b-256 88ca22e3e9dffb62daa39cbf1fb904ef428c40e18a7c1548252d0b5881988fe2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page