示例#1
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)
示例#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 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)
示例#4
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
"""
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)


示例#6
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)