Skip to content

consecutive_cluster

consecutive_cluster(cluster: array) -> Tuple[array, array]

Relabels cluster ids to consecutive integers.

Parameters:

Name Type Description Default
cluster array

Cluster id of each node, of shape [N].

required

Returns:

Type Description
A tuple `(cluster, perm)`

the new cluster ids in `[0,

array

num_clusters)` (in increasing order of the original ids), and for

Tuple[array, array]

each cluster the index of one of its nodes (the last one), as in PyG.

Source code in mlx_graphs/utils/clustering.py
def consecutive_cluster(cluster: mx.array) -> Tuple[mx.array, mx.array]:
    """Relabels cluster ids to consecutive integers.

    Args:
        cluster: Cluster id of each node, of shape ``[N]``.

    Returns:
        A tuple ``(cluster, perm)``: the new cluster ids in ``[0,
        num_clusters)`` (in increasing order of the original ids), and for
        each cluster the index of one of its nodes (the last one), as in PyG.
    """
    values = _to_numpy(cluster).reshape(-1)
    _, inverse = np.unique(values, return_inverse=True)
    inverse = inverse.reshape(-1)
    perm = np.empty(int(inverse.max(initial=-1)) + 1, dtype=np.int64)
    perm[inverse] = np.arange(inverse.size)
    return mx.array(inverse.astype(np.int64)), mx.array(perm)

from mlx_graphs.utils import consecutive_cluster · mlx_graphs/utils/clustering.py