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
%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')
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
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
G=nx.florentine_families_graph()
name = "Florentine Families graph"
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
node | degree | closeness | betweenness | eigenvector | HITS | Katz | PageRank | load | communicability | current flow | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | Medici | 0.428571 | 0.560000 | 0.521978 | 0.430315 | 0.122853 | 0.298431 | 0.145818 | 0.521978 | 0.591358 | 0.070954 |
12 | Guadagni | 0.285714 | 0.466667 | 0.254579 | 0.289117 | 0.082543 | 0.273357 | 0.098399 | 0.250916 | 0.305821 | 0.061155 |
8 | Albizzi | 0.214286 | 0.482759 | 0.212454 | 0.243961 | 0.069650 | 0.262470 | 0.079122 | 0.210623 | 0.261155 | 0.055316 |
9 | Salviati | 0.142857 | 0.388889 | 0.142857 | 0.145921 | 0.041659 | 0.248768 | 0.061303 | 0.142857 | 0.165934 | 0.039851 |
6 | Ridolfi | 0.214286 | 0.500000 | 0.113553 | 0.341554 | 0.097514 | 0.263966 | 0.069574 | 0.117216 | 0.231622 | 0.060037 |
11 | Bischeri | 0.214286 | 0.400000 | 0.104396 | 0.282794 | 0.080740 | 0.262620 | 0.068862 | 0.104396 | 0.162607 | 0.055459 |
4 | Strozzi | 0.285714 | 0.437500 | 0.102564 | 0.355973 | 0.101633 | 0.274623 | 0.088098 | 0.106227 | 0.211959 | 0.058547 |
5 | Barbadori | 0.142857 | 0.437500 | 0.093407 | 0.211706 | 0.060442 | 0.250113 | 0.050301 | 0.093407 | 0.124094 | 0.050897 |
7 | Tornabuoni | 0.214286 | 0.482759 | 0.091575 | 0.325847 | 0.093028 | 0.263906 | 0.071279 | 0.089744 | 0.222783 | 0.060156 |
2 | Castellani | 0.214286 | 0.388889 | 0.054945 | 0.259020 | 0.073952 | 0.261458 | 0.069330 | 0.054945 | 0.114575 | 0.052962 |
3 | Peruzzi | 0.214286 | 0.368421 | 0.021978 | 0.275722 | 0.078721 | 0.262053 | 0.067875 | 0.021978 | 0.099599 | 0.051921 |
0 | Acciaiuoli | 0.071429 | 0.368421 | 0.000000 | 0.132157 | 0.037730 | 0.237040 | 0.030657 | 0.000000 | 0.027055 | 0.036909 |
10 | Pazzi | 0.071429 | 0.285714 | 0.000000 | 0.044815 | 0.012794 | 0.234557 | 0.036054 | 0.000000 | 0.007073 | 0.026251 |
13 | Ginori | 0.071429 | 0.333333 | 0.000000 | 0.074925 | 0.021390 | 0.235242 | 0.032418 | 0.000000 | 0.011310 | 0.032177 |
14 | Lamberteschi | 0.071429 | 0.325581 | 0.000000 | 0.088793 | 0.025350 | 0.235786 | 0.030909 | 0.000000 | 0.013586 | 0.034069 |
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
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
G=nx.karate_club_graph()
name = "Karate Club graph"
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
node | degree | closeness | betweenness | eigenvector | HITS | Katz | PageRank | load | communicability | current flow | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0 | 0.484848 | 0.568966 | 0.437635 | 0.355483 | 0.071413 | 0.258081 | 0.097002 | 0.434683 | 0.603031 | 0.058567 |
33 | 33 | 0.515152 | 0.550000 | 0.304075 | 0.373371 | 0.075003 | 0.263960 | 0.100918 | 0.298434 | 0.555540 | 0.059183 |
32 | 32 | 0.363636 | 0.515625 | 0.145247 | 0.308651 | 0.062002 | 0.228805 | 0.071692 | 0.147635 | 0.400896 | 0.055095 |
2 | 2 | 0.303030 | 0.559322 | 0.143657 | 0.317189 | 0.063719 | 0.217703 | 0.057078 | 0.148343 | 0.403343 | 0.055765 |
31 | 31 | 0.181818 | 0.540984 | 0.138276 | 0.191036 | 0.038376 | 0.184357 | 0.037157 | 0.134075 | 0.228675 | 0.047583 |
8 | 8 | 0.151515 | 0.515625 | 0.055927 | 0.227405 | 0.045682 | 0.180862 | 0.029765 | 0.051531 | 0.226617 | 0.047106 |
1 | 1 | 0.272727 | 0.485294 | 0.053937 | 0.265954 | 0.053427 | 0.205489 | 0.052878 | 0.054936 | 0.287543 | 0.052525 |
13 | 13 | 0.151515 | 0.515625 | 0.045863 | 0.226470 | 0.045495 | 0.180436 | 0.029536 | 0.047888 | 0.215709 | 0.046758 |
19 | 19 | 0.090909 | 0.500000 | 0.032475 | 0.147911 | 0.029713 | 0.160412 | 0.019604 | 0.033920 | 0.107013 | 0.039096 |
5 | 5 | 0.121212 | 0.383721 | 0.029987 | 0.079481 | 0.015967 | 0.159543 | 0.029113 | 0.029987 | 0.079311 | 0.034106 |
6 | 6 | 0.121212 | 0.383721 | 0.029987 | 0.079481 | 0.015967 | 0.159543 | 0.029113 | 0.029987 | 0.079311 | 0.034106 |
27 | 27 | 0.121212 | 0.458333 | 0.022333 | 0.133479 | 0.026813 | 0.164194 | 0.025639 | 0.023927 | 0.097338 | 0.041269 |
23 | 23 | 0.151515 | 0.392857 | 0.017614 | 0.150123 | 0.030156 | 0.172579 | 0.031521 | 0.017677 | 0.119574 | 0.043622 |
30 | 30 | 0.121212 | 0.458333 | 0.014412 | 0.174760 | 0.035106 | 0.167991 | 0.024589 | 0.014207 | 0.124886 | 0.043043 |
3 | 3 | 0.181818 | 0.464789 | 0.011909 | 0.211174 | 0.042423 | 0.182787 | 0.035861 | 0.014234 | 0.180573 | 0.046827 |
25 | 25 | 0.090909 | 0.375000 | 0.003840 | 0.059208 | 0.011894 | 0.149329 | 0.021006 | 0.004956 | 0.026842 | 0.033763 |
29 | 29 | 0.121212 | 0.383721 | 0.002922 | 0.134965 | 0.027112 | 0.164576 | 0.026287 | 0.003650 | 0.088119 | 0.039676 |
24 | 24 | 0.090909 | 0.375000 | 0.002210 | 0.057054 | 0.011461 | 0.148929 | 0.021075 | 0.002328 | 0.025293 | 0.033587 |
28 | 28 | 0.090909 | 0.452055 | 0.001795 | 0.131079 | 0.026332 | 0.157336 | 0.019573 | 0.001762 | 0.076814 | 0.038170 |
9 | 9 | 0.060606 | 0.434211 | 0.000848 | 0.102675 | 0.020626 | 0.148118 | 0.014309 | 0.000789 | 0.043313 | 0.031630 |
10 | 10 | 0.090909 | 0.379310 | 0.000631 | 0.075966 | 0.015261 | 0.152544 | 0.021979 | 0.000631 | 0.048145 | 0.032735 |
4 | 4 | 0.090909 | 0.379310 | 0.000631 | 0.075966 | 0.015261 | 0.152544 | 0.021979 | 0.000631 | 0.048145 | 0.032735 |
26 | 26 | 0.060606 | 0.362637 | 0.000000 | 0.075582 | 0.015183 | 0.145462 | 0.015043 | 0.000000 | 0.026115 | 0.029202 |
18 | 18 | 0.060606 | 0.370787 | 0.000000 | 0.101406 | 0.020370 | 0.148674 | 0.014535 | 0.000000 | 0.042483 | 0.030946 |
22 | 22 | 0.060606 | 0.370787 | 0.000000 | 0.101406 | 0.020370 | 0.148674 | 0.014535 | 0.000000 | 0.042483 | 0.030946 |
21 | 21 | 0.060606 | 0.375000 | 0.000000 | 0.092397 | 0.018562 | 0.147214 | 0.014559 | 0.000000 | 0.038289 | 0.030849 |
20 | 20 | 0.060606 | 0.370787 | 0.000000 | 0.101406 | 0.020370 | 0.148674 | 0.014535 | 0.000000 | 0.042483 | 0.030946 |
16 | 16 | 0.060606 | 0.284483 | 0.000000 | 0.023635 | 0.004748 | 0.139990 | 0.016785 | 0.000000 | 0.007873 | 0.023957 |
15 | 15 | 0.060606 | 0.370787 | 0.000000 | 0.101406 | 0.020370 | 0.148674 | 0.014535 | 0.000000 | 0.042483 | 0.030946 |
14 | 14 | 0.060606 | 0.370787 | 0.000000 | 0.101406 | 0.020370 | 0.148674 | 0.014535 | 0.000000 | 0.042483 | 0.030946 |
12 | 12 | 0.060606 | 0.370787 | 0.000000 | 0.084252 | 0.016925 | 0.146079 | 0.014645 | 0.000000 | 0.032312 | 0.030222 |
11 | 11 | 0.030303 | 0.366667 | 0.000000 | 0.052854 | 0.010618 | 0.136939 | 0.009565 | 0.000000 | 0.014449 | 0.020377 |
7 | 7 | 0.121212 | 0.440000 | 0.000000 | 0.170955 | 0.034343 | 0.167238 | 0.024491 | 0.000000 | 0.115486 | 0.042033 |
17 | 17 | 0.060606 | 0.375000 | 0.000000 | 0.092397 | 0.018562 | 0.147214 | 0.014559 | 0.000000 | 0.038289 | 0.030849 |
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
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
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"
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
node | degree | closeness | betweenness | eigenvector | HITS | Katz | PageRank | load | communicability | current flow | |
---|---|---|---|---|---|---|---|---|---|---|---|
22 | 22 | 0.189189 | 0.406593 | 0.226258 | 0.377517 | 0.072597 | 0.193590 | 0.050526 | 0.221607 | 0.307490 | 0.028592 |
14 | 14 | 0.162162 | 0.381443 | 0.191134 | 0.286500 | 0.055095 | 0.185204 | 0.045783 | 0.187218 | 0.235023 | 0.026943 |
11 | 11 | 0.135135 | 0.373737 | 0.168914 | 0.195100 | 0.037515 | 0.176752 | 0.037863 | 0.166531 | 0.210115 | 0.026688 |
26 | 26 | 0.189189 | 0.355769 | 0.150422 | 0.330978 | 0.063649 | 0.191922 | 0.051751 | 0.153539 | 0.214864 | 0.027353 |
30 | 30 | 0.108108 | 0.336364 | 0.141808 | 0.111821 | 0.021504 | 0.167740 | 0.033191 | 0.145406 | 0.166563 | 0.023740 |
9 | 9 | 0.135135 | 0.352381 | 0.138330 | 0.208382 | 0.040068 | 0.177452 | 0.037524 | 0.136397 | 0.184999 | 0.025612 |
31 | 31 | 0.081081 | 0.276119 | 0.131081 | 0.041450 | 0.007971 | 0.159111 | 0.030169 | 0.133269 | 0.137812 | 0.017959 |
29 | 29 | 0.108108 | 0.366337 | 0.111916 | 0.207475 | 0.039896 | 0.169512 | 0.030937 | 0.110663 | 0.155138 | 0.024925 |
35 | 35 | 0.081081 | 0.330357 | 0.111496 | 0.111978 | 0.021532 | 0.160518 | 0.025580 | 0.108942 | 0.130932 | 0.022119 |
37 | 37 | 0.108108 | 0.373737 | 0.108235 | 0.225710 | 0.043402 | 0.170667 | 0.029747 | 0.107634 | 0.164675 | 0.024917 |
24 | 24 | 0.108108 | 0.345794 | 0.101827 | 0.142070 | 0.027321 | 0.168523 | 0.032240 | 0.101675 | 0.132221 | 0.023831 |
34 | 34 | 0.081081 | 0.278195 | 0.096005 | 0.041333 | 0.007948 | 0.158425 | 0.030423 | 0.095752 | 0.102028 | 0.017428 |
10 | 10 | 0.108108 | 0.324561 | 0.089652 | 0.171572 | 0.032991 | 0.168724 | 0.030879 | 0.088244 | 0.116353 | 0.023428 |
28 | 28 | 0.108108 | 0.318966 | 0.081632 | 0.193346 | 0.037182 | 0.168918 | 0.033795 | 0.081519 | 0.112307 | 0.021921 |
7 | 7 | 0.081081 | 0.313559 | 0.068186 | 0.128941 | 0.024796 | 0.160854 | 0.024869 | 0.071300 | 0.084268 | 0.021629 |
18 | 18 | 0.108108 | 0.303279 | 0.065490 | 0.131280 | 0.025243 | 0.167436 | 0.033786 | 0.065722 | 0.094750 | 0.021292 |
17 | 17 | 0.081081 | 0.330357 | 0.061061 | 0.096523 | 0.018561 | 0.160092 | 0.025301 | 0.061937 | 0.078619 | 0.021420 |
33 | 33 | 0.081081 | 0.349057 | 0.060183 | 0.130830 | 0.025158 | 0.160638 | 0.024002 | 0.060832 | 0.091428 | 0.022840 |
25 | 25 | 0.108108 | 0.262411 | 0.059674 | 0.119289 | 0.022936 | 0.167380 | 0.033384 | 0.060514 | 0.081819 | 0.020167 |
15 | 15 | 0.054054 | 0.220238 | 0.054054 | 0.011167 | 0.002147 | 0.150379 | 0.024813 | 0.054054 | 0.056777 | 0.011150 |
2 | 2 | 0.054054 | 0.313559 | 0.048345 | 0.126770 | 0.024378 | 0.153316 | 0.016793 | 0.052469 | 0.052084 | 0.019583 |
8 | 8 | 0.108108 | 0.300813 | 0.038679 | 0.150578 | 0.028953 | 0.168605 | 0.031366 | 0.041197 | 0.084023 | 0.022092 |
21 | 21 | 0.081081 | 0.333333 | 0.037663 | 0.204003 | 0.039231 | 0.162151 | 0.023985 | 0.036818 | 0.074063 | 0.021773 |
1 | 1 | 0.054054 | 0.313559 | 0.037087 | 0.128346 | 0.024680 | 0.153335 | 0.017262 | 0.036224 | 0.050380 | 0.019115 |
23 | 23 | 0.081081 | 0.352381 | 0.032307 | 0.211722 | 0.040715 | 0.162591 | 0.023167 | 0.031782 | 0.084188 | 0.023142 |
16 | 16 | 0.054054 | 0.286822 | 0.018769 | 0.083741 | 0.016104 | 0.152088 | 0.018499 | 0.018769 | 0.029674 | 0.017360 |
6 | 6 | 0.054054 | 0.321739 | 0.017056 | 0.124607 | 0.023962 | 0.153019 | 0.017008 | 0.015776 | 0.036652 | 0.019586 |
13 | 13 | 0.081081 | 0.276119 | 0.015695 | 0.120642 | 0.023197 | 0.160955 | 0.024086 | 0.016735 | 0.048358 | 0.020203 |
36 | 36 | 0.081081 | 0.330357 | 0.013917 | 0.232825 | 0.044773 | 0.162673 | 0.022945 | 0.013295 | 0.064735 | 0.022283 |
12 | 12 | 0.081081 | 0.310924 | 0.011562 | 0.214490 | 0.041248 | 0.162273 | 0.023219 | 0.011512 | 0.056307 | 0.022151 |
4 | 4 | 0.054054 | 0.274074 | 0.005631 | 0.045472 | 0.008744 | 0.150893 | 0.018978 | 0.006819 | 0.014521 | 0.016449 |
5 | 5 | 0.054054 | 0.282443 | 0.004429 | 0.068363 | 0.013147 | 0.151752 | 0.017844 | 0.004348 | 0.017761 | 0.017940 |
19 | 19 | 0.054054 | 0.270073 | 0.001502 | 0.100232 | 0.019275 | 0.151837 | 0.017924 | 0.001502 | 0.015256 | 0.017534 |
32 | 32 | 0.027027 | 0.181373 | 0.000000 | 0.002817 | 0.000542 | 0.142802 | 0.014494 | 0.000000 | 0.001523 | 0.007957 |
27 | 27 | 0.027027 | 0.234177 | 0.000000 | 0.033116 | 0.006368 | 0.143655 | 0.011127 | 0.000000 | 0.002843 | 0.012053 |
20 | 20 | 0.027027 | 0.218935 | 0.000000 | 0.010427 | 0.002005 | 0.143205 | 0.012568 | 0.000000 | 0.002905 | 0.010709 |
3 | 3 | 0.027027 | 0.243421 | 0.000000 | 0.048772 | 0.009379 | 0.143729 | 0.011129 | 0.000000 | 0.003351 | 0.012252 |
0 | 0 | 0.027027 | 0.209040 | 0.000000 | 0.030092 | 0.005786 | 0.143653 | 0.011041 | 0.000000 | 0.002312 | 0.011684 |
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
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
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"
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
node | out_degree | in_degree | closeness | betweenness | eigenvector | HITS_hubs | HITS_auths | Katz | PageRank | load | |
---|---|---|---|---|---|---|---|---|---|---|---|
34 | 34 | 0.138889 | 0.083333 | 0.294597 | 0.328981 | 1.461160e-01 | 3.787329e-02 | 1.999978e-02 | 0.170230 | 0.042628 | 0.331680 |
24 | 24 | 0.166667 | 0.138889 | 0.308761 | 0.275873 | 3.139998e-01 | 1.361471e-01 | 4.583840e-02 | 0.186816 | 0.064041 | 0.273611 |
9 | 9 | 0.055556 | 0.083333 | 0.302935 | 0.180106 | 1.638268e-01 | 1.268608e-02 | 5.037644e-02 | 0.170944 | 0.038200 | 0.177381 |
19 | 19 | 0.055556 | 0.083333 | 0.274454 | 0.171429 | 1.814470e-01 | 4.825224e-02 | 7.571678e-02 | 0.170598 | 0.033317 | 0.172024 |
17 | 17 | 0.055556 | 0.111111 | 0.241437 | 0.139550 | 7.752088e-02 | 2.605966e-33 | 4.076437e-02 | 0.177041 | 0.022211 | 0.138757 |
22 | 22 | 0.055556 | 0.055556 | 0.250868 | 0.134074 | 8.763370e-02 | 2.020377e-03 | 9.303265e-03 | 0.162489 | 0.026199 | 0.132474 |
8 | 8 | 0.083333 | 0.083333 | 0.276820 | 0.133915 | 1.463518e-01 | 3.341253e-02 | 2.181484e-02 | 0.170160 | 0.048554 | 0.133796 |
4 | 4 | 0.083333 | 0.055556 | 0.207168 | 0.128069 | 4.595637e-02 | 1.688295e-02 | 6.154909e-04 | 0.161660 | 0.022420 | 0.130060 |
6 | 6 | 0.027778 | 0.055556 | 0.237860 | 0.113413 | 8.961433e-02 | 1.837834e-04 | 3.364468e-02 | 0.162437 | 0.015426 | 0.115278 |
35 | 35 | 0.027778 | 0.055556 | 0.236111 | 0.101005 | 9.885858e-02 | 5.971864e-03 | 1.962088e-02 | 0.162087 | 0.021998 | 0.102381 |
11 | 11 | 0.055556 | 0.111111 | 0.311758 | 0.100608 | 3.400422e-01 | 3.163838e-39 | 2.645272e-02 | 0.179600 | 0.055462 | 0.098214 |
0 | 0 | 0.055556 | 0.055556 | 0.214074 | 0.092460 | 5.241772e-02 | 1.507767e-02 | 6.099589e-03 | 0.162066 | 0.021099 | 0.092196 |
36 | 36 | 0.055556 | 0.166667 | 0.369093 | 0.090926 | 4.130098e-01 | 2.209106e-02 | 1.415970e-01 | 0.195863 | 0.064127 | 0.090608 |
21 | 21 | 0.083333 | 0.027778 | 0.194613 | 0.088757 | 3.582924e-02 | 1.235933e-02 | 3.333564e-33 | 0.154712 | 0.014967 | 0.089153 |
13 | 13 | 0.027778 | 0.055556 | 0.218443 | 0.083333 | 5.170069e-02 | 2.810053e-54 | 6.666566e-04 | 0.162048 | 0.030315 | 0.083201 |
14 | 14 | 0.055556 | 0.027778 | 0.180400 | 0.083333 | 2.389538e-02 | 1.652429e-02 | 9.410870e-54 | 0.153963 | 0.031294 | 0.083201 |
5 | 5 | 0.083333 | 0.027778 | 0.237860 | 0.072831 | 1.571682e-01 | 6.587572e-02 | 5.297849e-39 | 0.154840 | 0.029100 | 0.070437 |
25 | 25 | 0.111111 | 0.027778 | 0.227738 | 0.071296 | 6.753397e-02 | 3.190944e-02 | 1.137607e-02 | 0.154372 | 0.012774 | 0.071429 |
23 | 23 | 0.027778 | 0.083333 | 0.297325 | 0.068122 | 1.795768e-01 | 6.513834e-03 | 3.318244e-02 | 0.169868 | 0.032722 | 0.067725 |
32 | 32 | 0.027778 | 0.083333 | 0.286706 | 0.068029 | 3.043469e-01 | 1.368718e-02 | 2.460250e-02 | 0.170767 | 0.037921 | 0.068188 |
27 | 27 | 0.083333 | 0.055556 | 0.219939 | 0.067460 | 8.265757e-02 | 9.241164e-02 | 5.393820e-33 | 0.163588 | 0.035682 | 0.067063 |
1 | 1 | 0.083333 | 0.055556 | 0.176435 | 0.054180 | 2.577802e-02 | 1.828642e-02 | 5.631447e-03 | 0.161621 | 0.015660 | 0.055853 |
28 | 28 | 0.027778 | 0.027778 | 0.156640 | 0.046693 | 1.104412e-02 | 2.260877e-02 | 4.963430e-03 | 0.153558 | 0.018828 | 0.046693 |
33 | 33 | 0.055556 | 0.083333 | 0.274454 | 0.041204 | 1.370632e-01 | 4.879421e-02 | 6.582502e-02 | 0.170266 | 0.032108 | 0.041402 |
15 | 15 | 0.111111 | 0.027778 | 0.227738 | 0.038413 | 6.753397e-02 | 9.332102e-02 | 1.137607e-02 | 0.154372 | 0.012774 | 0.040146 |
16 | 16 | 0.083333 | 0.027778 | 0.171717 | 0.035661 | 2.124058e-02 | 7.992499e-02 | 5.071160e-03 | 0.153943 | 0.011880 | 0.035516 |
20 | 20 | 0.055556 | 0.055556 | 0.297325 | 0.033571 | 3.360225e-01 | 2.878863e-02 | 4.753028e-02 | 0.164994 | 0.041854 | 0.033995 |
10 | 10 | 0.111111 | 0.055556 | 0.205840 | 0.024868 | 4.777348e-02 | 7.413684e-02 | 1.329709e-02 | 0.161314 | 0.012483 | 0.025529 |
18 | 18 | 0.027778 | 0.055556 | 0.167245 | 0.022354 | 1.655983e-02 | 1.504223e-02 | 8.094547e-03 | 0.160889 | 0.012118 | 0.021825 |
29 | 29 | 0.055556 | 0.055556 | 0.241437 | 0.021257 | 1.763425e-01 | 1.524491e-02 | 6.892575e-02 | 0.162920 | 0.017315 | 0.021230 |
26 | 26 | 0.027778 | 0.111111 | 0.245123 | 0.016667 | 1.013158e-01 | 1.610576e-33 | 1.020647e-01 | 0.177521 | 0.024371 | 0.017460 |
7 | 7 | 0.055556 | 0.027778 | 0.146626 | 0.015873 | 9.817187e-03 | 1.865314e-03 | 2.400721e-02 | 0.153557 | 0.008894 | 0.015873 |
3 | 3 | 0.027778 | 0.055556 | 0.185613 | 0.007275 | 2.422687e-02 | 1.990613e-04 | 9.730774e-03 | 0.161257 | 0.016060 | 0.007209 |
12 | 12 | 0.083333 | 0.000000 | 0.000000 | 0.000000 | 1.073051e-17 | 1.731810e-02 | 0.000000e+00 | 0.145860 | 0.005528 | 0.000000 |
30 | 30 | 0.000000 | 0.083333 | 0.288371 | 0.000000 | 3.225184e-01 | 0.000000e+00 | 7.181065e-02 | 0.171517 | 0.035041 | 0.000000 |
31 | 31 | 0.000000 | 0.027778 | 0.246578 | 0.000000 | 1.571682e-01 | 0.000000e+00 | 5.297849e-39 | 0.154840 | 0.029100 | 0.000000 |
2 | 2 | 0.055556 | 0.000000 | 0.000000 | 0.000000 | 1.073051e-17 | 1.458910e-02 | 0.000000e+00 | 0.145860 | 0.005528 | 0.000000 |
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
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
G=nx.florentine_families_graph()
name = "Florentine Families graph"
# 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
G=nx.karate_club_graph()
name = "Karate Club graph"
# 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
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"
# 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
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"
# 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