mantispy.tl.neighbors_local_density#

mantispy.tl.neighbors_local_density(adata, k=15, by='Metadata_ImageID', key_added='Metadata_LocalDensity', copy=False)[source]#

Local cell density: how many cells per unit area surround each cell in its field of view.

The density is the k-nearest-neighbour estimate, k divided by the area of the disc reaching the k-th nearest cell, in cells per square unit of Metadata_Center_X and Metadata_Center_Y (usually pixels). Higher means more crowded. Local crowding changes morphology independently of treatment. Consider regressing it out with regress_out() before attributing a change to a perturbation.

Parameters:
adata AnnData

Single-cell object carrying Metadata_Center_X and Metadata_Center_Y.

k int (default: 15)

Number of neighbors whose disc sets the density; a field with fewer cells uses all of them.

by str (default: 'Metadata_ImageID')

obs column identifying the field of view. Neighbors are searched within each field only, since coordinates from different images are not comparable.

key_added str (default: 'Metadata_LocalDensity')

obs column written.

copy bool (default: False)

Return a modified copy instead of mutating in place.

Return type:

AnnData | None

Returns:

None, or the modified copy. Writes obs[key_added], and a cell alone in its field, or one sharing its position with all of its neighbours, gets NaN.

Raises:

KeyError – obs is missing Metadata_Center_X or Metadata_Center_Y.