|
| 1 | +""" |
| 2 | + ecg(g; γ=1, ensemble_size=16, min_edge_weight=0.05, min_weight_outside_2core=true, distmx=weights(g), max_moves=1000, max_merges=1000, move_tol=1e-9, merge_tol=1e-9, rng=nothing, seed=nothing) |
| 3 | +
|
| 4 | +Community detection using ensemble clustering for graphs (ECG). Weights the edges based on the |
| 5 | +proportion of time the endpoints are in the same cluster of a Louvain without merges before running |
| 6 | +a final Louvain to detect communities. |
| 7 | +
|
| 8 | +### Optional Arguments |
| 9 | +- `distmx=weights(g)`: distance matrix for weighted graphs |
| 10 | +- `ensemble_size=16`: the number of no merge Louvains in the ensemble |
| 11 | +- `min_edge_weight=0.05`: the minimum edge weight passed to the final Louvain (to retain the original topology). |
| 12 | +- `min_weight_outside_2core=true`: a flag to set the weight of edges outside the 2-core to the minimum value. If the graph is directed, the coreness is computed only using out degrees. Must be false is the graph has loops or parallel edges. |
| 13 | +- `γ=1.0`: where `γ > 0` is a resolution parameter. Higher resolutions lead to more |
| 14 | + communities, while lower resolutions lead to fewer communities. Where `γ=1.0` it |
| 15 | + leads to the traditional definition of the modularity. |
| 16 | +- `max_moves=1000`: maximum number of rounds moving vertices before merging for each Louvain. |
| 17 | +- `max_merges=1000`: maximum number of merges in the final Louvain. |
| 18 | +- `move_tol=1e-9`: necessary increase of modularity to move a vertex in each Louvain. |
| 19 | +- `merge_tol=1e-9`: necessary increase of modularity in the move stage to merge in the final Louvain. |
| 20 | +- `rng=nothing`: rng to use for reproducibility. May only pass one of rng or seed. |
| 21 | +- `seed=nothing`: seed to use for reproducibility. May only pass one of rng or seed. |
| 22 | +
|
| 23 | +### References |
| 24 | +- [Valérie Poulin and François Théberge. Ensemble Clustering for Graphs: Comparisons and Applications. Applied Network Science, 4:4 (2019)][https://doi.org/10.1007/s41109-019-0162-z] |
| 25 | +
|
| 26 | +
|
| 27 | +# Examples |
| 28 | +```jldoctest |
| 29 | +julia> using Graphs |
| 30 | +
|
| 31 | +julia> barbell = blockdiag(complete_graph(3), complete_graph(3)); |
| 32 | +
|
| 33 | +julia> add_edge!(barbell, 1, 4); |
| 34 | +
|
| 35 | +julia> ecg(barbell) |
| 36 | +6-element Vector{Int64}: |
| 37 | + 1 |
| 38 | + 1 |
| 39 | + 1 |
| 40 | + 2 |
| 41 | + 2 |
| 42 | + 2 |
| 43 | +
|
| 44 | +julia> ecg(barbell, γ=0.01) |
| 45 | +6-element Vector{Int64}: |
| 46 | + 1 |
| 47 | + 1 |
| 48 | + 1 |
| 49 | + 1 |
| 50 | + 1 |
| 51 | + 1 |
| 52 | +``` |
| 53 | +""" |
| 54 | +function ecg( |
| 55 | + g::AbstractGraph{T}; |
| 56 | + γ=1.0, |
| 57 | + ensemble_size::Integer=16, |
| 58 | + min_edge_weight::Real=0.05, |
| 59 | + min_weight_outside_2core::Bool=true, |
| 60 | + distmx::AbstractArray{<:Number}=weights(g), |
| 61 | + max_moves::Integer=1000, |
| 62 | + max_merges::Integer=1000, |
| 63 | + move_tol::Real=1e-9, |
| 64 | + merge_tol::Real=1e-9, |
| 65 | + rng::Union{Nothing,AbstractRNG}=nothing, |
| 66 | + seed::Union{Nothing,Integer}=nothing, |
| 67 | +) where {T} |
| 68 | + min_weight_outside_2core && |
| 69 | + has_self_loops(g) && |
| 70 | + throw( |
| 71 | + ArgumentError("min_weight_outside_2core must be false if the graph has loops.") |
| 72 | + ) |
| 73 | + rng = rng_from_rng_or_seed(rng, seed) |
| 74 | + if nv(g) == 0 |
| 75 | + return T[] |
| 76 | + end |
| 77 | + ensemble_weights = ecg_weights( |
| 78 | + g; |
| 79 | + γ=γ, |
| 80 | + ensemble_size=ensemble_size, |
| 81 | + distmx=distmx, |
| 82 | + max_moves=max_moves, |
| 83 | + move_tol=move_tol, |
| 84 | + rng=rng, |
| 85 | + ) |
| 86 | + if min_weight_outside_2core |
| 87 | + corenum = core_number(g) |
| 88 | + indices = findall( |
| 89 | + i -> (corenum[i[1]] < 2) || (corenum[i[2]] < 2), |
| 90 | + CartesianIndices(ensemble_weights), |
| 91 | + ) |
| 92 | + ensemble_weights[indices] .= 0.0 |
| 93 | + end |
| 94 | + weights = |
| 95 | + (1-min_edge_weight)*ensemble_weights + |
| 96 | + min_edge_weight * adjacency_matrix(g, Float64) |
| 97 | + return louvain( |
| 98 | + g; |
| 99 | + γ=γ, |
| 100 | + distmx=weights, |
| 101 | + max_moves=max_moves, |
| 102 | + max_merges=max_merges, |
| 103 | + move_tol=move_tol, |
| 104 | + merge_tol=merge_tol, |
| 105 | + rng=rng, |
| 106 | + ) |
| 107 | +end |
| 108 | + |
| 109 | +""" |
| 110 | + ecg_weights(g; γ=1.0, ensemble_size=16, distmx=weights(g), max_moves=1000, move_tol=1e-9, rng=nothing, seed=nothing) |
| 111 | +
|
| 112 | +Compute edge weights via an ensemble of no merge Louvains. The weight of each edge is |
| 113 | +the proportion of time the endpoints are in the same community. |
| 114 | +""" |
| 115 | +function ecg_weights( |
| 116 | + g::AbstractGraph{T}; |
| 117 | + γ=1.0, |
| 118 | + ensemble_size::Integer=16, |
| 119 | + distmx::AbstractArray{<:Number}=weights(g), |
| 120 | + max_moves::Integer=1000, |
| 121 | + move_tol::Real=1e-9, |
| 122 | + rng::Union{Nothing,AbstractRNG}=nothing, |
| 123 | + seed::Union{Nothing,Integer}=nothing, |
| 124 | +) where {T} |
| 125 | + rng = rng_from_rng_or_seed(rng, seed) |
| 126 | + # Create sparse adjacency matrix full of explicit zeros |
| 127 | + ensemble_weights = adjacency_matrix(g, Float64) |
| 128 | + ensemble_weights.nzval .= 0 |
| 129 | + |
| 130 | + for _ in 1:ensemble_size |
| 131 | + ensemble_communities = louvain( |
| 132 | + g; |
| 133 | + γ=γ, |
| 134 | + distmx=distmx, |
| 135 | + max_moves=max_moves, |
| 136 | + max_merges=0, |
| 137 | + move_tol=move_tol, |
| 138 | + rng=rng, |
| 139 | + ) |
| 140 | + for e in edges(g) |
| 141 | + if ensemble_communities[src(e)] == ensemble_communities[dst(e)] |
| 142 | + ensemble_weights[src(e), dst(e)] += 1 / ensemble_size |
| 143 | + if !is_directed(g) |
| 144 | + ensemble_weights[dst(e), src(e)] += 1 / ensemble_size |
| 145 | + end |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + return ensemble_weights |
| 151 | +end |
0 commit comments