def infomap(self, inter_edge, threshold, update_method = None, **kwargs): ''' Infomap helper function. ''' im = Infomap("--two-level --directed --silent") ######### Make Network ## add intra edges thresholded_adjacency = [] for l in range(self.length): thresholded_adjacency.append(self.threshold(self.list_adjacency[l], thresh = threshold)) for n1,e in enumerate(thresholded_adjacency[l]):## list of length 2 corresponding to the adjacency matrices in each layer for n2,w in enumerate(e): s = MultilayerNode(layer_id = l, node_id = n1) t = MultilayerNode(layer_id = l, node_id = n2) im.add_multilayer_link(s, t, w) im.add_multilayer_link(t, s, w) ## add inter edges if update_method == 'local' or update_method == 'global': updated_interlayer = self.update_interlayer(kwargs['spikes'], 0, inter_edge, 0.1, update_method) for l in range(self.length-1): for k in range(self.size):# number of nodes which is 60 in the multilayer network s = MultilayerNode(layer_id = l, node_id = k) t = MultilayerNode(layer_id = l+1, node_id = k) im.add_multilayer_link(s, t, updated_interlayer[l][k]) im.add_multilayer_link(t, s, updated_interlayer[l][k]) elif update_method == 'neighborhood': updated_interlayer_indices, updated_interlayer_weights = self.get_normalized_outlinks(thresholded_adjacency, inter_edge) for l in range(self.length-1): for k in range(self.size): w, nbr = self.neighborhood_flow(l, k, updated_interlayer_indices, updated_interlayer_weights, threshold) for n in nbr: s = MultilayerNode(layer_id = l, node_id = k) t = MultilayerNode(layer_id = l+1, node_id = n) im.add_multilayer_link(s, t, w) im.add_multilayer_link(t, s, w) elif update_method == None: for l in range(self.length-1): for k in range(self.size):# number of nodes which is 60 in the multilayer network s = MultilayerNode(layer_id = l, node_id = k) t = MultilayerNode(layer_id = l+1, node_id = k) im.add_multilayer_link(s, t, inter_edge) im.add_multilayer_link(t, s, inter_edge) im.run() return(im)
def print_result(im): print( f"Found {im.num_top_modules} modules with codelength {im.codelength:.8f} bits" ) print("#layer_id node_id module_id:") for node in im.nodes: print(f"{node.layer_id} {node.node_id} {node.module_id}") im = Infomap(silent=True) print("\nAdding multilayer network...") # from (layer1, node1) to (layer2, node2) with optional weight im.add_multilayer_link((2, 1), (1, 2), 1.0) im.add_multilayer_link((1, 2), (2, 1), 1.0) im.add_multilayer_link((3, 2), (2, 3), 1.0) im.run() print_result(im) # Add only intra-layer links and let Infomap provide # inter-layer links by relaxing the random walker's # constraint to its current layer im = Infomap(silent=True) print("\nAdding intra-layer network...") # Add layer_id, source_node_id, target_node_id and optional weight im.add_multilayer_intra_link(1, 1, 2)