vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
575 stars 77 forks source link

optimise_partition_multiplex: how to get the layer:partition mapping #87

Closed vivekbhr closed 2 years ago

vivekbhr commented 2 years ago

Hi @vtraag . I ran optimise_partition_multiplex on a 3 layer graph and get 30 communities. After this, I see that some of the nodes are in multiple communities, and therefore some nodes are also present multiple times in the output partition object. Now I assume that this is due to these nodes having different memberships in different layers, but how do I get the layer:membership mapping for these nodes?

My partition type is ModularityVertexPartition.

Thanks in advance

vtraag commented 2 years ago

This may indeed benefit from some additional clarity in the documentation I see.

If you construct layers using slices_to_layers the resulting graphs will have at least two vertex attributes: the indicated vertex_id_attr (='id' by default) and the indicated slice_attr (='slice' by default).

For example, if we have the following three graphs:

G1 = ig.Graph.Formula('A-B-D-C-A, D-E-F-G-D')
G2 = G1.copy(); G2.add_edge('C', 'F')
G3 = G1.copy(); G3.delete_vertices('A')

and then create slices as follows

G_coupling = ig.Graph.Formula('1 -- 2 -- 3')

G_coupling.vs['time'] = [G1, G2, G3]
G_coupling.es['weight'] = 1

G_layers, G_interslice, G = la.slices_to_layers(G_coupling,
                                                slice_attr='time', vertex_id_attr='name',
                                                edge_type_attr='type', weight_attr='weight' )

You will see that all graphs in G_layers, the G_interslice and G have the stated vertex attributes. For example: G.vs[0] yields

igraph.Vertex(<igraph.Graph object at 0x7f9795b5bb50>, 0, {'name': 'A', 'time': 0})

Here, the time vertex attribute refers to the index of the node that represents G1 in the coupling graph.

For any partition, the membership corresponds to the indicated vertices, and hence you can recover the original slice and names for all nodes in this way. Does this clarify it sufficiently?

vivekbhr commented 2 years ago

Got it, so I just have to take out the name and time attribute from any of the partitions after I run optimise_partition_multiplex in above example. Just checked it and indeed the attributes are there in my case. :+1:

vtraag commented 2 years ago

Great, good to hear this clarified it. I'll leave the issue open to remind me to improve the documentation on this point.