API reference
All public functions of CoDeBetHe, in Python and Julia.
The two packages expose the same functions with the same arguments. In Python, labels are 0-based and every random function accepts an optional seed. In Julia, labels are 1-based.
from codebethe import (community_detection, dynamic_community_detection, # algorithms
matrix_C, create_label_vector, adjacency_matrix_DCSBM, # static generators
dynamic_label_HMM, adjacency_matrix_DDCSBM, # dynamic generators
compute_overlap, modularity) # utilities
using CoDeBetHe
# exports: community_detection_optimal_BH, dynamic_community_detection_BH,
# matrix_C, create_label_vector, adjacency_matrix_DCSBM, adjacency_matrix_DDCSBM
Community detection
community_detection_optimal_BH
Community detection in a static graph: Algorithm 2 of the JMLR paper.
res = community_detection(A, k=None, ℓ=None, ε=2e-5, projection=True, verbose=1, seed=None)
# community_detection_optimal_BH is an alias of community_detection
cluster = community_detection_optimal_BH(A; k = nothing, ℓ = nothing, ϵ = 2e-5, projection = true, verbose = 1)
| Parameter | Default | Description |
|---|---|---|
A |
required | Sparse symmetric adjacency matrix ($n \times n$): scipy.sparse in Python, SparseMatrixCSC{Float64,Int64} in Julia. |
k |
None / nothing |
Number of communities. If not given, it is estimated. |
ℓ |
None / nothing |
Ground-truth labels. If given, the overlap is computed. |
ε / ϵ |
2e-5 |
Precision of the solver. |
projection |
True |
Project the rows of the embedding onto the unit sphere before k-means. |
verbose |
1 |
0: silent, 1: some information, 2: all information. |
seed |
None |
Python only: random seed. |
Returns an object with fields:
ℓ: estimated labels (array of size $n$);k: number of communities;overlap: overlap with the ground truth (ifℓwas given);modularity: modularity of the partition;ζ: the values $\zeta_p$, $2 \le p \le k$, of the Bethe-Hessian parameters;Y: Python only, the $n \times (k-1)$ informative eigenvectors used for k-means.
dynamic_community_detection_BH
Community detection in a time-evolving graph: Algorithm 1 of the NeurIPS 2020 paper.
res = dynamic_community_detection(AT, η, k, ℓ_T=None, approx_embedding=False, m=None, verbose=1, seed=None)
# dynamic_community_detection_BH is an alias of dynamic_community_detection
cluster = dynamic_community_detection_BH(AT, η, k; ℓ_T = nothing, approx_embedding = false, m = nothing, verbose = 1)
| Parameter | Default | Description |
|---|---|---|
AT |
required | The $T$ sparse adjacency matrices, one per snapshot, on the same $n$ nodes. |
η |
required | Label persistence, in $[0, 1)$. |
k |
required | Number of communities. |
ℓ_T |
None / nothing |
Ground-truth labels ($T \times n$). If given, the overlap is computed. |
approx_embedding |
False |
Compute the embedding with the Chebyshev polynomial approximation (Algorithm 2 of the paper): faster for large $nT$. |
m |
None / nothing |
Order of the polynomial approximation. If not given, it is chosen adaptively. |
verbose |
1 |
0: silent, 1: some information, 2: all information. |
seed |
None |
Python only: random seed. |
Returns an object with fields:
ℓ: estimated labels, matched across time ($T \times n$ array: row $t$ contains the labels at time $t$);overlap: overlap with the ground truth at each time (ifℓ_Twas given);modularity: modularity at each time.
Graph generators
matrix_C
C = matrix_C(c_out, c, f, π_v)
Class affinity matrix $C \in \mathbb{R}^{k \times k}$ such that $C\,{\rm diag}(\pi)$ has the all-ones vector as leading eigenvector, with eigenvalue $c$.
| Parameter | Description |
|---|---|
c_out |
Average value of the off-diagonal terms of $C$. |
c |
Leading eigenvalue of $C\,{\rm diag}(\pi)$: the expected average degree. |
f |
Relative fluctuation of the off-diagonal terms (0 makes them all equal). |
π_v |
Community proportions (size $k$, summing to 1). |
create_label_vector
ℓ = create_label_vector(n, k, π_v)
Label vector of size $n$ in which a fraction π_v[a] of the nodes belongs to community $a$.
adjacency_matrix_DCSBM
A = adjacency_matrix_DCSBM(C, c, ℓ, θ)
Sparse symmetric adjacency matrix of a graph drawn from the degree-corrected stochastic block model, with affinity matrix C, average degree c, labels ℓ and degree correction θ (the expected degree of node $i$ is proportional to $\theta_i$).
adjacency_matrix_DDCSBM
AT, ℓ_T = adjacency_matrix_DDCSBM(T, C, c, η, θ, π_v)
Sequence of $T$ adjacency matrices drawn from the dynamical degree-corrected stochastic block model, with label persistence η, and the corresponding $T \times n$ labels ℓ_T.
dynamic_label_HMM
ℓ_T = dynamic_label_HMM(η, T, k, n, π_v)
Labels evolving in time with persistence η (the Markov process of the D-DCSBM). Exported in Python only.
Utilities (Python)
The Python package also exposes:
| Function | Description |
|---|---|
compute_overlap(ℓ, estimated_labels) |
Overlap between the ground-truth and the estimated labels, after matching them: $({\rm accuracy} - 1/k)/(1 - 1/k)$. |
modularity(A, est_ℓ) |
Newman’s modularity of a partition. |
CD_BH(A, k=None, verbose=0) |
Returns the tuple (ℓ, k, modularity, ζ): a drop-in replacement for the former PyJulia bridge. |
estimate_number_of_clusters(ρ, A) |
Number of communities, estimated from the regularized Laplacian. |
match_labels(ℓ) |
Relabels the communities at each time to match those at the previous time. |