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)
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)
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)
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
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())
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)
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)
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)
def testGenerate(self): numFeatures = 1 numVertices = 20 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) ell = 2 m = 0 generator = BarabasiAlbertGenerator(ell, m) graph = generator.generate(graph) self.assertEquals(graph.getNumEdges(), 0) ell = 5 graph.removeAllEdges() generator.setEll(ell) graph = generator.generate(graph) self.assertEquals(graph.getNumEdges(), 0) #Now test case where we m != 0 ell = 2 m = 1 graph.removeAllEdges() generator.setEll(ell) generator.setM(m) graph = generator.generate(graph) self.assertEquals(graph.getNumEdges(), (numVertices - ell) * m) m = 2 graph.removeAllEdges() generator.setM(m) graph = generator.generate(graph) self.assertEquals(graph.getNumEdges(), (numVertices - ell) * m)
def testMeanSeqScalarStats(self): numFeatures = 1 numVertices = 10 vList = VertexList(numVertices, numFeatures) p = 0.1 generator = ErdosRenyiGenerator(p) numGraphs = 50 graphList = [] subgraphIndices = [list(range(3)), list(range(6)), list(range(10))] for i in range(numGraphs): graph = generator.generate(SparseGraph(vList)) graphList.append((graph, subgraphIndices)) growthStatistics = GraphStatistics() meanStats, stdStats = growthStatistics.meanSeqScalarStats(graphList) #Check some of the stats for i in range(len(subgraphIndices)): self.assertEquals(meanStats[i, growthStatistics.numVerticesIndex], len(subgraphIndices[i])) self.assertEquals(stdStats[i, growthStatistics.numVerticesIndex], 0) self.assertAlmostEquals(meanStats[i, growthStatistics.numEdgesIndex], 0.5*(len(subgraphIndices[i])*(len(subgraphIndices[i])-1))*p, places=0)
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())
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)
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)
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 = KroneckerGenerator(initialGraph, k) graph = generator.generate() logging.debug((graph.outDegreeSequence())) logging.debug((graph.degreeDistribution())) k = 3 generator = KroneckerGenerator(initialGraph, k) graph = generator.generate() logging.debug((graph.degreeDistribution()))
def testEvaluate(self): numVertices = 6 numFeatures = 1 vList = VertexList(numVertices, numFeatures) g1 = DenseGraph(vList) g1.addEdge(0, 1) g1.addEdge(2, 1) g1.addEdge(3, 1) g1.addEdge(4, 1) g1.addEdge(5, 2) g2 = DenseGraph(vList) g2.addEdge(0, 2) g2.addEdge(1, 2) g2.addEdge(3, 2) g2.addEdge(4, 2) g2.addEdge(5, 1) g3 = DenseGraph(vList) g3.addEdge(0, 1) g4 = SparseGraph(vList) g4.addEdge(0, 1) g4.addEdge(2, 1) g4.addEdge(3, 1) g4.addEdge(4, 1) g4.addEdge(5, 2) lmbda = 0.01 pgk = RandWalkGraphKernel(lmbda) logging.debug((pgk.evaluate(g1, g1))) logging.debug((pgk.evaluate(g1, g2))) logging.debug((pgk.evaluate(g2, g1))) logging.debug((pgk.evaluate(g2, g2))) logging.debug((pgk.evaluate(g1, g3))) logging.debug((pgk.evaluate(g3, g3))) #Tests - graph kernel is symmetric, permutations of indices are identical self.assertAlmostEquals(pgk.evaluate(g1, g2), pgk.evaluate(g2, g1), places=6) self.assertAlmostEquals(pgk.evaluate(g1, g3), pgk.evaluate(g3, g1), places=6) self.assertAlmostEquals(pgk.evaluate(g1, g1), pgk.evaluate(g1, g2), places=6) self.assertAlmostEquals(pgk.evaluate(g2, g4), pgk.evaluate(g1, g2), places=6) #All evaluation of themselves are above zero self.assertTrue(pgk.evaluate(g1, g1) >= 0) self.assertTrue(pgk.evaluate(g2, g2) >= 0) self.assertTrue(pgk.evaluate(g3, g3) >= 0)
def setUp(self): self.numVertices = 100 self.numFeatures = 2 p = 0.1 k = 10 self.vList = VertexList(self.numVertices, self.numFeatures) self.graph = SparseGraph(self.vList) self.swg = SmallWorldGenerator(p, k)
def testTreeDepth(self): numVertices = 4 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList, False) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(2, 3) self.assertEquals(GraphUtils.treeDepth(graph), 2) numVertices = 5 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList, False) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(2, 3) graph.addEdge(3, 4) self.assertEquals(GraphUtils.treeDepth(graph), 3)
def __init__(self, vList, maxEdgeTypes, undirected=True): Parameter.checkInt(maxEdgeTypes, 1, float('inf')) self.vList = vList self.undirected = undirected self.maxEdgeTypes = maxEdgeTypes self.sparseGraphs = [] for i in range(maxEdgeTypes): self.sparseGraphs.append(SparseGraph(vList, undirected))
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)
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()))
def testUnNormSpectralClusterer(self): numVertices = 10 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) #We form two cliques with an edge between then graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(0, 3) graph.addEdge(1, 2) graph.addEdge(1, 3) graph.addEdge(2, 3) graph.addEdge(2, 4) graph.addEdge(4, 5) graph.addEdge(4, 6) graph.addEdge(4, 7) graph.addEdge(5, 6) graph.addEdge(5, 7) graph.addEdge(6, 7) graph.addEdge(7, 8) graph.addEdge(7, 9) #graph.addEdge(0, 4) k = 3 clusterer = SpectralClusterer(k) clusters = clusterer.cluster(graph) self.assertEquals(clusters.shape[0], numVertices) self.assertEquals(numpy.unique(clusters).shape[0], k) logging.debug(clusters) realClusters = numpy.array([1,1,1,1, 0,0,0,0, 2,2]) similarityMatrix1 = numpy.zeros((numVertices, numVertices)) similarityMatrix2 = numpy.zeros((numVertices, numVertices)) for i in range(numVertices): for j in range(numVertices): if clusters[i] == clusters[j]: similarityMatrix1[i, j] = 1 if realClusters[i] == realClusters[j]: similarityMatrix2[i, j] = 1 self.assertTrue((similarityMatrix1 == similarityMatrix2).all())
def setUp(self): self.tol = 10**-4 self.numVertices = 5 self.numFeatures = 2 vertexList1 = VertexList(self.numVertices, self.numFeatures) vertexList1.setVertex(0, numpy.array([1, 1])) vertexList1.setVertex(1, numpy.array([1, 2])) vertexList1.setVertex(2, numpy.array([3, 2])) vertexList1.setVertex(3, numpy.array([4, 2])) vertexList1.setVertex(4, numpy.array([2, 6])) vertexList2 = VertexList(self.numVertices, self.numFeatures) vertexList2.setVertex(0, numpy.array([1, 3])) vertexList2.setVertex(1, numpy.array([7, 2])) vertexList2.setVertex(2, numpy.array([3, 22])) vertexList2.setVertex(3, numpy.array([54, 2])) vertexList2.setVertex(4, numpy.array([2, 34])) self.sGraph1 = SparseGraph(vertexList1) self.sGraph1.addEdge(0, 1) self.sGraph1.addEdge(0, 2) self.sGraph1.addEdge(1, 2) self.sGraph1.addEdge(2, 3) self.sGraph2 = SparseGraph(vertexList2) self.sGraph2.addEdge(0, 1) self.sGraph2.addEdge(0, 2) self.sGraph2.addEdge(1, 2) self.sGraph2.addEdge(2, 3) self.sGraph2.addEdge(3, 4) self.sGraph3 = SparseGraph(vertexList2) self.sGraph3.addEdge(4, 1) self.sGraph3.addEdge(4, 2) self.sGraph3.addEdge(1, 2) self.sGraph3.addEdge(1, 0)
def testGenerateGraph(self): numFeatures = 0 numVertices = 20 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) alpha1 = 10.0 alpha2 = 20.0 p = 0.001 dim = 2 generator = GeometricRandomGenerator(graph) graph = generator.generateGraph(alpha1, p, dim) numEdges1 = graph.getNumEdges() #Check no self edges for i in range(numVertices): self.assertTrue(graph.getEdge(i, i) == None) graph.removeAllEdges() graph = generator.generateGraph(alpha2, p, dim) numEdges2 = graph.getNumEdges() #self.assertTrue(numEdges1 >= numEdges2) logging.debug(numEdges1) logging.debug(numEdges2) for i in range(numVertices): self.assertTrue(graph.getEdge(i, i) == None) #Test case with p=0 and alpha huge p = 0.0 alpha = 100.0 graph.removeAllEdges() graph = generator.generateGraph(alpha, p, dim) self.assertEquals(graph.getNumEdges(), 0) #When alpha=0, should get max edges alpha = 0.0 graph.removeAllEdges() graph = generator.generateGraph(alpha, p, dim) #self.assertEquals(graph.getNumEdges(), int(0.5*(numVertices + numVertices**2) - numVertices)) #TODO: Test variations in dimension """
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
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)
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
def testGenerate(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 = KroneckerGenerator(initialGraph, k) graph = generator.generate() 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.generate() 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)
def generateGraph(self): """ Generate a Kronecker graph """ W = self.initialGraph.getWeightMatrix() Wi = W for i in range(1, self.k): Wi = numpy.kron(Wi, W) P = numpy.random.rand(Wi.shape[0], Wi.shape[0]) Wi = numpy.array(P < Wi, numpy.float64) vList = VertexList(Wi.shape[0], 0) graph = SparseGraph(vList, self.initialGraph.isUndirected()) graph.setWeightMatrix(Wi) return graph
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)
def __init__(self): numVertices = 1000 graph = SparseGraph(GeneralVertexList(numVertices)) p = 0.1 generator = ErdosRenyiGenerator(p) graph = generator.generate(graph) subgraphIndicesList = [] for i in range(100, numVertices, 10): subgraphIndicesList.append(range(i)) k1 = 5 k2 = 100 self.graph = graph self.subgraphIndicesList = subgraphIndicesList self.clusterer = IterativeSpectralClustering(k1, k2, T=10, alg="IASC")