Multilayer Graph Class

We have created a Python class to store the graph information used by the multilayer belief propagation.the same class can be used for both the single layer and the multilayer case. In addition we have created a class for generating realization of the Dynamic Stochastic Block Model.

Multilayer Graph

class modbp.MultilayerGraph(intralayer_edges, layer_vec, interlayer_edges=None, comm_vec=None, directed=False)[source]

Wrapper class for storing a ‘multilayer graph’ that can be used to call the modularity belief propagation A graph here is represented by a collection of igraphs (each one representing a “layer”) as well as a set of edges between the layers. In this formulation, each node can only be present in a single layer

__init__(intralayer_edges, layer_vec, interlayer_edges=None, comm_vec=None, directed=False)[source]
  • intralayer_edges – list of intralayer edges between the nodes. If intralayer_edges.shape[1] > 2 intralayer_edges[:,2] is assumed to represent the weights of the edges. Default weight is 1.
  • layer_vec – vector denoting layer membership for each edge. Size of network is taken to be len(layer_vec)
  • interlayer_edges – list of edges across layers. If interlayer_edges.shape[1] > 2 interlayer_edges[:,2] is assumed to represent the weights of the edges. Default weight is 1.
  • comm_vec – Underlying known communitiies of the network. Default is None
  • directed – Are intralayer and interlayer edges directed. #TODO: allow one or the other to be directed.

list of weak references to the object (if defined)


Calculate AMI of each layer with corresponding community in labels. Return average AMI weighted by number of nodes in each layer. :param labels: commmunity to assess agreement with. Len(labels) must equal self.N :return:


Calculate adjusted mutual information of labels with underlying community of network. :param labels: commmunity to assess agreement with. Len(labels) must equal self.N :return:

get_accuracy_with_communities(labels, permute=True)[source]

Calculate accuracy between supplied labels and the known communities of the networks.

  • labels – commmunity to assess agreement with. Len(labels) must equal self.N
  • permute – Should maximum accurracy across label permuations be identified?


m for each layer

plot_communities(comvec=None, layers=None, ax=None, cmap=None)[source]

Plot communities as an nlayers by nodes/layer heatmap. Note this only works for the multiplex case where the number of nodes is fixed throughout each layer.

  • comvec – community label for each nodes. If none, used stored ground truth for the network.
  • layers – Subset of the layers to plot. If None, plots all layers.
  • ax – matplotlib.Axes to draw on
  • cmap – color map to label communities with. Defaults to cube_helix.


Resort all objects in the MultilayerGraph object by their community label :return:


Create sparse matrix representations of the multilayer network.

Returns:(A_sparse,C_sparse) = interlayer adjacency , interlayer adjacency

Random Graph Classes

class modbp.MultilayerSBM(n, comm_prob_mat, layers=2, transition_prob=0.1, block_sizes0=None, use_gcc=False)[source]

Subclass of MultilayerGraph to create the dynamic stochastic block model from Ghasemian et al. 2016.

__init__(n, comm_prob_mat, layers=2, transition_prob=0.1, block_sizes0=None, use_gcc=False)[source]
  • n – number of nodes in each layer
  • comm_prob_mat – probability of block connections in SBM(this is fixed across all layers)
  • layers – number of layers
  • transition_prob – probability of each node changing communities from one layer to next. if transistion_prob=0 then community structure is constant across all layers.
  • block_sizes0 – Initial size of the blocks. Default is to use even block sizes starting out. block sizes at subsequent layers are determined by number of nodes that randomly transition between communities
  • use_gcc – use only giant connected component of starting SBM (option for single layer only)

returns a single vector with block id for each node across all of the layers :return:


Singer interlayer adjencency matrix created by connecting each node to it’s equivalent node in the next layer. This is a temporal multiplex topology. :return: np.array of interlayer adjacency representation


Single list of edges giving the multilayer connections. For this model nodes are multiplex an connected to their neighboring slice identities.


Single adjacency matrix representing all layers. :return: np.array of intralayer adjacency representation


Single list of edges treating the group of single layer SBM’s as a surpra-adjacency format

Returns:np.array denoting which layer each node is in