Skip to main content

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

Project description

py-cellAdmix

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.0.tar.gz (34.0 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.0-py3-none-any.whl (32.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pycelladmix-0.1.0.tar.gz
  • Upload date:
  • Size: 34.0 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.0.tar.gz
Algorithm Hash digest
SHA256 1e2a47edba1685eca511c7ecf2dee123f2b2cf27c54fa05e6e2bfa5d4b8a76bd
MD5 f09157a75009174a9e9212d527586bbb
BLAKE2b-256 cdc0e9e71aed4cb10765821fa8fcc76fa09042b7bdc682d1cc5d216530d8a61a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pycelladmix-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.2 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3729acd1d42ccd0e03369c40cf54ededb8b3e4e01087e67247f19f2043600a7
MD5 44bc2a4bc58c2d2120d052b72f57ff21
BLAKE2b-256 576b7f5765f9a8ad036b4d88ff150a547d21f6ecf7b55209d4ef1aef14f695d8

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