Esempio n. 1
0
    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)
Esempio n. 2
0
    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 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)
Esempio n. 4
0
    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 )
Esempio n. 5
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)
Esempio n. 6
0
    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 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)
Esempio n. 8
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)
Esempio n. 9
0
import sys
import struct
#from apgl.graph import *
from apgl.graph.DenseGraph import DenseGraph
from apgl.graph.VertexList import VertexList
from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator
import numpy

if len(sys.argv) != 2:
  print('Usage {} <num-vertices>'.format(sys.argv[0]))
  sys.exit(1)

# Generate a random graph
p = 0.2
graph = DenseGraph(VertexList(int(sys.argv[1]), 1))
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)

numVertices = graph.getNumVertices()
numEdges = graph.getNumEdges()
print("{} vertices {} edges".format(numVertices, numEdges))

# Write packed adjacency list to a binary file
outfile = 'graph'
f = open(outfile, "wb")
try:
  f.write(struct.pack('i', numVertices))
  f.write(struct.pack('i', numEdges))
  index = 0
  # Vertices
Esempio n. 10
0
import sys
import struct
#from apgl.graph import *
from apgl.graph.DenseGraph import DenseGraph
from apgl.graph.VertexList import VertexList
from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator
import numpy

if len(sys.argv) != 2:
    print('Usage {} <num-vertices>'.format(sys.argv[0]))
    sys.exit(1)

# Generate a random graph
p = 0.2
graph = DenseGraph(VertexList(int(sys.argv[1]), 1))
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)

numVertices = graph.getNumVertices()
numEdges = graph.getNumEdges()
print("{} vertices {} edges".format(numVertices, numEdges))

# Write packed adjacency list to a binary file
outfile = 'graph'
f = open(outfile, "wb")
try:
    f.write(struct.pack('i', numVertices))
    f.write(struct.pack('i', numEdges))
    index = 0
    # Vertices
Esempio n. 11
0
    def testInit(self):
        numVertices = 0
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)

        graph = DenseGraph(vList)
        self.assertEquals(graph.weightMatrixDType(), numpy.float64)

        graph = DenseGraph(vList, dtype=numpy.int16)
        self.assertEquals(graph.weightMatrixDType(), numpy.int16)
        
        numVertices = 0
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        graph = DenseGraph(vList, dtype=numpy.int16)

        numVertices = 10
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        graph = DenseGraph(vList, dtype=numpy.int16)
        self.assertEquals(type(graph.W), numpy.ndarray)

        self.assertRaises(ValueError, DenseGraph, [])
        self.assertRaises(ValueError, DenseGraph, vList, 1)
        self.assertRaises(ValueError, DenseGraph, vList, True, 1)

        #Now test invalid values of W
        W = scipy.sparse.csr_matrix((numVertices, numVertices))
        self.assertRaises(ValueError, DenseGraph, vList, True, W)

        W = numpy.zeros((numVertices+1, numVertices))
        self.assertRaises(ValueError, DenseGraph, vList, True, W)

        W = numpy.zeros((numVertices, numVertices))
        W[0, 1] = 1
        self.assertRaises(ValueError, DenseGraph, vList, True, W)

        W = numpy.zeros((numVertices, numVertices))
        graph = DenseGraph(vList, W=W)

        self.assertEquals(type(graph.W), numpy.ndarray)
        
        #Test intialising with non-empty graph 
        numVertices = 10 
        W = numpy.zeros((numVertices, numVertices))
        W[1, 0] = 1.1 
        W[0, 1] = 1.1 
        graph = DenseGraph(numVertices, W=W)
        
        self.assertEquals(graph[1, 0], 1.1)
        
        #Test just specifying number of vertices 
        graph = DenseGraph(numVertices)
        self.assertEquals(graph.size, numVertices)
        
        #Try creating a sparse matrix of dtype int 
        graph = DenseGraph(numVertices, dtype=numpy.int)
        self.assertEquals(graph.W.dtype, numpy.int)
        graph[0, 0] = 1.2 
        
        self.assertEquals(graph[0, 0], 1)
"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

from apgl.graph.DenseGraph import DenseGraph
from apgl.graph.GeneralVertexList import GeneralVertexList
from apgl.generator.ErdosRenyiGenerator import *

numVertices = 20
graph = DenseGraph(GeneralVertexList(numVertices))
p = 0.2
generator = ErdosRenyiGenerator(p)
graph = generator.generate(graph)


Esempio n. 13
0
    def testInit(self):
        numVertices = 0
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)

        graph = DenseGraph(vList)
        self.assertEquals(graph.weightMatrixDType(), numpy.float64)

        graph = DenseGraph(vList, dtype=numpy.int16)
        self.assertEquals(graph.weightMatrixDType(), numpy.int16)

        numVertices = 0
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        graph = DenseGraph(vList, dtype=numpy.int16)

        numVertices = 10
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        graph = DenseGraph(vList, dtype=numpy.int16)
        self.assertEquals(type(graph.W), numpy.ndarray)

        self.assertRaises(ValueError, DenseGraph, [])
        self.assertRaises(ValueError, DenseGraph, vList, 1)
        self.assertRaises(ValueError, DenseGraph, vList, True, 1)

        #Now test invalid values of W
        W = scipy.sparse.csr_matrix((numVertices, numVertices))
        self.assertRaises(ValueError, DenseGraph, vList, True, W)

        W = numpy.zeros((numVertices + 1, numVertices))
        self.assertRaises(ValueError, DenseGraph, vList, True, W)

        W = numpy.zeros((numVertices, numVertices))
        W[0, 1] = 1
        self.assertRaises(ValueError, DenseGraph, vList, True, W)

        W = numpy.zeros((numVertices, numVertices))
        graph = DenseGraph(vList, W=W)

        self.assertEquals(type(graph.W), numpy.ndarray)

        #Test intialising with non-empty graph
        numVertices = 10
        W = numpy.zeros((numVertices, numVertices))
        W[1, 0] = 1.1
        W[0, 1] = 1.1
        graph = DenseGraph(numVertices, W=W)

        self.assertEquals(graph[1, 0], 1.1)

        #Test just specifying number of vertices
        graph = DenseGraph(numVertices)
        self.assertEquals(graph.size, numVertices)

        #Try creating a sparse matrix of dtype int
        graph = DenseGraph(numVertices, dtype=numpy.int)
        self.assertEquals(graph.W.dtype, numpy.int)
        graph[0, 0] = 1.2

        self.assertEquals(graph[0, 0], 1)