spateo.tools.cluster.leiden

Functions

distance_knn_graph(dist, num_neighbors)

Construct a k-nearest neighbor graph from a distance matrix.

embedding_knn_graph(X, num_neighbors)

Construct a k-nearest neighbor graph from an arbitrary array, of shape [n_samples, n_features]

adj_to_igraph(adj)

Convert an adjacency matrix to an igraph graph.

calculate_leiden_partition(→ numpy.ndarray)

Performs Leiden clustering on a given dataset.

calculate_louvain_partition(→ numpy.ndarray)

Performs Louvain clustering on a given dataset.

Module Contents

spateo.tools.cluster.leiden.distance_knn_graph(dist: numpy.ndarray, num_neighbors: int)[source]

Construct a k-nearest neighbor graph from a distance matrix.

Parameters:
dist

Pairwise distance matrix

num_neighbors

Number of nearest neighbors

Returns:

K-nearest neighbor graph

Return type:

G

spateo.tools.cluster.leiden.embedding_knn_graph(X: numpy.ndarray, num_neighbors: int)[source]

Construct a k-nearest neighbor graph from an arbitrary array, of shape [n_samples, n_features]

Parameters:
X

Embedding matrix

num_neighbors

Number of nearest neighbors

Returns:

K-nearest neighbor graph

Return type:

G

spateo.tools.cluster.leiden.adj_to_igraph(adj: numpy.ndarray)[source]

Convert an adjacency matrix to an igraph graph.

spateo.tools.cluster.leiden.calculate_leiden_partition(adj: scipy.sparse.spmatrix | numpy.ndarray | None = None, input_mat: numpy.ndarray | None = None, num_neighbors: int = 10, graph_type: Literal['distance', 'embedding'] = 'distance', resolution: float = 1.0, n_iterations: int = -1) numpy.ndarray[source]

Performs Leiden clustering on a given dataset.

Parameters:
adj

Optional precomputed adjacency matrix

input_mat

Optional, will be used only if ‘adj’ is not given. The input data, will be interepreted as either a distance matrix (if :param graph_type is “distance” or an embedding matrix (if :param graph_type is “embedding”)

num_neighbors

Only used if ‘adj’ is not given- the number of nearest neighbors for constructing the graph

graph_type

Only used if ‘adj’ is not given- specifies the input type, either ‘distance’ or ‘embedding’

resolution

The resolution parameter for the Leiden algorithm

n_iterations

The number of iterations for the Leiden algorithm (-1 for unlimited iterations)

Returns:

Array containing cluster assignments

Return type:

clusters

spateo.tools.cluster.leiden.calculate_louvain_partition(adj: scipy.sparse.spmatrix | numpy.ndarray | None = None, input_mat: numpy.ndarray | None = None, num_neighbors: int = 10, graph_type: Literal['distance', 'embedding'] = 'distance', resolution: float = 1.0, n_iterations: int = -1) numpy.ndarray[source]

Performs Louvain clustering on a given dataset.

Parameters:
adj

Optional precomputed adjacency matrix

input_mat

Optional, will be used only if ‘adj’ is not given. The input data, will be interepreted as either a distance matrix (if :param graph_type is “distance” or an embedding matrix (if :param graph_type is “embedding”)

num_neighbors

Only used if ‘adj’ is not given- the number of nearest neighbors for constructing the graph

graph_type

Only used if ‘adj’ is not given- specifies the input type, either ‘distance’ or ‘embedding’

resolution

The resolution parameter for the Louvain algorithm

n_iterations

The number of iterations for the Louvain algorithm (-1 for unlimited iterations)

Returns:

Array containing cluster assignments

Return type:

clusters