troutpy.tl.density_similarity#
- troutpy.tl.density_similarity(sdata, radius=10.0, ambient_floor=1.5, n_cells_for_model=500, process_all=False, segmentation_key='overlaps_cell', prob_density=0.5)#
Classify transcripts by local-density enrichment relative to a Bayesian cell/background model.
For every transcript (or, if
process_all=False, only those withsegmentation_keyfalse), counts neighboring transcripts withinradiusand compares that count to two reference distributions: a Poisson background model fit from the global transcript density, and an empirical PMF of local counts built from a random subset ofn_cells_for_modelcells. The resulting Bayesian posterior and enrichment ratio are used to assign each transcript to anenrichment_class.- Parameters:
sdata (spatialdata.SpatialData) – SpatialData object with a
"transcripts"points layer containingx,y,cell_id, andsegmentation_keycolumns.radius (float, optional) – Radius (in coordinate units) used to count local neighboring transcripts. Defaults to
10.0.ambient_floor (float, optional) – Minimum enrichment-over-random ratio for a transcript to be labeled
"Moderate Density". Defaults to1.5.n_cells_for_model (int, optional) – Number of cells randomly sampled to build the empirical cell-density PMF. Defaults to
500.process_all (bool, optional) – If
True, compute local counts for all transcripts; otherwise only for those wheresegmentation_keyisFalse. Defaults toFalse.segmentation_key (str, optional) – Boolean column identifying transcripts that overlap a segmented cell. Defaults to
"overlaps_cell".prob_density (float, optional) – Minimum Bayesian posterior probability for a transcript to be labeled
"High Density". Defaults to0.5.
- Returns:
spatialdata.SpatialData
sdatawithenrichment_class,density_similarity_score,enrichment_over_random, andlocal_count_in_radiuscolumns added to the"transcripts"layer (modified in place).