troutpy.tl.density_similarity

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 with segmentation_key false), counts neighboring transcripts within radius and 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 of n_cells_for_model cells. The resulting Bayesian posterior and enrichment ratio are used to assign each transcript to an enrichment_class.

Parameters:
  • sdata (spatialdata.SpatialData) – SpatialData object with a "transcripts" points layer containing x, y, cell_id, and segmentation_key columns.

  • 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 to 1.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 where segmentation_key is False. Defaults to False.

  • 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 to 0.5.

Returns:

spatialdata.SpatialData sdata with enrichment_class, density_similarity_score, enrichment_over_random, and local_count_in_radius columns added to the "transcripts" layer (modified in place).