troutpy.tl.identify_density_k_neighbors

troutpy.tl.identify_density_k_neighbors#

troutpy.tl.identify_density_k_neighbors(sdata, k_range=range(3, 61, 2), crop_size=400, d_threshold=0.8, segmentation_key='overlaps_cell')#

Find and visualize the smallest neighbor count k that separates cell from extracellular transcript density.

Crops a square region around the median transcript position, computes a k-nearest-neighbor density (k / (pi * r_k^2)) for every k in k_range, and measures the Cohen’s d effect size between the densities of transcripts inside vs. outside cells. A plot of effect size vs. k is shown.

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

  • k_range (range or iterable of int, optional) – Candidate neighbor counts to evaluate. Defaults to range(3, 61, 2).

  • crop_size (float, optional) – Side length of the square region (centered on the median transcript position) used for the analysis. Defaults to 400.

  • d_threshold (float, optional) – Minimum Cohen’s d effect size for k to be considered separating. Defaults to 0.8.

  • segmentation_key (str, optional) – Boolean column identifying transcripts that overlap a segmented cell. Defaults to "overlaps_cell".

Returns:

int The smallest k in k_range whose Cohen’s d reaches d_threshold, or the largest value in k_range if none does.