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 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()))
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 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 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 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 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 = 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 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 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 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
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
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)
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
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)
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 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)
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)
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)
def readFromFile(self, fileName): X = numpy.loadtxt(fileName, skiprows=1, converters=self.converters) vertexIds = numpy.zeros(X.shape[0] * 2) #First, we will map the vertex Ids to a set of numbers for i in range(0, X.shape[0]): vertexIds[2 * i] = X[i, self.vertex1IdIndex] vertexIds[2 * i + 1] = X[i, self.vertex2IdIndex] vertexIds = numpy.unique(vertexIds) numVertices = vertexIds.shape[0] numFeatures = len(self.vertex1Indices) vList = VertexList(numVertices, numFeatures) sGraph = SparseGraph(vList, self.undirected) for i in range(0, X.shape[0]): vertex1Id = X[i, self.vertex1IdIndex] vertex2Id = X[i, self.vertex2IdIndex] vertex1 = X[i, self.vertex1Indices] vertex2 = X[i, self.vertex2Indices] vertex1VListId = numpy.nonzero(vertexIds == vertex1Id)[0] vertex2VListId = numpy.nonzero(vertexIds == vertex2Id)[0] vertex1VListId = int(vertex1VListId) vertex2VListId = int(vertex2VListId) vList.setVertex(vertex1VListId, vertex1) vList.setVertex(vertex2VListId, vertex2) sGraph.addEdge(vertex1VListId, vertex2VListId, self.edgeWeight) logging.info("Read " + fileName + " with " + str(sGraph.getNumVertices()) + " vertices and " + str(sGraph.getNumEdges()) + " edges") return sGraph
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 readFromFile(self, fileName): X = numpy.loadtxt(fileName, skiprows=1, converters=self.converters) vertexIds = numpy.zeros(X.shape[0]*2) #First, we will map the vertex Ids to a set of numbers for i in range(0, X.shape[0]): vertexIds[2*i] = X[i, self.vertex1IdIndex] vertexIds[2*i+1] = X[i, self.vertex2IdIndex] vertexIds = numpy.unique(vertexIds) numVertices = vertexIds.shape[0] numFeatures = len(self.vertex1Indices) vList = VertexList(numVertices, numFeatures) sGraph = SparseGraph(vList, self.undirected) for i in range(0, X.shape[0]): vertex1Id = X[i, self.vertex1IdIndex] vertex2Id = X[i, self.vertex2IdIndex] vertex1 = X[i, self.vertex1Indices] vertex2 = X[i, self.vertex2Indices] vertex1VListId = numpy.nonzero(vertexIds==vertex1Id)[0] vertex2VListId = numpy.nonzero(vertexIds==vertex2Id)[0] vertex1VListId = int(vertex1VListId) vertex2VListId = int(vertex2VListId) vList.setVertex(vertex1VListId, vertex1) vList.setVertex(vertex2VListId, vertex2) sGraph.addEdge(vertex1VListId, vertex2VListId, self.edgeWeight) logging.info("Read " + fileName + " with " + str(sGraph.getNumVertices()) + " vertices and " + str(sGraph.getNumEdges()) + " edges") return sGraph
def graphFromMatFile(matFileName): """ Generate a sparse graph from a Matlab file of ego and alters and their transmissions. This is a mostly disconnected graph made up of pairs of connected vertices, i.e each vertex has degree 1. """ examplesList = ExamplesList.readFromMatFile(matFileName) numExamples = examplesList.getNumExamples() numFeatures = examplesList.getDataFieldSize("X", 1) numVertexFeatures = numFeatures/2+1 vList = VertexList(numExamples*2, int(numVertexFeatures)) sGraph = SparseGraph(vList) for i in range(0, examplesList.getNumExamples()): v1Index = i*2 v2Index = i*2+1 example = examplesList.getSubDataField("X", numpy.array([i])).ravel() vertex1 = numpy.r_[example[0:numFeatures/2], numpy.array([1])] vertex2 = numpy.r_[example[numFeatures/2:numFeatures], numpy.array([0])] sGraph.setVertex(v1Index, vertex1) sGraph.setVertex(v2Index, vertex2) sGraph.addEdge(v1Index, v2Index) return sGraph
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 testAdvanceGraph2(self): #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) simulator.advanceGraph() self.assertEquals(simulator.getNumIterations(), 1) self.assertEquals(sGraph.getVertex(0)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(1)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(2)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(3)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(4)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(5)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(6)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(7)[numFeatures-1], 0) self.assertEquals(sGraph.getVertex(8)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(9)[numFeatures-1], 0) #Advance again and all egos have information simulator.advanceGraph() self.assertEquals(simulator.getNumIterations(), 2) self.assertEquals(sGraph.getVertex(0)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(1)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(2)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(3)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(4)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(5)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(6)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(7)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(8)[numFeatures-1], 1) self.assertEquals(sGraph.getVertex(9)[numFeatures-1], 1) #Should be no change simulator.advanceGraph() self.assertEquals(simulator.getNumIterations(), 3) #Check the correct alters are added at each step self.assertTrue((simulator.getAlters(0) == numpy.array([1,2,3,6])).all()) self.assertTrue((simulator.getAlters(1) == numpy.array([7,9])).all()) self.assertTrue((simulator.getAlters(2) == numpy.array([])).all()) #Check that the transmission graph is okay transGraph = simulator.getTransmissionGraph() self.assertEquals(transGraph.getNumVertices(), 9) self.assertEquals(transGraph.getNumEdges(), 7) self.assertEquals(transGraph.getAllVertexIds(), [0, 1, 2, 3, 4, 6, 7, 8, 9]) for i in transGraph.getAllVertexIds(): self.assertTrue((transGraph.getVertex(i) == sGraph.getVertex(i)).all())
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())
class PermutationGraphKernelTest(unittest.TestCase): 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 testEvaluate(self): tau = 1.0 linearKernel = LinearKernel() graphKernel = PermutationGraphKernel(tau, linearKernel) """ First tests - if the graphs have identical edges then permutation is identity matrix provided that tau = 1. """ (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True) self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) S1, U = numpy.linalg.eigh(self.sGraph1.getWeightMatrix()) S2, U = numpy.linalg.eigh(self.sGraph2.getWeightMatrix()) evaluation2 = numpy.dot(S1, S1) self.assertTrue(numpy.linalg.norm(SW1 - S1) <= self.tol) self.assertTrue(numpy.linalg.norm(SW2 - S1) <= self.tol) self.assertTrue(abs(evaluation - evaluation2) <= self.tol) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph2, self.sGraph2, True) self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) evaluation2 = numpy.dot(S2, S2) self.assertTrue(numpy.linalg.norm(SW1 - S2) <= self.tol) self.assertTrue(numpy.linalg.norm(SW2 - S2) <= self.tol) self.assertTrue(abs(evaluation - evaluation2) <= self.tol) #Test symmetry self.assertEquals(graphKernel.evaluate(self.sGraph1, self.sGraph2), graphKernel.evaluate(self.sGraph2, self.sGraph1)) #Now we choose tau != 1 tau = 0.5 graphKernel = PermutationGraphKernel(tau, linearKernel) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True) self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) self.assertTrue(graphKernel.evaluate(self.sGraph1, self.sGraph1) >= 0) self.assertTrue(graphKernel.evaluate(self.sGraph2, self.sGraph2) >= 0) self.assertTrue((graphKernel.evaluate(self.sGraph1, self.sGraph2)- graphKernel.evaluate(self.sGraph2, self.sGraph1)) <= self.tol) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph2, True) self.assertTrue(numpy.linalg.norm(numpy.dot(P.T, P) - numpy.eye(self.numVertices)) <= self.tol) #Choose tau=0 tau = 0.0 graphKernel = PermutationGraphKernel(tau, linearKernel) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True) self.assertTrue(numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) self.assertTrue(numpy.linalg.norm(numpy.dot(P.T, P) - numpy.eye(self.numVertices)) <= self.tol) X1 = self.sGraph1.getVertexList().getVertices(list(range(0, (self.sGraph1.getNumVertices())))) X2 = self.sGraph2.getVertexList().getVertices(list(range(0, (self.sGraph2.getNumVertices())))) S1, U = numpy.linalg.eigh(numpy.dot(X1, X1.T)) S2, V = numpy.linalg.eigh(numpy.dot(X2, X2.T)) evaluation2 = numpy.dot(S1, S1) self.assertTrue(numpy.linalg.norm(SK1 - S1) <= self.tol) self.assertTrue(numpy.linalg.norm(SK2 - S1) <= self.tol) self.assertTrue(abs(evaluation - evaluation2) <= self.tol) self.assertTrue((graphKernel.evaluate(self.sGraph1, self.sGraph2)- graphKernel.evaluate(self.sGraph2, self.sGraph1)) <= self.tol) #Test value is zero when we have a graph which is a permutation of the next def testEvaluate2(self): tau = 1.0 linearKernel = LinearKernel() graphKernel = PermutationGraphKernel(tau, linearKernel) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph3, True) W1 = self.sGraph1.getWeightMatrix() W2 = self.sGraph3.getWeightMatrix() self.assertTrue(numpy.linalg.norm(Util.mdot(P, W1, P.T)-W2) <= self.tol) self.assertAlmostEquals(f, 0, 7)
sGraph = SparseGraph(vList) generator = ErdosRenyiGenerator(sGraph) sGraph = generator.generateGraph(p) graphsA.append(sGraph) #Form graphB by shuffling edges and adding/deleting ones sGraph2 = SparseGraph(vList) graphsB.append(sGraph2) #Permute edges here inds = numpy.random.permutation(size) edges = sGraph.getAllEdges() for i in range(0, edges.shape[0]): sGraph2.addEdge(inds[edges[i, 0]], inds[edges[i, 1]]) numExtraEdges = numpy.random.randint(0, maxEdges) for i in range(0, numExtraEdges): edgeIndex1 = numpy.random.randint(0, size) edgeIndex2 = numpy.random.randint(0, size) #print(str(edgeIndex1) + " " + str(edgeIndex2)) sGraph2.addEdge(edgeIndex1, edgeIndex2) #Check graphs are correct #Now, compute the kernel matrix between all edges graphs = graphsA graphs.extend(graphsB) numGraphs = len(graphs)
def testModularity(self): numVertices = 6 graph = SparseGraph(GeneralVertexList(numVertices)) graph.addEdge(0, 0) graph.addEdge(1, 1) graph.addEdge(2, 2) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(2, 1) graph.addEdge(3, 4, 2) graph.addEdge(3, 5, 2) graph.addEdge(4, 5, 2) graph.addEdge(3, 3, 2) graph.addEdge(4, 4, 2) graph.addEdge(5, 5, 2) W = graph.getWeightMatrix() clustering = numpy.array([0, 0, 0, 1, 1, 1]) #This is the same as the igraph result Q = GraphUtils.modularity(W, clustering) self.assertEquals(Q, 4.0 / 9.0) Ws = scipy.sparse.csr_matrix(W) Q = GraphUtils.modularity(Ws, clustering) self.assertEquals(Q, 4.0 / 9.0) W = numpy.ones((numVertices, numVertices)) Q = GraphUtils.modularity(W, clustering) self.assertEquals(Q, 0.0) Ws = scipy.sparse.csr_matrix(W) Q = GraphUtils.modularity(Ws, clustering) self.assertEquals(Q, 0.0)
def testModularity(self): numVertices = 6 graph = SparseGraph(GeneralVertexList(numVertices)) graph.addEdge(0,0) graph.addEdge(1,1) graph.addEdge(2,2) graph.addEdge(0,1) graph.addEdge(0,2) graph.addEdge(2,1) graph.addEdge(3,4,2) graph.addEdge(3,5,2) graph.addEdge(4,5,2) graph.addEdge(3,3,2) graph.addEdge(4,4,2) graph.addEdge(5,5,2) W = graph.getWeightMatrix() clustering = numpy.array([0,0,0,1,1,1]) #This is the same as the igraph result Q = GraphUtils.modularity(W, clustering) self.assertEquals(Q, 4.0/9.0) Ws = scipy.sparse.csr_matrix(W) Q = GraphUtils.modularity(Ws, clustering) self.assertEquals(Q, 4.0/9.0) W = numpy.ones((numVertices, numVertices)) Q = GraphUtils.modularity(W, clustering) self.assertEquals(Q, 0.0) Ws = scipy.sparse.csr_matrix(W) Q = GraphUtils.modularity(Ws, clustering) self.assertEquals(Q, 0.0)
class PajekWriterTest(unittest.TestCase): 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 tearDown(self): pass def testInit(self): pass def testWriteToFile(self): pw = PajekWriter() directory = PathDefaults.getOutputDir() + "test/" #Have to check the files fileName1 = directory + "denseTestUndirected" pw.writeToFile(fileName1, self.dGraph1) fileName2 = directory + "denseTestDirected" pw.writeToFile(fileName2, self.dGraph2) fileName3 = directory + "sparseTestUndirected" pw.writeToFile(fileName3, self.sGraph1) fileName4 = directory + "sparseTestDirected" pw.writeToFile(fileName4, self.sGraph2) fileName5 = directory + "dictTestUndirected" pw.writeToFile(fileName5, self.dctGraph1) fileName6 = directory + "dictTestDirected" pw.writeToFile(fileName6, self.dctGraph2) def testWriteToFile2(self): pw = PajekWriter() directory = PathDefaults.getOutputDir() + "test/" def setVertexColour(vertexIndex, graph): colours = ["grey05", "grey10", "grey15", "grey20", "grey25"] return colours[vertexIndex] def setVertexSize(vertexIndex, graph): return vertexIndex def setEdgeColour(vertexIndex1, vertexIndex2, graph): colours = ["grey05", "grey10", "grey15", "grey20", "grey25"] return colours[vertexIndex1] def setEdgeSize(vertexIndex1, vertexIndex2, graph): return vertexIndex1 + vertexIndex2 pw.setVertexColourFunction(setVertexColour) fileName1 = directory + "vertexColourTest" pw.writeToFile(fileName1, self.dGraph1) pw.setVertexColourFunction(None) pw.setVertexSizeFunction(setVertexSize) fileName1 = directory + "vertexSizeTest" pw.writeToFile(fileName1, self.dGraph1) pw.setVertexSizeFunction(None) pw.setEdgeColourFunction(setEdgeColour) fileName1 = directory + "edgeColourTest" pw.writeToFile(fileName1, self.dGraph1) pw.setEdgeColourFunction(None) pw.setEdgeSizeFunction(setEdgeSize) fileName1 = directory + "edgeSizeTest" pw.writeToFile(fileName1, self.dGraph1) pw.setEdgeColourFunction(None) 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)
class PermutationGraphKernelTest(unittest.TestCase): 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 testEvaluate(self): tau = 1.0 linearKernel = LinearKernel() graphKernel = PermutationGraphKernel(tau, linearKernel) """ First tests - if the graphs have identical edges then permutation is identity matrix provided that tau = 1. """ (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True) self.assertTrue( numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) S1, U = numpy.linalg.eigh(self.sGraph1.getWeightMatrix()) S2, U = numpy.linalg.eigh(self.sGraph2.getWeightMatrix()) evaluation2 = numpy.dot(S1, S1) self.assertTrue(numpy.linalg.norm(SW1 - S1) <= self.tol) self.assertTrue(numpy.linalg.norm(SW2 - S1) <= self.tol) self.assertTrue(abs(evaluation - evaluation2) <= self.tol) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph2, self.sGraph2, True) self.assertTrue( numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) evaluation2 = numpy.dot(S2, S2) self.assertTrue(numpy.linalg.norm(SW1 - S2) <= self.tol) self.assertTrue(numpy.linalg.norm(SW2 - S2) <= self.tol) self.assertTrue(abs(evaluation - evaluation2) <= self.tol) #Test symmetry self.assertEquals(graphKernel.evaluate(self.sGraph1, self.sGraph2), graphKernel.evaluate(self.sGraph2, self.sGraph1)) #Now we choose tau != 1 tau = 0.5 graphKernel = PermutationGraphKernel(tau, linearKernel) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True) self.assertTrue( numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) self.assertTrue(graphKernel.evaluate(self.sGraph1, self.sGraph1) >= 0) self.assertTrue(graphKernel.evaluate(self.sGraph2, self.sGraph2) >= 0) self.assertTrue( (graphKernel.evaluate(self.sGraph1, self.sGraph2) - graphKernel.evaluate(self.sGraph2, self.sGraph1)) <= self.tol) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph2, True) self.assertTrue( numpy.linalg.norm(numpy.dot(P.T, P) - numpy.eye(self.numVertices)) <= self.tol) #Choose tau=0 tau = 0.0 graphKernel = PermutationGraphKernel(tau, linearKernel) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph1, True) self.assertTrue( numpy.linalg.norm(P - numpy.eye(self.numVertices)) <= self.tol) self.assertTrue( numpy.linalg.norm(numpy.dot(P.T, P) - numpy.eye(self.numVertices)) <= self.tol) X1 = self.sGraph1.getVertexList().getVertices( list(range(0, (self.sGraph1.getNumVertices())))) X2 = self.sGraph2.getVertexList().getVertices( list(range(0, (self.sGraph2.getNumVertices())))) S1, U = numpy.linalg.eigh(numpy.dot(X1, X1.T)) S2, V = numpy.linalg.eigh(numpy.dot(X2, X2.T)) evaluation2 = numpy.dot(S1, S1) self.assertTrue(numpy.linalg.norm(SK1 - S1) <= self.tol) self.assertTrue(numpy.linalg.norm(SK2 - S1) <= self.tol) self.assertTrue(abs(evaluation - evaluation2) <= self.tol) self.assertTrue( (graphKernel.evaluate(self.sGraph1, self.sGraph2) - graphKernel.evaluate(self.sGraph2, self.sGraph1)) <= self.tol) #Test value is zero when we have a graph which is a permutation of the next def testEvaluate2(self): tau = 1.0 linearKernel = LinearKernel() graphKernel = PermutationGraphKernel(tau, linearKernel) (evaluation, f, P, SW1, SW2, SK1, SK2) = graphKernel.evaluate(self.sGraph1, self.sGraph3, True) W1 = self.sGraph1.getWeightMatrix() W2 = self.sGraph3.getWeightMatrix() self.assertTrue( numpy.linalg.norm(Util.mdot(P, W1, P.T) - W2) <= self.tol) self.assertAlmostEquals(f, 0, 7)
def testGenerate(self): #undirected = True p = 0.0 self.graph.removeAllEdges() self.erg.setP(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), 0) undirected = False self.graph = SparseGraph(self.vList, undirected) self.graph.removeAllEdges() self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), 0) p = 1.0 undirected = True self.graph = SparseGraph(self.vList, undirected) self.graph.removeAllEdges() self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), (self.numVertices*self.numVertices-self.numVertices)/2) p = 1.0 undirected = False self.graph = SparseGraph(self.vList, undirected) self.graph.removeAllEdges() self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertEquals(graph.getNumEdges(), self.numVertices*self.numVertices-self.numVertices) self.assertEquals(graph.getEdge(1, 2), 1) self.assertEquals(graph.getEdge(1, 1), None) p = 0.5 numVertices = 1000 numFeatures = 0 vList = VertexList(numVertices, numFeatures) undirected = False self.graph = SparseGraph(vList, undirected) self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertAlmostEquals(graph.getNumEdges()/float(numVertices**2 - numVertices), p, places=2) p = 0.1 self.graph = SparseGraph(vList, undirected) self.erg = ErdosRenyiGenerator(p) graph = self.erg.generate(self.graph) self.assertAlmostEquals(graph.getNumEdges()/float(numVertices**2 - numVertices), p, places=2) #Test the case in which we have a graph with edges p = 0.5 numVertices = 10 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList, undirected) graph.addEdge(0, 1, 5) graph.addEdge(0, 2) graph.addEdge(0, 5, 0.7) graph.addEdge(1, 8) graph.addEdge(2, 9) numEdges = graph.getNumEdges() graph = self.erg.generate(graph, False) self.assertTrue(graph.getNumEdges() > numEdges)
class PajekWriterTest(unittest.TestCase): 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 tearDown(self): pass def testInit(self): pass def testWriteToFile(self): pw = PajekWriter() directory = PathDefaults.getOutputDir() + "test/" #Have to check the files fileName1 = directory + "denseTestUndirected" pw.writeToFile(fileName1, self.dGraph1) fileName2 = directory + "denseTestDirected" pw.writeToFile(fileName2, self.dGraph2) fileName3 = directory + "sparseTestUndirected" pw.writeToFile(fileName3, self.sGraph1) fileName4 = directory + "sparseTestDirected" pw.writeToFile(fileName4, self.sGraph2) fileName5 = directory + "dictTestUndirected" pw.writeToFile(fileName5, self.dctGraph1) fileName6 = directory + "dictTestDirected" pw.writeToFile(fileName6, self.dctGraph2) def testWriteToFile2(self): pw = PajekWriter() directory = PathDefaults.getOutputDir() + "test/" def setVertexColour(vertexIndex, graph): colours = ["grey05", "grey10", "grey15", "grey20", "grey25"] return colours[vertexIndex] def setVertexSize(vertexIndex, graph): return vertexIndex def setEdgeColour(vertexIndex1, vertexIndex2, graph): colours = ["grey05", "grey10", "grey15", "grey20", "grey25"] return colours[vertexIndex1] def setEdgeSize(vertexIndex1, vertexIndex2, graph): return vertexIndex1+vertexIndex2 pw.setVertexColourFunction(setVertexColour) fileName1 = directory + "vertexColourTest" pw.writeToFile(fileName1, self.dGraph1) pw.setVertexColourFunction(None) pw.setVertexSizeFunction(setVertexSize) fileName1 = directory + "vertexSizeTest" pw.writeToFile(fileName1, self.dGraph1) pw.setVertexSizeFunction(None) pw.setEdgeColourFunction(setEdgeColour) fileName1 = directory + "edgeColourTest" pw.writeToFile(fileName1, self.dGraph1) pw.setEdgeColourFunction(None) pw.setEdgeSizeFunction(setEdgeSize) fileName1 = directory + "edgeSizeTest" pw.writeToFile(fileName1, self.dGraph1) pw.setEdgeColourFunction(None) 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 setUp(self): numpy.random.seed(21) numVertices = 10 numFeatures = 5 vList = VertexList(numVertices, numFeatures) vList.setVertices(numpy.random.rand(numVertices, numFeatures)) graph = SparseGraph(vList, False) graph.addEdge(0, 1, 1) graph.addEdge(0, 2, 1) graph.addEdge(0, 3, 1) graph.addEdge(0, 4, -1) graph.addEdge(0, 5, -1) graph.addEdge(1, 2, 1) graph.addEdge(1, 3, -1) graph.addEdge(1, 8, 1) graph.addEdge(2, 3, -1) graph.addEdge(2, 4, -1) graph.addEdge(2, 5, -1) graph.addEdge(2, 6, 1) self.graph = graph
def testScalarStatistics(self): numFeatures = 1 numVertices = 10 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) graph.addEdge(0, 1) growthStatistics = GraphStatistics() statsArray = growthStatistics.scalarStatistics(graph) #logging.debug(statsArray) self.assertTrue(statsArray[growthStatistics.numVerticesIndex] == 10.0) self.assertTrue(statsArray[growthStatistics.numEdgesIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.maxComponentSizeIndex] == 2.0) self.assertTrue(statsArray[growthStatistics.maxComponentEdgesIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.numComponentsIndex] == 9.0) self.assertEquals(statsArray[growthStatistics.meanComponentSizeIndex], 10.0/9.0) self.assertTrue(statsArray[growthStatistics.meanDegreeIndex] == 0.2) self.assertTrue(statsArray[growthStatistics.diameterIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.effectiveDiameterIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.densityIndex] == 1.0/45) self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 1.0/55) self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 55.0) self.assertEquals(statsArray[growthStatistics.geodesicDistMaxCompIndex], 1.0/3) self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], 1.0) self.assertEquals(statsArray[growthStatistics.numTriOrMoreComponentsIndex], 0.0) self.assertEquals(statsArray[growthStatistics.secondComponentSizeIndex], 1.0) self.assertEquals(statsArray[growthStatistics.maxCompMeanDegreeIndex], 1.0) graph.addEdge(0, 2) graph.addEdge(3,4) graph.addEdge(3,5) graph.addEdge(3,6) graph.addEdge(7,8) statsArray = growthStatistics.scalarStatistics(graph) self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], 3.0) self.assertEquals(statsArray[growthStatistics.numTriOrMoreComponentsIndex], 2.0) self.assertEquals(statsArray[growthStatistics.secondComponentSizeIndex], 3.0) self.assertEquals(statsArray[growthStatistics.maxCompMeanDegreeIndex], 1.5) #Test on a directed graph graph = SparseGraph(vList, False) graph.addEdge(0, 1) statsArray = growthStatistics.scalarStatistics(graph, treeStats=True) self.assertTrue(statsArray[growthStatistics.numVerticesIndex] == 10.0) self.assertTrue(statsArray[growthStatistics.numEdgesIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.maxComponentSizeIndex] == -1) self.assertTrue(statsArray[growthStatistics.maxComponentEdgesIndex] == -1) self.assertTrue(statsArray[growthStatistics.numComponentsIndex] == -1) self.assertTrue(statsArray[growthStatistics.meanComponentSizeIndex] == -1) self.assertEquals(statsArray[growthStatistics.meanDegreeIndex], 0.1) self.assertTrue(statsArray[growthStatistics.diameterIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.effectiveDiameterIndex] == 1.0) self.assertTrue(statsArray[growthStatistics.densityIndex] == 1.0/90) self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 1.0/100) self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 100) self.assertEquals(statsArray[growthStatistics.meanTreeSizeIndex], 10.0/9) self.assertEquals(statsArray[growthStatistics.meanTreeDepthIndex], 1.0/9) self.assertEquals(statsArray[growthStatistics.maxTreeSizeIndex], 2.0) self.assertEquals(statsArray[growthStatistics.maxTreeDepthIndex], 1.0) self.assertEquals(statsArray[growthStatistics.numTreesIndex], 9.0) self.assertEquals(statsArray[growthStatistics.numNonSingletonTreesIndex], 1.0) #Test that the max tree is decribed correctly graph.addEdge(2, 3) graph.addEdge(2, 4) graph.addEdge(3, 5) graph.addEdge(3, 6) statsArray = growthStatistics.scalarStatistics(graph, treeStats=True) self.assertEquals(statsArray[growthStatistics.maxTreeSizeIndex], 5.0) self.assertEquals(statsArray[growthStatistics.maxTreeDepthIndex], 2.0) self.assertEquals(statsArray[growthStatistics.secondTreeSizeIndex], 2.0) self.assertEquals(statsArray[growthStatistics.secondTreeDepthIndex], 1.0) self.assertEquals(statsArray[growthStatistics.numTreesIndex], 5.0) self.assertEquals(statsArray[growthStatistics.numNonSingletonTreesIndex], 2.0) #Try a zero size graph numFeatures = 0 numVertices = 0 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) statsArray = growthStatistics.scalarStatistics(graph) self.assertEquals(statsArray[growthStatistics.numVerticesIndex], 0) self.assertEquals(statsArray[growthStatistics.numEdgesIndex], 0) self.assertEquals(statsArray[growthStatistics.maxComponentSizeIndex], 0) self.assertTrue(statsArray[growthStatistics.maxComponentEdgesIndex] == 0) self.assertEquals(statsArray[growthStatistics.numComponentsIndex], 0) self.assertEquals(statsArray[growthStatistics.meanComponentSizeIndex], 0) self.assertEquals(statsArray[growthStatistics.meanDegreeIndex], 0) self.assertEquals(statsArray[growthStatistics.diameterIndex], 0) self.assertEquals(statsArray[growthStatistics.effectiveDiameterIndex], 0) self.assertEquals(statsArray[growthStatistics.densityIndex], 0) self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 0.0) self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 0.0) self.assertEquals(statsArray[growthStatistics.geodesicDistMaxCompIndex], 0.0) self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], 0.0) graph = SparseGraph(vList, False) statsArray = growthStatistics.scalarStatistics(graph) self.assertEquals(statsArray[growthStatistics.numVerticesIndex], 0) self.assertEquals(statsArray[growthStatistics.numEdgesIndex], 0) self.assertEquals(statsArray[growthStatistics.maxComponentSizeIndex], -1) self.assertEquals(statsArray[growthStatistics.numComponentsIndex], -1) self.assertEquals(statsArray[growthStatistics.meanComponentSizeIndex], -1) self.assertEquals(statsArray[growthStatistics.maxComponentEdgesIndex], -1) self.assertEquals(statsArray[growthStatistics.meanDegreeIndex], 0) self.assertEquals(statsArray[growthStatistics.diameterIndex], 0) self.assertEquals(statsArray[growthStatistics.effectiveDiameterIndex], 0) self.assertEquals(statsArray[growthStatistics.densityIndex], 0) self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 0.0) self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 0.0) self.assertEquals(statsArray[growthStatistics.geodesicDistMaxCompIndex], -1) self.assertEquals(statsArray[growthStatistics.meanTreeSizeIndex], -1) self.assertEquals(statsArray[growthStatistics.meanTreeDepthIndex], -1) self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], -1) self.assertEquals(statsArray[growthStatistics.numTreesIndex], -1) self.assertEquals(statsArray[growthStatistics.numNonSingletonTreesIndex], -1)
def testVectorStatistics(self): numFeatures = 1 numVertices = 10 vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList) graph.addEdge(0, 2) graph.addEdge(0, 1) growthStatistics = GraphStatistics() statsDict = growthStatistics.vectorStatistics(graph) self.assertTrue((statsDict["outDegreeDist"] == numpy.array([7,2,1])).all()) self.assertTrue((statsDict["inDegreeDist"] == numpy.array([7,2,1])).all()) self.assertTrue((statsDict["hopCount"] == numpy.array([10,14,16])).all()) self.assertTrue((statsDict["triangleDist"] == numpy.array([10])).all()) W = graph.getWeightMatrix() W = (W + W.T)/2 lmbda, V = numpy.linalg.eig(W) maxEigVector = V[:, numpy.argmax(lmbda)] lmbda = numpy.flipud(numpy.sort(lmbda[lmbda>0])) self.assertTrue((statsDict["maxEigVector"] == maxEigVector).all()) self.assertTrue((statsDict["eigenDist"] == lmbda).all()) self.assertTrue((statsDict["componentsDist"] == numpy.array([0, 7, 0, 1])).all()) graph.addEdge(0, 3) graph.addEdge(0, 4) graph.addEdge(1, 4) growthStatistics = GraphStatistics() statsDict = growthStatistics.vectorStatistics(graph) self.assertTrue((statsDict["outDegreeDist"] == numpy.array([5,2,2,0,1])).all()) self.assertTrue((statsDict["inDegreeDist"] == numpy.array([5,2,2,0,1])).all()) self.assertTrue((statsDict["hopCount"] == numpy.array([10,20,30])).all()) self.assertTrue((statsDict["triangleDist"] == numpy.array([7, 0, 3])).all()) W = graph.getWeightMatrix() W = (W + W.T)/2 lmbda, V = numpy.linalg.eig(W) maxEigVector = V[:, numpy.argmax(lmbda)] lmbda = numpy.flipud(numpy.sort(lmbda[lmbda>0])) self.assertTrue((statsDict["maxEigVector"] == maxEigVector).all()) self.assertTrue((statsDict["eigenDist"] == lmbda).all()) self.assertTrue((statsDict["componentsDist"] == numpy.array([0, 5, 0, 0, 0, 1])).all()) #Test on a directed graph and generating tree statistics vList = VertexList(numVertices, numFeatures) graph = SparseGraph(vList, False) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(2, 3) graph.addEdge(4, 5) statsDict = growthStatistics.vectorStatistics(graph, treeStats=True) self.assertTrue(( statsDict["inDegreeDist"] == numpy.array([6, 4]) ).all()) self.assertTrue(( statsDict["outDegreeDist"] == numpy.array([7, 2, 1]) ).all()) self.assertTrue(( statsDict["triangleDist"] == numpy.array([10]) ).all()) self.assertTrue(( statsDict["treeSizesDist"] == numpy.array([0, 4, 1, 0, 1]) ).all()) self.assertTrue(( statsDict["treeDepthsDist"] == numpy.array([4, 1, 1]) ).all())