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
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 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 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.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 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 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 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)