Search code examples
pythonigraph

igraph plot for multiple layers partition does not return expected output


Assume there are 3 time slices (i.e., G_1, G_2, G_3) that converted to layers via time_slices_to_layers. These layers and inter slices were then subject to find_partition to get their communities. However, plotting these partition does not result the expected output.

Here, the node interaction for each slice is as per the diagram below.

enter image description here

However, plotting the partition result give a incorrect intraslice link connection.

enter image description here

While the interslice coupling should be random, but I expect the intralsice link should be maintain as below

enter image description here

May I know how to fix this issue.

I suspect this is due to wrongly assign the vertex_label

ig.plot(partition,vertex_label = ['A','B','C','D','E','F','G'])

or it can be related to other configuration ?

The complete code to reproduce the above graph

import leidenalg as la
import igraph as ig
import numpy as np


A1 = np.array ( [[0., 0., 0., 0., 0,0,0],[5., 0., 0., 0., 0,0,0],[1., 0., 0., 0., 0,0,0],
                 [0., 1., 2., 0., 0,0,0],[0., 0., 0., 1., 0,0,0], [0., 0., 0., 1., 0,0,0],
                 [0., 0., 0., 0., 1,1,0]] )


A2 = np.array ( [[0., 0., 0., 0., 0,0,0],[5., 0., 0., 0., 0,0,0], [1., 0., 0., 0., 0,0,0],
                 [0., 1., 2., 0., 0,0,0], [0., 0., 0., 1., 0,0,0],[0., 0., 1., 1., 0,0,0],
                 [0., 0., 0., 0., 1,1,0]] )

A3 = np.array ( [[0., 0., 0., 0., 0,0,0], [0., 0., 0., 0., 0,0,0],[0., 0., 0., 0., 0,0,0],
                 [0., 1., 2., 0., 0,0,0],[0., 0., 0., 1., 0,0,0],[0., 0., 0., 1., 0,0,0],
                 [0., 0., 0., 0., 1,1,0]] )


G_1 = ig.Graph.Weighted_Adjacency ( A1.tolist () )
G_2 = ig.Graph.Weighted_Adjacency ( A2.tolist () )
G_3 = ig.Graph.Weighted_Adjacency ( A3.tolist () )
G_1.vs ['id'] = ['A','B','C','D','E','F','G']
G_2.vs ['id'] = ['A','B','C','D','E','F','G']
G_3.vs ['id'] = ['A','B','C','D','E','F','G']

G_layers, G_interslice, G = la.time_slices_to_layers ( [G_1, G_2, G_3], interslice_weight=1,
                                                       slice_attr='slice', vertex_id_attr='id',
                                                       edge_type_attr='type', weight_attr='weight' )

partition = la.find_partition(G, la.CPMVertexPartition,resolution_parameter = 0.05);
ig.plot(partition,vertex_label = ['A','B','C','D','E','F','G'])

Cross-posted on igraph forum


Solution

  • Credit should be given to Vincent Traag for the detail explaination.

    Based on the creator feedback, the above issue can be resolve as below

    # Convert from slices to layers
    G_layers, G_interslice, G = la.time_slices_to_layers([G_1, G_2, G_3], interslice_weight=1e-1,
                                                         slice_attr='slice', vertex_id_attr='id')
    # Create partitions
    gamma = 0.3
    partitions = [la.CPMVertexPartition(H, node_sizes='node_size',weights='weight',
                                        resolution_parameter=gamma) for H in G_layers]
    interslice_partition = la.CPMVertexPartition(G_interslice, resolution_parameter=0,
                                                 node_sizes='node_size', weights='weight')
    
    
    # Detect communities
    optimiser = la.Optimiser()
    optimiser.set_rng_seed(11)
    diff = optimiser.optimise_partition_multiplex(partitions + [interslice_partition])
    
    
    # Plot network
    partition_all = ig.VertexClustering(G, partitions[0].membership)
    ig.plot(partition_all,
            vertex_label = [f'{v["id"]}-{v["slice"]}' for v in G.vs])
    

    Which produced

    enter image description here

    In depth conversation can be found in the igraph-forum