Skip to content

SPADEResult

densitree.result.SPADEResult(labels_, tree_, X_down, down_idx, n_features, feature_names=None, _cluster_stats=None) dataclass

Rich output object from a SPADE run.

Attributes:

Name Type Description
labels_ (ndarray[int], shape(n_cells))

Cluster assignment for every original cell.

tree_ Graph

MST connecting cluster centroids. Each node has size (int) and median (ndarray) attributes.

X_down (ndarray, shape(n_down, n_features))

Downsampled cells used for clustering.

down_idx (ndarray[int], shape(n_down))

Indices into the original array for the downsampled cells.

cluster_stats_ DataFrame

One row per cluster with size and median_<feature> columns. Built lazily on first access.

plot_tree(color_by=None, size_by='count', backend='matplotlib')

Visualize the SPADE tree.

Parameters:

Name Type Description Default
color_by int | str | None

Feature index (int) or name (str) to color nodes by median expression. None colors all nodes the same.

None
size_by str

'count' scales node size by cell count. Any other value uses uniform size.

'count'
backend str

'matplotlib' for static plots, 'plotly' for interactive.

'matplotlib'
Source code in densitree/result.py
def plot_tree(
    self,
    color_by: int | str | None = None,
    size_by: str = "count",
    backend: str = "matplotlib",
):
    """Visualize the SPADE tree.

    Parameters
    ----------
    color_by:
        Feature index (int) or name (str) to color nodes by median expression.
        ``None`` colors all nodes the same.
    size_by:
        ``'count'`` scales node size by cell count. Any other value uses uniform size.
    backend:
        ``'matplotlib'`` for static plots, ``'plotly'`` for interactive.
    """
    if backend == "matplotlib":
        from densitree.plot.matplotlib import plot_tree as _plot
    elif backend == "plotly":
        try:
            from densitree.plot.plotly import plot_tree as _plot
        except ImportError as e:
            raise ImportError(
                "plotly is required for backend='plotly'. "
                "Install it with: pip install plotly"
            ) from e
    else:
        raise ValueError(f"Unknown backend '{backend}'. Use 'matplotlib' or 'plotly'.")

    return _plot(self, color_by=color_by, size_by=size_by)