Python port of TranspaceR — Statistical analysis of Spatial transcriptomic data.
| Function | Pearson r | Max Abs Error |
|---|---|---|
C_normalisation |
1.000000 | 4.10e-05 |
Otsu_thresholding |
— | 3.54e-05 |
colvars_sparse |
1.000000 | 4.40e-05 |
Get_variogram_map |
Deterministic match | 0 |
Get_isotropic_vario |
1.000000 | 0 |
All outputs are highly consistent with R references, with errors within floating-point precision.
| Function | R Time | Python Time | Speedup |
|---|---|---|---|
C_normalisation |
1.47s | 0.157s | 9.4x |
Otsu_thresholding |
0.31s | 0.031s | 10.0x |
colvars_sparse |
1.72s | 0.011s | 156x |
Get_variogram_map |
0.02s | 0.0004s | 50x |
Get_isotropic_vario |
0.01s | 0.0004s | 25x |
| Total | 3.53s | 0.20s | 17.7x |
- NumPy/SciPy compiled C backend vs R interpreted execution
- Direct CSC sparse matrix memory layout access
- Broadcasting replaces R's row-wise
applyloops
pip install py-TranspaceRWith optional dependencies:
pip install "py-TranspaceR[all]"import transspacer as ts
import numpy as np
import pandas as pd
# Load data
expr = pd.read_csv("Expression_file.csv.gz", index_col=0)
meta = pd.read_csv("Meta_data.csv", index_col=0)
# Cell-size normalisation
normed = ts.c_normalisation(expr.values.astype(float), meta["Area"].values)
# Otsu thresholding
threshold = ts.otsu_thresholding(np.log10(expr.values.sum(axis=1) + 1))
# Variogram analysis
result = ts.compute_variogram(normed, meta["cell_centroid_x"].values,
meta["cell_centroid_y"].values)
# Geary's C spatial autocorrelation
gc = ts.geary_c_score(normed, coords, pvalue_threshold=0.01)
# Clustering
labels = ts.cell_clustering_function(pca_data, K=10, resolution=1.0)| Module | Description |
|---|---|
fft_utils |
fftshift, ifftshift, pad_definitor |
normalization |
C_normalisation cell-size normalisation |
sparse_utils |
Sparse matrix column variance, group aggregation |
variogram |
FFT variogram map, variogram model fitting |
spatial_stats |
Geary's C, NB excess variance / excess zero score |
clustering |
KNN + Leiden/Louvain clustering, UMAP |
gene_selection |
log2FC, gene set union |
qc |
Otsu thresholding, QC gene filtering |
plotting |
Spatial visualization, heatmaps, UMAP plots |
pytest tests/ -q
# 29 passedCore: numpy, scipy, scikit-learn, pandas, matplotlib
Optional: umap-learn (UMAP), python-igraph + leidenalg (Leiden clustering), statsmodels (FDR correction)
MIT