def readGraph(self, vertexFileName, edgeFileNames, undirected=True, delimiter=None): """ Read a MultiGraph from at least 2 files: one is the information about vertices and the other(s) are lists of edges. For the list of vertices the first column must be the ID of the vertex. """ X = numpy.loadtxt(vertexFileName, skiprows=1, converters=self.converters, usecols=self.vertexIndices, delimiter=delimiter) numVertices = X.shape[0] numFeatures = X.shape[1]-1 vertexIds = X[:, 0] vertexIdsDict = {} for i in range(0, numVertices): vertexIdsDict[vertexIds[i]] = i if self.nanProcessor != None: X[:, 1:numFeatures+1] = self.nanProcessor(X[:, 1:numFeatures+1]) vertexList = VertexList(numVertices, numFeatures) vertexList.setVertices(X[:, 1:numFeatures+1]) maxEdgeTypes = len(edgeFileNames) sparseMultiGraph = SparseMultiGraph(vertexList, maxEdgeTypes, undirected) for i in range(0, maxEdgeTypes): self.__readEdgeFile(vertexIdsDict, edgeFileNames[i], sparseMultiGraph, i) logging.info("MultiGraph read with " + str(sparseMultiGraph.getNumVertices()) + " vertices and " + str(sparseMultiGraph.getNumEdges()) + " edges") return sparseMultiGraph
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)
class VertexListTest(unittest.TestCase, AbstractVertexListTest): def setUp(self): self.numVertices = 10 self.numFeatures = 3 self.vList = VertexList(self.numVertices, self.numFeatures) self.emptyVertex = numpy.zeros(self.numFeatures) self.initialise() def testConstructor(self): self.assertEquals(self.vList.getNumFeatures(), self.numFeatures) self.assertEquals(self.vList.getNumVertices(), self.numVertices) def testSaveLoad(self): try: vList = VertexList(self.numVertices, self.numFeatures) vList.setVertex(0, numpy.array([1, 2, 3])) vList.setVertex(1, numpy.array([4, 5, 6])) vList.setVertex(2, numpy.array([7, 8, 9])) tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = VertexList.load(fileName) self.assertTrue( (vList.getVertices() == vList2.getVertices()).all()) except IOError as e: logging.warn(e) pass def testGetItem2(self): V = numpy.random.rand(self.numVertices, self.numFeatures) self.vList.setVertices(V) for i in range(self.numVertices): nptst.assert_array_equal(self.vList[i, :], V[i, :]) def testSetItem2(self): V = numpy.random.rand(self.numVertices, self.numFeatures) for i in range(self.numVertices): self.vList[i, :] = V[i, :] nptst.assert_array_equal(self.vList[i, :], V[i, :]) def testAddVertices(self): numFeatures = 5 vList = VertexList(10, numFeatures) vList.setVertex(1, numpy.ones(numFeatures) * 0.1) self.assertEquals(vList.getNumVertices(), 10) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures) * 0.1) vList.addVertices(5) self.assertEquals(vList.getNumVertices(), 15) vList.setVertex(11, numpy.ones(numFeatures) * 2) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures) * 0.1) nptst.assert_array_equal(vList[11], numpy.ones(numFeatures) * 2)
class VertexListTest(unittest.TestCase, AbstractVertexListTest): def setUp(self): self.numVertices = 10 self.numFeatures = 3 self.vList = VertexList(self.numVertices, self.numFeatures) self.emptyVertex = numpy.zeros(self.numFeatures) self.initialise() def testConstructor(self): self.assertEquals(self.vList.getNumFeatures(), self.numFeatures) self.assertEquals(self.vList.getNumVertices(), self.numVertices) def testSaveLoad(self): try: vList = VertexList(self.numVertices, self.numFeatures) vList.setVertex(0, numpy.array([1, 2, 3])) vList.setVertex(1, numpy.array([4, 5, 6])) vList.setVertex(2, numpy.array([7, 8, 9])) tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = VertexList.load(fileName) self.assertTrue((vList.getVertices() == vList2.getVertices()).all()) except IOError as e: logging.warn(e) pass def testGetItem2(self): V = numpy.random.rand(self.numVertices, self.numFeatures) self.vList.setVertices(V) for i in range(self.numVertices): nptst.assert_array_equal(self.vList[i, :], V[i, :]) def testSetItem2(self): V = numpy.random.rand(self.numVertices, self.numFeatures) for i in range(self.numVertices): self.vList[i, :] = V[i, :] nptst.assert_array_equal(self.vList[i, :], V[i, :]) def testAddVertices(self): numFeatures = 5 vList = VertexList(10, numFeatures) vList.setVertex(1, numpy.ones(numFeatures)*0.1) self.assertEquals(vList.getNumVertices(), 10) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures)*0.1) vList.addVertices(5) self.assertEquals(vList.getNumVertices(), 15) vList.setVertex(11, numpy.ones(numFeatures)*2) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures)*0.1) nptst.assert_array_equal(vList[11], numpy.ones(numFeatures)*2)
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 testAddVertices(self): numFeatures = 5 vList = VertexList(10, numFeatures) vList.setVertex(1, numpy.ones(numFeatures) * 0.1) self.assertEquals(vList.getNumVertices(), 10) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures) * 0.1) vList.addVertices(5) self.assertEquals(vList.getNumVertices(), 15) vList.setVertex(11, numpy.ones(numFeatures) * 2) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures) * 0.1) nptst.assert_array_equal(vList[11], numpy.ones(numFeatures) * 2)
def testAddVertices(self): numFeatures = 5 vList = VertexList(10, numFeatures) vList.setVertex(1, numpy.ones(numFeatures)*0.1) self.assertEquals(vList.getNumVertices(), 10) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures)*0.1) vList.addVertices(5) self.assertEquals(vList.getNumVertices(), 15) vList.setVertex(11, numpy.ones(numFeatures)*2) nptst.assert_array_equal(vList[1], numpy.ones(numFeatures)*0.1) nptst.assert_array_equal(vList[11], numpy.ones(numFeatures)*2)
def testNormalisedLaplacianSym2(self): numVertices = 10 numFeatures = 0 vList = VertexList(numVertices, numFeatures) graph = self.GraphType(vList) graph.addEdge(0, 1) graph.addEdge(0, 2) graph.addEdge(0, 9) graph.addEdge(1, 1) graph.addEdge(1, 5) L = graph.normalisedLaplacianSym(sparse=True) W = graph.getWeightMatrix() L2 = numpy.zeros((numVertices, numVertices)) d = graph.outDegreeSequence() for i in range(numVertices): for j in range(numVertices): if d[i] != 0 and d[j] != 0: Wij = W[i, j] / (numpy.sqrt(d[i] * d[j])) else: Wij = 0 if i == j: L2[i, j] = 1 - Wij else: L2[i, j] = -Wij self.assertAlmostEquals(L[i, j], L2[i, j])
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 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 setUp(self): self.numVertices = 10 self.numFeatures = 3 self.maxEdgeTypes = 3 self.vList = VertexList(self.numVertices, self.numFeatures) self.sMultiGraph = SparseMultiGraph(self.vList, self.maxEdgeTypes)
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 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 readGraph(self, vertexFileName, edgeFileNames, undirected=True, delimiter=None): """ Read a MultiGraph from at least 2 files: one is the information about vertices and the other(s) are lists of edges. For the list of vertices the first column must be the ID of the vertex. """ X = numpy.loadtxt(vertexFileName, skiprows=1, converters=self.converters, usecols=self.vertexIndices, delimiter=delimiter) numVertices = X.shape[0] numFeatures = X.shape[1] - 1 vertexIds = X[:, 0] vertexIdsDict = {} for i in range(0, numVertices): vertexIdsDict[vertexIds[i]] = i if self.nanProcessor != None: X[:, 1:numFeatures + 1] = self.nanProcessor(X[:, 1:numFeatures + 1]) vertexList = VertexList(numVertices, numFeatures) vertexList.setVertices(X[:, 1:numFeatures + 1]) maxEdgeTypes = len(edgeFileNames) sparseMultiGraph = SparseMultiGraph(vertexList, maxEdgeTypes, undirected) for i in range(0, maxEdgeTypes): self.__readEdgeFile(vertexIdsDict, edgeFileNames[i], sparseMultiGraph, i) logging.info("MultiGraph read with " + str(sparseMultiGraph.getNumVertices()) + " vertices and " + str(sparseMultiGraph.getNumEdges()) + " edges") return sparseMultiGraph
def testSaveLoad(self): try: vList = VertexList(self.numVertices, self.numFeatures) vList.setVertex(0, numpy.array([1, 2, 3])) vList.setVertex(1, numpy.array([4, 5, 6])) vList.setVertex(2, numpy.array([7, 8, 9])) tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = VertexList.load(fileName) self.assertTrue( (vList.getVertices() == vList2.getVertices()).all()) except IOError as e: logging.warn(e) pass
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): 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 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 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 testInit(self): numVertices = 0 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = PySparseGraph(vList) numVertices = 10 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = PySparseGraph(vList) self.assertRaises(ValueError, PySparseGraph, []) self.assertRaises(ValueError, PySparseGraph, vList, 1) self.assertRaises(ValueError, PySparseGraph, vList, True, 1) #Now test invalid values of W W = scipy.sparse.csr_matrix((numVertices, numVertices)) self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = numpy.zeros((numVertices + 1, numVertices)) self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = numpy.zeros((numVertices, numVertices)) W[0, 1] = 1 self.assertRaises(ValueError, PySparseGraph, vList, True, W) W = spmatrix.ll_mat(numVertices, numVertices) graph = PySparseGraph(vList, W=W) self.assertTrue(isinstance(W, spmatrix.LLMatType)) #Test intialising with non-empty graph numVertices = 10 W = spmatrix.ll_mat(numVertices, numVertices) W[1, 0] = 1.1 W[0, 1] = 1.1 graph = PySparseGraph(numVertices, W=W) self.assertEquals(graph[1, 0], 1.1) #Test just specifying number of vertices graph = PySparseGraph(numVertices) self.assertEquals(graph.size, numVertices)
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 generateIndicatorVertices(self, numVertices, mu, sigma, p): """ Generate a set of vertices from the means and variances of Y using the multivariate normal distribution. Also add at the end an indicator variables which is 1 with probability p. """ if not (sigma.T == sigma).all(): raise ValueError("Must use symmetric sigma matrix: " + str(sigma)) n = mu.shape[0] X = numpy.zeros((numVertices, n+1), numpy.int32) X[:, 0:n] = numpy.round(random.multivariate_normal(mu, sigma, numVertices)).astype(numpy.int32) X[:, n] = (random.rand(numVertices) < p).astype(numpy.int32) vList = VertexList(numVertices, n+1) for i in range(0, numVertices): vList.setVertex(i, X[i, :]) return vList
def testSaveLoad(self): try: vList = VertexList(self.numVertices, self.numFeatures) vList.setVertex(0, numpy.array([1, 2, 3])) vList.setVertex(1, numpy.array([4, 5, 6])) vList.setVertex(2, numpy.array([7, 8, 9])) tempDir = PathDefaults.getTempDir() fileName = tempDir + "vList" vList.save(fileName) vList2 = VertexList.load(fileName) self.assertTrue((vList.getVertices() == vList2.getVertices()).all()) except IOError as e: logging.warn(e) pass
def load(cls, filename): """ Load the graph object from the corresponding file. Data is loaded in a zip format as created using save(). :param filename: The name of the file to load. :type filename: :class:`str` :returns: A graph corresponding to the one saved in filename. """ Parameter.checkClass(filename, str) import zipfile (path, filename) = os.path.split(filename) if path == "": path = "./" tempPath = tempfile.mkdtemp() originalPath = os.getcwd() try: os.chdir(path) myzip = zipfile.ZipFile(filename + '.zip', 'r') myzip.extractall(tempPath) myzip.close() os.chdir(tempPath) #Deal with legacy files try: W = cls.loadMatrix(cls._wFilename) metaDict = Util.loadPickle(cls._metaFilename) vList = globals()[metaDict["vListType"]].load(cls._verticesFilename) undirected = metaDict["undirected"] except IOError: W = cls.loadMatrix(filename + cls._matExt) vList = VertexList.load(filename) undirected = Util.loadPickle(filename + cls._boolExt) graph = cls(vList, undirected) graph.W = W for tempFile in myzip.namelist(): os.remove(tempFile) finally: os.chdir(originalPath) os.rmdir(tempPath) return graph
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 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 testInit(self): numVertices = 0 numFeatures = 1 vList = VertexList(numVertices, numFeatures) graph = CsArrayGraph(vList) self.assertEquals(graph.weightMatrixDType(), numpy.float) graph = CsArrayGraph(vList, dtype=numpy.int16) self.assertEquals(graph.weightMatrixDType(), numpy.int16) #Test just specifying number of vertices graph = CsArrayGraph(numVertices) self.assertEquals(graph.size, numVertices)
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 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 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 generateIndicatorVertices2(self, numVertices, mu, sigma, p, minVals, maxVals): """ Generate a set of vertices from the means and variances of Y using the multivariate normal distribution. Also add at the end an indicator variables which is 1 with probability p. Make sure the values fall within minVals and maxVals. """ if numpy.linalg.norm(sigma.T - sigma) > 10**-8 : raise ValueError("Must use symmetric sigma matrix: " + str(sigma.T - sigma)) if (minVals > maxVals).any(): raise ValueError("minVals must be less than or equal to maxVals") logging.info("Generating " + str(numVertices) + " vertices") n = mu.shape[0] X = numpy.round(random.multivariate_normal(mu, sigma, numVertices)).astype(numpy.int32) indVector = (random.rand(numVertices) < p).astype(numpy.int32) constantValueInds = numpy.nonzero(minVals == maxVals)[0] constantValues = minVals[constantValueInds] blockSize = 10000 X = X[numpy.logical_and(X >= minVals, X <= maxVals).all(1), :] while X.shape[0] < numVertices: logging.info("Generated " + str(X.shape[0]) + " vertices so far") XBlock = numpy.round(random.multivariate_normal(mu, sigma, blockSize)).astype(numpy.int32) XBlock[:, constantValueInds] = constantValues XBlock = XBlock[numpy.logical_and(XBlock >= minVals, XBlock <= maxVals).all(1), :] X = numpy.r_[X, XBlock] X = X[0:numVertices, :] X = numpy.c_[X, indVector] vList = VertexList(numVertices, n+1) vList.setVertices(X) return vList
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 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 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 setUp(self): self.numVertices = 10 self.numFeatures = 3 self.vList = VertexList(self.numVertices, self.numFeatures) self.emptyVertex = numpy.zeros(self.numFeatures) self.initialise()
def testVertexLabelPairs(self): numVertices = 6 numFeatures = 1 vList = VertexList(numVertices, numFeatures) vList.setVertices(numpy.array([numpy.arange(0, 6)]).T) graph = DenseGraph(vList, True) graph.addEdge(0, 1, 0.1) graph.addEdge(1, 3, 0.1) graph.addEdge(0, 2, 0.2) graph.addEdge(2, 3, 0.5) graph.addEdge(0, 4, 0.1) graph.addEdge(3, 4, 0.1) tol = 10**-6 edges = graph.getAllEdges() X = GraphUtils.vertexLabelPairs(graph, edges) self.assertTrue(numpy.linalg.norm(X - edges) < tol ) X = GraphUtils.vertexLabelPairs(graph, edges[[5, 2, 1], :]) self.assertTrue(numpy.linalg.norm(X - edges[[5,2,1], :]) < tol ) #Try a bigger graph numVertices = 6 numFeatures = 2 vList = VertexList(numVertices, numFeatures) vList.setVertices(numpy.random.randn(numVertices, numFeatures)) graph = DenseGraph(vList, True) graph.addEdge(0, 1, 0.1) graph.addEdge(1, 3, 0.1) edges = graph.getAllEdges() X = GraphUtils.vertexLabelPairs(graph, edges) self.assertTrue(numpy.linalg.norm(X[0, 0:numFeatures] - vList.getVertex(1)) < tol ) self.assertTrue(numpy.linalg.norm(X[0, numFeatures:numFeatures*2] - vList.getVertex(0)) < tol ) self.assertTrue(numpy.linalg.norm(X[1, 0:numFeatures] - vList.getVertex(3)) < tol ) self.assertTrue(numpy.linalg.norm(X[1, numFeatures:numFeatures*2] - vList.getVertex(1)) < tol ) #Try directed graphs graph = DenseGraph(vList, False) graph.addEdge(0, 1, 0.1) graph.addEdge(1, 3, 0.1) edges = graph.getAllEdges() X = GraphUtils.vertexLabelPairs(graph, edges) self.assertTrue(numpy.linalg.norm(X[0, 0:numFeatures] - vList.getVertex(0)) < tol ) self.assertTrue(numpy.linalg.norm(X[0, numFeatures:numFeatures*2] - vList.getVertex(1)) < tol ) self.assertTrue(numpy.linalg.norm(X[1, 0:numFeatures] - vList.getVertex(1)) < tol ) self.assertTrue(numpy.linalg.norm(X[1, numFeatures:numFeatures*2] - vList.getVertex(3)) < tol )
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 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())