Time-evolving graphs
Generate graphs from the dynamical degree-corrected stochastic block model and detect communities that evolve in time.
The dynamical degree-corrected stochastic block model
CoDeBetHe generates time-evolving graphs with communities from the dynamical degree-corrected stochastic block model (D-DCSBM). A dynamical graph $\{\mathcal{G}_t\}_{t=1,\dots,T}$ is a sequence of $T$ snapshots on the same $n$ nodes, with label vectors $\ell_t \in \{1,\dots,k\}^n$ and adjacency matrices $A^{(t)}$. The labels at $t = 1$ are assigned so that a fraction $\pi_a$ of the nodes is in community $a$. They then evolve according to the Markov process
where $\eta$ is the label persistence. Each $A^{(t)}$ is then drawn independently from the DC-SBM described in Static graphs.
Create a synthetic graph
The parameters are the same as for the static model, plus the persistence $\eta$ and the number of snapshots $T$:
import numpy as np
from codebethe import matrix_C, adjacency_matrix_DDCSBM
n = int(10 ** 3.5) # number of nodes
k = 10 # number of communities
η = 0.7 # label persistence
T = 6 # number of snapshots
θ = np.random.uniform(3, 10, n) ** 3 # degree correction
θ = θ / θ.mean()
π_v = np.abs(np.random.normal(1 / k, 1 / (2 * k), k)) # community sizes
π_v = π_v / π_v.sum()
c = 8. # expected average degree
c_out = 1.5 # expected off-diagonal elements of C
f = 1 / k # fluctuation of the off-diagonal elements
C = matrix_C(c_out, c, f, π_v)
AT, ℓ_T = adjacency_matrix_DDCSBM(T, C, c, η, θ, π_v) # list of T sparse matrices, T x n labels
using CoDeBetHe
using Distributions, DelimitedFiles, LinearAlgebra, DataFrames, StatsBase
n = floor(Int64, 10^3.5) # number of nodes
k = 10 # number of communities
η = 0.7 # label persistence
T = 6 # number of snapshots
θ = rand(Uniform(3, 10), n).^3 # degree correction
θ = θ./mean(θ)
π_v = abs.(rand(Normal(1/k, 1/(2*k)), k)) # community sizes
π_v = π_v/sum(π_v)
c = 8. # expected average degree
c_out = 1.5 # expected off-diagonal elements of C
f = 1/k # fluctuation of the off-diagonal elements
C = matrix_C(c_out, c, f, π_v)
AT, ℓ_T = adjacency_matrix_DDCSBM(T, C, c, η, θ, π_v)
Load a real time-evolving graph
The datasets folder also contains the primary school contact network collected by the SocioPatterns collaboration, in a slightly modified format. We build one snapshot per time step and drop the last ones, which contain almost no contacts.
import numpy as np
from scipy.sparse import csr_matrix
edge_list = np.loadtxt('datasets/el_primary.dat').astype(int) # edges (i, j)
index = np.loadtxt('datasets/index_primary.dat').astype(int).ravel() # 1 for the nodes to keep
tt = np.loadtxt('datasets/times_primary.dat').astype(int).ravel() # time of each edge
keep = np.flatnonzero(index == 1)
n = edge_list.max() + 1
AT = []
for t in range(1, tt.max() + 1):
el = edge_list[tt == t]
A = csr_matrix((np.ones(len(el)), (el[:, 0], el[:, 1])), shape=(n, n))
A.data[:] = 1 # unweighted
A = A[keep][:, keep]
AT.append(A + A.T) # undirected
T = int(np.argmin([A.sum() for A in AT])) - 1
AT = AT[:T]
using SparseArrays
edge_list = convert(Array{Int64}, readdlm("datasets/el_primary.dat")) # edges (i, j)
index = convert(Array{Int64}, readdlm("datasets/index_primary.dat")) # 1 for the nodes to keep
tt = convert(Array{Int64}, readdlm("datasets/times_primary.dat")) # time of each edge
id = findall(index .== 1)
index = [id[i][1] for i=1:length(id)]
T = maximum(tt)
n = maximum(unique(edge_list)) + 1
AT = [spdiagm(0 => zeros(n)) for i=1:T]
edge_list = edge_list .+ 1
for t=1:T
idx = [i[1] for i in findall(tt .== t)]
el = edge_list[idx,:]
AT[t] = sign.(sparse(el[:,1], el[:,2], ones(length(el[:,1])), n, n)) # unweighted
AT[t] = AT[t][index,index]
AT[t] = AT[t] + AT[t]' # undirected
end
T = findmin([sum(AT[t]) for t=1:T])[2] - 2
AT = AT[1:T]
If you use this dataset, please cite: J. Stehlé et al., High-resolution measurements of face-to-face contact patterns in a primary school, PLOS ONE 6(8): e23176 (2011), and V. Gemmetto, A. Barrat and C. Cattuto, Mitigation of infectious disease at school: targeted class closure vs school closure, BMC Infectious Diseases 14:695 (2014).
Detect the communities
dynamic_community_detection_BH (or dynamic_community_detection in Python) implements Algorithm 1 of our NeurIPS 2020 paper. It needs the persistence $\eta$ and the number of communities $k$. For large graphs, set approx_embedding = true to compute the embedding with a fast polynomial approximation; its order m is chosen automatically unless you set it (a larger m is more precise but slower).
from codebethe import dynamic_community_detection
res = dynamic_community_detection(AT, η=0.55, k=10, approx_embedding=False, verbose=1)
res.ℓ # T x n array: labels of each node at each time
res.modularity # modularity at each time
o Creating the dynamical Bethe-Hessian of size nT = 7986 Computing a first estimate for k_neg: found k_neg = 104 (polynomial order 131). Trying for k_neg = 104 Trying for k_neg = 132 o Embedding of dimension 113
η = 0.55 # label persistence
k = 10 # number of communities
approx_embedding = false # true: fast approximate embedding
m = nothing # polynomial order (used if approx_embedding = true)
cluster = dynamic_community_detection_BH(AT, η, k; approx_embedding = approx_embedding, m = m, verbose = 1)
o Creating the dynamical Bethe Hessian matrix H (of size nT = 7986). o Computing the eigenvectors associated to all negative eigenvalues of H: Computing a first estimate for k_neg: found k_neg = 120 (with a polynomial approximation with a mean polynomial order of 136). Trying for k_neg = 120 The computed embedding is of dimension 114. o Computing k-means for each time step
The labels are matched across time, so that community $a$ at time $t$ corresponds to community $a$ at time $t+1$. If you pass the ground-truth labels (ℓ_T = ...), the result also contains the overlap at each time. See the API reference for all options.