mantispy.tl.aggregate

Contents

mantispy.tl.aggregate#

mantispy.tl.aggregate(adata, by=('Metadata_Plate', 'Metadata_Well'), func='median', min_cells=10, layer=None, count_key='Metadata_CellCount', site_key='Metadata_SiteCount')#

Aggregate adata to one profile per group.

Parameters:
  • adata (AnnData) – Single cells, or profiles to aggregate further, as its recorded resolution says.

  • by (Sequence[str] (default: ('Metadata_Plate', 'Metadata_Well'))) – Columns defining a profile. The default is one profile per well.

  • func (str (default: 'median')) – "median" (the pycytominer default) or "mean".

  • min_cells (int (default: 10)) – Groups with fewer cells than this are dropped.

  • layer (str | None (default: None)) – Aggregate this layer instead of X.

  • count_key (str (default: 'Metadata_CellCount')) – obs column the cell count is written to, and read from when adata holds profiles.

  • site_key (str (default: 'Metadata_SiteCount')) – obs column the number of fields of view is written to, and read from when adata holds profiles.

Return type:

AnnData

Returns:

A new AnnData with one row per group. var is carried over unchanged; obs holds the grouping columns, count_key, site_key when the fields of view are known, and every other Metadata_ column that is constant within every group. count_key is the number of cells behind a row, so its scope follows by: grouping by site counts the cells of one field of view, grouping by well those of every field. Profiles contribute the cells they carry rather than one each, and profiles that carry no count give an unknown one. site_key is the number of fields that contributed cells, summed where the rows carry it and counted from Metadata_Site otherwise. The resolution recorded is "well" when by holds both Metadata_Plate and Metadata_Well, since a finer grouping such as one row per site is still per-well or finer, and "perturbation" otherwise.

Raises:

ValueErrorfunc is not one of FUNCTIONS.

Notes

This uses mantispy’s own NaN-skipping kernel rather than scanpy.get.aggregate(), which propagates NaN and is measurably slower on both mean and median.