Skip to content

plot_similarity_heatmap

Compute model similarity and plot it as a heatmap.

Parameters:

Name Type Description Default
models Dict[str, object]

Mapping of model ids to GRADIEND models.

required
measure str

Similarity measure.

'cosine'
part Optional[str]

Model part used by weight-based measures.

None
topk Optional[Union[int, float]]

Number or fraction of top weights for top-k measures.

None
value str

Value variant for overlap-style measures.

'intersection_frac'
order Union[str, List[str]]

"input", "cluster", or explicit model order.

'input'
cluster bool

Whether to cluster rows/columns.

False
annot Union[bool, str]

Whether/how to annotate cells.

'auto'
fmt Optional[str]

Deprecated alias for annot_fmt.

None
annot_fmt Optional[str]

Cell annotation format.

None
figsize Optional[Tuple[float, float]]

Optional figure size.

None
cmap str

Heatmap colormap.

'viridis'
vmin Optional[float]

Optional lower value bound.

None
vmax Optional[float]

Optional upper value bound.

None
title Optional[Union[str, bool]]

Optional title, or False to omit.

False
output_path Optional[str]

Optional output path.

None
show bool

Whether to display the plot.

True
return_data bool

Whether to include computed data.

True
return_fig_ax bool

Whether to include matplotlib figure/axis.

False
ax Optional[Any]

Optional existing matplotlib axis.

None
pretty_groups Optional[Dict[str, List[str]]]

Optional model-id groups shown as brackets.

None
scale str

Color scale type.

'linear'
scale_gamma Optional[float]

Optional gamma for power scaling.

None
annot_fontsize Optional[Union[int, float]]

Optional annotation font size.

None
tick_label_fontsize Optional[Union[int, float]]

Optional tick-label font size.

None
group_label_fontsize Optional[Union[int, float]]

Optional group-label font size.

None
group_label_rotation_top Union[int, float]

Rotation for top group labels.

0
group_label_rotation_right Union[int, float]

Rotation for right group labels.

0
cbar_pad Optional[float]

Optional colorbar padding.

None
cbar_y_pad Optional[float]

Optional vertical colorbar offset as a fraction of the heatmap height; negative values move it down.

None
cbar_fontsize Optional[Union[int, float]]

Optional colorbar font size.

None
cbar_shrink Optional[float]

Optional colorbar shrink factor (width relative to heatmap).

None
percentages bool

Whether to show values as percentages.

False
row_metric Optional[Dict[str, float]]

Optional side metric by row id.

None
row_metric_label Optional[str]

Label for the side metric.

None
row_metric_cmap str

Colormap for the side metric.

'magma'
row_metric_vmin Optional[float]

Optional side-metric lower bound.

None
row_metric_vmax Optional[float]

Optional side-metric upper bound.

None
row_label_mapping Optional[Dict[str, str]]

Optional mapping for row labels.

None
column_label_mapping Optional[Dict[str, str]]

Optional mapping for column labels.

None
seed_aggregate str

Seed aggregation mode.

'mean'
dispersion str

Dispersion mode.

'none'
seed_pairing_mode str

"all_pairs" or positionally aligned "matched".

'matched'
dispersion_display str

How to show dispersion values.

'none'
seed_annotation Union[bool, Dict[str, Any]]

Whether/how to annotate seed counts.

False
converged_by_id Optional[Dict[str, Optional[bool]]]

Optional explicit convergence status by stable model id.

None
highlight_non_convergence bool

Whether labels mark non-converged runs.

True
Source code in gradiend/visualizer/heatmaps/similarity.py
def plot_similarity_heatmap(
    models: Dict[str, object],
    *,
    measure: str = "cosine",
    part: Optional[str] = None,
    topk: Optional[Union[int, float]] = None,
    value: str = "intersection_frac",
    order: Union[str, List[str]] = "input",
    cluster: bool = False,
    annot: Union[bool, str] = "auto",
    fmt: Optional[str] = None,
    annot_fmt: Optional[str] = None,
    figsize: Optional[Tuple[float, float]] = None,
    cmap: str = "viridis",
    vmin: Optional[float] = None,
    vmax: Optional[float] = None,
    title: Optional[Union[str, bool]] = False,
    output_path: Optional[str] = None,
    show: bool = True,
    return_data: bool = True,
    return_fig_ax: bool = False,
    ax: Optional[Any] = None,
    pretty_groups: Optional[Dict[str, List[str]]] = None,
    scale: str = "linear",
    scale_gamma: Optional[float] = None,
    annot_fontsize: Optional[Union[int, float]] = None,
    tick_label_fontsize: Optional[Union[int, float]] = None,
    axis_label_fontsize: Optional[Union[int, float]] = None,
    group_label_fontsize: Optional[Union[int, float]] = None,
    group_label_rotation_top: Union[int, float] = 0,
    group_label_rotation_right: Union[int, float] = 0,
    cbar_pad: Optional[float] = None,
    cbar_y_pad: Optional[float] = None,
    cbar_fontsize: Optional[Union[int, float]] = None,
    cbar_shrink: Optional[float] = None,
    percentages: bool = False,
    row_metric: Optional[Dict[str, float]] = None,
    row_metric_label: Optional[str] = None,
    row_metric_cmap: str = "magma",
    row_metric_vmin: Optional[float] = None,
    row_metric_vmax: Optional[float] = None,
    row_label_mapping: Optional[Dict[str, str]] = None,
    column_label_mapping: Optional[Dict[str, str]] = None,
    seed_aggregate: str = "mean",
    dispersion: str = "none",
    seed_pairing_mode: str = "matched",
    dispersion_display: str = "none",
    seed_annotation: Union[bool, Dict[str, Any]] = False,
    converged_by_id: Optional[Dict[str, Optional[bool]]] = None,
    highlight_non_convergence: bool = True,
) -> Any:
    """Compute model similarity and plot it as a heatmap.

    Args:
        models: Mapping of model ids to GRADIEND models.
        measure: Similarity measure.
        part: Model part used by weight-based measures.
        topk: Number or fraction of top weights for top-k measures.
        value: Value variant for overlap-style measures.
        order: ``"input"``, ``"cluster"``, or explicit model order.
        cluster: Whether to cluster rows/columns.
        annot: Whether/how to annotate cells.
        fmt: Deprecated alias for ``annot_fmt``.
        annot_fmt: Cell annotation format.
        figsize: Optional figure size.
        cmap: Heatmap colormap.
        vmin: Optional lower value bound.
        vmax: Optional upper value bound.
        title: Optional title, or False to omit.
        output_path: Optional output path.
        show: Whether to display the plot.
        return_data: Whether to include computed data.
        return_fig_ax: Whether to include matplotlib figure/axis.
        ax: Optional existing matplotlib axis.
        pretty_groups: Optional model-id groups shown as brackets.
        scale: Color scale type.
        scale_gamma: Optional gamma for power scaling.
        annot_fontsize: Optional annotation font size.
        tick_label_fontsize: Optional tick-label font size.
        group_label_fontsize: Optional group-label font size.
        group_label_rotation_top: Rotation for top group labels.
        group_label_rotation_right: Rotation for right group labels.
        cbar_pad: Optional colorbar padding.
        cbar_y_pad: Optional vertical colorbar offset as a fraction of the
            heatmap height; negative values move it down.
        cbar_fontsize: Optional colorbar font size.
        cbar_shrink: Optional colorbar shrink factor (width relative to heatmap).
        percentages: Whether to show values as percentages.
        row_metric: Optional side metric by row id.
        row_metric_label: Label for the side metric.
        row_metric_cmap: Colormap for the side metric.
        row_metric_vmin: Optional side-metric lower bound.
        row_metric_vmax: Optional side-metric upper bound.
        row_label_mapping: Optional mapping for row labels.
        column_label_mapping: Optional mapping for column labels.
        seed_aggregate: Seed aggregation mode.
        dispersion: Dispersion mode.
        seed_pairing_mode: ``"all_pairs"`` or positionally aligned ``"matched"``.
        dispersion_display: How to show dispersion values.
        seed_annotation: Whether/how to annotate seed counts.
        converged_by_id: Optional explicit convergence status by stable model id.
        highlight_non_convergence: Whether labels mark non-converged runs.
    """
    comparison_data = compute_similarity_matrix(
        models,
        measure=measure,
        part=part,
        topk=topk,
        value=value,
        seed_aggregate=seed_aggregate,
        dispersion=dispersion,
        seed_pairing_mode=seed_pairing_mode,
    )
    return plot_comparison_heatmap(
        comparison_data,
        order=order,
        cluster=cluster,
        annot=annot,
        fmt=fmt,
        annot_fmt=annot_fmt,
        figsize=figsize,
        cmap=cmap,
        vmin=vmin,
        vmax=vmax,
        title=title,
        output_path=output_path,
        show=show,
        return_data=return_data,
        return_fig_ax=return_fig_ax,
        ax=ax,
        pretty_groups=pretty_groups,
        scale=scale,
        scale_gamma=scale_gamma,
        annot_fontsize=annot_fontsize,
        tick_label_fontsize=tick_label_fontsize,
        axis_label_fontsize=axis_label_fontsize,
        group_label_fontsize=group_label_fontsize,
        group_label_rotation_top=group_label_rotation_top,
        group_label_rotation_right=group_label_rotation_right,
        cbar_pad=cbar_pad,
        cbar_y_pad=cbar_y_pad,
        cbar_fontsize=cbar_fontsize,
        cbar_shrink=cbar_shrink,
        percentages=percentages,
        row_metric=row_metric,
        row_metric_label=row_metric_label,
        row_metric_cmap=row_metric_cmap,
        row_metric_vmin=row_metric_vmin,
        row_metric_vmax=row_metric_vmax,
        row_label_mapping=row_label_mapping,
        column_label_mapping=column_label_mapping,
        dispersion_display=dispersion_display,
        seed_annotation=seed_annotation,
        models=models,
        converged_by_id=converged_by_id,
        highlight_non_convergence=highlight_non_convergence,
    )