troutpy.tl.calculate_heuristic_radius_by_cells

troutpy.tl.calculate_heuristic_radius_by_cells#

troutpy.tl.calculate_heuristic_radius_by_cells(sdata, k=10, n_cells=500, segmentation_key='overlaps_cell')#

Estimate a neighbor-distance radius from the local geometry of a sample of cells.

Randomly selects n_cells cells and computes the mean distance to each transcript’s k-th nearest neighbor among the transcripts of those cells.

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

  • k (int, optional) – Neighbor rank used for the distance heuristic. Defaults to 10.

  • n_cells (int, optional) – Number of cells to randomly sample. Defaults to 500.

  • segmentation_key (str, optional) – Boolean column used to restrict the sampled transcripts to those that overlap a cell. Defaults to "overlaps_cell".

Returns:

float Mean distance to the k-th nearest neighbor across the sampled transcripts.

Raises:

ValueError – If no valid (non-UNASSIGNED) cells are found, or if fewer than k + 1 transcripts remain after filtering by segmentation_key.