Skip to content

Dendrogram utils

Functions

create_dendrogram(x, add_sep, nb_clusters, orientation='bottom', labels=None, colorscale=None, hovertext=None)

Return a dendrogram Plotly figure object. This is a thin wrapper around scipy.cluster.hierarchy.dendrogram.

Parameters:

Name Type Description Default
x ndarray

Result of scipy.cluster.hierarchy.linkage() function

required
add_sep bool

Whether a separation is shown on the TAK or no. This parameter allows the dendogram to be cut and only displays clusters on the dendogram

required
nb_clusters int

Number of clusters on the TAK

required
orientation str

'top', 'right', 'bottom', or 'left'

'bottom'
labels list[str] | None

List of axis category labels(observation labels)

None
colorscale list | None

Optional colorscale for the dendrogram tree. Requires 8 colors to be specified, the 7th of which is ignored. With scipy>=1.5.0, the 2nd, 3rd and 6th are used twice as often as the others. Given a shorter list, the missing values are replaced with defaults and with a longer list the extra values are ignored.

None
hovertext list | None

List of hovertext for constituent traces of dendrogram clusters

None

Returns:

Type Description
tuple[Figure, list[float], float]

Dendogram representing the hiearchical clustering

Source code in opentak/dendrogram_utils.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def create_dendrogram(
    # ruff: noqa: N803
    x: np.ndarray,
    add_sep: bool,
    nb_clusters: int,
    orientation: str = "bottom",
    labels: list[str] | None = None,
    colorscale: list | None = None,
    hovertext: list | None = None,
) -> tuple[graph_objs.Figure, list[float], float]:
    # ruff: noqa: D205
    """Return a dendrogram Plotly figure object. This is a thin
    wrapper around scipy.cluster.hierarchy.dendrogram.

    :param x: Result of scipy.cluster.hierarchy.linkage() function
    :param add_sep: Whether a separation is shown on the TAK or no. This parameter allows the dendogram to be cut and
    only displays clusters on the dendogram
    :param nb_clusters: Number of clusters on the TAK
    :param orientation: 'top', 'right', 'bottom', or 'left'
    :param labels: List of axis category labels(observation labels)
    :param colorscale: Optional colorscale for the dendrogram tree.
                              Requires 8 colors to be specified, the 7th of
                              which is ignored.  With scipy>=1.5.0, the 2nd, 3rd
                              and 6th are used twice as often as the others.
                              Given a shorter list, the missing values are
                              replaced with defaults and with a longer list the
                              extra values are ignored.
    :param hovertext: List of hovertext for constituent traces of dendrogram
                               clusters

    :return: Dendogram representing the hiearchical clustering
    """
    dendrogram = _Dendrogram(
        x,
        add_sep,
        nb_clusters,
        orientation,
        labels,
        colorscale,
        hovertext=hovertext,
    )

    fig = graph_objs.Figure(data=dendrogram.data, layout=dendrogram.layout)

    return (
        fig,
        dendrogram.yaxis_range,
        dendrogram.threshold_vertical,
    )