Given a communities
extracted using VertexClustering as below
partition_all = ig.VertexClustering(G, partitions[0].membership)
I using the following line to convert it into numpy
membership_arr=np.array(partition_all.membership).reshape(3,-1).transpose()
label_arr=np.array([f'{v["name"]}-{v["slice"]}' for v in G.vs]).reshape(3,-1).transpose()
However, the above solution only work if all node are present across slices and will throw an error if some nodes are missing,
ValueError: cannot reshape array of size 20 into shape (3,newaxis)
Hence, I wonder whether there is build-in function to cater transformation to numpy array?
The full code to reproduce the above error
import leidenalg as la
import igraph as ig
import numpy as np
# Create first slice graph
G1 = ig.Graph.Formula('A-B-D-C-A, D-E-G-F-D')
# Make a copy, remove edge between C and F
G2 = G1.copy(); G2.add_edge('C', 'F')
# Make a copy, remove vertex A
G3 = G1.copy();G3.delete_vertices('A')
# Convert from slices to layers
G_layers, G_interslice, G = la.time_slices_to_layers([G1, G2, G3], interslice_weight=1e-1,
slice_attr='slice', vertex_id_attr='name',
edge_type_attr='type', weight_attr='weight' )
# 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])
partition_all = ig.VertexClustering(G, partitions[0].membership)
membership_arr=np.array(partition_all.membership).reshape(3,-1).transpose()
label_arr=np.array([f'{v["name"]}-{v["slice"]}' for v in G.vs]).reshape(3,-1).transpose()
# G3.delete_vertices('A')