本文整理汇总了Python中apgl.graph.DictGraph.DictGraph.getEdge方法的典型用法代码示例。如果您正苦于以下问题:Python DictGraph.getEdge方法的具体用法?Python DictGraph.getEdge怎么用?Python DictGraph.getEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apgl.graph.DictGraph.DictGraph
的用法示例。
在下文中一共展示了DictGraph.getEdge方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testGetAllEdgeIndices
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
def testGetAllEdgeIndices(self):
graph = DictGraph()
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d")
graph.addEdge("d", "e")
edgeIndices = graph.getAllEdgeIndices()
keys = graph.getAllVertexIds()
self.assertEquals(edgeIndices.shape[0], graph.getNumEdges())
for i in range(edgeIndices.shape[0]):
self.assertTrue(graph.getEdge(keys[int(edgeIndices[i, 0])], keys[edgeIndices[i, 1]]) == 1)
graph = DictGraph(False)
graph.addEdge("a", "b")
graph.addEdge("b", "a")
graph.addEdge("a", "c")
graph.addEdge("a", "d")
graph.addEdge("d", "e")
edgeIndices = graph.getAllEdgeIndices()
keys = graph.getAllVertexIds()
self.assertEquals(edgeIndices.shape[0], graph.getNumEdges())
for i in range(edgeIndices.shape[0]):
self.assertTrue(graph.getEdge(keys[int(edgeIndices[i, 0])], keys[edgeIndices[i, 1]]) == 1)
示例2: testGetWeightMatrix
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
def testGetWeightMatrix(self):
graph = DictGraph()
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d")
graph.addEdge("d", "e")
W = graph.getWeightMatrix()
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.getWeightMatrix()
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)
示例3: testAddEdges
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
def testAddEdges(self):
graph = DictGraph()
edgeList = [(1, 2), (2, 1), (5, 2), (8, 8)]
graph.addEdges(edgeList)
self.assertEquals(graph.getNumEdges(), 3)
self.assertEquals(graph.getEdge(1, 2), 1)
self.assertEquals(graph.getEdge(5, 2), 1)
self.assertEquals(graph.getEdge(2, 1), 1)
self.assertEquals(graph.getEdge(8, 8), 1)
edgeValues = [1, 2, 3, 4]
graph.addEdges(edgeList, edgeValues)
self.assertEquals(graph.getEdge(1, 2), 2)
self.assertEquals(graph.getEdge(5, 2), 3)
self.assertEquals(graph.getEdge(2, 1), 2)
self.assertEquals(graph.getEdge(8, 8), 4)
#Now test directed graphs
graph = DictGraph(False)
graph.addEdges(edgeList)
self.assertEquals(graph.getNumEdges(), 4)
self.assertEquals(graph.getEdge(1, 2), 1)
self.assertEquals(graph.getEdge(5, 2), 1)
self.assertEquals(graph.getEdge(2, 1), 1)
self.assertEquals(graph.getEdge(8, 8), 1)
edgeValues = [1, 2, 3, 4]
graph.addEdges(edgeList, edgeValues)
self.assertEquals(graph.getEdge(1, 2), 1)
self.assertEquals(graph.getEdge(5, 2), 3)
self.assertEquals(graph.getEdge(2, 1), 2)
self.assertEquals(graph.getEdge(8, 8), 4)
示例4: testGetEdge
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
def testGetEdge(self):
dictGraph = DictGraph(True)
dictGraph.addEdge(1, 2, 12)
self.assertEquals(dictGraph.getEdge(1, 2), 12)
self.assertEquals(dictGraph.getEdge(2, 1), 12)
self.assertEquals(dictGraph.getEdge(2, 2), None)
self.assertRaises(ValueError, dictGraph.getEdge, 5, 8)
dictGraph = DictGraph(False)
dictGraph.addEdge(1, 2, 12)
self.assertEquals(dictGraph.getEdge(1, 2), 12)
self.assertEquals(dictGraph.getEdge(2, 1), None)
示例5: testAddEdge
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
def testAddEdge(self):
dictGraph = DictGraph()
dictGraph.addEdge("A", "B", [1,2,3])
dictGraph.addEdge("A", "C", "HelloThere")
dictGraph.addEdge(12, 8, [1,2,3, 12])
self.assertEquals(dictGraph.getEdge("A", "B"), [1,2,3])
self.assertEquals(dictGraph.getEdge("B", "A"), [1,2,3])
self.assertEquals(dictGraph.getEdge("A", "C"), "HelloThere")
self.assertEquals(dictGraph.getEdge("C", "A"), "HelloThere")
self.assertEquals(dictGraph.getEdge(12, 8), [1,2,3, 12])
self.assertEquals(dictGraph.getEdge(8, 12), [1,2,3, 12])
dictGraph.addEdge(2, 8)
dictGraph = DictGraph(False)
dictGraph.addEdge("A", "B", [1,2,3])
dictGraph.addEdge("A", "C", "HelloThere")
dictGraph.addEdge(12, 8, [1,2,3, 12])
self.assertEquals(dictGraph.getEdge("A", "B"), [1,2,3])
self.assertEquals(dictGraph.getEdge("B", "A"), None)
self.assertEquals(dictGraph.getEdge("A", "C"), "HelloThere")
self.assertEquals(dictGraph.getEdge("C", "A"), None)
self.assertEquals(dictGraph.getEdge(12, 8), [1,2,3, 12])
self.assertEquals(dictGraph.getEdge(8, 12), None)
dictGraph.addEdge(2, 8)
示例6: testRemoveEdge
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
def testRemoveEdge(self):
dictGraph = DictGraph()
dictGraph.addEdge(1, 2, 12)
dictGraph.addEdge(1, 3, 18)
dictGraph.addEdge(3, 4, 1)
self.assertEquals(dictGraph.getEdge(1, 2), 12)
self.assertEquals(dictGraph.getEdge(1, 3), 18)
self.assertEquals(dictGraph.getEdge(3, 4), 1)
dictGraph.removeEdge(1, 3)
self.assertEquals(dictGraph.getEdge(1, 3), None)
self.assertEquals(dictGraph.getEdge(1, 2), 12)
self.assertEquals(dictGraph.getEdge(3, 4), 1)
#Some tests on directed graphs
dictGraph = DictGraph(False)
dictGraph.addEdge(1, 2, 12)
dictGraph.addEdge(2, 1, 12)
dictGraph.removeEdge(1, 2)
self.assertEquals(dictGraph.getEdge(1, 2), None)
self.assertEquals(dictGraph.getEdge(2, 1), 12)
示例7: testGetSparseWeightMatrix
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [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)
示例8: DatedPurchasesGraphListIterator
# 需要导入模块: from apgl.graph.DictGraph import DictGraph [as 别名]
# 或者: from apgl.graph.DictGraph.DictGraph import getEdge [as 别名]
class DatedPurchasesGraphListIterator(object):
def __init__(self, purchasesByWeek, nb_purchases_per_it=None):
"""
The background graph is a bi-partite graph of purchases. Purchases are
grouped by date (week by week), and we consider the graph of purchases
between first week and $i$-th week.
The returned graph considers only users and counts the number of common
purchases between two users.
Purchases are given in a list of [user, prod, week, year] with increasing
date.
nb_purchases_per_it is the maximum number of purchases to put in each
week (if there is more, randomly split the week). None corresponds to
no-limit case.
"""
# args
self.group_by_iterator = DatedPurchasesGroupByIterator(purchasesByWeek, nb_purchases_per_it)
# init variables
self.dictUser = MyDictionary()
self.dictProd = MyDictionary()
for user, prod, week, year in purchasesByWeek:
self.dictUser.index(user)
self.dictProd.index(prod)
self.backgroundGraph = DictGraph(False) # directed
self.W = scipy.sparse.csr_matrix((len(self.dictUser), len(self.dictUser)), dtype='int16')
self.usefullEdges = numpy.array([])
def __iter__(self):
return self
def __next__(self):
# next group of purchases (StopIteration is raised here)
purchases_sublist = next(self.group_by_iterator)
#logging.debug(" nb purchases: " + str(len(purchases_sublist)))
# to check that the group really induces new edges
W_has_changed = False
# update graphs adding current-week purchases
for user, prod, week, year in purchases_sublist:
# update only if this purchase is seen for the first time
try:
newEdge = not self.backgroundGraph.getEdge(prod, user)
except ValueError:
newEdge = True
if newEdge:
self.backgroundGraph.addEdge(prod, user)
newCommonPurchases = self.backgroundGraph.neighbours(prod)
# print prod, newCommonPurchases
for neighbour in filter(lambda neighbour: neighbour != user, newCommonPurchases):
W_has_changed = True
self.W[neighbour, user] += 1
self.W[user, neighbour] += 1
# the returned graph will be restricted to usefull edges
currentUsefullEdges = numpy.array(self.W.sum(1)).ravel().nonzero()[0]
newUsefullEdges = numpy.setdiff1d(currentUsefullEdges, self.usefullEdges)
self.usefullEdges = numpy.r_[self.usefullEdges, newUsefullEdges]
if W_has_changed:
return self.W[self.usefullEdges,:][:,self.usefullEdges]
else:
return next(self)
next = __next__