本文整理汇总了Python中apgl.graph.SparseGraph.SparseGraph.addEdge方法的典型用法代码示例。如果您正苦于以下问题:Python SparseGraph.addEdge方法的具体用法?Python SparseGraph.addEdge怎么用?Python SparseGraph.addEdge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类apgl.graph.SparseGraph.SparseGraph
的用法示例。
在下文中一共展示了SparseGraph.addEdge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testIndicesFromScores
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testIndicesFromScores(self):
numVertices = 10
numFeatures = 1
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(0, 3)
graph.addEdge(1, 2)
graph.addEdge(3, 4)
graph.addEdge(5, 6)
graph.addEdge(4, 6)
graph.addEdge(9, 8)
graph.addEdge(9, 7)
graph.addEdge(9, 6)
windowSize = 10
predictor = RandomEdgePredictor(windowSize)
predictor.learnModel(graph)
scores = numpy.random.randn(numVertices)
ind = 0
p, s = predictor.indicesFromScores(ind , scores)
self.assertTrue(p.shape[0] == windowSize)
self.assertTrue(s.shape[0] == windowSize)
infIndices = p[numpy.nonzero(s==-float('Inf'))]
self.assertTrue((numpy.sort(infIndices) == numpy.sort(graph.neighbours(ind))).all())
示例2: testDegreeDistribution
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testDegreeDistribution(self):
#We want to see how the degree distribution changes with kronecker powers
numVertices = 3
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
initialGraph = SparseGraph(vList)
initialGraph.addEdge(0, 1)
initialGraph.addEdge(1, 2)
for i in range(numVertices):
initialGraph.addEdge(i, i)
logging.debug((initialGraph.outDegreeSequence()))
logging.debug((initialGraph.degreeDistribution()))
k = 2
generator = StochasticKroneckerGenerator(initialGraph, k)
graph = generator.generateGraph()
logging.debug((graph.outDegreeSequence()))
logging.debug((graph.degreeDistribution()))
k = 3
generator = StochasticKroneckerGenerator(initialGraph, k)
graph = generator.generateGraph()
logging.debug((graph.degreeDistribution()))
示例3: testCvModelSelection
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testCvModelSelection(self):
numVertices = 10
numFeatures = 1
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(0, 3)
graph.addEdge(1, 2)
graph.addEdge(3, 4)
graph.addEdge(5, 6)
graph.addEdge(4, 6)
graph.addEdge(9, 8)
graph.addEdge(9, 7)
graph.addEdge(9, 6)
windowSize = 3
predictor = RandomEdgePredictor(windowSize)
folds = 5
paramList = [[1, 2], [2, 1], [12, 1]]
paramFunc = [predictor.setC, predictor.setD]
errors = predictor.cvModelSelection(graph, paramList, paramFunc, folds)
self.assertTrue(errors.shape[0] == len(paramList))
for i in range(errors.shape[0]):
self.assertTrue(errors[i]>= 0 and errors[i]<= 1)
示例4: testSequenceVectorStats
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testSequenceVectorStats(self):
numFeatures = 1
numVertices = 10
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
graph.addEdge(0, 2)
graph.addEdge(0, 1)
subgraphIndices = [[0, 1, 3], [0, 1, 2, 3]]
growthStatistics = GraphStatistics()
statsList = growthStatistics.sequenceVectorStats(graph, subgraphIndices)
示例5: testNativeAdjacencyMatrix
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testNativeAdjacencyMatrix(self):
numVertices = 10
graph = SparseGraph(GeneralVertexList(numVertices))
graph.addEdge(1, 1, 0.1)
graph.addEdge(1, 3, 0.5)
graph.addEdge(2, 5, 1)
graph.addEdge(7, 0, 2)
A = graph.nativeAdjacencyMatrix()
self.assertEquals(A[0, 7], 1)
self.assertEquals(A[7, 0], 1)
self.assertEquals(A[1, 3], 1)
self.assertEquals(A[3, 1], 1)
self.assertEquals(A[1, 1], 1)
self.assertEquals(A[2, 5], 1)
self.assertEquals(A[5, 2], 1)
self.assertEquals(A.getnnz(), 7)
graph = SparseGraph(GeneralVertexList(numVertices), False)
graph.addEdge(1, 1, 0.1)
graph.addEdge(1, 3, 0.5)
graph.addEdge(2, 5, 1)
A = graph.nativeAdjacencyMatrix()
self.assertEquals(A[1, 3], 1)
self.assertEquals(A[1, 1], 1)
self.assertEquals(A[2, 5], 1)
self.assertEquals(A.getnnz(), 3)
示例6: readFromFile
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def readFromFile(self, fileName):
"""
Read vertices and edges of the graph from the given file name. The file
must have as its first line "Vertices" followed by a list of
vertex indices (one per line). Then the lines following "Arcs" or "Edges"
have a list of pairs of vertex indices represented directed or undirected
edges.
"""
infile = open(fileName, "r")
line = infile.readline()
line = infile.readline()
ind = 0
vertexIdDict = {}
while infile and line != "Edges" and line != "Arcs":
vertexIdDict[int(line)] = ind
line = infile.readline().strip()
ind += 1
if line == "Edges":
undirected = True
elif line == "Arcs":
undirected = False
else:
raise ValueError("Unknown edge types: " + line)
numVertices = len(vertexIdDict)
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
sGraph = SparseGraph(vList, undirected)
line = infile.readline()
while line:
s = line.split()
try:
i = vertexIdDict[int(s[0].strip(',').strip())]
j = vertexIdDict[int(s[1].strip(',').strip())]
k = float(s[2].strip(',').strip())
except KeyError:
print("Vertex not found in list of vertices.")
raise
sGraph.addEdge(i, j, k)
line = infile.readline()
logging.info("Read graph with " + str(numVertices) + " vertices and " + str(sGraph.getNumEdges()) + " edges")
return sGraph
示例7: testGenerate
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testGenerate(self):
k = 2
numVertices = 1000
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
initialGraph = SparseGraph(vList)
initialGraph.addEdge(0, 1)
initialGraph.addEdge(1, 2)
for i in range(numVertices):
initialGraph.addEdge(i, i)
generator = KroneckerGenerator(initialGraph, k)
graph = generator.generate()
print (graph.size)
示例8: testSequenceScalarStats
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testSequenceScalarStats(self):
numFeatures = 1
numVertices = 10
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
graph.addEdge(0, 2)
graph.addEdge(0, 1)
subgraphIndices = [[0, 1, 3], [0, 1, 2, 3]]
growthStatistics = GraphStatistics()
statsArray = growthStatistics.sequenceScalarStats(graph, subgraphIndices)
self.assertTrue(statsArray[0, 0] == 3.0)
self.assertTrue(statsArray[1, 0] == 4.0)
self.assertTrue(statsArray[0, 1] == 1.0)
self.assertTrue(statsArray[1, 1] == 2.0)
示例9: fullTransGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def fullTransGraph(self):
"""
This function will return a new graph which contains a directed edge if
a transmission will occur between two vertices.
"""
if self.iteration != 0:
raise ValueError("Must run fullTransGraph before advanceGraph")
#First, find all the edges in the graph and create an ExampleList
numEdges = self.edges.shape[0]
X = numpy.zeros((numEdges*2, self.numPersonFeatures*2))
ind = 0
for i in range(numEdges):
vertex1 = self.graph.getVertex(self.edges[i,0])
vertex2 = self.graph.getVertex(self.edges[i,1])
X[ind, :] = numpy.r_[vertex1[0:self.numPersonFeatures], vertex2[0:self.numPersonFeatures]]
X[ind+numEdges, :] = numpy.r_[vertex2[0:self.numPersonFeatures], vertex1[0:self.numPersonFeatures]]
ind = ind + 1
name = "X"
examplesList = ExamplesList(X.shape[0])
examplesList.addDataField(name, X)
examplesList.setDefaultExamplesName(name)
if self.preprocessor != None:
X = self.preprocessor.process(examplesList.getDataField(examplesList.getDefaultExamplesName()))
examplesList.overwriteDataField(examplesList.getDefaultExamplesName(), X)
y = self.egoPairClassifier.classify(examplesList.getSampledDataField(name))
fullTransmissionGraph = SparseGraph(self.graph.getVertexList(), False)
transIndices = numpy.nonzero(y==1)[0]
#Now, write out the transmission graph
for i in range(len(transIndices)):
if transIndices[i] < numEdges:
fullTransmissionGraph.addEdge(self.edges[transIndices[i],0], self.edges[transIndices[i],1])
else:
fullTransmissionGraph.addEdge(self.edges[transIndices[i]-numEdges,1], self.edges[transIndices[i]-numEdges,0])
return fullTransmissionGraph
示例10: readFromFile
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def readFromFile(self, fileName):
inFile = open(fileName,"r")
numFeatures = 1
graphList = []
line = inFile.readline()
while line != "":
#First 3 lines are useless
inFile.readline()
inFile.readline()
#4th line has edge information
line = inFile.readline()
valueList = line.split(None)
numVertices = int(valueList[0])
#Not strictly the number of edges, as molecules can have multiple edges
#between a pair of atoms
numEdges = int(valueList[1])
vList = VertexList(numVertices, numFeatures)
for i in range(numVertices):
line = inFile.readline()
valueList = line.split(None)
vList.setVertex(i, numpy.array([self.atomDict[valueList[3]]]))
graph = SparseGraph(vList)
for i in range(numEdges):
line = inFile.readline()
valueList = line.split(None)
graph.addEdge(int(valueList[0])-1, int(valueList[1])-1)
graphList.append(graph)
#Ignore next two lines
inFile.readline()
inFile.readline()
line = inFile.readline()
return graphList
示例11: testGenerateGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testGenerateGraph(self):
k = 2
numVertices = 3
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
initialGraph = SparseGraph(vList)
initialGraph.addEdge(0, 1)
initialGraph.addEdge(1, 2)
for i in range(numVertices):
initialGraph.addEdge(i, i)
d = initialGraph.diameter()
degreeSequence = initialGraph.outDegreeSequence()
generator = StochasticKroneckerGenerator(initialGraph, k)
graph = generator.generateGraph()
d2 = graph.diameter()
degreeSequence2 = graph.outDegreeSequence()
self.assertTrue((numpy.kron(degreeSequence, degreeSequence) == degreeSequence2).all())
self.assertTrue(graph.getNumVertices() == numVertices**k)
self.assertTrue(graph.getNumDirEdges() == initialGraph.getNumDirEdges()**k)
self.assertEquals(d, d2)
#Try different k
k = 3
generator.setK(k)
graph = generator.generateGraph()
d3 = graph.diameter()
degreeSequence3 = graph.outDegreeSequence()
self.assertTrue((numpy.kron(degreeSequence, degreeSequence2) == degreeSequence3).all())
self.assertTrue(graph.getNumVertices() == numVertices**k)
self.assertTrue(graph.getNumDirEdges() == initialGraph.getNumDirEdges()**k)
self.assertEquals(d, d3)
#Test the multinomial degree distribution
logging.debug(degreeSequence)
logging.debug(degreeSequence2)
logging.debug(degreeSequence3)
示例12: testGraphInfuence
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testGraphInfuence(self):
#We test the influence using a real graph
numVertices = 5
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
sGraph = SparseGraph(vList, False)
sGraph.addEdge(0, 1, 0.1)
sGraph.addEdge(0, 2, 0.5)
sGraph.addEdge(1, 3, 0.9)
sGraph.addEdge(2, 3, 0.7)
sGraph.addEdge(2, 4, 0.8)
P = sGraph.maxProductPaths()
self.assertTrue((P[0, :] == numpy.array([0,0.1, 0.5, 0.35, 0.4])).all())
self.assertTrue((P[1, :] == numpy.array([0,0,0,0.9,0])).all())
self.assertTrue((P[2, :] == numpy.array([0,0,0,0.7,0.8])).all())
self.assertTrue((P[3, :] == numpy.array([0,0,0,0,0])).all())
self.assertTrue((P[4, :] == numpy.array([0,0,0,0,0])).all())
k = 5
influence = GreedyInfluence()
inds = influence.maxInfluence(P, k)
示例13: testConcat
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testConcat(self):
numVertices = 5
graph = SparseGraph(GeneralVertexList(numVertices))
graph.addEdge(1, 1, 0.1)
graph.addEdge(1, 3, 0.5)
graph.addEdge(2, 4, 1)
graph.addEdge(2, 3, 2)
graph.setVertex(0, "abc")
graph2 = SparseGraph(GeneralVertexList(numVertices))
graph2.addEdge(1, 1)
graph2.addEdge(1, 4)
graph2.setVertex(1, "def")
graph3 = graph.concat(graph2)
self.assertTrue(graph3.getNumVertices, 10)
self.assertEquals(graph3.getVertex(0), "abc")
self.assertEquals(graph3.getVertex(6), "def")
self.assertEquals(graph3.getEdge(1, 1), 0.1)
self.assertEquals(graph3.getEdge(1, 3), 0.5)
self.assertEquals(graph3.getEdge(2, 4), 1)
self.assertEquals(graph3.getEdge(2, 3), 2)
self.assertEquals(graph3.getEdge(6, 6), 1)
self.assertEquals(graph3.getEdge(6, 9), 1)
示例14: testKwayNormalisedCut
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testKwayNormalisedCut(self):
numVertices = 6
graph = SparseGraph(GeneralVertexList(numVertices))
graph.addEdge(0, 1)
graph.addEdge(0, 2)
graph.addEdge(2, 1)
graph.addEdge(3, 4)
graph.addEdge(3, 5)
graph.addEdge(5, 4)
W = graph.getWeightMatrix()
clustering = numpy.array([0,0,0, 1,1,1])
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 0.0)
#Try sparse W
Ws = scipy.sparse.csr_matrix(W)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 0.0)
graph.addEdge(2, 3)
W = graph.getWeightMatrix()
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 1.0/7)
Ws = scipy.sparse.csr_matrix(W)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 1.0/7)
clustering = numpy.array([0,0,0, 1,1,2])
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 61.0/105)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 61.0/105)
#Test two vertices without any edges
W = numpy.zeros((2, 2))
clustering = numpy.array([0, 1])
self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 0.0)
Ws = scipy.sparse.csr_matrix(W)
self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 0.0)
示例15: testFullTransGraph
# 需要导入模块: from apgl.graph.SparseGraph import SparseGraph [as 别名]
# 或者: from apgl.graph.SparseGraph.SparseGraph import addEdge [as 别名]
def testFullTransGraph(self):
transGraph = self.egoSimulator.fullTransGraph()
#Create a simple graph and deterministic classifier
numExamples = 10
numFeatures = 3
#Here, the first element is gender (say) with female = 0, male = 1
vList = VertexList(numExamples, numFeatures)
vList.setVertex(0, numpy.array([0,0,1]))
vList.setVertex(1, numpy.array([1,0,0]))
vList.setVertex(2, numpy.array([1,0,0]))
vList.setVertex(3, numpy.array([1,0,0]))
vList.setVertex(4, numpy.array([0,0,1]))
vList.setVertex(5, numpy.array([0,0,1]))
vList.setVertex(6, numpy.array([0,0,0]))
vList.setVertex(7, numpy.array([1,0,0]))
vList.setVertex(8, numpy.array([0,0,1]))
vList.setVertex(9, numpy.array([1,0,0]))
sGraph = SparseGraph(vList)
sGraph.addEdge(0, 1, 1)
sGraph.addEdge(0, 2, 1)
sGraph.addEdge(0, 3, 1)
sGraph.addEdge(4, 5, 1)
sGraph.addEdge(4, 6, 1)
sGraph.addEdge(6, 7, 1)
sGraph.addEdge(6, 8, 1)
sGraph.addEdge(6, 9, 1)
simulator = EgoSimulator(sGraph, self.dc)
logging.debug("Writing out full transmission graph")
transGraph = simulator.fullTransGraph()
self.assertEquals(transGraph.isUndirected(), False)
self.assertEquals(transGraph.getNumEdges(), 11)
self.assertEquals(transGraph.getEdge(0,1), 1)
self.assertEquals(transGraph.getEdge(0,2), 1)
self.assertEquals(transGraph.getEdge(0,3), 1)
self.assertEquals(transGraph.getEdge(4,5), 1)
self.assertEquals(transGraph.getEdge(4,6), 1)
self.assertEquals(transGraph.getEdge(5,4), 1)
self.assertEquals(transGraph.getEdge(6,4), 1)
self.assertEquals(transGraph.getEdge(6,7), 1)
self.assertEquals(transGraph.getEdge(6,8), 1)
self.assertEquals(transGraph.getEdge(6,9), 1)
self.assertEquals(transGraph.getEdge(8,6), 1)
self.assertEquals(transGraph.getVertexList(), vList)