Multiplex/Temporal modularity score
Hello Vincent
I have been using leidenalg for years, and I would like to thank you for your work.
My question is if there is a helper function within leidenalg or igraph that calculates the multilayer modularity score for a given quality function and VertexPartition, similar to G.modularity() in igraph?
In particular, for a given list of upper triangular np.arrays of adjacency matrices (list_adjacency):
length = len(list_adjacency)
size = list_adjacency[0].shape[0]
Gs = []
for l in range(length):
conn_indices = np.where(list_adjacency[l])
weights = list_adjacency[l][conn_indices] # get the weights corresponding to these indices
edges = zip(*conn_indices) # a sequence of (i, j) tuples, each corresponding to an edge from i -> j
G = ig.Graph(n = size, edges=edges, directed=False)# initialize the graph from the edge sequence
G.vs['id'] = list(range(list_adjacency[l].shape[0]))
G.vs['node_size'] = 0
G.es['weight'] = weights #assign node names and weights to be attributes of the vertices and edgesrespectively
Gs.append(G)
I'm trying to compute the Multilayer Modularity score (or whatever quality function is being used) for the interslice_partition below:
layers, interslice_layer, G_full = time_slices_to_layers(Gs)
partitions = [la.RBConfigurationVertexPartition(H, weights = 'weight', resolution_parameter = 0.98) for H in layers]
interslice_partition = la.RBConfigurationVertexPartition(interslice_layer, weights = 'weight', resolution_parameter = 0)
optimiser = la.Optimiser()
diff = optimiser.optimise_partition_multiplex(partitions + [interslice_partition], n_iterations = 10)
Something like:
G_full.multiplex_modularity(interslice_partition._membership, weights='weight', resolution=0.98, directed=False, VertexPart = la.RBConfigurationVertexPartition)
Note that last argument VertexPart would indicate the quality function that is used, which is different from G.modularity().
Thank you
Apologies for the late reply.
This is not implemented indeed, only the diff output provides that "total" multilayer modularity score, in terms of the improvement. That is, diff provides the sum over all layers
$$q = \sum_k w_k q_k,$$
where $q_k$ is the quality of layer $k$ with some weight $w_k$ (defaults to 1), compared to the quality of the starting partition. In principle, you should be able to simply sum over the various layers yourself. That is, for all_partitions = partitions + [interslice_partition], you should be able to do
sum(p.quality() for p in all_partitions)
where p.quality refers to the quality for partition p. If necessary, you can of course compute the weighted sum.
This is easy to calculate, so I won't specifically add it to the package. However, I think the documentation is indeed lacking some clarity on that quality function, and that you can calculate that multilayer quality in this way. I'll leave it open for now, in order to remind me to clarify the documentation.