示例#1
0
def add_friend_links(fileName, g, edgetype):
	from get_stats import haversine

	#indices_to_remove = [ node.index for node in g.vs if node['latlng'] == None ]
	#g.delete_vertices( indices_to_remove )

	mySet = set(g.vs['name'])

	friend_edges = []
	with open(fileName) as f:
		for line in f:
			splitted = line.split('::', 2)
			if splitted[0] in mySet and splitted[1] in mySet:
				friend_edges.append( (splitted[0], splitted[1]) )

	g.add_edges( friend_edges )
	g.es['edgetype'] = [1] * len(friend_edges)
	if edgetype == 1:
		return
	if edgetype == 3:
		num_edges = 445602
		spatial_edges = []

		for i in range(num_edges):
			v1 = random.randrange( len( g.vs ) )
			v2 = random.randrange( len( g.vs ) )
			
			spatial_edges.append( (v1,v2) )

		g.add_edges( spatial_edges )
		g.es['edgetype'] = [1] * len(friend_edges) + [2] * len(spatial_edges)
			
		return
		

	"""
	spatial_edges = []
	if edgetype == 2:
		for e in g.es:
			v1 = g.vs[e.source]
			v2 = g.vs[e.target]
		
			if v1['latlng'] != None and v2['latlng'] != None and haversine( v1['latlng'], v2['latlng'] ) < 21:
				spatial_edges.append( (v1.index, v2.index) )
	"""

	spatial_edges = []
	for v1 in g.vs:
		for v2 in g.vs:
			if v1 != v2 and v1['latlng'] != None and v2['latlng'] != None \
				and haversine( v1['latlng'], v2['latlng'] ) < 5.0:
				spatial_edges.append( (v1.index, v2.index) )
	g.add_edges( spatial_edges )

	g.es['edgetype'] = [1] * len(friend_edges) + [2] * len(spatial_edges)
		
	#g.es['edgetype'] = [1] * len(friend_edges) + [2] * len(spatial_edges)

	"""
示例#2
0
def add_friend_links_OLD(fileName, g, edgetype):
	from get_stats import haversine

	edgesList = []
	edgeTypes = [] 

	mySet = set(g.vs['name'])

	if edgetype == 1:
		friend_edges = []
		with open(fileName) as f:
			for line in f:
				splitted = line.split('::', 2)
				if splitted[0] in mySet and splitted[1] in mySet:
					friend_edges.append( (splitted[0], splitted[1]) )

		g.add_edges( friend_edges )
		return
	
	with open(fileName) as f:
		for line in f:
			splitted = line.split('::', 2)
			if splitted[0] in mySet and splitted[1] in mySet:
				edgesList.append( (splitted[0], splitted[1]) )

				v1 = g.vs.find(splitted[0])
				v2 = g.vs.find(splitted[1])
				
				if v1['latlng'] != None and v2['latlng'] != None and \
						 haversine( v1['latlng'], v2['latlng'] ) < 39.0:
					edgeTypes.append(2)
				elif v1['latlng'] != None and v2['latlng'] != None and \
						 haversine( v1['latlng'], v2['latlng'] ) < 140.0:
					edgeTypes.append(3)
				else:
					edgeTypes.append(1)
				
	g.add_edges(edgesList)
	g.es['edgetype'] = edgeTypes