Beispiel #1
0
	def GenerateRank(self, partitionMethod = "ConnectedComponents"):
		timeSpan = StringProcessing.GetTimeSpan(self.firstStartTime, self.firstEndTime)
		nodesDict = InitialData.InitialNodesPairWeightDict(self.firstStartTime, self.firstEndTime)
		nodesDictLabel = InitialData.InitialNodesPairWeightDict(self.secondStartTime, self.secondEndTime)
		if partitionMethod == "ConnectedComponents":
			connectedComponents = ConnectedComponents.ReadAllConnectedComponentsFromFile(self.firstStartTime, self.firstEndTime)
		elif partitionMethod == "CommunityDetection":
			connectedComponents = CommunityDetection.ReadCommunitiesFromFile(self.firstStartTime, self.firstEndTime)
		else:
			connectedComponents = LinkClustering.ReadAllConnectedComponentsFromFile(self.firstStartTime, self.firstEndTime)
		pairDict = {}
		y_true = []
		y_score = []
		vectorsDict = {}
		for nodes in connectedComponents:
			print("Components size = %d"%len(nodes))
			
			G = nx.Graph()
			componentNodesDict = {}
			for i in nodes:
				if i not in componentNodesDict:
					componentNodesDict[i] = []
				for j in nodesDict[i]:
					if j in nodes:
						componentNodesDict[i].append(j)
						G.add_edge(i, j)

			for i in xrange(0, len(nodes)):
				startTime = time.time()
				for j in xrange(i + 1, len(nodes)):
					if nodes[j] not in componentNodesDict[nodes[i]]:
						if nodes[i] not in pairDict:
							pairDict[nodes[i]] = {}
						pairDict[nodes[i]][nodes[j]] = 1
						# startTime = time.time()
						iNeighbors = []
						for neighbor in nodesDict[nodes[i]]:
							iNeighbors.append(neighbor)
						jNeighbors = []
						for neighbor in nodesDict[nodes[j]]:
							jNeighbors.append(neighbor)
						intersect = set(iNeighbors)&set(jNeighbors)
						y_score.append(len(intersect))
						if nodes[i] in nodesDictLabel and nodes[j] in nodesDictLabel[nodes[i]]:
							y_true.append(1)
						else:
							y_true.append(-1)
						# endTime = time.time()
						# print("find paths time:%f"%(endTime - startTime))
						
				endTime = time.time()
				print("nodes[%s] generated, finished in %f s"%(nodes[i], endTime - startTime))
		components = ConnectedComponents.ReadAllConnectedComponentsFromFile(self.firstStartTime, self.firstEndTime)
		for component in components:
			for i in component:
				for j in component:
					if i != j:
						if i not in pairDict or (i in pairDict and j not in pairDict[i]):
							if i not in nodesDict or (i in nodesDict and j not in nodesDict[i]):
								if i in nodesDictLabel and j in nodesDictLabel[i]:
									y_true.append(1)
								else:
									y_true.append(-1)
								y_score.append(0)
		return y_true, y_score