/
visTools.py
78 lines (73 loc) · 1.83 KB
/
visTools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from graphvisu import myDraw
import networkx as nx
import pylab as pl
import os
from myBasic import pickColor
def communityGraph(fName,w,pos=None,s=1):
#for the name of the graph add .G
# for the name of communities add .C
tit=w.replace('.G','').replace('s','/')
gFile=os.getcwd()+'/CSV/Graphs/'+fName+'/'+w+'.G'
pDir=os.getcwd()+'/PIC/'+fName
wFile=os.getcwd()+'/CSV/WalkTrap/'+fName+'/'+w+'.C'
if (not os.path.exists(gFile)) or (not os.path.exists(wFile)):
print 'Error: '+gFile+' or '+wFile+' not found'
return 101
if (not os.path.exists(pDir)):
os.mkdir(pDir)
print '--------------------'
G=nx.read_graphml(gFile)
fn=wFile
try:
f=open(fn,'r')
except IOError:
print 'Could not open '+fn
return
a=sorted(G.nodes())
#~ b=[str(xx) for xx in range(len(a))]
#~ myDic=list2dic(b,a)
C=0
for k,line in enumerate(f):
C=C+1
t1=line.strip(' {}\t\n')
t2=t1.split(',')
cc = pickColor(k).strip()
for ww in t2:
#~ n=myDic[ww.strip()]
n=a[int(ww)]
#~ G.node[n[0].strip()]['color'] = cc
G.node[n]['color'] = cc
print w
print "Number of communities: "+str(C)
if C<1:
lab=str(C)+' community'
else:
lab=str(C)+' communities'
if pos is None:
myDraw(G,pDir+"/"+w+".png",s)
else:
myDraw(G,pDir+"/"+w+".png",s,pos=pos,tit=tit+'\n'+lab)
print '---------------------'
f.close()
#~ raw_input('=============>')
if __name__=='__main__':
fName='ndt201311'
pD='PIC/'+fName
if os.path.exists(pD):
print 'Remove '+pD+' for PIC/ then rerun'
else:
os.mkdir(pD)
fn=[]
if fn==[]:
for u1,u2,u3 in os.walk('CSV/WalkTrap/'+fName+'/'):
fn=[xx.replace('.w','') for xx in u3 if ('.w' in xx)]
break
for w in fn:
gF='CSV/Graphs/'+fName+'/'+w.replace('/','s')+'.G'
pl.figure()
pl.subplot(121)
G=nx.read_graphml(gF)
pos=myDraw(G)
pl.subplot(122)
communityGraph(fName,w,pos)
raw_input('====================>')