Beispiel #1
0
def BarabasiAlbertEdgeList(n):
    graph = SparseGraph(n)
    generator = BarabasiAlbertGenerator(10, 10)
    graph = generator.generate(graph)
    l, _ = graph.adjacencyList()

    return convertAdjListToEdgeList(l)
    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 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)
Beispiel #4
0
def BarabasiAlbertEdgeList(n):
    graph = SparseGraph(n)
    generator = BarabasiAlbertGenerator(10, 10)
    graph = generator.generate(graph)
    l, _ = graph.adjacencyList()
    
    return convertAdjListToEdgeList(l)
Beispiel #5
0
    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 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 testEfficientNystrom(self): 
        numVertices = 50
        graph = SparseGraph(GeneralVertexList(numVertices))

        ell = 2 
        m = 2 
        generator = BarabasiAlbertGenerator(ell, m)
        graph = generator.generate(graph)

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

        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
Beispiel #8
0
    def testShiftLaplacian(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.shiftLaplacian(W)

        L2 = 2*numpy.eye(numVertices) - graph.normalisedLaplacianSym()

        tol = 10**-6
        self.assertTrue(numpy.linalg.norm(L - L2) < tol)
    def testShiftLaplacian(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.shiftLaplacian(W)

        L2 = 2 * numpy.eye(numVertices) - graph.normalisedLaplacianSym()

        tol = 10**-6
        self.assertTrue(numpy.linalg.norm(L - L2) < tol)
    def testIncreasingSubgraphListIterator(self):
        #Create a small graph and try the iterator increasing the number of vertices.
        numVertices = 50
        graph = SparseGraph(GeneralVertexList(numVertices))

        ell = 2 
        m = 2 
        generator = BarabasiAlbertGenerator(ell, m)
        graph = generator.generate(graph)

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

        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)

        #Try a much longer sequence of vertices
        subgraphIndicesList = []
        for i in range(10, numVertices):
            subgraphIndicesList.append(range(i))

        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)

        k1 = 3
        k2 = 6
        clusterer = IterativeSpectralClustering(k1, k2)
        clustersList = clusterer.clusterFromIterator(graphIterator)

        #Now test the Nystrom method
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clusterer = IterativeSpectralClustering(k1, alg="nystrom")
        clustersList = clusterer.clusterFromIterator(graphIterator)
        
        #Test efficient Nystrom method 
        graphIterator = IncreasingSubgraphListIterator(graph, subgraphIndicesList)
        clusterer = IterativeSpectralClustering(k1, alg="efficientNystrom")
        clustersList = clusterer.clusterFromIterator(graphIterator)
    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 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)