def test_overlapping_K5(): G = nx.Graph() G.add_edges_from(combinations(range(5), 2)) # Add a five clique G.add_edges_from(combinations(range(2, 7), 2)) # Add another five clique c = list(k_clique_communities(G, 4)) assert_equal(c, [frozenset(range(7))]) c = set(k_clique_communities(G, 5)) assert_equal(c, {frozenset(range(5)), frozenset(range(2, 7))})
def kcliques(agraph): i = 3 x = list(k_clique_communities(agraph, i)) comms = dict() while x != list(): #print(x) j = 1 for el in x: comms[(i, j)] = el j += 1 i += 1 x = list(k_clique_communities(agraph, i)) return comms
def apply_kclique(g, subsize=1000): print 'COMPUTING K-CLIQUE SCORE' g = g.to_undirected() partitions = dict() for k in [3, 4, 5]: kclique = list(community.k_clique_communities(g, k)) kclique = [tuple(x) for x in kclique] if len(kclique) == 0: print 'NO COMMUNTIES FOR K = ' + str(k) else: max_len = max([len(c) for c in kclique]) min_len = min([len(c) for c in kclique]) max_community = [c for c in kclique if len(c) == max_len][0] print 'GREATEST COMMUNITY COMPOSED BY ' + str(max_len) + \ ' NODES FOR K = ' + str(k) extract_info({'community': max_community, 'fname': './results/k_clique/' + str(k), 'ncommunities': len(kclique), 'maxcomlen': max_len, 'mincomlen': min_len}) evaluate_partition({'alg': 'k-clique', 'network': g, 'k': k, 'partition': kclique}) partitions[k] = kclique return partitions
def community_detection_k_clique(g, node_set, k=3): """Apply k-clique community detection. Return communities.""" from networkx.algorithms import community as nxcommunity if len(node_set) > 1: return [set(i) for i in nxcommunity.k_clique_communities(g.subgraph(node_set), k)] return []
def clique(self,k): '''寻找网络中的社团,最好能着色,有向图Label Propagation,无向图clique''' # 有向图转为无向图 G=self._G.to_undirected() c_G=list(k_clique_communities(G,k)) # community可视化 pos = nx.spring_layout(G) # 这种布局较慢,呈放射状 # pos=nx.random_layout(G) # 随机布局,基本是正方形 # pos=nx.shell_layout(G) # 同心圆 nx.draw_networkx_nodes(G, pos,node_size=10) count = 0 color = ['m', 'g', 'c', 'b', 'y', 'k', 'w']*3 for i in range(100): hex_color='#{:06x}'.format(random.randint(0, 256**3)) color.append(hex_color) # TODO 颜色不够用了 for com in c_G: count = count + 1 list_nodes = list(com) nx.draw_networkx_nodes(G, pos, list_nodes, node_size=50,node_color=color[count - 1]) print("Community", count, "is:", list_nodes) nx.draw_networkx_edges(G, pos,style='dotted',edge_color='green') # nx.draw_networkx_labels(G, pos) plt.axis("off") plt.show() print("-"*20)
def search_communities(graph, graph_name): community_gen = community.k_clique_communities(graph, 2) print(list(community_gen)) with open('communities_{}'.format(graph_name[13:]), 'wb') as file: pickle.dump(list(community_gen), file, protocol=pickle.HIGHEST_PROTOCOL)
def exectueCliqueAlgorithm(directory): os.chdir(directory) for file in glob.glob("*.net"): file_name = file.split(".")[0] + ".clu" multigraph = nx.read_pajek(file) modelGraph = nx.Graph(multigraph) file_name = file.split(".")[0] + ".clu" file_directory = os.path.join("../../results-kclique/", file_name) f = open(file_directory, "w+") communities = community.k_clique_communities(modelGraph, 3) lines = [None] * (len(modelGraph) + 1) group_number = 1 for group in communities: group_as_string = str(group_number) for node_value in group: id = modelGraph.nodes.get(node_value)['id'] index = int(id) lines[index] = group_as_string group_number += 1 lines[0] = "*Vertices " + str(len(modelGraph)) for index, x in enumerate(lines): if x is None: lines[index] = str(group_number) group_number += 1 f.writelines('\n'.join(lines)) f.write("\n") f.close() os.chdir("../../radatools/Communities_Tools/") st = os.stat('./Compare_Partitions.exe') os.chmod("./Compare_Partitions.exe", st.st_mode | stat.S_IEXEC) if ("model" in directory) and ("rb125" in file_name): index = 1 while (index <= 3): os.system("./Compare_Partitions.exe ../../results-kclique/" + file_name + " ../" + directory + "rb125-" + str(index) + ".clu" + " ../../results-kclique/" + file_name + "-" + str(index) + ".exit " + " V") index += 1 else: os.system("./Compare_Partitions.exe ../../results-kclique/" + file_name + " ../" + directory + file_name + " ../../results-kclique/" + file_name + ".exit " + " V") st = os.stat('./Modularity_Calculation.exe') os.chmod("./Modularity_Calculation.exe", st.st_mode | stat.S_IEXEC) os.system("./Modularity_Calculation.exe ../" + directory + file + " ../../results-kclique/" + file_name + " 0 0 UN TC 2 >> " + " ../../results-kclique/" + file_name + ".modularity") os.chdir("../" + directory) os.chdir("../../source")
def K_clique(Graph, k): """Use k_clique_communities(Graph, k) to create communites""" community_generator = community.k_clique_communities(Graph, k) community_size = {} NewGraph = nx.Graph() count = 0 DensitySum = 0 """Use iterations to make graph of communities and calculate average density""" while (True): try: EdgeCount = 0 Component = next(community_generator) NewGraph.add_nodes_from(Component) if len(Component) not in community_size.keys(): community_size[len(Component)] = 1 else: community_size[len(Component)] += 1 for i in Component: for j in Component: if i in Graph.neighbors(j) and i not in NewGraph.neighbors(j): EdgeCount += 1 NewGraph.add_edge(i, j) if len(Component) > 1: DensitySum += EdgeCount / (len(Component) * (len(Component) - 1) / 2) count += 1 except StopIteration: break """Print result""" print("------------------------------------------------------------") print("The community detection by k-clique algorithm with k="+str(k)) nx.draw(NewGraph, node_size=[1]*NewGraph.number_of_nodes()) plt.savefig('NewGraph.png', bbox_inches='tight') plt.show() print("The number of communities: " + str(count)) print("The average density of communities: " + str(DensitySum / count)) print("------------------------------------------------------------") CreateScatterChart(community_size) labels = 'size:1~3', 'size:4', 'size:5~7', 'size:>7' sizes = [0, 0, 0, 0] for key, value in community_size.items(): if key <= 3: sizes[0] += value elif key == 4: sizes[1] += value elif key >= 5 and key <= 7: sizes[2] += value else: sizes[3] += value fig1, ax1 = plt.subplots() ax1.pie(sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=90) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. plt.title("The distribution of communities") plt.savefig('Pie.png', bbox_inches='tight') plt.show()
def compute_community(G): ng = nx.Graph() for start in G.iternodes(): others = G.neighbors(start) for other in others: ng.add_edge(start, other) c = k_clique_communities(ng, 7) return c
def otherAlgos(movieName): speakers = getTheSpeakersNames(movieName) gUW = buildGraphFromListUndirectedWeighted(speakers) #drawGraph(abGraph, 0) smallgUW = narrowGraphTo10MainCharacters(gUW, speakers) #drawGraph(smallABGraph, 0) print("modularity_communities") print(nxac.greedy_modularity_communities(smallgUW)) print(nxac.greedy_modularity_communities(gUW)) print("centrality") print(tuple(sorted(c) for c in next(nxac.centrality.girvan_newman(smallgUW)))) print(tuple(sorted(c) for c in next(nxac.centrality.girvan_newman(gUW)))) print("k_clique_communities") print(sorted(list(nxac.k_clique_communities(smallgUW,5)))) print(sorted(list(nxac.k_clique_communities(gUW, 5)))) print("hierarchy") print (networkx.algorithms.hierarchy.flow_hierarchy(smallgUW.to_directed())) print(networkx.algorithms.hierarchy.flow_hierarchy(gUW.to_directed()))
def k_clique(G, number_of_nodes, matlab_bs=False): print('\nK Clique Algorithm') k_clique = list(k_clique_communities(G, 4)) #print('K-Clique:\n', k_clique) print('Number of Communities with K-Clique:', len(k_clique)) k_clique_labels_predicted = get_labels_from_community(k_clique, number_of_nodes, matlab_bs) #print('K-Clique Truth Labels:\n', k_clique_labels_predicted) return k_clique_labels_predicted
def generate_communities(graph, filename, min_size=6): # generates a topology of communities from a given networkx graph print('Generating communities from networkx graph') data = [] for community in k_clique_communities(graph, min_size): data.extend([list(community)]) with open(filename, 'w') as output: for community in data: output.write(str(community) + '\n')
def get_clique_communities(graph, k=3): comm = list(k_clique_communities(graph, k)) node_cl_comm = np.empty(len(graph)) for i, c in enumerate(comm): for node in c: node_cl_comm[node - 1] = i return node_cl_comm
def k_clique_communities(self, k=2, **kwargs): """Devuelve un diccionario con el valor k_clique_communities para cada nodo""" if k < 2: k = 2 g = nx.Graph(self.g) c = list(comm.k_clique_communities(g, k, **kwargs)) communities = {} for i in range(len(c)): for j in c[i]: communities[j] = i return communities
def get_graph_cliques(graph, smallest_clique = 10): """Determine the cliques of a graph and return an array of subgraphs that correspond to those cliques """ c = list(community.k_clique_communities(graph, k=smallest_clique ) ) cliques = [clique for clique in c] subgraphs = [graph.subgraph(value) for value in cliques] return subgraphs
def makegroups(df): """ Replicates the functionality of the make group tool in Alteryx Expects a pandas dataframe ("df") with two columns. """ try: # Renames df columns df.columns = ['a', 'b'] # Converting the df to a list for network edges edgesList = df.values.tolist() # Converting the df to a dictionary for network nodes nodeDict = df.to_dict('list') # Creating an empty graph G = nx.Graph() # Adding nodes from both lists in the dictionary # Effectively a merge of the two lists G.add_nodes_from(nodeDict['a']) G.add_nodes_from(nodeDict['b']) # Adding the edges from the edges list G.add_edges_from(edgesList) # Generates groups # TODO check that size of smallest clique, here 2 is dynamic comm = list(community.k_clique_communities(G, 2)) # Splits the communities into two lists comm1 = list(comm[0]) comm2 = list(comm[1]) # Generates list of the group name # Replicates the group name to match community list length group1 = list(repeat(comm1[0], len(comm1))) group2 = list(repeat(comm2[0], len(comm2))) # combines each group name and community list into a dataframe dfGroup1 = pd.DataFrame(list(zip(group1, comm1)), columns=['Group', 'key']) dfGroup2 = pd.DataFrame(list(zip(group2, comm2)), columns=['Group', 'key']) # Combines both dataframes into the final dataframe df = dfGroup1.append(dfGroup2, ignore_index=True) print("Success: Groups created") return df except Exception as e: print("Error: Function - makegroups:", e)
def Clique_Percolation(G, most_valualble_edge=None): cliques_list = [] for k in range(3, G.number_of_nodes()): communities = list(nxalgo.k_clique_communities(G, k)) if len(communities) == 0: # stop when there are no more communities with k break cliques_list.append((k, communities)) max_mod = (0, 0) for clique in cliques_list: mod = 0 for comm in clique[1]: total_links_inside = 0 adj_calc = 0 if len(comm) == 1: # in case there is only one node inside a community its modularity is 0 mod += 0 else: for node in comm: num_clusters_belongs_to = 0 for comm_check in clique[1]: if node in comm_check: num_clusters_belongs_to += 1 in_size = 0 out_size = 0 node_deg = G.degree(node) neighbor_list_for_calc = list(nx.neighbors(G, node)) for neighbor in neighbor_list_for_calc: if neighbor in comm: in_size += 1 total_links_inside += 1 else: out_size += 1 adj_calc += (in_size - out_size) / (node_deg * num_clusters_belongs_to) mod += (1 / len(comm)) * adj_calc * (total_links_inside / 2) / special.binom(len(comm), 2) absent_nodes = 0 absent_nodes_names = [] for node in G.nodes(): if any(node in community for community in clique[1]) == False: absent_nodes += 1 absent_nodes_names.append([node]) sum_mod = mod / (len(clique[1]) + absent_nodes) if (sum_mod > max_mod[1]): max_mod = (clique[0], sum_mod) num_partitions = len(clique[1]) partition = list(map(list, clique[1])) result_dic = { 'num_partitions': num_partitions, 'modularity': max_mod[1], 'partition': partition } return result_dic
def consensus(cluG, k=5, f=1.0, ct=100): ''' create a more parsimonious results from the cluster graph :param cluG: the cluster graph :param k: delete clusters with lower degree :param f: take this fraction of clusters (ordered by degree in cluster graph) :param ct: nodes that do not participate in the majority of clusters in a component will be removed :return: ''' components = [c for c in nx.connected_components(cluG) if len(c) >= k] components = sorted(components, key=len, reverse=True) components_new = [] # use k-clique percolation to recalculate components for component in components: component = list(component) clusters = [cluG.nodes[v]['data'].binary for v in component] mat = np.stack(clusters) matsp = sp.sparse.coo_matrix(mat, ) matsp = matsp.tocsr() jacmat = jaccard_matrix(matsp, matsp) Gcli = nx.Graph() for i in range(len(jacmat[0])): na, nb = jacmat[0][i], jacmat[1][i] if na != nb: Gcli.add_edge(na, nb) clic_percolation = list(k_clique_communities( Gcli, k)) # this parameter better to stay for clic in clic_percolation: clic = list(clic) original_nodes = [component[c] for c in clic] components_new.append(set(original_nodes)) components = components_new.copy() components = sorted(components, key=len, reverse=True) ntaken = int(f * len(components)) components = components[:ntaken] # cluG_collapsed = collapse_cluster_graph(cluG, components, ct) len_components = [len(c) for c in components] cluG_collapsed_w_len = [(cluG_collapsed[i], len_components[i]) for i in range(len(cluG_collapsed))] cluG_collapsed_w_len = sorted(cluG_collapsed_w_len, key=lambda x: np.sum(x[0]), reverse=True) # sort by cluster size return cluG_collapsed_w_len
def k_clique(self): nodes_in_community = list(k_clique_communities(self.G, 3)) all_nodes = [] for community in nodes_in_community: community_nodes = [] for node in community: community_nodes.append(node) all_nodes.append(community_nodes) colors = [ 'red', 'yellow', 'green', 'pink', 'orange', 'olive', 'cyan', 'purple' ] nodes_color = [] def find_node_in_community(IP): color_flag = 0 for community in all_nodes: for IP in community: if node == IP: nodes_color.append(colors[color_flag]) return color_flag += 1 nodes_color.append("blue") for node in self.G.nodes: find_node_in_community(node) weight = [] # 得到G图边列表[(IP1,IP2,weight)...] edgelists = [(u, v, d['weight']) for (u, v, d) in self.G.edges(data=True)] # 将边权重按顺序加入weight列表.需要注意的是,在生成G图时给边的权重和之后生成png时所画出的边的粗细权重是不同的东西,之后会提到 for edgelist in edgelists: row_weight = edgelist[2] # 取原始数据权重 weight.append(math.sqrt(row_weight / 10) + 1) # 计算绘画粗细的权重 pos = nx.spring_layout(self.G, k=2) # layout决定点的位置,k决定散布距离 nx.draw_networkx_nodes(self.G, pos, node_size=100, node_color=nodes_color) # 画点,确定点位置,点大小,点颜色 nx.draw_networkx_edges(self.G, pos, edgelists, width=weight) # 画边,输入边列表,边粗细权重.两个列表必须顺序对应,否则会权重不匹配 nx.draw_networkx_labels(self.G, pos, font_size=5, font_family='sans-serif') # 打标签 plt.axis('off') # 关闭网格 timestr = time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".png" plt.savefig(timestr) # 存图 plt.clf() # 清空plt,存完图一定要清,不然会重叠.整个作图逻辑是:nx生成图结构,plt作图.两者都应该及时清空,否则会重叠 return timestr
def count_cliques(G,name): #cliques=list(nx.enumerate_all_cliques(G)) cliques = list(k_clique_communities(G, 2)) myedges=list(G.edges) #print([list(x) for x in cliques]) graphletFreq={} size={} count=1; for s in cliques: s=list(s) if len(s)>2: #print(s) local_edges=search_edges(s, myedges) G=nx.Graph() G.add_edges_from(local_edges) degree_seq = [d for v, d in G.degree()] a=sorted(degree_seq) degname='' for i in a: degname+=str(i)+'-' if degname in graphletFreq: graphletFreq[degname]+=1 else: graphletFreq[degname]=1 ''' fig, ax = plt.subplots( nrows=1, ncols=1 ) nx.draw(G, with_labels = True, cmap = plt.get_cmap('jet')) fig.savefig('Random/clique'+str(name)+'_'+str(count)+'.png') count=count+1 plt.close(fig) if len(s) not in size: size[len(s)]=1 else: size[len(s)]+=1 ''' f=open('communityInRandomNetwork/'+str(name)+'.txt','w') for key in graphletFreq: f.write(key +'\t'+ str(graphletFreq[key])+'\n') return size
def find_k_cliques_modules(id, idx, mat_weights, th): from networkx.algorithms.community import k_clique_communities matbin = mat_weights > th matbin = matbin * 1 matth = matbin[id] matth = matth[:, id] a = np.sort(idx) gnew = nx.Graph() gnew.add_nodes_from(a) # gnew is non-weighted graph, edges masked using th g = nx.from_numpy_array(matth) gnew.add_edges_from(g.edges) G0 = find_largest_connected_component() c = list(k_clique_communities(G0, 4)) return c
def clusters_kclique(evs, node_list, verbose=True): import networkx as nx from networkx.algorithms import community if len(evs) > 0: # clustering: create network if verbose: logging.info("Create network") evs_e = evs[["in_gene", "out_gene", "branch_support"]] evs_n = nx.convert_matrix.from_pandas_edgelist( evs_e, source="in_gene", target="out_gene", edge_attr="branch_support") evs_n.add_nodes_from(node_list) # clustering: asynchronous label propagation if verbose: logging.info("Find communities k-clique") clu_c = community.k_clique_communities(evs_n, k=2) clu_c = {frozenset(c) for c in clu_c} if verbose: logging.info("Find communities k-clique num clusters = %i" % len(clu_c)) clu_c_clu = [i for i, cluster in enumerate(clu_c) for node in cluster] clu_c_noi = [ node for i, cluster in enumerate(clu_c) for node in cluster ] else: if verbose: logging.info("There are no speciation events in this tree.") clu_c_noi = node_list clu_c_clu = [i for i in range(len(node_list))] # clustering: save output clu = pd.DataFrame({ "node": clu_c_noi, "cluster": clu_c_clu, }, columns=["node", "cluster"]) if verbose: logging.info("Find communities k-clique | num clustered genes = %i" % len(clu)) return clu
def get_clique_communities(graph, k=4): comm = list(k_clique_communities(graph, k)) try: mod = modularity(graph, comm) except: mod = 0 node_cl_comm = np.zeros(max(graph.nodes) + 1) for i, c in enumerate(comm): for node in c: node_cl_comm[node] = i + 1 return node_cl_comm[list(graph.nodes)], mod
def community_detection(graph, max_community_keywords_num, k): ''' use k-clique algorithm for community_detection on keywords graph built above :param graph: :param max_community_keywords_num:the max keywords num which community has :param k: k in k-clique algorithm :return: community list(community is represented by keywords set) ''' print('===community detection===') max_single_community_size = sys.maxsize pre_community_result = len(graph.nodes) temp_g = copy.deepcopy(graph) k_clique_result_community = [] epoch_num = 1 while (max_single_community_size > max_community_keywords_num): print('========================================') print('community detection epoch: %d' % epoch_num) print('k=%d' % k) k_clique_community = community.k_clique_communities(temp_g, k) k_clique_community = [ list(single_community) for single_community in list(k_clique_community) ] k_clique_community = sorted(k_clique_community, key=lambda c: -len(c)) # print(k_clique_community[0]) if len(k_clique_community) > 1: delete_node = k_clique_community[1:] max_single_community_size = len(k_clique_community[0]) for delete_list in delete_node: k_clique_result_community.append(delete_list) temp_g.remove_nodes_from(delete_list) if max_single_community_size == pre_community_result: k += 1 pre_community_result = max_single_community_size epoch_num += 1 if len(k_clique_community) == 0: break if len(k_clique_community) > 0: k_clique_result_community.append(k_clique_community[0]) with open('../temp/first_cluster/community_detection.txt', 'w', encoding='utf-8') as fin: for single in k_clique_result_community: fin.writelines(';'.join(single) + '\n') return k_clique_result_community
def computeKClique(tweet_graph): ''' This function takes the tweet_gragh as an input and computes number of communities in the graph using the k-clique algorithm. For our usage, k=2 Arguments: tweet_graph: the tweet graph built in buildGraph() Returns: list of nodes which form communities Example: >>> computeKClique(tweet_graph) ''' networkx_tweet_graph = nx.Graph(tweet_graph) graph_communities = list(k_clique_communities(networkx_tweet_graph, 2)) return graph_communities
def executeKClique(self, k): udgraph = self.graph.to_undirected() communities = k_clique_communities(udgraph, k) nodes = self.graph.node communitynum = -1 for n in nodes: nodes[n]['communities'] = [] coms = {} for community in communities: communitynum += 1 com = [] for n in community: nodes[n]['communities'].append(communitynum) com.append(n) coms[communitynum] = com return coms
def clear_graph(G): l1 = len(G) n1 = G.number_of_edges() G = nx.k_core(G, k=2) l2 = len(G) n2 = G.number_of_edges() cliques3 = k_clique_communities(G, 3) # cliques = nx.find_cliques(G) # cliques3 = [clq for clq in cliques if len(clq) >= 3] nodes = set(n for clq in cliques3 for n in clq) h = G.subgraph(nodes) # deg = nx.degree(h) # nodes = [n for n in nodes if deg[n] >= 3] # k = h.subgraph(nodes) k = nx.k_core(h, k=2) l3 = len(k) n3 = k.number_of_edges() print("1 step Number of nodes " + str(l1) + " edges " + str(n1)) print("2 step Number of nodes " + str(l2) + " edges " + str(n2)) print("3 step Number of nodes " + str(l3) + " edges " + str(n3)) return k
def draw_netwrok_graph(): graph = nx.Graph() for node in HOST_LIST: graph.add_node(node) # for item_key in SRC_DST_DIC.keys(): # for peer in SRC_DST_DIC[item_key]: # graph.add_edge(item_key, peer) for item_key in TUNNEL_LIST.keys(): for peer_turple in TUNNEL_LIST[item_key]: peer_name = peer_turple[0] peer_protocol = peer_turple[1] graph.add_edge(item_key, peer_name) klist = list(k_clique_communities(graph, 3)) # nx.draw_networkx_nodes(graph, pos) # nx.draw_networkx_labels(graph, pos) # nx.draw_networkx_edges(graph, pos) # nx.draw_networkx_edge_labels(graph, pos) pos = nx.spring_layout(graph) plt.clf() nx.draw(graph, pos=pos, with_labels=False) if klist.__len__() > 1: nx.draw(graph, pos=pos, nodelist=klist[0], node_color='b') nx.draw(graph, pos=pos, nodelist=klist[1], node_color='y') elif klist.__len__() > 0: nx.draw(graph, pos=pos, nodelist=klist[0], node_color='b') plt.show()
def find_communities(nnodes, edges, alg, params=None): def membership2cs(membership): cs = {} for i, m in enumerate(membership): cs.setdefault(m, []).append(i) return cs.values() def connected_subgraphs(G: nx.Graph): for comp in nx.connected_components(G): sub = nx.induced_subgraph(G, comp) sub = nx.convert_node_labels_to_integers(sub, label_attribute='old') yield sub def apply_subgraphs(algorithm, **params): cs = [] for sub in connected_subgraphs(G): if len(sub.nodes) <= 3: coms = [sub.nodes] # let it be a cluster else: coms = algorithm(sub, **params) if hasattr(coms, 'communities'): coms = coms.communities for com in coms: cs.append([sub.nodes[i]['old'] for i in set(com)]) return cs def karate_apply(algorithm, graph, **params): model = algorithm(**params) model.fit(graph) return membership2cs(model.get_memberships().values()) if alg == 'big_clam': c = -1 if params['c'] == 'auto' else int(params['c']) cs = BigClam('../../snap').run(edges, c=c, xc=int(params['xc'])) elif alg in ('gmm', 'kclique', 'lprop', 'lprop_async', 'fluid', 'girvan_newman', 'angel', 'congo', 'danmf', 'egonet_splitter', 'lfm', 'multicom', 'nmnf', 'nnsed', 'node_perception', 'slpa', 'GEMSEC', 'EdMot', 'demon'): G = nx.Graph() G.add_edges_from(edges) if alg == 'gmm': cs = community.greedy_modularity_communities(G) elif alg == 'kclique': params = {k: float(v) for k, v in params.items()} cs = community.k_clique_communities(G, **params) elif alg == 'lprop': cs = community.label_propagation_communities(G) elif alg == 'lprop_async': cs = community.asyn_lpa_communities(G, seed=0) elif alg == 'fluid': params = {k: int(v) for k, v in params.items()} params['seed'] = 0 cs = apply_subgraphs(community.asyn_fluidc, **params) elif alg == 'girvan_newman': comp = community.girvan_newman(G) for cs in itertools.islice(comp, int(params['k'])): pass elif alg == 'angel': params = {k: float(v) for k, v in params.items()} cs = cdlib.angel(G, **params).communities elif alg == 'congo': # too slow ncoms = int(params['number_communities']) cs = [] for sub in connected_subgraphs(G): if len(sub.nodes) <= max(3, ncoms): cs.append(sub.nodes) # let it be a cluster else: coms = cdlib.congo(sub, number_communities=ncoms, height=int(params['height'])) for com in coms.communities: cs.append([sub.nodes[i]['old'] for i in set(com)]) elif alg == 'danmf': # no overlapping cs = apply_subgraphs(cdlib.danmf) elif alg == 'egonet_splitter': params['resolution'] = float(params['resolution']) cs = apply_subgraphs(cdlib.egonet_splitter, **params) elif alg == 'lfm': coms = cdlib.lfm(G, float(params['alpha'])) cs = coms.communities elif alg == 'multicom': cs = cdlib.multicom(G, seed_node=0).communities elif alg == 'nmnf': params = {k: int(v) for k, v in params.items()} cs = apply_subgraphs(cdlib.nmnf, **params) elif alg == 'nnsed': cs = apply_subgraphs(cdlib.nnsed) elif alg == 'node_perception': # not usable params = {k: float(v) for k, v in params.items()} cs = cdlib.node_perception(G, **params).communities elif alg == 'slpa': params["t"] = int(params["t"]) params["r"] = float(params["r"]) cs = cdlib.slpa(G, **params).communities elif alg == 'demon': params = {k: float(v) for k, v in params.items()} cs = cdlib.demon(G, **params).communities elif alg == 'GEMSEC': # gamma = float(params.pop('gamma')) params = {k: int(v) for k, v in params.items()} # params['gamma'] = gamma params['seed'] = 0 _wrap = partial(karate_apply, karateclub.GEMSEC) cs = apply_subgraphs(_wrap, **params) elif alg == 'EdMot': params = {k: int(v) for k, v in params.items()} _wrap = partial(karate_apply, karateclub.EdMot) cs = apply_subgraphs(_wrap, **params) elif alg in ('infomap', 'community_leading_eigenvector', 'leig', 'multilevel', 'optmod', 'edge_betweenness', 'spinglass', 'walktrap', 'leiden', 'hlc'): G = igraph.Graph() G.add_vertices(nnodes) G.add_edges(edges) if alg == 'infomap': vcl = G.community_infomap(trials=int(params['trials'])) cs = membership2cs(vcl.membership) elif alg == 'leig': clusters = None if params['clusters'] == 'auto' else int( params['clusters']) vcl = G.community_leading_eigenvector(clusters=clusters) cs = membership2cs(vcl.membership) elif alg == 'multilevel': vcl = G.community_multilevel() cs = membership2cs(vcl.membership) elif alg == 'optmod': # too long membership, modularity = G.community_optimal_modularity() cs = membership2cs(vcl.membership) elif alg == 'edge_betweenness': clusters = None if params['clusters'] == 'auto' else int( params['clusters']) dendrogram = G.community_edge_betweenness(clusters, directed=False) try: clusters = dendrogram.as_clustering() except: return [] cs = membership2cs(clusters.membership) elif alg == 'spinglass': # only for connected graph vcl = G.community_spinglass(parupdate=True, update_rule=params['update_rule'], start_temp=float(params['start_temp']), stop_temp=float(params['stop_temp'])) cs = membership2cs(vcl.membership) elif alg == 'walktrap': dendrogram = G.community_walktrap(steps=int(params['steps'])) try: clusters = dendrogram.as_clustering() except: return [] cs = membership2cs(clusters.membership) elif alg == 'leiden': vcl = G.community_leiden( objective_function=params['objective_function'], resolution_parameter=float(params['resolution_parameter']), n_iterations=int(params['n_iterations'])) cs = membership2cs(vcl.membership) elif alg == 'hlc': algorithm = HLC(G, min_size=int(params['min_size'])) cs = algorithm.run(None) elif alg in ("sbm", "sbm_nested"): np.random.seed(42) gt.seed_rng(42) G = gt.Graph(directed=False) G.add_edge_list(edges) deg_corr = bool(params['deg_corr']) B_min = None if params['B_min'] == 'auto' else int(params['B_min']) B_max = None if params['B_max'] == 'auto' else int(params['B_max']) if alg == "sbm": state = gt.minimize_blockmodel_dl(G, deg_corr=deg_corr, B_min=B_min, B_max=B_max) membership = state.get_blocks() cs = membership2cs(membership) if alg == "sbm_nested": state = gt.minimize_nested_blockmodel_dl(G, deg_corr=deg_corr, B_min=B_min, B_max=B_max) levels = state.get_bs() level_max = int(params['level']) membership = {} for nid in range(nnodes): cid = nid level_i = len(levels) for level in levels: cid = level[cid] if level_i == level_max: membership.setdefault(cid, []).append(nid) break level_i -= 1 cs = membership.values() else: return None return list(cs)
def get_communities_k_clique(G, k): return list(community.k_clique_communities(G, k))