edit

Measuring the modularity

In this example, we will use label_propagation to optimize the modularity of a network. The data are from the mcmullen dataset.

using EcologicalNetwork

# Get the data in an object
N = mcmullen();
EcologicalNetwork.BipartiteNetwork(Bool[false false … false false; false false … false false; … ; true false … false false; true true … true true])

The next step is to generate starting communities for every species. We will assign species to random initial modules:

L = rand(1:richness(N), richness(N));
159-element Array{Int64,1}:
 154
   5
 118
  99
 125
  36
  83
 137
  38
 137
   ⋮
  30
  69
  40
  17
  48
  78
  31
 135
  42

We can now start a repeated number of attempts to find the best partition, here using brim. If julia is started in parallel, this will use all assigned CPUs.

M = modularity(N, L, brim, replicates=100);

# Finally, we can get the best partition
b_part = best_partition(M);

# And print its modularity and number of modules
println("Q: ",
        round(b_part[1].Q, 2),
        " |c|: ",
        length(unique(b_part[1].L))
        )
Q: 0.53 |c|: 25