get_defined_color_map

spac.utils.get_defined_color_map(adata, defined_color_map=None, annotations=None, colorscale='viridis')[source]

Retrieve or generate a predefined color mapping dictionary from an AnnData object.

If defined_color_map is provided and found within adata.uns, the corresponding dictionary is returned. Otherwise, if it is not provided, a color mapping is generated using the unique values of the annotation column specified by annotations and the given colorscale.

Parameters:
  • adata (anndata.AnnData) – Annotated data matrix object that should contain a color mapping in its uns attribute if a predefined mapping is desired.

  • defined_color_map (str, optional) – The key in adata.uns that holds the predefined color mapping. If None, a new mapping is generated using annotations.

  • annotations (str, optional) – The annotation column name in adata.obs from which to obtain unique labels if defined_color_map is not provided.

  • colorscale (str, optional) – The Matplotlib colormap name to use when generating a color mapping if defined_color_map is not provided. Default is ‘viridis’.

Returns:

A dictionary mapping unique labels to colors.

Return type:

dict

Raises:
  • TypeError – If defined_color_map is provided but is not a string.

  • ValueError – If a predefined mapping is requested but not found, or if neither defined_color_map nor annotations is provided.