def infomap_find_communities(input_file, n_trials): """ Partition network with the Infomap algorithm. Annotates nodes with 'community' id and return number of communities found. :param input_file: Input file with data :param n_trials: Number of trials options for infomap :rtype: Total number of communities, python dictionary of detected communities """ options = '--two-level -z' + ' -N ' + n_trials print('Number of trials: {}'.format(n_trials), log_type='info') # Create Infomap wrapper infomap_wrapper = infomap.Infomap(options) # print("Building Infomap network from a NetworkX graph.....", log_type='info') # for e in graph.edges(): # infomap_wrapper.addLink(*e) print('Building Infomap network from the input file.....', log_type='info') infomap_wrapper.readInputData(input_file) tree = run_algorithm(infomap_wrapper) # Find communities communities = {} for node in tree.leafIter(): communities[node.originalLeafIndex] = node.moduleIndex() # nx.set_node_attributes(graph, name='community', values=communities) # return number of modules found return tree.numTopModules(), communities
def findCommunities(G): """ Partition network with the Infomap algorithm. Annotates nodes with 'community' id and return number of communities found. """ infomapWrapper = infomap.Infomap("--two-level") print("Building Infomap network from a NetworkX graph...") for e in G.edges(): infomapWrapper.addLink(*e) print("Find communities with Infomap...") infomapWrapper.run() tree = infomapWrapper.tree print("Found %d top modules with codelength: %f" % (tree.numTopModules(), tree.codelength())) communities = {} for node in tree.leafIter(): communities[node.originalLeafIndex] = node.moduleIndex() nx.set_node_attributes(G, name='community', values=communities) return tree.numTopModules()
def runInfomapAsLibrary(input, infomapArgs): if (noLibrary): sys.exit("Infomap library not available") infomapWrapper = infomap.Infomap("{} {}".format(input, infomapArgs), True) infomapWrapper.run() return infomapWrapper.codelength()
id2word = word2vec.wv.index2word[:num_words] word2id = {j: i for i, j in enumerate(id2word)} links = {} # 每个词找与它相似度不小于0.6的词(不超过50个),来作为图上的边 for i in tqdm(range(num_words)): sims = np.dot(word_vecs, word_vecs[i]) idxs = sims.argsort()[::-1][1:] for j in idxs[:50]: if sims[j] >= min_sim: links[(i, j)] = float(sims[j]) else: break infomapWrapper = infomap.Infomap("--two-level --directed") # 如果重叠社区发现,则只需要: # infomapWrapper = infomap.Infomap("--two-level --directed --overlapping") for (i, j), sim in tqdm(links.items()): _ = infomapWrapper.addLink(i, j, sim) infomapWrapper.run() tree = infomapWrapper.tree word2class = {} class2word = {} for node in tree.leafIter(): if id2word[node.physIndex] not in word2class: word2class[id2word[node.physIndex]] = [] word2class[id2word[node.physIndex]].append(node.moduleIndex())
from infomap import infomap name = "ninetriangles" filename = "../../{}.net".format(name) infomapWrapper = infomap.Infomap("-N5 --silent") infomapWrapper.readInputData(filename) infomapWrapper.run() tree = infomapWrapper.tree print("Found %d top modules with codelength: %f" % (tree.numTopModules(), tree.codelength())) print("Writing top level modules to %s_level1.clu..." % name) tree.writeClu("%s_level1.clu" % name, 1) print("Writing second level modules to %s_level2.clu..." % name) tree.writeClu("%s_level2.clu" % name, 2) print("Writing tree to %s.tree..." % name) tree.writeHumanReadableTree("%s.tree" % name) print("\nModules at depth 1:\n#node module") for node in tree.leafIter(1): print("%d %d" % (node.physIndex, node.moduleIndex())) print("\nModules at depth 2:\n#node module") for node in tree.leafIter(2): print("%d %d" % (node.physIndex, node.moduleIndex()))
# nx.layou #first compute the best partition # louvain partition = community.best_partition(G, resolution=0.2) # dendogram = community.generate_dendrogram(G, resolution=0.2) # partition = community.partition_at_level(dendogram, 1) #drawing size = float(len(set(partition.values()))) pos = nx.kamada_kawai_layout(G) # infomap infomapWrapper = infomap.Infomap("--two-level") print("Building Infomap network from a NetworkX graph...") for e in G.edges(): infomapWrapper.addLink(*e) print("Find communities with Infomap...") infomapWrapper.run() tree = infomapWrapper.tree print("Found %d top modules with codelength: %f" % (tree.numTopModules(), tree.codelength())) partition = {} for node in tree.leafIter():