def infomap_communities(graph): node2i = bidict({n: i for i, n in enumerate(graph.nodes)}) if len(node2i) == 0: return {} infomapWrapper = Infomap() for (n1, n2) in graph.edges(): infomapWrapper.addLink(node2i[n1], node2i[n2]) infomapWrapper.run() to_return_temp = infomapWrapper.getModules() to_return = {} for n, c in to_return_temp.items(): to_return[node2i.inv[n]] = c return to_return
def run_infomap_alt(g): from infomap import Infomap n2num = {u: num for num, u in enumerate(g)} num2u = sorted(n2num, key=lambda x: n2num[x]) g_num = nx.Graph() for n, n1 in g.edges(): g_num.add_edge(n2num[n], n2num[n1]) im = Infomap("--undirected") for n, n1 in g_num.edges(): im.addLink(n, n1) im.run() part = {num2u[i]: m for i, m in im.getModules().items()} return part