from IPython.display import Image
%matplotlib inline
%load_ext sage
%load_ext autoreload
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib
import random
from sage.graphs.graph_coloring import vertex_coloring
%autoreload 2
from chAs import create_conn_random_graph_chrom,create_conn_random_graph,draw_domcomms
nodes=30
p=0.15
G=create_conn_random_graph(nodes,p)
g=Graph(G)
vc=vertex_coloring(g, value_only=False)
print 'An example of a connected E-R random graph with n =', nodes, 'and p =', "%.2f" % p
print
print 'G is connected:', nx.is_connected(G)
print 'Number of vertices of G:', len(G.nodes())
print 'Number of edges of G:', len(G.edges())
print 'List of edges of random graph G:'
print G.edges()
# print vc
%autoreload 2
from chAs import create_conn_random_graph_chrom,draw_domcomms,modul_arity
import community as comm
cn=vertex_coloring(g, value_only=True)
collpar={}
for v,i in enumerate(vc):
for j in i:
collpar[j]=v
part=comm.best_partition(G)
print 'Chromatic number of G =',cn
print 'Chromatic partition of G:'
print vc
print 'Chromatic modularity of G =', "%.4f" % modul_arity(G,'vertex_colors')
print
print 'Community number of G =', max(part.values())+1
for nd in G.nodes():
G.add_node(nd,vertex_colors=collpar[nd],comm_coll=part[nd])
print 'Community partition of G:'
parLis=[]
partdi={}
for i,k in part.items():
if k not in partdi:
partdi[k]=[i]
else:
partdi[k].append(i)
for i,k in partdi.items():
parLis.append(k)
print parLis
for i,v in enumerate(parLis):
gg=Graph(G.subgraph(v))
print 'Community %i (as a subgraph) has chromatic number %i' %(i,vertex_coloring(gg, value_only=True))
print 'Community modularity of G =', "%.4f" % comm.modularity(part,G)
d=.8
dd=.8
c=1.2
cc=1.4
alpha=0.2
ealpha=.2
vcc={}
for i,nd in enumerate(vc):
for ndd in nd:
vcc[ndd]=i
draw_domcomms(G,G.nodes(),[],[],[] ,vcc,part,d,dd,c,cc,alpha,ealpha)
import pickle
import networkx as nx
lg = pickle.load(open('lazega_graph.txt'))
# print nx.info(lg)
G = nx.Graph(lg)
# print nx.info(G)
# print G.nodes()
nodes = G.nodes()
nodes.sort()
# print nodes
# print len(nodes)
llist=[]
xlist=[]
for i in nodes:
if 'l' in i:
llist.append(i)
if 'x' in i:
xlist.append(i)
# print llist
# print xlist
# print len(llist)
# print len(xlist)
names = llist+xlist
# print names
# print len(names)
idies = range(208)
mapping_dict={}
for i in range(len(names)):
mapping_dict[names[i]] = idies[i]
# print mapping_dict
nx.relabel_nodes(G, mapping_dict, copy=False)
# print G.nodes()
%autoreload 2
from chAs import plot_paral
layer1=range(82)
layer2=range(82,208)
# d1=0.6 #1.5
# d2=15. # 5.
# d3=0.3 #0.8
nodesize=200
withlabels=False
edgelist=[]
layout=True
alpha=0.25
plot_paral(G,layer1,layer2,d1=1.,d2=11.,d3=.8,d4=.5,d5=.5,d6=7,nodesize=50,withlabels=True,edgelist=[],layout=True,alpha=0.5)
F=nx.Graph(G)
print nx.isolates(F)
F.remove_nodes_from(nx.isolates(F))
layer1=range(82)
fl1=nx.subgraph(F,layer1)
F.remove_nodes_from(nx.isolates(fl1))
fl1.remove_nodes_from(nx.isolates(fl1))
# fl1=nx.subgraph(F,layer1)
print nx.is_connected(fl1)
layer2=range(82,208)
fl2=nx.subgraph(F,layer2)
F.remove_nodes_from(nx.isolates(fl2))
fl2.remove_nodes_from(nx.isolates(fl2))
print nx.is_connected(fl2)
layer1=fl1.nodes()
layer2=fl2.nodes()
print
print "INSTITUTIONS (UPPER LAYER) ARE DENOTED AS SQUARES"
print
print layer1
print
print "RESEARCHERS (LOWER LAYER) ARE DENOTED AS CIRCLES"
print
print layer2
# d1=0.6 #1.5
# d2=15. # 5.
# d3=0.3 #0.8
# print type(F)
nodesize=200
withlabels=False
edgelist=[]
layout=True
alpha=0.25
plot_paral(F,layer1,layer2,d1=1.,d2=9.,d3=.8,d4=1.,d5=.5,d6=11,nodesize=200,withlabels=True,edgelist=[],layout=True,alpha=0.5)
%autoreload 2
from chAs import create_conn_random_graph_chrom,draw_domcomms_sr,modul_arity,plot_paral_chr_comm
import community as comm
g=Graph(F)
cn=vertex_coloring(g, value_only=True)
# print 'done',cn
vc=vertex_coloring(g,value_only=False)
# print 'done'
collpar={}
for v,i in enumerate(vc):
for j in i:
collpar[j]=v
part=comm.best_partition(F)
print 'Chromatic number of F =',cn
print 'Chromatic partition of F:'
print vc
print 'Chromatic modularity of F =', "%.4f" % modul_arity(F,'vertex_colors')
print
print 'Community number of F =', max(part.values())+1
for nd in F.nodes():
F.add_node(nd,vertex_colors=collpar[nd],comm_coll=part[nd])
print 'Community partition of F:'
parLis=[]
partdi={}
for i,k in part.items():
if k not in partdi:
partdi[k]=[i]
else:
partdi[k].append(i)
for i,k in partdi.items():
parLis.append(k)
print parLis
for i,v in enumerate(parLis):
gg=Graph(F.subgraph(v))
print 'Community %i (as a subgraph) has chromatic number %i' %(i,vertex_coloring(gg, value_only=True))
print 'Community modularity of F =', "%.4f" % comm.modularity(part,F)
d=.6
dd=.8
c=1.2
cc=1.4
alpha=.1
ealpha=.1
nodesize=20
vcc={}
for i,nd in enumerate(vc):
for ndd in nd:
vcc[ndd]=i
draw_domcomms_sr(F,layer1,layer2,F.nodes(),[],[],[] ,vcc,part,d,dd,c,cc,alpha,ealpha,nodesize=nodesize)
nodesize=200
withlabels=False
edgelist=[]
layout=True
alpha=0.125
plot_paral_chr_comm(F,layer1,layer2,vc,parLis,d1=.75,d2=9.,d3=.8,d4=1.,d5=.75,d6=11,nodesize=200,withlabels=False,edgelist=[],layout=True,alpha=0.15)
%autoreload 2
from syntheticThreeLayerGraph import synthetic_three_level, plot_graph_comms
n1=n2=n3=50
p1=p2=p3=0.1
q1=q2=q3=0.01
H1,H2,H3,G,J,FF,DD,edgeList = synthetic_three_level(n1,n2,n3,p1,p2,p3,q1,q2,q3,no_isolates=True)
g=Graph(G)
vc=vertex_coloring(g, value_only=False)
print 'An example of a 3-layer E-R random graph G with n =', n1+n2+n3, 'p =', "%.2f," % p1, 'q =', "%.2f" % q1, \
'and maximum degree =', max(G.degree().values())
print 'G is connected:', nx.is_connected(G)
print
print 'Chromatic number of the 3-layer graph G is %i' %len(vc)
gg=Graph(J)
print 'Top left layer (as a subgraph) has chromatic number %i' %vertex_coloring(gg, value_only=True)
gg=Graph(FF)
print 'Top right layer (as a subgraph) has chromatic number %i' %vertex_coloring(gg, value_only=True)
gg=Graph(DD)
print 'Bottom layer (as a subgraph) has chromatic number %i' %vertex_coloring(gg, value_only=True)
part=comm.best_partition(G)
print
print 'Community number of the 3-layer graph G is %i' %len(set(part.values()))
parLis=[]
partdi={}
for i,k in part.items():
if k not in partdi:
partdi[k]=[i]
else:
partdi[k].append(i)
for i,k in partdi.items():
parLis.append(k)
pos=plot_graph_comms(G,J,FF,DD,n1,n2,n3,vc,parLis,d1=1,d2=10.,d3=0.8,nodesize=150,withlabels=False,edgelist=edgeList,layout=False,b_alpha=0.25)