lazyslide.tl.feature_aggregation

lazyslide.tl.feature_aggregation#

feature_aggregation(wsi, feature_key, layer_key=None, encoder='mean', tile_key='tiles', by=None, agg_key=None, amp=False, autocast_dtype=torch.float16, device='cpu')#

Aggregate features by groups.

The feature aggregation is done by applying an encoder to a group of features to acquire a 1d representation of the group. Notice that the final shape of the aggregated features might not be the same as the original features.

Parameters:
wsiWSIData

The whole-slide image object.

feature_keystr

The key to indicate which feature to aggregate.

layer_keystr, optional

The key of the layer in the feature table.

encoderstr or callable, default: ‘mean’
  • Numpy functions: ‘mean’, ‘median’, ‘sum’, ‘std’, ‘var’, …

  • prism: Prism slide encoder. The feature must be extracted by Virchow model.

  • titan: Titan slide encoder. The feature must be extracted by Titan/CONCH_v1.5 model.

  • madeleine: Madeleine slide encoder. The feature must be extracted by CONCH model.

  • chief: Chief slide encoder. The feature must be extracted by CHIEF model.

tile_keystr, default: ‘tiles’

The key of the tiles dataframe in the spatial data object.

bystr or array of str, default: None

The level to aggregate the features.

  • By default will aggregate the features from all tiles in the slide.

  • Column name in tile dataframe: Aggregate the features by specific column. For example, to aggregate by tissue pieces, set by=’tissue_id’.

agg_keystr, optional

The key to store the aggregated features. If not provided, the key will be ‘agg_{by}’.

ampbool, default: False

Whether to use automatic mixed precision.

autocast_dtypetorch.dtype, default: torch.float16

The dtype for automatic mixed precision.

devicestr, optional

The device to use for inference. If not provided, the device will be automatically selected.

Returns:
None

Note

The aggregation features and operation will be recorded in the uns slot of features AnnData. The aggregated features will only be added to the varm slot if their shape is the same as the original features.

Examples

>>> import lazyslide as zs
>>> wsi = zs.datasets.sample(with_data=False)
>>> zs.pp.find_tissues(wsi)
>>> zs.pp.tile_tissues(wsi, 256, mpp=0.5)
>>> zs.tl.feature_extraction(wsi, "resnet50")
>>> zs.tl.feature_aggregation(wsi, feature_key="resnet50", by="tissue_id")
>>> wsi.tables['resnet50_tiles'].uns['agg_tissue_id']