Пример #1
0
class MyInfomap:
    def __init__(self):
        self.handler = Infomap("--two-level")

    def add_network_edge(self, first_id, second_id, weight=1.00):
        self.handler.addLink(first_id, second_id, weight)

    def detect_communities(self):
        self.handler.run()
        communities = {}

        for node in self.handler.iterTree():
            if node.isLeaf():
                if node.moduleIndex() in communities:
                    communities[node.moduleIndex()].append(node.physicalId)
                else:
                    communities[node.moduleIndex()] = [node.physicalId]

        return communities
Пример #2
0
 def _apply_infomap(self):
     """Partition network with infomap algorithm
         Annotates node with community_id and returns number of communities found"""
     infomapWrapper = Infomap("--two-level --directed")
     print("Building Infomap network from a NetworkX graph...")
     for e in self.graph.edges():
         infomapWrapper.addLink(*e)
     print("Find communities with Infomap...")
     infomapWrapper.run()
     print("Found %d top modules with codelength: %f" %
           (infomapWrapper.numTopModules(), infomapWrapper.codelength()))
     communities = {}
     for node in infomapWrapper.iterTree():
         if node.isLeaf():
             communities[node.physicalId] = node.moduleIndex()
     nx.set_node_attributes(self.graph,
                            name='community',
                            values=communities)
     self.graph = nx.relabel.relabel_nodes(self.graph,
                                           self.catalog,
                                           copy=True)
     self.num_modules = infomapWrapper.numTopModules()
     self.community_labels = set(
         nx.get_node_attributes(self.graph, "community").values())