General utils

Select one Visium slide from anndata object

cell2location.utils.select_slide(adata, s, batch_key='sample')[source]

This function selects the data for one slide from the spatial anndata object.

  • adata – Anndata object with multiple spatial experiments

  • s – name of selected experiment

  • batch_key – column in adata.obs listing experiment name for each location

Compute KNN graph using spatial coordinates

cell2location.utils.spatial_neighbours(coords, n_sp_neighbors=7, radius=None, include_source_location=True, sample_id=None)[source]

Find spatial neighbours using the number of neighbours or radius (KDTree approach).

  • coords – numpy.ndarray with x,y positions of spots.

  • n_sp_neighbors – how many spatially-adjacent neighbors to report for each spot (including the source spot). Use 7 for hexagonal grid.

  • radius – Supersedes n_sp_neighbors - radius within which to report spatially-adjacent neighbors for each spot. Pick radius based on spot size.

  • include_source_location – include the observation itself into the list of neighbours.

  • sample_id – pd.Series or np.array listing sample membership for each observation (each row of coords).

Add the values of neighbours in a KNN graph for each index node

cell2location.utils.sum_neighbours(X_data, neighbours)[source]

Sum X_data values across neighbours.

  • coords – numpy.ndarray with variable measurements for each observation (observations * variables)

  • neighbours – numpy.ndarray with neigbour indices for each observation (observations * neigbours)