def Community_Analysis(graph_path, save_path, graphname, draw=False, delimiter=' ', weighted=False): fig_path = graph_path + '.png' if weighted: g = nx.read_weighted_edgelist(os.path.join(GRAPH_DIR, graph_path), delimiter=delimiter) else: g = nx.read_edgelist(os.path.join(GRAPH_DIR, graph_path), delimiter=delimiter) partition = community_louvain.best_partition(g) with open(os.path.join(DATA_DIR, save_path), 'w') as f: for k, v in partition.items(): f.write(str(k) + ',' + str(v) + '\n') if draw: pos = community_layout(g, partition) plt.figure() nx.draw(g, pos, node_color=list(partition.values())) plt.title('Communties of {}'.format(graphname)) plt.savefig(os.path.join(GRAPH_DIR, fig_path)) plt.show() return partition
def community_detection(self): G = nx.read_gpickle(self.affiliation_network_gpickle) coms = community_louvain.best_partition(G.to_undirected(), weight='shared_doctors', randomize=False, resolution=0.05, random_state=42) cdlib_coms = louvain_to_cdlib_coms(G, coms) print(f"unique communities:{len(np.unique(list(coms.values())))}") PickleUtils.saver(cdlib_coms, '../data/network/affiliation_community.pkl')
def compute_clusters_community(graph, resolution, logger): """Compute the clusters in a graph using Louvain's community detection method Parameters : graph (networkx.Graph): graph of UDN patients computed using the pairwise similarity between patients resolution (float): resolution for the Louvain method logger (Logger): logger Returns: clusters (dict): dictionary with the cluster number as key and a list containing all the patients in the cluster as value """ partition = community_louvain.best_partition(graph, resolution=resolution) logger.info("Partition done") clusters = {} for node in partition.keys(): if not (partition[node] in clusters.keys()): clusters[partition[node]] = [node] else: clusters[partition[node]].append(node) count = 0 for cluster in clusters.keys(): logger.info("Length of cluster {} : {}".format(cluster, len(clusters[cluster]))) if len(clusters[cluster]) <= 1: count += 1 logger.info( "Number of clusters with less than 3 patients (outliers) : {}".format( count)) return clusters
def do_gof_anlysis(self, key_word="issue", draw=True, out=False, space_en_dir="en"): """ performs the graph of words analysis Arguments: ---------- key_word -- str : keyword to use to extract subgraph on which clustering is performed draw -- bool : wether to draw subgraph with clusters or not out -- bool : wether to output results or not spacy_en_dir : sapcy english environement directory "en" should be sufficient if symlink is set otherwise specify package directory Returns: ---------- G -- networkix.Graph : subgraph on which the clustering is done partition -- dict : clustering of the graph """ graph_of_words = self._build_gof(spacy_en_dir=space_en_dir) G = nx.ego_graph(G=graph_of_words, radius=1, n=key_word) print("building partition") partition = community_louvain.best_partition(G) if draw: pos = ReviewApp._community_layout(g=G, partition=partition) rcParams['figure.figsize'] = (40, 40) nx.draw(G, pos, node_color=list([partition[key] for key in list(G.nodes)]), labels=dict((n, n) for n, d in G.nodes(data=True)), font_color='black', font_size=6, font_weight='bold', edge_color='lightgray', node_size=35) print(self._get_partition_resume(graph_of_words, partition, 6)) if out: return G, partition
def period_graph(pn, grphdict=grphdict, periodcentralities=periodcentralities, commonnames=commonnames): periodgraph = grphdict[pn] for f in periodgraph.nodes(): # comty = periodgraph.nodes[f].get('community') # comty = ''.join([c[0] for c in comty]) if f in commonnames[pn]: label = f else: label = '' periodgraph.nodes[f].update({#"community" : comty, # "edgecolor": colors.get(comty) or 'purple', "centrality" : periodcentralities[pn][f]*4, "name" : f, "label" : label }) for f in periodgraph.edges(): for i, c in communities.items(): if f[0] and f[1] in c: comty = periodgraph.edges[f].get('community') or '' comty = ', '.join([comty,i]) # comty = ''.join([c[0] for c in comty]) periodgraph.edges[f].update({"community" : comty, }) for f in periodgraph.edges(): comty = periodgraph.edges[f].get('community') or [] comty = ''.join([c[0] for c in comty]) periodgraph.edges[f].update({"community" : comty}) partition = community_louvain.best_partition(periodgraph) pos = community_layout(periodgraph, partition) return periodgraph, pos
def cluster_graph(self, graph=None): graph = graph or self.graph self.classes = com.best_partition(graph) self.nb_clusters = max(self.classes.values()) + 1 #dendrogram = com.generate_dendrogram(graph) self.ids_in_clusters = [[e for e in self.classes.keys() if self.classes[e]==cl] for cl in range(self.nb_clusters)] print '\n >>> Graph Clustered <<<\n Found %d clusters'%self.nb_clusters
def generate_subgraphs(G): """ Generates the Louvain partitions of the given graph. Parameters ---------- G : networkx.Graph the graph Returns ------- subgraphs : list of networkx.Graph the list of all partitions generated by the Louvain algorithm """ partitions = community_louvain.best_partition(G) num_partitions = len(np.unique(list(partitions.values()))) print(r"""Number of partition from Louvain: {}""".format(num_partitions)) sets = [] for i in range(num_partitions): sets.append(set()) for k, v in partitions.items(): sets[v].add(k) subgraphs = [G.subgraph(s).copy() for s in sets] return subgraphs
def initialize_clusters_best_community(self, Ztrain): ''' in case of initilizing cluster assignments, we use best_partition method of Python's library this partitions the vertices. We use the partioned vertices and partition their edges based on highest rates in a partition ''' G = nx.Graph() nodes = list(np.unique(Ztrain)) G.add_nodes_from(nodes) G.add_edges_from(Ztrain) partitions = community_louvain.best_partition(G) to_remove = [] for n in partitions.keys(): if n not in nodes: to_remove.append(n) for n in to_remove: partitions.pop(n) cluster_edges = defaultdict(lambda: []) for i in range(Ztrain.shape[0]): (u, v) = Ztrain[i] if u in partitions.keys(): self.cluster[i] = partitions[u] elif v in partitions.keys(): self.cluster[i] = partitions[v] else: self.cluster[i] = 0 cluster_edges[self.cluster[i]].append(i) for c, edges in cluster_edges.items(): edges = np.array(edges) self.cluster[edges] = edges[0]
def plotCommunitiesByLouvainForChosenMajor(g, major_name): majors = list(set(idsToMajors.values())) removeBottomMajors(g, majors, [major_name]) # partition by Louvain Algorithm for community detection partition = community_louvain.best_partition(g) weights = [g[u][v]['weight'] for u,v in g.edges()] fig, ax = plt.subplots() pos = community_layout(g, partition) nx.draw(g,pos,cmap=plt.get_cmap('jet'), with_labels=False, arrows=True, node_color=major_to_color_d[major_name], width=weights) # legend info g.add_node(major_name) ax.plot([0],[0],color=major_to_cname[major_name],label=major_name,linewidth=5.0) plt.legend(numpoints=1,loc='lower left',fontsize='x-small',bbox_to_anchor=(-0.15,-0.1)) cc_weighted = nx.average_clustering(g, weight = 'weight') plt.title(major_name+" Network \n " + "Clustering Coefficient: " + str(round(cc_weighted,5))) # plt.show() plt.savefig('new_plots/community_plot_by_louvain_' + major_name + '.png')
def draw_nx_community2(self, limitdeg=1, limitcoo=1, title="noun network in article", min_component_size=1): # to install networkx 2.0 compatible version of python-louvain use: # pip install -U git+https://github.com/taynaud/python-louvain.git@networkx2 full_graph = nx.Graph(self.G) for H in nx.connected_component_subgraphs(full_graph, copy=True): if (min_component_size < H.number_of_nodes()): partition = community_louvain.best_partition(H) pos = self.community_layout(H, partition) nx.draw_networkx_labels(H, pos, fontsize=14) font = {'color': 'k', 'fontweight': 'bold', 'fontsize': 15} plt.title(title, font) plt.axis('off') nx.draw(H, pos, cmap=plt.get_cmap("Set3"), node_color=[ float(x) for x in partition.values() ]) #[self.get_cmap(x) for x in partition.values()]) plt.show()
def ego_network_construction(nearestNeighbors, word_vectors, idxByCandEidMap, seedEids, wmg, print_info, query_id): start = time.time() idxByNNEidMap = create_idxByXXXMap(nearestNeighbors) graph = nx.Graph() graph.add_nodes_from(range(len(nearestNeighbors))) wm = list() for eid in nearestNeighbors: wm.append(wmg.minhash(word_vectors[idxByCandEidMap[eid]])) # The following loop is slow, because of the jaccard function. for i in range(len(nearestNeighbors)): for j in range(i + 1, len(nearestNeighbors)): eid1 = nearestNeighbors[i] eid2 = nearestNeighbors[j] if eid2 != eid1 and eid1 not in seedEids and eid2 not in seedEids: # graph.add_weighted_edges_from([(idxByNNEidMap[eid1], idxByNNEidMap[eid2], wm[i].jaccard(wm[j]))]) if wm[i].jaccard(wm[j]) > 0.05: graph.add_edge(idxByNNEidMap[eid1], idxByNNEidMap[eid2]) partition = community_louvain.best_partition(graph) end = time.time() print( '[utils.py] Done Ego-network Construction & Detection using time %.1f seconds' % (end - start)) print_info[query_id] += ( '[utils.py] Done Ego-network Construction & Detection using time %.1f seconds\n' % (end - start)) return partition
def knnWeightedGraph(similarity_matrix, k, threshold=0.1): graph = create_knn_weighted_graph(similarity_matrix, k, threshold) # clusters = nx.algorithms.community.asyn_fluidc(graphm ) # return clusters_to_classes(clusters) # the community module works only for networkx<2 classes = com.best_partition(graph) return [classes[k] for k in range(len(classes.keys()))]
def cluster(graph): partition = cl.best_partition(graph) pos = community_layout(graph, partition) options = { 'node_color': list(partition.values()), 'node_size': 10, 'line_color': 'black', 'linewidths': 1.5, 'width': 1, } # nx.draw_networkx(graph, pos, node_color=list(partition.values())) plt.figure(figsize=(18, 18)) plt.title(str(graph.name)) nx.draw_networkx(graph, pos, node_size=100, node_color=list(partition.values()), nodelist=partition.keys(), width=.5, with_labels=False) plt.savefig(graph.name + 'clustering' + '.png', format='PNG') plt.show() return
def detect_communities_louvain(G): partition = community_louvain.best_partition(G) communities = list() for com in set(partition.values()) : list_nodes = [nodes for nodes in partition.keys() if partition[nodes] == com] communities.append(sorted(list_nodes)) return sorted(communities)
def main(): n = 500 G = generate_network(n) print(nx.info(G)) # visualize graph pos = nx.spring_layout(G) nx.draw(G, pos, node_size=75, alpha=0.8) plt.show() comms = community_louvain.best_partition(G) unique_coms = np.unique(list(comms.values())) cmap = { 0: 'maroon', 1: 'teal', 2: 'black', 3: 'orange', 4: 'green', 5: 'yellow' } node_cmap = [cmap[v] for _, v in comms.items()] pos = nx.spring_layout(G) nx.draw(G, pos, node_size=75, alpha=0.8, node_color=node_cmap) plt.show()
def community(self, resolution=1): partition = community_louvain.best_partition(G, resolution=resolution) n_community = max(list(partition.values())) print('Modularity:', community_louvain.modularity(partition, G)) print('Number of communities:', n_community) #drawing size = float(len(set(partition.values()))) pos = nx.spring_layout(G) count = 0 plt.figure(figsize=(12, 8)) for com in set(partition.values()): count = count + 1. list_nodes = [ nodes for nodes in partition.keys() if partition[nodes] == com ] nx.draw_networkx_nodes(G, pos, list_nodes, node_size=20, node_color=[random(), random(), random()]) nx.draw_networkx_edges(G, pos, alpha=0.5, width=0.5) plt.show() return partition
def cluster(G, verbose=3): """Clustering of graph labels. Parameters ---------- G : NetworkX object Graph. verbose : int [1-5], default: 3 Print information to screen. 0: nothing, 1: Error, 2: Warning, 3: information, 4: debug, 5: trace. Returns ------- tuple (G, labx). """ if verbose >= 3: print('[hnet] >Clustering using best partition') # Partition partition = community_louvain.best_partition(G) # Set property to node nx.set_node_attributes(G, partition, 'clusterlabel') # Extract labels labx = [partition.get(node) for node in G.nodes()] labx = np.array(labx) return (G, labx)
def draw_graph(adjacency_matrix, node_color=None): """ Draw a modular graph, also color its nodes based on the node communities detected by the Louvain algorithm. Parameters: ---------- adjacency_matrix : numpy ndarray The adjacency matrix. node_color : list, default=None A list of colors to color nodes. If `None` the nodes will be colored based on the node communities detected by the Louvain algorithm. Returns: -------- pos : dict mapping int node -> (float x, float y) node positions """ rows, cols = np.where(adjacency_matrix == 1) edges = zip(rows.tolist(), cols.tolist()) g = nx.Graph() g.add_edges_from(edges) partition = community_louvain.best_partition(g) pos = community_layout(g, partition) if node_color == None: node_color = list(partition.values()) nx.draw(g, pos, node_color=node_color, node_size=10); return list(partition.values())
def gen_graph(DELTA, labels, figname="nucleus_graph"): """ generate graph and plot from DELTA distance matrix - labels is list of node labels corresponding to columns/rows in DELTA """ DELTA = DELTA * 10 # scale dt = [("len", float)] DELTA = DELTA.view(dt) # Graphviz G = nx.from_numpy_matrix(DELTA) G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())), labels))) pos = graphviz_layout(G) G = nx.drawing.nx_agraph.to_agraph(G) G = nx.from_numpy_matrix(DELTA) G = nx.relabel_nodes(G, dict(zip(range(len(G.nodes())), labels))) #nx.draw(G, pos=pos, with_labels=True, node_size=100) #plt.savefig(os.path.join("fig", os.path.basename(figname).split(".")[0] + "_0.png")) #plt.close() np.random.seed(seed=1234) parts = community_louvain.best_partition(G) values = [parts.get(node) for node in G.nodes()] plt.figure(figsize=(10, 10), dpi=300, facecolor='w', edgecolor='k') plt.axis("off") nx.draw_networkx( G, pos=pos, cmap=plt.get_cmap("Set3"), node_color=values, node_size=500, font_size=12, width=1.5, font_weight="bold", font_color="k", alpha=1, edge_color="gray" ) plt.tight_layout() plt.savefig(figname + "_1.png") plt.close()
def support_group_score(G): att_dic = nx.get_node_attributes(G, 'att') communities = community_louvain.best_partition(G) communities_dic = defaultdict(list) for key, value in sorted(communities.items()): communities_dic[value].append(key) results=[] min_result=1 for k, v in communities_dic.items(): count = 0 male = 0 female = 0 for u in v: gen = att_dic[u] count += 1 if gen == 'male': male += 1 elif gen == 'female': female += 1 n1 = male/count n2 = female/count n = min(n1,n2) min_result= min(min_result,n) results.append(n) return st.mean(results), min_result, communities_dic
def get_the_clusters(G): print("Now it is getting the clusters") clusters = community_louvain.best_partition(G) print('Done clustering and now grouping the clusters') clusters_dic = defaultdict(list) for key, value in clusters.items(): clusters_dic[value].append(key) return clusters_dic
def find_community_best_partition(graph): parts = community_louvain.best_partition(graph) values = [parts.get(node) for node in graph.nodes()] clusters = values n_clusters = len(np.unique(values)) del parts del values return clusters, n_clusters
def _calculate(self, include: set, is_regression=False): partition = community_louvain.best_partition(self._gnx) # partition = community.best_partition(self._gnx) com_size_dict = Counter(partition.values()) self._features = { node: com_size_dict[partition[node]] for node in self._gnx }
def cluster_graph(self, graph=None): graph = graph or self.graph classes = com.best_partition(graph) self.nb_clusters = max(classes.values()) + 1 #dendrogram = com.generate_dendrogram(graph) self.ids_in_clusters = [[ e for e in classes.keys() if classes[e] == cl ] for cl in range(self.nb_clusters)]
def communities(publications: List[Tuple[int, Set[int]]]) -> None: graph = get_collaboration_graph(publications, 2006, 2016) partition = community_louvain.best_partition(graph) values = [partition.get(node) for node in graph.nodes()] networkx.draw_spring(graph, cmap=matplotlib.pyplot.get_cmap('tab20'), node_size=60, node_color=values, font_size=8, with_labels=True) matplotlib.pyplot.show()
def node_partitions(U, sources, sinks): core_numbers = listify(nx.core_number(U))[sources] core_same = core_numbers[sources] == core_numbers[sinks] louvain = listify(community_louvain.best_partition(U))[sources] louvain_same = louvain[sources] == louvain[sinks] res = [core_same, louvain_same] node_partitions = np.vstack(res).T logger.info(f'node_partitions generated: {node_partitions.shape}') return node_partitions
def find_communities(graph): parts = community_louvain.best_partition(graph) nx.set_node_attributes(graph, parts, 'community_num') size = len(set(parts.values())) print("\nnum of comm = ", size) mod = community.modularity(parts, graph) print("Modularity: ", mod) return parts
def plot_graph_community(graph): parts = community_louvain.best_partition(graph) values = [parts.get(node) for node in graph.nodes()] nx.draw_spring(graph, cmap=plt.get_cmap('jet'), node_color=values, node_size=35, with_labels=False) plt.axis('off') plt.show()
def test(): # to install networkx 2.0 compatible version of python-louvain use: # pip install -U git+https://github.com/taynaud/python-louvain.git@networkx2 from community import community_louvain g = nx.karate_club_graph() partition = community_louvain.best_partition(g) pos = community_layout(g, partition) nx.draw(g, pos, node_color=list(partition.values())); plt.show() return
def test(): #to install networkx 2.0 compatible version of python-louvain use: from community import community_louvain partition = community_louvain.best_partition(graph) pos = community_layout(graph, partition) nx.draw(graph, pos, node_color=list(partition.values())) plt.show() return
def cluster_graph(self, graph=None): graph = graph or self.graph classes = com.best_partition(graph) self.nb_clusters = max(classes.values()) + 1 #dendrogram = com.generate_dendrogram(graph) self.ids_in_clusters = [[e for e in classes.keys() if classes[e]==cl] for cl in range(self.nb_clusters)]
def knnGraph(similarity_matrix, k): graph = create_knn_graph(similarity_matrix, k) classes = com.best_partition(graph) return [classes[k] for k in range(len(classes.keys()))]