Skip to content

scdef.pl.obs_cell_factor_heatmap

obs_cell_factor_heatmap(model: scDEF, subset_obs_key: str, subset_obs: Union[str, Sequence[str]], group_obs_key: str, layer_idx: int = 0, factors: Optional[Sequence[Union[str, int]]] = None, values: Literal['score', 'prob'] = 'score', cluster_cells: bool = True, sort_layer_factors: bool = True, merge_batch_technical: bool = False, group_order: Optional[Sequence[str]] = None, show_group_track: bool = True, figsize: Optional[Tuple[float, float]] = None, figwidth: float = 10.0, row_height: float = 0.004, group_track_width: float = 0.05, cmap: Union[str, Colormap] = FACTOR_CMAP, norm: Optional[Normalize] = None, vmin: Optional[float] = None, vmax: Optional[float] = None, colorbar_label: Optional[str] = None, group_separator_linewidth: float = 1.0, group_separator_color: str = 'white', group_separator_alpha: float = 0.9, xlabel: str = 'Factors', factor_label_rotation: float = 90.0, factor_fontsize: int = 8, label_fontsize: int = 10, colorbar_label_fontsize: int = 9, colorbar_tick_fontsize: int = 8, show_annotations: bool = False, annotation_fontsize: int = 8, annotation_rotation: float = 45.0, save: Optional[str] = None, show: bool = True) -> Optional[Figure]

Heatmap of per-cell factor scores or probabilities for one or more obs subsets.

Restricts to cells matching subset_obs in subset_obs_key (for example one patient or coarse cell type), orders rows by group_obs_key (for example treatment or subtype), and within each group optionally sorts cells by Ward hierarchical clustering on the displayed factor vectors. Columns are factors from layer_idx. By default columns follow the hierarchy graph (model.get_layer_factor_orders()); pass factors to select and order columns explicitly.

When subset_obs contains multiple values, one heatmap panel is drawn per value as a vertical stack of subplots with a shared x-axis (factor columns). The group_obs_key color strip is drawn to the left of each heatmap.

Requires model.annotate() (or equivalent) so layer scores and/or probabilities are stored in adata.obsm as X_<layer> and X_<layer>_probs.

Parameters:

Name Type Description Default
model scDEF

scDEF model instance.

required
subset_obs_key str

adata.obs column selecting the outer group (e.g. patient).

required
subset_obs Union[str, Sequence[str]]

Value or values of subset_obs_key to include.

required
group_obs_key str

adata.obs column defining row blocks (e.g. treatment).

required
layer_idx int

Layer whose factors form the heatmap columns.

0
factors Optional[Sequence[Union[str, int]]]

Optional list of factor names (or indices within the layer) to plot. Columns appear in this order; overrides sort_layer_factors.

None
values Literal['score', 'prob']

"score" for raw cell factor scores; "prob" for normalized probabilities from X_<layer>_probs. With merge_batch_technical, probabilities are instead re-derived by row-normalizing the merged matrix, so a merged column holds the probability mass of the whole group rather than of one batch half (identical to summing the members' probabilities when reduce="sum").

'score'
cluster_cells bool

If True, hierarchically cluster cells within each group_obs_key block (Ward linkage on Euclidean distance).

True
sort_layer_factors bool

If True and factors is None, order columns with model.get_layer_factor_orders(). This applies with merge_batch_technical too: a merged column's members share a parent, so they are contiguous in the hierarchy ordering and the merged column simply takes their place.

True
merge_batch_technical bool

If True, replace each group of batch-technical splits with one merged column via factor_batch_correction, so the program reads as a single column firing across both batch blocks instead of two columns each firing in one. Merged columns are labelled "L0_7+L0_14" so the corrected split stays visible. Layer 0 only. factors may reference merged labels. With values="prob", probabilities are re-derived from the merged scores (see values).

False
group_order Optional[Sequence[str]]

Optional explicit order of group_obs_key categories; default uses categorical order when available, else sorted unique values.

None
show_group_track bool

Draw a narrow color strip for group_obs_key to the left of each heatmap.

True
figsize Optional[Tuple[float, float]]

Figure size (width, height) in inches. If None, inferred from figwidth, row_height, and the number of cells per panel.

None
figwidth float

Figure width in inches when figsize is None.

10.0
row_height float

Height in inches per cell row when figsize is None.

0.004
group_track_width float

Gridspec width ratio for the group strip (default matches the colorbar column width).

0.05
cmap Union[str, Colormap]

Matplotlib colormap name or Colormap instance for the heatmap.

FACTOR_CMAP
norm Optional[Normalize]

Optional normalization for the heatmap colormap (e.g. matplotlib.colors.Normalize). If set, overrides vmin/vmax.

None
vmin Optional[float]

Optional lower bound for the color scale (ignored when norm is set).

None
vmax Optional[float]

Optional upper bound for the color scale (ignored when norm is set).

None
colorbar_label Optional[str]

Optional colorbar title; inferred from values if None.

None
group_separator_linewidth float

Line width of horizontal separators between group_obs_key blocks.

1.0
group_separator_color str

Color of subgroup separator lines.

'white'
group_separator_alpha float

Alpha of subgroup separator lines.

0.9
xlabel str

X-axis label.

'Factors'
factor_label_rotation float

Rotation of factor names on the x-axis.

90.0
factor_fontsize int

Font size for factor name tick labels.

8
label_fontsize int

Font size for the x-axis label.

10
colorbar_label_fontsize int

Font size for the colorbar title.

9
colorbar_tick_fontsize int

Font size for colorbar tick labels.

8
show_annotations bool

If True, show factor_obs['annotation'] labels on a secondary x-axis at the top of the first (top-row) heatmap panel (45° by default).

False
annotation_fontsize int

Font size for top annotation labels.

8
annotation_rotation float

Rotation of top annotation labels in degrees.

45.0
save Optional[str]

If set, save the figure to this path (in addition to returning it when show=False).

None
show bool

Whether to call plt.show() and return None. If False, returns the Figure so it can be saved or further customized.

True

Returns:

Type Description
Optional[Figure]

Figure if show is False, else None.