Esempio n. 1
0
def growSub(br,g='/24'):
	if br is None:
		return None
	ll=len(br)
	brr=[['0.0.0.0','0.0.0.0']]*ll
	for i,w in enumerate(br):
		temp=list(w)
		pref,l=commonPrefix(w)
		if l <= int(g.strip('/')):
			brr[i]=w
		else:
			subg=subnet(pref)
			while (l > int(g.strip('/'))):
				subg=subg.grow()
				l=l-1
				net=ix.ip_network(unicode(subg.string()))
				if (ll==1):
					temp=[subg.first(),subg.last()]
				elif (i==0 and (ix.ip_address(unicode(ipClass(br[i+1][0]).first(g))) not in net)):
					temp=[subg.first(),subg.last()]
				elif(i==(ll-1) and (ix.ip_address(unicode(ipClass(br[i-1][1]).last(g))) not in net)):
					temp=[subg.first(),subg.last()]
				elif ((ix.ip_address(unicode(ipClass(br[i-1][1]).last(g))) not in net) and (ix.ip_address(unicode(ipClass(br[i+1][0]).first(g))) not in net)):
					temp=[subg.first(),subg.last()]
				else : break				
				brr[i]=temp
	return brr
Esempio n. 2
0
def bracket2sub(bracket,a1,b1,g):
	gi=int(g.strip('/'))
	# all IPs should be in form of strings
	out=''
	if a1 is None:
		a=-1
	else:
		a=int(ix.ip_address(unicode(a1)))
	if b1 is None:
		b=2**33
	else:
		b=int(ix.ip_address(unicode(b1)))
	sub,pl=commonPrefix(bracket)
	rootsub=subnet(sub)
	re=ipClass(rootsub.first()).int()
	le=ipClass(rootsub.last()).int()
	root=ix.ip_network(unicode(sub))
	if ((re <= a) or (le >= b)):
		if pl < gi :
			L,R=rootsub.split()
			brL=[L.first(),L.last()]
			brR=[R.first(),R.last()]
			out=(out+' U '+bracket2sub(brL,a1,b1,g)).strip('U ')
			out=(out+' U '+bracket2sub(brR,a1,b1,g)).strip('U ')
		else:
			return out
	else:
		return rootsub.string()
	out=out.strip('U ')
	return out
Esempio n. 3
0
def bracketIP(V,b,g='/24'):
	# Boundry IPs must be revisited e.g. 255.255.255.xxx and 0.0.0.xxx
	# V is value list (IP string) and 'b' is cluster labels
	# output is a list of list and a list of clusters
	# g is maximum gap allowed in subnet
	a=[int(ix.ip_address(unicode(xx))) for xx in V]
	V=[ipClass(xx) for xx in V]
	l=len(a)
	if l!=len(b):
		print 'length error in input of bracketing'
		return
	if l==0:
		print 'IP set is empty'
		return [None,None]
	unused,b=order(a,b)
	a,V=order(a,V)
	prev=b[0]
	p=V[0].string()
	prevV=V[0].next(g)
	if prevV is not None:
		prevV_int=int(ix.ip_address(unicode(prevV)))
	else:
		prevV_int=2**32
	outB=[]
	outC=[]
	s=V[0].string()
	bb=0
	for i in range(len(a)):
		curV=V[i].first(g)
		curV_int=int(ix.ip_address(unicode(curV)))
		if (b[i]!=prev or ((curV_int) > prevV_int)):
			outB.append(temp)
			outC.append(prev)
			s=V[i].string()
			prevV=V[i].next(g)
			p=V[i].string()
			prevV_int=int(ix.ip_address(unicode(prevV)))
			prev=b[i]
			temp=[s,p]
			bb=1
			if i==len(a)-1:
				outB.append([s,p])
				outC.append(prev)
		else:
			bb=0
			prevV=V[i].next(g)
			p=V[i].string()
			prevV_int=int(ix.ip_address(unicode(prevV)))
			prev=b[i]
			temp=[s,p]
	if bb==0:
		outB.append([s,p])
		outC.append(prev)
	else:
		pass

	return [outB,outC]
Esempio n. 4
0
def countAxial(fName):
	lIP=clientRead(fName)
	sIP1=serverRead(fName)
	sIP=[ipClass(xx).sub('/24') for xx in sIP1]
	myDic=list2dic(lIP,sIP)
	nIP=len(myDic)
	val=myDic.values()
	temp=[1 for w in val if len(set(w))>1]
	nA=len(temp)
	return [nIP,nA]
Esempio n. 5
0
def joinBrIP(br,lb):
	b=list(br)
	ll=len(br)
	for i in range(ll):
		b[i]=[ipClass(xx).int() for xx in br[i]]
	temp,lbr=joinBr(b,lb)
	l=len(temp)
	brr=[['0.0.0.0','0.0.0.0']]*l
	for i,w in enumerate(temp):
		brr[i]=[str(ix.ip_address(w[0])),str(ix.ip_address(w[1]))]
	return [brr,lbr]
Esempio n. 6
0
def IPaxVis(b,c,y=1,anot=True):
	#b is ip bracket
	#c is ip cluster
	bb=list(b)
	for i in range(len(b)):
		bb[i]=[ipClass(xx).int() for xx in b[i]]
	cc=['#999999','#FF3300','#99CC00','#FFFF00','#996666','#003300',
	'#3399FF','#000000','#FF00CC','#333333']
	colors=dict(zip(sorted(list(set(c))),cc))
	mark=[]
	mM=[]
	for i,w in enumerate(bb):
		if w[0]==w[1]:
			mark.append(w[0])
			mM.append(c[i])
			line=pl.plot(w[0],y,'*')
			if anot:
				pl.annotate(str(c[i]), xy=(w[0], y), xytext=(w[0], y+.1),rotation=90,
				size=20,arrowprops=dict(facecolor=colors[c[i]], shrink=0.05,ec="none")
				)
		else:
			line=pl.plot(w,[y,y])
			if anot:
				pl.annotate(str(c[i]), xy=(0.5*(w[0]+w[1]), y), xytext=(0.5*(w[0]+w[1]), y+.1),rotation=90,
				size=20,arrowprops=dict(facecolor=colors[c[i]], shrink=0.05,ec="none"),
				)
		if i==32:
			print 'Go'
		pl.setp(line,lw=10,ms=15,c=colors[c[i]],mfc=colors[c[i]],mew=0)
		frame1=pl.gca()
		#~ frame1.axes.get_xaxis().set_visible(False)
	lm=len(mark)
	if lm>0:
		for i,w in enumerate(mark):
			line=pl.plot(w,y,'*')
			if anot:
				pl.annotate(str(mM[i]), xy=(w, y), xytext=(w, y+.1),rotation=90,
				size=20,arrowprops=dict(facecolor=colors[mM[i]], shrink=0.05,ec="none")
				)
			pl.setp(line,ms=15,mfc=colors[mM[i]],mew=0)
	frame1.axes.get_yaxis().set_visible(False)
Esempio n. 7
0
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()
Esempio n. 8
0
def scatterValue(fName,C={}):
	lS1=serverRead(fName)
	lS=[ipClass(xx).sub('/24').string() for xx in lS1]
	myDic=list2dic(lS)
	dim1=''
	dim2=''
	n1=0
	n2=0
	for w in myDic.keys():
		if myDic[w]>n1:
			dim1=w
			n1=myDic[w]
		elif myDic[w]>n2:
			dim2=w
			n2=myDic[w]
		else:
			pass
	lIP={}
	h=0
	with open('csv/'+fName,'r') as f:
		val=csv.reader(f)
		for row in val:
			if h==0:
				h=1
				continue
			if ipClass(row[-1]).sub('/24').string()==dim1:
				ip=test_id_2_ip(row[0])
				if ip is None : continue
				if ip not in lIP.keys():
					lIP[ip]=[int(row[1]),None]
				else:
					lIP[ip][0]=int(row[1])
			elif ipClass(row[-1]).sub('/24').string()==dim2:
				ip=test_id_2_ip(row[0])
				if ip is None : continue
				if ip not in lIP.keys():
					lIP[ip]=[None,int(row[1])]
				else:
					lIP[ip][1]=int(row[1])
			else:
				pass
	x=[]
	y=[]
	lab=[]
	cc=['#999999','#FF3300','#99CC00','#FFFF00','#996666','#003300',
	'#3399FF','#000000','#FF00CC','#333333']
	pl.figure()
	for u in lIP.keys():
		found=False
		w=lIP[u]
		if (w[0] is None or w[1] is None): continue
		if C!={}:
			for k,li in enumerate(C):
				if u in li:
					co=cc[k]
					found=True
					break
			if found:
				pl.plot(w[0],w[1],'o',color=co)
			else:
				pl.plot(w[0],w[1],'*',color='black')
		else:
			x.append(w[0])
			y.append(w[1])
			lab.append(u)
	if C=={}:
		pl.scatter(x,y)
		#~ pl.annotate(lab, xy=(x, y), xytext=(-20,20))
	pl.show()
Esempio n. 9
0
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
Esempio n. 10
0
def cardinal(br):
	a=0
	for w in br:
		a=a+ipClass(w[1]).int()-ipClass(w[0]).int()+1
	return a
Esempio n. 11
0
def cluster2sub(C,g_in='/24',g_out=None):
	if g_out is None:
		g_out=g_in
	#C is a list of lists, each item being a cluster
	labels1=[[str(i)]*len(w) for i,w in enumerate(C)]
	labels=flatten(labels1)
	V=flatten(C)
	N1,pN = commonPrefix(V) #tentative
	N=subnet(N1) #tentative
	br,lb=bracketIP(V,labels,g_in)
	br=growSub(br,g_out)
	sub1=['0']*len(br)
	sub=['0']*len(set(lb))
	for i,w in enumerate(br):
		abr=ipClass(w[0])
		bbr=ipClass(w[1])
		a1=abr.prev(g_out)
		b1=bbr.next(g_out)
		if i==0:
			a_p = ipClass(N.first()).prev('/32') #tentative
		else :
			a_p = br[i-1][1]
		if i==len(br)-1:
			b_n = ipClass(N.last()).next('/32') #tentative
		else:
			b_n = br[i+1][0]
		if ((a1 is not None) and (a_p is not None)):
			if ipClass(a1).int() > ipClass(a_p).int() :
				a = a1
			else:
				a = a_p
		elif ((a1 is None) and (a_p is not None)):
			a=a_p
		elif ((a1 is not None) and (a_p is None)):
			a=a1
		else:
			a=None
		if ((b1 is not None) and (b_n is not None)):
			if ipClass(b1).int() > ipClass(b_n).int() :
				b = b_n
			else :
				b = b1
		elif ((b1 is None) and (b_n is not None)):
			b=b_n
		elif ((b1 is not None) and (b_n is None)):
			b=b1
		else:
			b=None
		sub1[i]=bracket2sub(w,a,b,g_out)
	dic=list2dic(lb,sub1)
	fl=['0']*len(dic.keys())
	for i,w in enumerate(dic.keys()):
		temp1=list(dic[w])
		to_del=[]
		for j in range(len(temp1)):
			if temp1[j].find('/')==-1:
				to_del.append(j)
		temp1=del_indices(temp1,to_del)
		sub[i]=' U '.join(temp1)
		fl[i]=w
	fl,sub=order(fl,sub)
	return sub
def NG(fName,prefix,eps=.4):
	lIP1,minRTT1,lS=prefQuery(fName,prefix)
	N=len(lIP1)
	lIP=['0']*N
	minRTT=[0.0]*N
	sim={}
	E=[]
	axial={}
	uds=0
	to_del=[]
	ippat=re.compile('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
	for i in range(N):
		ip=lIP1[i]
		rtt=minRTT1[i]
		m=re.search(ippat,ip)
		if m is None:
			to_del.append(i)
			continue
		try:
			lIP[i] = ipClass(ip.strip()).sub('/24').string().split('/')[0].strip()
			minRTT[i]=rtt
		except ValueError:
			print 'Value Error for ip: '+ip
			to_del.append(i)
			continue
		try:
			axial[lIP[i]]=[lS[i]]+axial[lIP[i]]
		except KeyError:
			axial[lIP[i]]=[lS[i]]
	invalid=len(to_del)
	for i in range(N):
		try:
			if len(set(axial[lIP[i]])) < 2:
				to_del.append(i)
		except KeyError:
			invalid=invalid+1
			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 'No.  invalid tests: '+str(invalid)
	print 'ratio of axial tests: '+str(len(lIP))+' / '+str(len(lIP1)-invalid)
	print 'Number of servers : '+ str(ll)
	if ll<2:
		print 'Not enough servers'
		print '|||||||||||||||||||||||||||||||||||||||'
		return None
	for i,w in enumerate(myDic.keys()):
		print '--------------------------------'
		print 'Server: '+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)

			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()
	print 'Parameter eps in building Graph: '+str(eps)
	for w in sim.keys():
		weight=combSum(sim[w])   # weighting function 1
		if weight > eps:
			G.add_edge(w[0],w[1],weight=weight)
	if G.size()==0 or G.order()==0:
		print 'Graph could not be formed.'
		print '||||||||||||||||||||||||||||||||||'
		return None

	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,'Debug/'+prefix.replace('/','s')+'.G')
	#~ myDraw(G,picDir+'/Raw_'+prefixOn.replace('/','s')+'.png')
	print prefix+' Specs:'
	print 'Size of Graph: '+str(G.size())
	print 'Order of Graph: '+str(G.order())		
Esempio n. 13
0
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()
Esempio n. 14
0
from ipClass import *
from subnet import *
import pylab as pl
import numpy as num
from ipCluster import *
if __name__=='__main__':
	lse=[]
	lsb=[]
	oo=[]
	with open('Inputs/comcast','r') as f:
		st=f.read()
	ip=st.split(',')
	for xx in ip:
		a=subnet(xx.strip())
		aa=ipClass(a.first())
		az=ipClass(a.last())
		lse.append(az.int())
		lsb.append(aa.int())
	b=sorted(lsb)
	e=sorted(lse)
	l=len(b)
	clus=[('0','0')]*l
	state_end=-1
	state_beg=-1
	for i,st in enumerate(b):
		if i==l-1:
			pass
		if e[i]>state_end and st> state_beg:
			state_end=e[i]
			state_beg=st
			clus[i][0]=st
Esempio n. 15
0
def prefix_break(P):
	# P is list of prefixes
	S=[(ipClass(subnet(xx).first()).int(),0) for xx in list(set(P))]
	E=[(ipClass(subnet(xx).last()).int(),1) for xx in list(set(P))]
	A=sorted(S+E)
	ipx,fg=zip(*A)
	ipB=[]
	q=list2Queue(list(ipx),list(fg))
	j=-1
	ns=0
	while not q.empty():
		j=j+1
		x=q.get()
		if j==0:
			S=x[-1]
			a=x[0]
			l=x[1]
			ns=ns+l
			j=j+1
			continue
		Sn=x[-1]
		an=x[0]
		l=x[1]
		if S==0 and Sn==0:
			ipB.append([a,an-1])
			ns=ns+l
			a=an
		elif S==0 and Sn==1:
			ipB.append([a,an])
			ns=ns-l
			if ns>0:
				a=an+1
			else:
				a=None
			S=Sn
		elif S==1 and Sn==1:
			ipB.append([a,an])
			ns=ns-l
			if ns>0 :
				a=an+1
			else:
				a=None
		else:
			if ns > 0:
				if a is None :
					print 'ip Break code has a bug:Report'
				else:
					ipB.append([a,an-1])
					a=an
					ns=ns+l
			else:
				ns=ns+l
				a=an
			S=0
	ipR=[]
	for w in ipB:
		b1=ipClass(w[0]).string()
		b2=ipClass(w[1]).string()
		bw=[b1,b2]
		if w[0]==0:
			a=None
		else:
			a=ipClass(w[0]-1).string()
		if w[1]==2*32-1:
			b=None
		else:
			b=ipClass(w[1]+1).string()
		ipR=ipR+[xx.strip() for xx in bracket2sub(bw,a,b,'/24').split('U')]
	return ipR
Esempio n. 16
0
def csv2gml(fName,eps=0):
	th=600 ### parameter for prolific prefix
	print 'Finding prolific Prefixes with more than '+str(th)+' tests...'
	prolificPrefix=ppf(fName,th)
	if prolificPrefix==-1:
		print 'Error(csv2gml): Database not found'
		return -1
	grDir=os.getcwd()+"/CSV/Graphs/"+fName

	if os.path.exists(grDir):
		print 'Directory:'+grDir+' already exists'
		print 'Please remove '+fName+' directory and rerun'
		return -1
	else:
		os.mkdir(grDir)

	lll=len(prolificPrefix)
	ippat=re.compile('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
	for qqq,prefixOn in enumerate(prolificPrefix):
		print '|||||||||||||||||||||||||||||||||||||||||||||'
		print '=================> '+str(qqq+1)+' / '+str(lll)+' <==================='
		print 'Graph formation for prefix: '+prefixOn
		lIP1,minRTT1,lS=prefQuery(fName,prefixOn)
		N=len(lIP1)
		lIP=['0']*N
		minRTT=[0.0]*N
		sim={}
		E=[]
		axial={}
		uds=0
		to_del=[]
		for i in range(N):
			ip=lIP1[i]
			rtt=minRTT1[i]
			m=re.search(ippat,ip)
			if m is None:
				to_del.append(i)
				continue
			try:
				lIP[i] = ipClass(ip.strip()).sub('/24').string().split('/')[0].strip()
				minRTT[i]=rtt
			except ValueError:
				print 'Value Error for ip: '+ip
				to_del.append(i)
				continue
			try:
				axial[lIP[i]]=[lS[i]]+axial[lIP[i]]
			except KeyError:
				axial[lIP[i]]=[lS[i]]
		invalid=len(to_del)
		for i in range(N):
			try:
				if len(set(axial[lIP[i]])) < 2:
					to_del.append(i)
			except KeyError:
				invalid=invalid+1
				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 'No.  invalid tests: '+str(invalid)
		print 'ratio of axial tests: '+str(len(lIP))+' / '+str(len(lIP1)-invalid)
		print 'Number of servers : '+ str(ll)
		if ll<2:
			print 'Not enough servers'
			print '|||||||||||||||||||||||||||||||||||||||'
			continue
		for i,w in enumerate(myDic.keys()):
			print '--------------------------------'
			print 'Server: '+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)

				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()
		print 'Parameter eps in building Graph: '+str(eps)
		for w in sim.keys():
			weight=combSum(sim[w])   # weighting function 1
			if weight > eps:
				G.add_edge(w[0],w[1],weight=weight)
		if G.size()==0 or G.order()==0:
			print 'Graph could not be formed.'
			print '||||||||||||||||||||||||||||||||||'
			continue

		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,grDir+'/'+prefixOn.replace('/','s')+'.G')
		#~ myDraw(G,picDir+'/Raw_'+prefixOn.replace('/','s')+'.png')
		print prefixOn+' Specs:'
		print 'Size of Graph: '+str(G.size())
		print 'Order of Graph: '+str(G.order())