grassp.pp.set_sensible_compartment_colors

grassp.pp.set_sensible_compartment_colors#

set_sensible_compartment_colors(data, columns=None, cutoff=0.3, verbose=False, plot_mapping=False)[source]#

Assign colors to compartment annotation columns in .obs using the built-in color dictionary.

Scans columns in .obs and identifies those that look like compartment annotation columns by checking what fraction of their unique values appear in the built-in MARKER_COLORS dictionary. Matching is case-insensitive and treats underscores as spaces (e.g. "endoplasmic_reticulum" matches "Endoplasmic Reticulum").

Columns where at least cutoff fraction of unique values are recognised compartments receive a '{col}_colors' entry in .uns, following scanpy plotting conventions. Known compartments get their canonical color from MARKER_COLORS; unknown categories receive distinct colors drawn from matplotlib’s tab20 / tab20b / tab20c palettes. All assigned colors are deduplicated so that every category in a column maps to a unique color.

Parameters:
data AnnData

AnnData object whose .obs columns are scanned for compartment annotations.

columns list[str] | None (default: None)

List of column names in .obs to process. If None (default), all columns in .obs are scanned.

cutoff float (default: 0.3)

Minimum fraction of unique values in a column that must be recognised compartments for that column to receive a '{col}_colors' entry in .uns. Default is 0.3.

verbose bool (default: False)

If True, print diagnostic information for each processed column, including the matched fraction, assigned colors, and any fallback assignments.

plot_mapping bool (default: False)

If True, display a grid plot showing the color assigned to each category for every processed column, with fallback colors visually distinguished.

Return type:

None