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 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 testClusterOnIncreasingGraphs(self):
        #Create a large graph and try the clustering.
        numClusters = 3
        ClusterSize = 30
        numFeatures = 0
        
        pNoise = 0
        pClust = 1

        numVertices = numClusters*ClusterSize
        vList = GeneralVertexList(numVertices)

        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

#        ell = 2 
#        m = 2 
#        generator = BarabasiAlbertGenerator(ell, m)
#        graph = generator.generate(graph)
        #Generate matrix of probabilities
        W = numpy.ones((numVertices, numVertices))*pNoise
        for i in range(numClusters):
            W[ClusterSize*i:ClusterSize*(i+1), ClusterSize*i:ClusterSize*(i+1)] = pClust
        P = numpy.random.rand(numVertices, numVertices)
        W = numpy.array(P < W, numpy.float)
        upTriInds = numpy.triu_indices(numVertices)
        W[upTriInds] = 0
        W = W + W.T
        graph = SparseGraph(vList)
        graph.setWeightMatrix(W)

        indices = numpy.random.permutation(numVertices)
        subgraphIndicesList = [indices[0:numVertices/2], indices]

        k1 = numClusters
        k2 = 10
        clusterer = IterativeSpectralClustering(k1, k2)
        #Test full computation of eigenvectors
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList = clusterer.clusterFromIterator(graphIterator, False)

        self.assertEquals(len(clustersList), len(subgraphIndicesList))

        for i in range(len(clustersList)):
            clusters = clustersList[i]
            self.assertEquals(len(subgraphIndicesList[i]), len(clusters))
            #print(clusters)

        #Test full computation of eigenvectors with iterator
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList = clusterer.clusterFromIterator(graphIterator, False)

        self.assertEquals(len(clustersList), len(subgraphIndicesList))

        for i in range(len(clustersList)):
            clusters = clustersList[i]
            self.assertEquals(len(subgraphIndicesList[i]), len(clusters))
            #print(clusters)

        #Now test approximation of eigenvectors with iterator
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList2 = clusterer.clusterFromIterator(graphIterator)

        for i in range(len(clustersList2)):
            clusters = clustersList2[i]
            self.assertEquals(len(subgraphIndicesList[i]), len(clusters))
            #print(clusters)

        #Test case where 2 graphs are identical
        subgraphIndicesList = []
        subgraphIndicesList.append(range(graph.getNumVertices()))
        subgraphIndicesList.append(range(graph.getNumVertices()))

        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clustersList = clusterer.clusterFromIterator(graphIterator, True)
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)
Beispiel #5
0
from scipy.sparse import csr_matrix
from pyamg import smoothed_aggregation_solver
from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.GeneralVertexList import GeneralVertexList

numpy.set_printoptions(suppress=True, precision=4, linewidth=100)
numpy.random.seed(21)

p = 0.001
numVertices = 10000
generator = ErdosRenyiGenerator(p)
graph = SparseGraph(GeneralVertexList(numVertices))
graph = generator.generate(graph)

print("Num vertices = " + str(graph.getNumVertices()))
print("Num edges = " + str(graph.getNumEdges()))

L = graph.normalisedLaplacianSym(sparse=True)
# L = csr_matrix(L)
print("Created Laplacian")

# ml = smoothed_aggregation_solver(L)
# M = ml.aspreconditioner()


start = time.clock()
w, V = scipy.sparse.linalg.eigsh(L, k=20)
totalTime = time.clock() - start
print(totalTime)
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)
Beispiel #7
0
from scipy.sparse import csr_matrix
from pyamg import smoothed_aggregation_solver
from apgl.generator.ErdosRenyiGenerator import ErdosRenyiGenerator
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.GeneralVertexList import GeneralVertexList

numpy.set_printoptions(suppress=True, precision=4, linewidth=100)
numpy.random.seed(21)

p = 0.001
numVertices = 10000
generator = ErdosRenyiGenerator(p)
graph = SparseGraph(GeneralVertexList(numVertices))
graph = generator.generate(graph)

print("Num vertices = " + str(graph.getNumVertices()))
print("Num edges = " + str(graph.getNumEdges()))

L = graph.normalisedLaplacianSym(sparse=True)
#L = csr_matrix(L)
print("Created Laplacian")

#ml = smoothed_aggregation_solver(L)
#M = ml.aspreconditioner()

start = time.clock()
w, V = scipy.sparse.linalg.eigsh(L, k=20)
totalTime = time.clock() - start
print(totalTime)

#print(w)