本文整理汇总了Python中apgl.graph.SparseGraph.SparseGraph类的典型用法代码示例。如果您正苦于以下问题:Python SparseGraph类的具体用法?Python SparseGraph怎么用?Python SparseGraph使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SparseGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testGraphDisplay
def testGraphDisplay(self):
try:
import networkx
import matplotlib
except ImportError as error:
logging.debug(error)
return
#Show
numFeatures = 1
numVertices = 20
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
ell = 2
m = 2
generator = BarabasiAlbertGenerator(ell, m)
graph = generator.generate(graph)
logging.debug((graph.degreeDistribution()))
nxGraph = graph.toNetworkXGraph()
nodePositions = networkx.spring_layout(nxGraph)
nodesAndEdges = networkx.draw_networkx(nxGraph, pos=nodePositions)
开发者ID:awj223,项目名称:Insight-Data-Engineering-Code-Challenge,代码行数:26,代码来源:BarabasiAlbertGeneratorTest.py
示例2: setUp
def setUp(self):
numpy.set_printoptions(suppress=True, precision=3)
numpy.random.seed(21)
numpy.set_printoptions(threshold=numpy.nan, linewidth=100)
#Use the example in the document
self.numVertices = 10
self.numFeatures = 2
self.graph1 = SparseGraph(VertexList(self.numVertices, self.numFeatures))
self.graph1.setVertices(range(self.numVertices), numpy.random.rand(self.numVertices, self.numFeatures))
edges = numpy.array([[0,1], [0, 2], [0,4], [0,5], [0,8], [0,9]])
self.graph1.addEdges(edges)
edges = numpy.array([[1,3], [1, 5], [1,6], [1,8], [2,9], [3,4], [3,5], [3,6], [3,7], [3,8], [3,9]])
self.graph1.addEdges(edges)
edges = numpy.array([[4,2], [4, 7], [4,9], [5,8], [6, 7]])
self.graph1.addEdges(edges)
self.graph2 = SparseGraph(VertexList(self.numVertices, self.numFeatures))
self.graph2.setVertices(range(self.numVertices), numpy.random.rand(self.numVertices, self.numFeatures))
edges = numpy.array([[0,3], [0, 4], [0,5], [0,8], [0,9], [1,2]])
self.graph2.addEdges(edges)
edges = numpy.array([[1,3], [1,5], [1, 7], [1,8], [1,9], [2,3], [2,5], [3,5], [4,5], [4,6]])
self.graph2.addEdges(edges)
edges = numpy.array([[4,9], [6, 8], [7,8], [7,9], [8, 9]])
self.graph2.addEdges(edges)
示例3: testNormalisedLaplacianRw
def testNormalisedLaplacianRw(self):
numVertices = 10
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
ell = 2
m = 2
generator = BarabasiAlbertGenerator(ell, m)
graph = generator.generate(graph)
k = 10
W = graph.getSparseWeightMatrix()
L = GraphUtils.normalisedLaplacianRw(W)
L2 = graph.normalisedLaplacianRw()
tol = 10**-6
self.assertTrue(numpy.linalg.norm(L - L2) < tol)
#Test zero rows/cols
W = scipy.sparse.csr_matrix((5, 5))
W[1, 0] = 1
W[0, 1] = 1
L = GraphUtils.normalisedLaplacianRw(W)
for i in range(2, 5):
self.assertEquals(L[i, i], 0)
示例4: testWriteToFile3
def testWriteToFile3(self):
"""
We will test out writing out some random graphs to Pajek
"""
numVertices = 20
numFeatures = 0
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
p = 0.1
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)
pw = PajekWriter()
directory = PathDefaults.getOutputDir() + "test/"
pw.writeToFile(directory + "erdosRenyi20", graph)
#Now write a small world graph
p = 0.2
k = 3
graph.removeAllEdges()
generator = SmallWorldGenerator(p, k)
graph = generator.generate(graph)
pw.writeToFile(directory + "smallWorld20", graph)
示例5: testGraphDisplay
def testGraphDisplay(self):
try:
import networkx
import matplotlib
except ImportError as error:
logging.debug(error)
return
#Show
numFeatures = 1
numVertices = 20
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
p = 0.2
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)
logging.debug((graph.getNumEdges()))
nxGraph = graph.toNetworkXGraph()
nodePositions = networkx.spring_layout(nxGraph)
nodesAndEdges = networkx.draw_networkx(nxGraph, pos=nodePositions)
ax = matplotlib.pyplot.axes()
ax.set_xticklabels([])
ax.set_yticklabels([])
示例6: testGenerate2
def testGenerate2(self):
numVertices = 20
graph = SparseGraph(GeneralVertexList(numVertices))
p = 0.2
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)
self.assertTrue((graph.getNumEdges() - p*numVertices*numVertices/2) < 8)
示例7: setUp
def setUp(self):
#Let's set up a very simple graph
numVertices = 5
numFeatures = 1
edges = []
vList = VertexList(numVertices, numFeatures)
#An undirected dense graph
self.dGraph1 = DenseGraph(vList, True)
self.dGraph1.addEdge(0, 1, 1)
self.dGraph1.addEdge(0, 2, 1)
self.dGraph1.addEdge(2, 4, 1)
self.dGraph1.addEdge(2, 3, 1)
self.dGraph1.addEdge(3, 4, 1)
#A directed sparse graph
self.dGraph2 = DenseGraph(vList, False)
self.dGraph2.addEdge(0, 1, 1)
self.dGraph2.addEdge(0, 2, 1)
self.dGraph2.addEdge(2, 4, 1)
self.dGraph2.addEdge(2, 3, 1)
self.dGraph2.addEdge(3, 4, 1)
#Now try sparse graphs
vList = VertexList(numVertices, numFeatures)
self.sGraph1 = SparseGraph(vList, True)
self.sGraph1.addEdge(0, 1, 1)
self.sGraph1.addEdge(0, 2, 1)
self.sGraph1.addEdge(2, 4, 1)
self.sGraph1.addEdge(2, 3, 1)
self.sGraph1.addEdge(3, 4, 1)
self.sGraph2 = SparseGraph(vList, False)
self.sGraph2.addEdge(0, 1, 1)
self.sGraph2.addEdge(0, 2, 1)
self.sGraph2.addEdge(2, 4, 1)
self.sGraph2.addEdge(2, 3, 1)
self.sGraph2.addEdge(3, 4, 1)
#Finally, try DictGraphs
self.dctGraph1 = DictGraph(True)
self.dctGraph1.addEdge(0, 1, 1)
self.dctGraph1.addEdge(0, 2, 2)
self.dctGraph1.addEdge(2, 4, 8)
self.dctGraph1.addEdge(2, 3, 1)
self.dctGraph1.addEdge(12, 4, 1)
self.dctGraph2 = DictGraph(False)
self.dctGraph2.addEdge(0, 1, 1)
self.dctGraph2.addEdge(0, 2, 1)
self.dctGraph2.addEdge(2, 4, 1)
self.dctGraph2.addEdge(2, 3, 1)
self.dctGraph2.addEdge(12, 4, 1)
示例8: testSequenceVectorStats
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)
示例9: cvModelSelection
def cvModelSelection(self, graph, paramList, paramFunc, folds, errorFunc):
"""
ParamList is a list of lists of parameters and paramFunc
is a list of the corresponding functions to call with the parameters
as arguments. Note that a parameter can also be a tuple which is expanded
out before the function is called.
e.g.
paramList = [[1, 2], [2, 1], [12, 1]]
paramFunc = [predictor.setC, predictor.setD]
"""
inds = Sampling.crossValidation(folds, graph.getNumEdges())
errors = numpy.zeros((len(paramList), folds))
allEdges = graph.getAllEdges()
for i in range(len(paramList)):
paramSet = paramList[i]
logging.debug("Using paramSet=" + str(paramSet))
for j in range(len(paramSet)):
if type(paramSet[j]) == tuple:
paramFunc[j](*paramSet[j])
else:
paramFunc[j](paramSet[j])
predY = numpy.zeros(0)
y = numpy.zeros(0)
j = 0
for (trainInds, testInds) in inds:
trainEdges = allEdges[trainInds, :]
testEdges = allEdges[testInds, :]
trainGraph = SparseGraph(graph.getVertexList(), graph.isUndirected())
trainGraph.addEdges(trainEdges, graph.getEdgeValues(trainEdges))
testGraph = SparseGraph(graph.getVertexList(), graph.isUndirected())
testGraph.addEdges(testEdges, graph.getEdgeValues(testEdges))
self.learnModel(trainGraph)
predY = self.predictEdges(testGraph, testGraph.getAllEdges())
y = testGraph.getEdgeValues(testGraph.getAllEdges())
#Note that the order the edges is different in testGraphs as
#opposed to graph when calling getAllEdges()
errors[i, j] = errorFunc(y, predY)
j = j+1
logging.info("Error of current fold: " + str(numpy.mean(errors[i, :])))
meanErrors = numpy.mean(errors, 1)
strErrors = numpy.std(errors, 1)
return meanErrors, strErrors
示例10: testGenerate2
def testGenerate2(self):
"""
Make sure that the generated degree is less than or equal to the given degree
"""
numVertices = 10
for i in range(10):
degSequence = numpy.random.randint(0, 3, numVertices)
generator = ConfigModelGenerator(degSequence)
graph = SparseGraph(GeneralVertexList(numVertices))
graph = generator.generate(graph)
self.assertTrue((graph.outDegreeSequence()<=degSequence).all())
#We try to match an evolving degree sequence
degSequence1 = numpy.array([0,0,1,1,1,2,2,2,3, 4])
degSequence2 = numpy.array([2,0,3,1,2,2,2,2,3, 4])
degSequence3 = numpy.array([2,1,4,1,2,2,2,2,3, 6])
generator = ConfigModelGenerator(degSequence1)
graph = SparseGraph(GeneralVertexList(numVertices))
graph = generator.generate(graph)
self.assertTrue((degSequence1>= graph.outDegreeSequence()).all())
deltaSequence = degSequence2 - graph.outDegreeSequence()
generator = ConfigModelGenerator(deltaSequence)
graph = generator.generate(graph, False)
self.assertTrue((degSequence2>= graph.outDegreeSequence()).all())
deltaSequence = degSequence3 - graph.outDegreeSequence()
generator = ConfigModelGenerator(deltaSequence)
graph = generator.generate(graph, False)
self.assertTrue((degSequence3>= graph.outDegreeSequence()).all())
示例11: testGraphInfuence
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)
示例12: testDegreeDistribution
def testDegreeDistribution(self):
numFeatures = 0
numVertices = 100
vList = VertexList(numVertices, numFeatures)
graph = SparseGraph(vList)
alpha = 10.0
p = 0.01
dim = 2
generator = GeometricRandomGenerator(graph)
graph = generator.generateGraph(alpha, p, dim)
logging.debug((graph.degreeDistribution()))
示例13: findInfoDecayGraph
def findInfoDecayGraph(self, egoTestFileName, alterTestFileName, egoIndicesR, alterIndices, egoIndicesNR, alterIndicesNR, egoFileName, alterFileName, missing=0):
(egoTestArray, egoTitles) = self.readFile(egoTestFileName, self.egoTestIds, missing=0)
(alterTestArray, alterTitles) = self.readFile(alterTestFileName, self.alterTestIds, missing=0)
egoMarks = numpy.zeros(egoTestArray.shape[0])
alterMarks = numpy.zeros(alterTestArray.shape[0])
decays = numpy.zeros(egoIndicesR.shape[0])
correctAnswers = numpy.array([1,2,4,5,8,9,10,12])
wrongAnswers = numpy.array([3, 6, 7, 11])
for i in range(egoTestArray.shape[0]):
egoMarks[i] = numpy.intersect1d(egoTestArray[i], correctAnswers).shape[0]
egoMarks[i] += wrongAnswers.shape[0] - numpy.intersect1d(egoTestArray[i], wrongAnswers).shape[0]
for i in range(alterMarks.shape[0]):
alterMarks[i] = numpy.intersect1d(alterTestArray[i], correctAnswers).shape[0]
alterMarks[i] += wrongAnswers.shape[0] - numpy.intersect1d(alterTestArray[i], wrongAnswers).shape[0]
"""
We just return how much the alter understood, since this represents the
decay in understanding of the ego and transmission.
"""
for i in range(decays.shape[0]):
decays[i] = alterMarks[alterIndices[i]]
#A lot of people could not be bothered to fill the questions and hence
#get 4 correct by default
decays = (decays) / float(self.numTestQuestions)
defaultDecay = 10**-6
#Finally, put the data into a graph
(egoArray, egoTitles) = self.readFile(egoFileName, self.egoQuestionIds, missing)
(alterArray, alterTitles) = self.readFile(alterFileName, self.alterQuestionIds, missing)
V = egoArray
V = numpy.r_[V, alterArray[alterIndices, :]]
V = numpy.r_[V, egoArray[alterIndicesNR, :]]
vList = VertexList(V.shape[0], V.shape[1])
vList.setVertices(V)
graph = SparseGraph(vList, False)
edgesR = numpy.c_[egoIndicesR, egoArray.shape[0]+numpy.arange(alterIndices.shape[0])]
graph.addEdges(edgesR, decays)
edgesNR = numpy.c_[egoIndicesNR, egoArray.shape[0]+alterIndices.shape[0]+numpy.arange(alterIndicesNR.shape[0])]
graph.addEdges(edgesNR, numpy.ones(edgesNR.shape[0]) * defaultDecay)
return graph
示例14: readFromFile
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
示例15: generate
def generate(self):
"""
Generate a Kronecker graph using the adjacency matrix of the input graph.
:returns: The generate graph as a SparseGraph object.
"""
W = self.initialGraph.adjacencyMatrix()
Wi = W
for i in range(1, self.k):
Wi = np.kron(Wi, W)
vList = VertexList(Wi.shape[0], 0)
graph = SparseGraph(vList, self.initialGraph.isUndirected())
graph.setWeightMatrix(Wi)
return graph