def test_community_graph(self): g = nx.karate_club_graph() coms = algorithms.louvain(g) viz.plot_community_graph(g, coms) plt.savefig("cg.pdf") os.remove("cg.pdf") coms = algorithms.angel(g, 0.25) viz.plot_community_graph(g, coms, plot_overlaps=True, plot_labels=True) plt.savefig("cg.pdf") os.remove("cg.pdf")
#creates graph G = nx.Graph(filtered_entities) print(G.nodes()) print(G.edges()) #remove outliers/self-loops G.remove_edges_from(nx.selfloop_edges(G)) G = nx.k_core(G,k=2) #Louvain/infomap algorithm and graph plot #coms = algorithms.louvain(G) coms = algorithms.infomap(G) pos = nx.spring_layout(G) viz.plot_community_graph(G, coms, figsize=(8, 8), node_size=200, plot_overlaps=False, plot_labels=True, cmap=None, top_k=None, min_size=None) viz.plot_network_clusters(G, coms, position=None, figsize=(8, 8), node_size=200, plot_overlaps=False, plot_labels=False, cmap=None, top_k=None, min_size=None) #converting this to an nx graph for calculations. mod = evaluation.modularity_density(G,coms) print(mod) #calculating modularity mod = evaluation.modularity_density(G,coms) print(mod) #calculating purity #communities = eva(G, coms) #pur = evaluation.purity(communities) #print(pur)
df_nodes = pd.merge(df_nodes, df_clusts, how='left', left_on='node_id', right_on='node_id') # We can then just adjusted mutual info to find similarity score ami_score = metrics.adjusted_mutual_info_score(df_nodes[name], df_nodes['truth']) print(f'The AMI for {name} algorithm is {round(ami_score, 3)}, and there were {len(communities)} communities.') results_dict[name] = {'AMI' : round(ami_score, 3), 'pred_coms' : pred_coms, 'numb_communities' : len(communities), 'truth_communities' : len(df_nodes['truth'].unique())} # plot the network clusters viz.plot_network_clusters(nx_g, pred_coms, pos, figsize=(5, 5)) plt.title(f'{name} algo of {graph_name}, AMI = {round(ami_score, 3)}') plt.show() # plot the graph viz.plot_community_graph(nx_g, pred_coms, figsize=(5, 5)) plt.title(f'Communities for {name} algo of {graph_name}.') plt.show() #%% analysis plots coms = [ground_truth_com] for name, results in results_dict.items(): coms.append(results['pred_coms']) #%% viz.plot_sim_matrix(coms,evaluation.adjusted_mutual_information) plt.show() viz.plot_com_properties_relation(coms, evaluation.size, evaluation.internal_edge_density) plt.title('Internal Edge Density vs. Size') plt.show()