本文整理汇总了Python中apgl.graph.DictGraph.DictGraph.getSparseWeightMatrix方法的典型用法代码示例。如果您正苦于以下问题:Python DictGraph.getSparseWeightMatrix方法的具体用法?Python DictGraph.getSparseWeightMatrix怎么用?Python DictGraph.getSparseWeightMatrix使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apgl.graph.DictGraph.DictGraph
的用法示例。
在下文中一共展示了DictGraph.getSparseWeightMatrix方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testGetSparseWeightMatrix
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getSparseWeightMatrix [as 别名]
def testGetSparseWeightMatrix(self):
graph = DictGraph()
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d")
graph.addEdge("d", "e")
W = graph.getSparseWeightMatrix()
keys = graph.getAllVertexIds()
for i in range(len(keys)):
for j in range(len(keys)):
if W[i, j] == 1:
self.assertEquals(graph.getEdge(keys[i], keys[j]), 1)
else:
self.assertEquals(graph.getEdge(keys[i], keys[j]), None)
#Try a directed graph
graph = DictGraph(False)
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d")
graph.addEdge("d", "e")
W = graph.getSparseWeightMatrix()
for i in range(len(keys)):
for j in range(len(keys)):
if W[i, j] == 1:
self.assertEquals(graph.getEdge(keys[i], keys[j]), 1)
else:
self.assertEquals(graph.getEdge(keys[i], keys[j]), None)
示例2: testGetSparseWeightMatrix
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getSparseWeightMatrix [as 别名]
def testGetSparseWeightMatrix(self):
graph = DictGraph()
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d", "blah")
graph.addEdge("d", "e", -1.1)
graph.addEdge("c", "b", 2)
W = graph.getSparseWeightMatrix()
keys = graph.getAllVertexIds()
for i in range(len(keys)):
for j in range(len(keys)):
if graph.edgeExists(keys[i], keys[j]) and not isinstance(graph.getEdge(keys[i], keys[j]), numbers.Number):
self.assertEquals(1, W[i, j])
elif W[i, j] != 0:
self.assertEquals(graph.getEdge(keys[i], keys[j]), W[i, j])
else:
self.assertEquals(graph.getEdge(keys[i], keys[j]), None)
#Try a directed graph
graph = DictGraph(False)
graph.addEdge("a", "b")
graph.addEdge("a", "c", "test")
graph.addEdge("a", "d")
graph.addEdge("d", "e")
graph.addEdge("c", "a", 0.1)
W = graph.getSparseWeightMatrix()
for i in range(len(keys)):
for j in range(len(keys)):
if graph.edgeExists(keys[i], keys[j]) and not isinstance(graph.getEdge(keys[i], keys[j]), numbers.Number):
self.assertEquals(1, W[i, j])
elif W[i, j] != 0:
self.assertEquals(graph.getEdge(keys[i], keys[j]), W[i, j])
else:
self.assertEquals(graph.getEdge(keys[i], keys[j]), None)
示例3: __init__
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getSparseWeightMatrix [as 别名]
def __init__(self, minGraphSize=500, maxGraphSize=None, dayStep=30):
dataDir = PathDefaults.getDataDir() + "cluster/"
edgesFilename = dataDir + "Cit-HepTh.txt"
dateFilename = dataDir + "Cit-HepTh-dates.txt"
#Note the IDs are integers but can start with zero so we prefix "1" to each ID
edges = []
file = open(edgesFilename, 'r')
file.readline()
file.readline()
file.readline()
file.readline()
for line in file:
(vertex1, sep, vertex2) = line.partition("\t")
vertex1 = vertex1.strip()
vertex2 = vertex2.strip()
edges.append([vertex1, vertex2])
#if vertex1 == vertex2:
# print(vertex1)
file.close()
logging.info("Loaded edge file " + str(edgesFilename) + " with " + str(len(edges)) + " edges")
#Keep an edge graph
graph = DictGraph(False)
graph.addEdges(edges)
logging.info("Created directed citation graph with " + str(graph.getNumEdges()) + " edges and " + str(graph.getNumVertices()) + " vertices")
#Read in the dates articles appear in a dict which used the year and month
#as the key and the value is a list of vertex ids. For each month we include
#all papers uploaded that month and those directed cited by those uploads.
startDate = datetime.date(1990, 1, 1)
file = open(dateFilename, 'r')
file.readline()
numLines = 0
subgraphIds = []
for line in file:
(id, sep, date) = line.partition("\t")
id = id.strip()
date = date.strip()
inputDate = datetime.datetime.strptime(date.strip(), "%Y-%m-%d")
inputDate = inputDate.date()
if graph.vertexExists(id):
tDelta = inputDate - startDate
graph.vertices[id] = tDelta.days
subgraphIds.append(id)
#If a paper cites another, it must have been written before
#the citing paper - enforce this rule.
for neighbour in graph.neighbours(id):
if graph.getVertex(neighbour) == None:
graph.setVertex(neighbour, tDelta.days)
subgraphIds.append(neighbour)
elif tDelta.days < graph.getVertex(neighbour):
graph.setVertex(neighbour, tDelta.days)
numLines += 1
file.close()
subgraphIds = set(subgraphIds)
graph = graph.subgraph(list(subgraphIds))
logging.debug(graph)
logging.info("Loaded date file " + str(dateFilename) + " with " + str(len(subgraphIds)) + " dates and " + str(numLines) + " lines")
W = graph.getSparseWeightMatrix()
W = W + W.T
vList = VertexList(W.shape[0], 1)
vList.setVertices(numpy.array([graph.getVertices(graph.getAllVertexIds())]).T)
#Note: we have 16 self edges and some two-way citations so this graph has fewer edges than the directed one
self.graph = SparseGraph(vList, W=W)
logging.debug(self.graph)
#Now pick the max component
components = self.graph.findConnectedComponents()
self.graph = self.graph.subgraph(components[0])
logging.debug("Largest component graph: " + str(self.graph))
self.minGraphSize = minGraphSize
self.maxGraphSize = maxGraphSize
self.dayStep = dayStep