コード例 #1
0
ファイル: ng.py プロジェクト: kamalshadi/NDTdataProcessing
def communityGraph(fName,G,pos=None,ax=None,lbs=None):
	#for the name of the graph add .G
	# for the name of communities add .C
	wFile=os.getcwd()+'/CSV/WalkTrap/'+fName+'.C'
	wFile2=os.getcwd()+'/CSV/WalkTrap/'+fName+'.Clabel'
	ff=open(wFile2,'w')
	fn=wFile
	try:
		f=open(fn,'r')
	except IOError:
		print 'Could not open '+fn
		return
	a=sorted(G.nodes())
	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=a[int(ww)]
			ff.write(' '+n+' ')
			G.node[n]['color'] = cc
		ff.write('\n')
	print "Number of communities: "+str(C)
	if C<1:
		lab=str(C)+' community'
	else:
		lab=str(C)+' communities'
	myDraw(G,s=0,pos=pos,tit=lab,ax=ax,labels=lbs)
	print '---------------------'
	f.close()
	return pos
コード例 #2
0
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()
コード例 #3
0
ファイル: myParser.py プロジェクト: kamalshadi/MLAB
def communityGraph(fName):
	#for the name of the graph add .G
	# for the name of communities add .C
	G=nx.read_graphml("CSV/"+fName+'.G')
	a=sorted(G.nodes())
	b=[str(xx) for xx in range(len(a))]
	myDic=list2dic(b,a)
	C=0
	with open('CSV/'+fName+'.C','r') as f:
		for k,line in enumerate(f):
			C=C+1
			t1=line.strip(' {}\t\n')
			t2=t1.split(',')
			cc = pickColor(k).strip()
			for w in t2:
				n=myDic[w.strip()]
				G.node[n[0].strip()]['color'] = cc
	print "Number of communities: "+str(C)
	myDraw(G,"PIC/C_"+fName+".png")
コード例 #4
0
ファイル: myParser.py プロジェクト: kamalshadi/MLAB
def csv2gml(fName,eps=.3):
	""" this function reads csv file with heading :
		test_id  ,  minRTT  , .... ,  server
		and then makes edges between clients if their similarities
		is above eps and returns the graph """
	p = readCol(fName)
	lIP = ['a'*9]*len(p)
	lS = ['a'*9]*len(p)
	minRTT=[0]*len(p)
	sim={}
	E=[]
	axial={}
	for i,xx in enumerate(p):
		lIP[i] = xx[0].strip()
		try:
			lS[i] = ipClass(xx[-1]).sub('/24').string().strip()
			minRTT[i] = float(xx[1])
		except ValueError:
			continue
		try:
			axial[(lS[i],lIP[i])]=axial[(lS[i],lIP[i])]+1
		except KeyError:
			axial[(lS[i],lIP[i])]=1
	ll=len(axial)
	to_del=[]
	for i in range(ll):
		if axial[(lS[i],lIP[i])]<2:
			to_del.append(i)
	lS=del_indices(lS,to_del)
	lIP=del_indices(lIP,to_del)
	minRTT=del_indices(minRTT,to_del)
	myDic=list2dic(lS,zip(lIP,minRTT))
	ll=len(myDic)
	print 'Number of servers : '+ str(ll)
	if ll<2:
		return 0
	for i,w in enumerate(myDic.keys()):
		occur={}
		l=len(myDic[w])
		print '\n loop '+str(i+1)+' of '+str(ll)+' :'
		print '          '+str(l)+' clients -> '+str(l*(l-1)/2)+' loops'
		v=[ww[1] for ww in myDic[w]]
		sigma=num.std(v)
		if sigma < 1:
			continue
		for comb in combinations(myDic[w],2):
			a=comb[0][0]
			b=comb[1][0]
			if a==b:
				continue
			delta=abs(comb[0][1]-comb[1][1])
			link=(a,b)
			if link in occur.keys():
				occur[link]=occur[link]+1
			else:
				occur[link]=0
			if link not in sim.keys():
				sim[link]=[math.exp(-delta/sigma)]
			elif (link in sim.keys() and occur[link]==0):
				sim[link]=sim[link]+[math.exp(-delta/sigma)]
			else:
				pass  # This definitely has to be changed
	G=nx.Graph()
	for w in sim.keys():
		#~ G.add_node(w[0])
		#~ G.add_node(w[1])
		weight=combSum(sim[w])
		if weight > eps:
			G.add_edge(w[0],w[1],weight=weight)
	if G.size()==0 or G.order()==0:
		return 0
	G=score(G,1)  # Added robustness
	if G.size()==0 or G.order()==0:
		return 0
	if not nx.is_connected(G):
		print "Graph is not connected, Largest component is used\n"
		G=nx.connected_component_subgraphs(G)[0]
	nx.write_graphml(G,"CSV/"+fName+'.G')
	myDraw(G,'PIC/Raw_'+fName+'.png')
	return G.size()
コード例 #5
0
ファイル: ipCluster.py プロジェクト: kamalshadi/MLAB
def formCluster(fName,eps,draw=False):	
	epsilon=.5
	S=4
	minRTT=[]
	lIP=[]
	lS=[]
	sim={}
	E=[]
	city={}
	with open('csv/'+fName,'r') as f:
		val=csv.reader(f)
		i=0
		for row in val:
			if i==0:
				i=1
				continue
			if test_id_2_ip(row[0]) is None:
				continue
			lIP.append(test_id_2_ip(row[0]))
			lS.append(ipClass(row[-1]).sub('/24').string())
			minRTT.append(int(row[1]))
			city[test_id_2_ip(row[0])]=row[2]
	myDic=list2dic(lS,zip(lIP,minRTT))
	for i,w in enumerate(myDic.keys()):
		occur={}
		l=len(myDic[w])
		print '\n loop:'
		print l*(l-1)/2
		if l>500: 
			print 'Jumped'
			continue
		v=[ww[1] for ww in myDic[w]]
		sigma=num.std(v)
		print sigma
		for comb in combinations(myDic[w],2):
			a=comb[0][0]
			b=comb[1][0]
			delta=abs(comb[0][1]-comb[1][1])
			link=(a,b)
			if link in occur.keys():
				occur[link]=occur[link]+1
			else:
				occur[link]=0
			if link not in sim.keys():
				sim[link]=[math.exp(-delta/sigma)]
			elif (link in sim.keys() and occur[link]==0):
				sim[link]=sim[link]+[math.exp(-delta/sigma)]
			else:
				pass  # This definitely has to be changed
	G=nx.Graph()
	for w in sim.keys():
		G.add_node(w[0],labels=city[w[0]])
		G.add_node(w[1],labels=city[w[1]])
		weight=combSum(sim[w])
		if weight > epsilon:
			G.add_edge(w[0],w[1],width=weight)
	G=s_core(G,S)
	if draw:
		myDraw(G,'labels')
	Gn=nx.connected_components(G)
	return Gn
コード例 #6
0
ファイル: myParser.py プロジェクト: kamalshadi/NDT
def csv2gml(fName,eps=.1):
	""" this function reads csv file with heading :
		test_id  ,  minRTT  , .... ,  server
		and then makes edges between clients if their similarities
		is above eps and returns the graph """
	p = readCol(fName)
	lIP = ['a'*9]*len(p)
	lS = ['a'*9]*len(p)
	minRTT=[0]*len(p)
	sim={}
	E=[]
	axial={}
	with open('Files/serverMap','r') as f:
		st=f.read()
	serverMap=eval(st)
	uds=0
	to_del=[]
	for i,xx in enumerate(p):
		try:
			lIP[i] = ipClass(xx[0].strip()).sub('/24').string().split('/')[0].strip()
			#~ lIP[i] = xx[0].strip()
			#~ lS[i] = ipClass(xx[-1]).sub('/24').string().strip()
			lS[i]=serverMap[xx[-1]][0]
			minRTT[i] = float(xx[1])
		except ValueError:
			to_del.append(i)
			continue
		except KeyError:
			to_del.append(i)
			uds=uds+1
			continue
		try:
			axial[lIP[i]]=[lS[i]]+axial[lIP[i]]
		except KeyError:
			axial[lIP[i]]=[lS[i]]
	print 'Unrecognized test (no server info):'+str(uds)
	ll=len(p)
	for i in range(ll):
		try:
			if len(set(axial[lIP[i]])) < 2:
				to_del.append(i)
		except KeyError:
			to_del.append(i)
	lS=del_indices(lS,to_del)
	lIP=del_indices(lIP,to_del)
	minRTT=del_indices(minRTT,to_del)
	myDic=list2dic(lS,zip(lIP,minRTT))
	ll=len(myDic)
	print 'Number of servers : '+ str(ll)
	if ll<2:
		return 0
	for i,w in enumerate(myDic.keys()):
		print '--------------------------------'
		print w
		occur={}
		l=len(myDic[w])
		print 'loop '+str(i+1)+' of '+str(ll)+' :'
		print '          '+str(l)+' clients -> '+str(l*(l-1)/2)+' loops'
		print '--------------------------------'
		print '\n\n'
		v=[ww[1] for ww in myDic[w]]
		sigma=num.std(v)
		if sigma < 1:
			continue
		for comb in combinations(myDic[w],2):
			a=comb[0][0]
			b=comb[1][0]
			if a==b:
				continue
			delta=abs(comb[0][1]-comb[1][1])
			if a > b :
				link=(a,b)
			else:
				link=(b,a)
			#~ if link in occur.keys():
				#~ occur[link]=occur[link]+1
			#~ else:
				#~ occur[link]=0
			#~ if link not in sim.keys():
				#~ sim[link]=[math.exp(-delta/sigma)]
			#~ elif (link in sim.keys() and occur[link]==0):
				#~ sim[link]=sim[link]+[math.exp(-delta/sigma)]
			#~ else:
				#~ pass  # This definitely has to be changed
			try:
				occur[link]=occur[link]+1
			except KeyError:
				occur[link]=0
			try:
				if occur[link]==0:
					sim[link]=sim[link]+[math.exp(-delta/sigma)]
				else:
					sim[link][-1]=max(sim[link][-1],math.exp(-delta/sigma))
			except KeyError:
				sim[link]=[math.exp(-delta/sigma)]
	G=nx.Graph()
	for w in sim.keys():
		weight=combSum(sim[w])   # weighting function 1
		#~ if len(sim[w]) < 2:			# weighting function 2
			#~ weight=0
		#~ else:
			#~ weight=float(num.mean(sim[w]))
		if weight > eps:
			G.add_edge(w[0],w[1],weight=weight)
	if G.size()==0 or G.order()==0:
		return 0
	#~ G=score(G,1)  # Added robustness
	#~ if G.size()==0 or G.order()==0:
		#~ print 'SCORE Nullification'
		#~ return 0
	if not nx.is_connected(G):
		print "Graph is not connected, Largest component is used\n"
		G=nx.connected_component_subgraphs(G)[0]
	nx.write_graphml(G,"CSV/"+fName+'.G')
	myDraw(G,'PIC/Raw_'+fName+'.png')
	return G.size()
コード例 #7
0
		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('====================>')