#!/usr/bin/env python # coding: utf-8 # # COMPUTATIONAL SOCIAL NETWORK ANALYSIS # ## Moses Boudourides # # IV. Centralities and Communities # # # ### [1. Centrality Indices](#1) # # ### [2. Community Partitions](#2) # # # In[1]: import random, operator, os, math, re, string, copy, itertools, pickle, datetime, pandas as pd, numpy as np, matplotlib.pyplot as plt, networkx as nx from collections import Counter, OrderedDict import operator import pygraphviz from networkx.drawing.nx_agraph import graphviz_layout from networkx.algorithms import community import community as louvain import spacy nlp = spacy.load('en_core_web_lg') # Plotting-related import matplotlib import matplotlib.pyplot as plt from matplotlib.patches import Rectangle # from matplotlib import pyplot as plt # from matplotlib.gridspec import GridSpec from matplotlib.ticker import FuncFormatter import matplotlib.colors as mcolors import matplotlib._color_data as mcd get_ipython().run_line_magic('matplotlib', 'inline') import seaborn as sns import plotly from plotly import tools # import plotly.plotly as py import chart_studio.plotly as py import plotly.graph_objs as go import plotly.figure_factory as ff from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot #connects JS to notebook so plots work inline init_notebook_mode(connected=True) import bokeh from bokeh.io import push_notebook, show, output_notebook, save import bokeh.plotting as bp from bokeh.plotting import figure, save, output_file, show #, from_networkx from bokeh.models import (ColumnDataSource, LabelSet, Label, BoxSelectTool, Circle, EdgesAndLinkedNodes, HoverTool,MultiLine, NodesAndLinkedEdges, Plot, Range1d, TapTool,) from holoviews.element.graphs import layout_nodes # bokeh.sampledata.download() from bokeh.sampledata.airport_routes import routes, airports output_notebook() import holoviews as hv from holoviews import dim, opts hv.extension('bokeh', 'matplotlib') from holoviews.operation import gridmatrix from holoviews.operation.datashader import datashade, bundle_graph from holoviews import Graph, Nodes from holoviews.plotting.bokeh import GraphPlot, LabelsPlot import hvplot.networkx as hvnx import hvplot.pandas import warnings warnings.filterwarnings("ignore", category=RuntimeWarning) warnings.simplefilter('ignore') # In[2]: def HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap, edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode, selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize, text_font_size, text_color,bgcolor): graph = hvnx.draw(G, pos) graph.opts(edge_color=edge_color,edge_line_width=edge_line_width,node_size=node_size,node_color=node_color,node_cmap=node_cmap) graph.opts(padding=0.15) if bundled==0: graph.opts(selection_policy=selection_policy,title=title,edge_hover_line_color=edge_hover_line_color,node_hover_fill_color=node_hover_fill_color,fontsize=fontsize,width=width,height=height,arrowhead_length=arrowhead_length) #,tools=tools) #,'box_zoom',"tap"]) if nodelabels==1: labels = hv.Labels(graph.nodes, ['x', 'y'], 'index') graph=(graph * labels.opts(xoffset=xoffset, yoffset=yoffset,text_font_size=text_font_size, text_color=text_color, bgcolor=bgcolor)) return graph else: return graph else: graph = bundle_graph(graph) graph.opts(selection_policy=selection_policy,title=title,edge_hover_line_color=edge_hover_line_color,node_hover_fill_color=node_hover_fill_color,fontsize=fontsize,width=width,height=height,arrowhead_length=arrowhead_length) #,tools=tools) #,'box_zoom',"tap"]) if nodelabels==1: labels = hv.Labels(graph.nodes, ['x', 'y'], 'index') graph=(graph * labels.opts(xoffset=xoffset, yoffset=yoffset,text_font_size=text_font_size, text_color=text_color, bgcolor=bgcolor)) return graph else: return graph # # ## 1. Centrality Indices # In[3]: def create_centralities_list(G,maxiter=2000,pphi=5,centList=[]): if len(centList)==0: centList=['degree','closeness','betweenness','eigenvector','Katz','PageRank','HITS','load','communicability','current flow'] cenLen=len(centList) valus={} for uu,centr in enumerate(centList): if centr=='degree': if isinstance(G,nx.DiGraph): cent=nx.in_degree_centrality(G) sstt='In Degree Centralities ' valus['in_degree']=cent cent=nx.out_degree_centrality(G) sstt+= 'and Out Degree Centralities' valus['out_degree']=cent else: cent=nx.degree_centrality(G) sstt='Degree Centralities' ssttt='degree centrality' valus[centr]=cent elif centr=='closeness': cent=nx.closeness_centrality(G) sstt='Closeness Centralities' ssttt='closeness centrality' valus[centr]=cent elif centr =='load': cent=nx.load_centrality(G) sstt='Load Centraities' valus[centr]=cent elif centr == 'communicability': if not isinstance(G, nx.DiGraph): cent=nx.communicability_betweenness_centrality(G) sstt='Communicability Centralities' valus[centr]=cent elif centr=='betweenness': cent=nx.betweenness_centrality(G) sstt='Betweenness Centralities' ssttt='betweenness centrality' valus[centr]=cent elif centr=='current flow': if not isinstance(G, nx.DiGraph): cent=nx.current_flow_closeness_centrality(G) sstt='Current Flow Closeness Centrality' valus[centr]=cent elif centr=='eigenvector': try: cent=nx.eigenvector_centrality(G,max_iter=maxiter) sstt='Eigenvector Centralities' ssttt='eigenvector centrality' valus[centr]=cent except: valus[centr]=None continue elif centr=='Katz': phi = (1+math.sqrt(pphi))/2.0 # largest eigenvalue of adj matrix cent=nx.katz_centrality_numpy(G,1/phi-0.01) cent=nx.katz_centrality_numpy(G,.05)#,1/phi-0.01) sstt='Katz Centralities' ssttt='Katz centrality' valus[centr]=cent # valus[centr+'_%i' %pphi]=cent elif centr=='PageRank': try: cent=nx.pagerank(G) sstt='PageRank' ssttt='pagerank' valus[centr]=cent except: valus[centr]=None continue elif centr=='HITS': if isinstance(G,nx.DiGraph): dd=nx.hits(G,max_iter=maxiter) sstt='HITS hubs ' valus['HITS_hubs']=dd[0] sstt+= 'and HITS authorities' valus['HITS_auths']=dd[1] else: dd=nx.hits(G,max_iter=maxiter) cent=nx.degree_centrality(G) sstt='HITS' ssttt='HITS Centralities' valus[centr]=dd[0] else: continue # print('%s done!!!' %sstt) return valus dindices=['out_degree','in_degree','closeness','betweenness','eigenvector','HITS_hubs','HITS_auths','Katz','PageRank','load'] indices=['degree','closeness','betweenness','eigenvector','HITS','Katz','PageRank','load','communicability','current flow'] # indices=['degree','closeness','betweenness','eigenvector'] # Without 'communicability' and 'current flow' (undirected case) dindicesd=['out_degree','in_degree','closeness','betweenness','eigenvector','HITS_hubs','HITS_auths','Katz','PageRank','load'] indicesd=['degree','closeness','betweenness','eigenvector','HITS','Katz','PageRank','load'] # indicesd=['degree','closeness','betweenness','eigenvector'] dindicesdr=dindices indicesdr=indices # Plus 'node' dindicesdrn=["node"]+dindices indicesdrn=['node']+indices def central_df(G,node,central_pd): central_pd[node]=central_pd.index.values if isinstance(G,nx.DiGraph): central_pd=central_pd[[node]+dindices] else: central_pd=central_pd[[node]+indices] central_pd[node]=central_pd.index.values central_pd.reset_index(drop = True, inplace = True) # central_pd=central_pd[['node']] central_pd.sort_values(node) #.head() # central_pd['node']=G.nodes() return central_pd # ### The Florentine Families Graph # In[4]: G=nx.florentine_families_graph() name = "Florentine Families graph" # In[5]: central_pd=pd.DataFrame(create_centralities_list(G)) node="node" cdf=central_df(G,node,central_pd) cdf=cdf.sort_values('betweenness',ascending=False) cdf # In[6]: centrs=[c for c in cdf.columns if c!='node'] cmaps=["blues","Greens","Oranges","Purples","Reds","Greys","YlOrBr","PuRd","BuPu","YlGnBu"] titles=[c.title() for c in centrs] degree_d=G.degree #Degree(G) pos=graphviz_layout(G) width=250 height=250 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in centrs: cdfc=cdf[["node",c]] dictc=cdfc.set_index('node')[c].to_dict() for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictc[n] title=titles[centrs.index(c)]+" Centrality Index" node_color=c node_cmap=cmaps[centrs.index(c)] #"summer" node_size=6*np.log(2+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Centrality Indices of a %s" %name) layout # In[7]: cdf=cdf[[c for c in list(cdf.columns) if c!='node']] grid=hvplot.scatter_matrix(cdf) #, diagonal_type=hv.Scatter) gc=grid.opts(opts.Scatter(tools=['hover', 'box_select'], fill_alpha=0.2, size=4,bgcolor='#efe8e2')) gc=gc.opts(title="Pairplots of Correlations among Centrality Indices of a %s" %name) gc # ### The Karate Club Graph # In[8]: G=nx.karate_club_graph() name = "Karate Club graph" # In[9]: central_pd=pd.DataFrame(create_centralities_list(G)) node="node" cdf=central_df(G,node,central_pd) cdf=cdf.sort_values('betweenness',ascending=False) cdf # In[10]: centrs=[c for c in cdf.columns if c!='node'] cmaps=["blues","Greens","Oranges","Purples","Reds","Greys","YlOrBr","PuRd","BuPu","YlGnBu"] titles=[c.title() for c in centrs] degree_d=G.degree #Degree(G) pos=graphviz_layout(G) width=250 height=250 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in centrs: cdfc=cdf[["node",c]] dictc=cdfc.set_index('node')[c].to_dict() for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictc[n] title=titles[centrs.index(c)]+" Centrality Index" node_color=c node_cmap=cmaps[centrs.index(c)] #"summer" node_size=6*np.log(2+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Centrality Indices of a %s" %name) layout # In[11]: cdf=cdf[[c for c in list(cdf.columns) if c!='node']] grid=hvplot.scatter_matrix(cdf) #, diagonal_type=hv.Scatter) gc=grid.opts(opts.Scatter(tools=['hover', 'box_select'], fill_alpha=0.2, size=4,bgcolor='#efe8e2')) gc=gc.opts(title="Pairplots of Correlations among Centrality Indices of a %s" %name) gc # ### A Connected Erdos-Renyi Random Undirected Grap # In[12]: while True: nodes=random.randint(30,40) p=random.uniform(0.05,0.08) G=nx.erdos_renyi_graph(nodes,p) if nx.is_connected(G)==True: break name="Connected Erdos-Renyi Random Undirected Graph" # In[13]: central_pd=pd.DataFrame(create_centralities_list(G)) node="node" cdf=central_df(G,node,central_pd) cdf=cdf.sort_values('betweenness',ascending=False) cdf # In[14]: centrs=[c for c in cdf.columns if c!='node'] cmaps=["blues","Greens","Oranges","Purples","Reds","Greys","YlOrBr","PuRd","BuPu","YlGnBu"] titles=[c.title() for c in centrs] degree_d=G.degree #Degree(G) pos=graphviz_layout(G) width=250 height=250 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in centrs: cdfc=cdf[["node",c]] dictc=cdfc.set_index('node')[c].to_dict() for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictc[n] title=titles[centrs.index(c)]+" Centrality Index" node_color=c node_cmap=cmaps[centrs.index(c)] #"summer" node_size=6*np.log(2+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Centrality Indices of a %s" %name) layout # In[15]: cdf=cdf[[c for c in list(cdf.columns) if c!='node']] grid=hvplot.scatter_matrix(cdf) #, diagonal_type=hv.Scatter) gc=grid.opts(opts.Scatter(tools=['hover', 'box_select'], fill_alpha=0.2, size=4,bgcolor='#efe8e2')) gc=gc.opts(title="Pairplots of Correlations among Centrality Indices of a %s" %name) gc # ### A Weakly Connected Erdos-Renyi Random Directed Graph # In[16]: while True: nodes=random.randint(30,40) p=random.uniform(0.05,0.08) G=nx.erdos_renyi_graph(nodes,p,directed=True) if nx.is_weakly_connected(G)==True: break name="Weakly Connected Erdos-Renyi Random Directed Graph" # In[17]: central_pd=pd.DataFrame(create_centralities_list(G)) node="node" cdf=central_df(G,node,central_pd) cdf=cdf.sort_values('betweenness',ascending=False) cdf # In[18]: centrs=[c for c in cdf.columns if c!='node'] cmaps=["blues","Greens","Oranges","Purples","Reds","Greys","YlOrBr","PuRd","BuPu","YlGnBu"] titles=[c.title() for c in centrs] in_degree_d=G.in_degree #Degree(G) pos=graphviz_layout(G) width=250 height=250 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in centrs: cdfc=cdf[["node",c]] dictc=cdfc.set_index('node')[c].to_dict() for n in G.nodes(): G.nodes[n]['node in_degree']=in_degree_d[n] G.nodes[n][c]=dictc[n] title=titles[centrs.index(c)]+" Centrality Index" node_color=c node_cmap=cmaps[centrs.index(c)] #"summer" node_size=6*np.log(3+hv.dim('node in_degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Centrality Indices of a %s" %name) layout # In[19]: cdf=cdf[[c for c in list(cdf.columns) if c!='node']] grid=hvplot.scatter_matrix(cdf) #, diagonal_type=hv.Scatter) gc=grid.opts(opts.Scatter(tools=['hover', 'box_select'], fill_alpha=0.2, size=4,bgcolor='#efe8e2')) gc=gc.opts(title="Pairplots of Correlations among Centrality Indices of a %s" %name) gc # # ## 2. Community Partitions # ### The Florentine Families Graph # In[20]: G=nx.florentine_families_graph() name = "Florentine Families graph" # In[21]: # Girvan-Newman communities: gncp = community.girvan_newman(G) top_level_communities = next(gncp) next_level_communities = next(gncp) lc=sorted(sorted(map(sorted, next_level_communities)), key=len,reverse=True) gncp_d={n:i for i,c in enumerate(lc) for n in c} # Louvain communities: lcp_d=louvain.best_partition(G) # Label Propagation communities: lpc=nx.algorithms.community.label_propagation.label_propagation_communities(G) lpc=sorted(lpc, key=len, reverse=True) lpc_d={n:i for i,c in enumerate(lpc) for n in c} # Asynchronous Label Propagation communities: alpc=nx.algorithms.community.label_propagation.asyn_lpa_communities(G) alpc=sorted(alpc, key=len, reverse=True) alpc_d={n:i for i,c in enumerate(alpc) for n in c} # Fluid communities # For the partition of fluid communities, we need to give the number (k) of wanted communities: k=5 fcp=nx.algorithms.community.asyn_fluid.asyn_fluidc(G, k, max_iter=100, seed=None) fcp=sorted(fcp, key=len, reverse=True) fcp_d={n:i for i,c in enumerate(fcp) for n in c} comms=["Girvan-Newman communities","Louvain communities","Label propagation communities","Asynchronous label propagation communities","Fluid communities"] dictc=[gncp_d,lcp_d,lpc_d,alpc_d,fcp_d] cmaps=["blues","Greens","Oranges","Purples","PuRd"] degree_d=G.degree pos=graphviz_layout(G) width=300 height=300 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in comms: dictcc=dictc[comms.index(c)] nc=max(list(dictcc.values()))+1 for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictcc[n] title="%s (%i)" %(c,nc) node_color=c node_cmap=cmaps[comms.index(c)] node_size=6*np.log(3+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Various Community Partitions of a %s" %name) layout # ### The Karate Club Graph # In[22]: G=nx.karate_club_graph() name = "Karate Club graph" # In[23]: # Girvan-Newman communities: gncp = community.girvan_newman(G) top_level_communities = next(gncp) next_level_communities = next(gncp) lc=sorted(sorted(map(sorted, next_level_communities)), key=len,reverse=True) gncp_d={n:i for i,c in enumerate(lc) for n in c} # Louvain communities: lcp_d=louvain.best_partition(G) # Label Propagation communities: lpc=nx.algorithms.community.label_propagation.label_propagation_communities(G) lpc=sorted(lpc, key=len, reverse=True) lpc_d={n:i for i,c in enumerate(lpc) for n in c} # Asynchronous Label Propagation communities: alpc=nx.algorithms.community.label_propagation.asyn_lpa_communities(G) alpc=sorted(alpc, key=len, reverse=True) alpc_d={n:i for i,c in enumerate(alpc) for n in c} # Fluid communities # For the partition of fluid communities, we need to give the number (k) of wanted communities: k=5 fcp=nx.algorithms.community.asyn_fluid.asyn_fluidc(G, k, max_iter=100, seed=None) fcp=sorted(fcp, key=len, reverse=True) fcp_d={n:i for i,c in enumerate(fcp) for n in c} comms=["Girvan-Newman communities","Louvain communities","Label propagation communities","Asynchronous label propagation communities","Fluid communities"] dictc=[gncp_d,lcp_d,lpc_d,alpc_d,fcp_d] cmaps=["blues","Greens","Oranges","Purples","PuRd"] degree_d=G.degree pos=graphviz_layout(G) width=300 height=300 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in comms: dictcc=dictc[comms.index(c)] nc=max(list(dictcc.values()))+1 for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictcc[n] title="%s (%i)" %(c,nc) node_color=c node_cmap=cmaps[comms.index(c)] node_size=6*np.log(3+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Various Community Partitions of a %s" %name) layout # ### A Connected Erdos-Renyi Random Undirected Graph # In[24]: while True: nodes=random.randint(30,40) p=random.uniform(0.05,0.08) G=nx.erdos_renyi_graph(nodes,p) if nx.is_connected(G)==True: break name="Connected Erdos-Renyi Random Undirected Graph" # In[25]: # Girvan-Newman communities: gncp = community.girvan_newman(G) top_level_communities = next(gncp) next_level_communities = next(gncp) lc=sorted(sorted(map(sorted, next_level_communities)), key=len,reverse=True) gncp_d={n:i for i,c in enumerate(lc) for n in c} # Louvain communities: lcp_d=louvain.best_partition(G) # Label Propagation communities: lpc=nx.algorithms.community.label_propagation.label_propagation_communities(G) lpc=sorted(lpc, key=len, reverse=True) lpc_d={n:i for i,c in enumerate(lpc) for n in c} # Asynchronous Label Propagation communities: alpc=nx.algorithms.community.label_propagation.asyn_lpa_communities(G) alpc=sorted(alpc, key=len, reverse=True) alpc_d={n:i for i,c in enumerate(alpc) for n in c} # Fluid communities # For the partition of fluid communities, we need to give the number (k) of wanted communities: k=5 fcp=nx.algorithms.community.asyn_fluid.asyn_fluidc(G, k, max_iter=100, seed=None) fcp=sorted(fcp, key=len, reverse=True) fcp_d={n:i for i,c in enumerate(fcp) for n in c} comms=["Girvan-Newman communities","Louvain communities","Label propagation communities","Asynchronous label propagation communities","Fluid communities"] dictc=[gncp_d,lcp_d,lpc_d,alpc_d,fcp_d] cmaps=["blues","Greens","Oranges","Purples","PuRd"] degree_d=G.degree pos=graphviz_layout(G) width=300 height=300 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in comms: dictcc=dictc[comms.index(c)] nc=max(list(dictcc.values()))+1 for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictcc[n] title="%s (%i)" %(c,nc) node_color=c node_cmap=cmaps[comms.index(c)] node_size=6*np.log(3+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(3) layout.opts(title="Various Community Partitions of a %s" %name) layout # ### A Weakly Connected Erdos-Renyi Random Directed Graph # In[26]: while True: nodes=random.randint(30,40) p=random.uniform(0.05,0.08) G=nx.erdos_renyi_graph(nodes,p,directed=True) if nx.is_weakly_connected(G)==True: break name="Weakly Connected Erdos-Renyi Random Directed Graph" # In[27]: # Girvan-Newman communities: gncp = community.girvan_newman(G) top_level_communities = next(gncp) next_level_communities = next(gncp) lc=sorted(sorted(map(sorted, next_level_communities)), key=len,reverse=True) gncp_d={n:i for i,c in enumerate(lc) for n in c} # Asynchronous Label Propagation communities: alpc=nx.algorithms.community.label_propagation.asyn_lpa_communities(G) alpc=sorted(alpc, key=len, reverse=True) alpc_d={n:i for i,c in enumerate(alpc) for n in c} comms=["Girvan-Newman communities","Asynchronous label propagation communities"] dictc=[gncp_d,alpc_d] cmaps=["blues","Oranges"] degree_d=G.degree pos=graphviz_layout(G) width=400 height=400 bundled=0 nodelabels=0 xoffset=0 yoffset=0 arrowhead_length=0.04 selection_mode='nodes' selection_policy="nodes" edge_hover_line_color='green' node_hover_fill_color='red' fontsize={'title': '9pt'} text_font_size='9pt' text_color='white' bgcolor='white' graphs=[] for c in comms: dictcc=dictc[comms.index(c)] nc=max(list(dictcc.values()))+1 for n in G.nodes(): G.nodes[n]['node degree']=degree_d[n] G.nodes[n][c]=dictcc[n] title="%s (%i)" %(c,nc) node_color=c node_cmap=cmaps[comms.index(c)] node_size=6*np.log(3+hv.dim('node degree')) edge_color='lightgreen' edge_line_width=1 g=HVNX_PLOT(G,pos,width,height,bundled,nodelabels,title,node_size,node_color,node_cmap,edge_color,edge_line_width,xoffset,yoffset,arrowhead_length,selection_mode,selection_policy,edge_hover_line_color,node_hover_fill_color,fontsize,text_font_size, text_color,bgcolor) graphs.append(g) for n in G: del G.nodes[n][c] layout = hv.Layout(graphs).cols(2) layout.opts(title="%s of a %s" %(c,name)) layout # In[ ]: