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)

        p = 0.2
        generator = ErdosRenyiGenerator(p)

        graph = generator.generate(graph)
        logging.debug((graph.getNumEdges()))

        nxGraph = graph.toNetworkXGraph()
        nodePositions = networkx.spring_layout(nxGraph)
        nodesAndEdges = networkx.draw_networkx(nxGraph, pos=nodePositions)
        ax = matplotlib.pyplot.axes()
        ax.set_xticklabels([])
        ax.set_yticklabels([])
예제 #3
0
    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)
예제 #4
0
 def setUp(self):
     numpy.set_printoptions(suppress=True, precision=3)
     numpy.random.seed(21)
     numpy.set_printoptions(threshold=numpy.nan, linewidth=100)
     
     #Use the example in the document
     self.numVertices = 10 
     self.numFeatures = 2 
     self.graph1 = SparseGraph(VertexList(self.numVertices, self.numFeatures))
     self.graph1.setVertices(range(self.numVertices), numpy.random.rand(self.numVertices, self.numFeatures))
     
     edges = numpy.array([[0,1], [0, 2], [0,4], [0,5], [0,8], [0,9]])
     self.graph1.addEdges(edges) 
     edges = numpy.array([[1,3], [1, 5], [1,6], [1,8], [2,9], [3,4], [3,5], [3,6], [3,7], [3,8], [3,9]])
     self.graph1.addEdges(edges)         
     edges = numpy.array([[4,2], [4, 7], [4,9], [5,8], [6, 7]])
     self.graph1.addEdges(edges)  
    
     self.graph2 = SparseGraph(VertexList(self.numVertices, self.numFeatures))
     self.graph2.setVertices(range(self.numVertices), numpy.random.rand(self.numVertices, self.numFeatures))
     
     edges = numpy.array([[0,3], [0, 4], [0,5], [0,8], [0,9], [1,2]])
     self.graph2.addEdges(edges) 
     edges = numpy.array([[1,3], [1,5], [1, 7], [1,8], [1,9], [2,3], [2,5], [3,5], [4,5], [4,6]])
     self.graph2.addEdges(edges)         
     edges = numpy.array([[4,9], [6, 8], [7,8], [7,9], [8, 9]])
     self.graph2.addEdges(edges)  
예제 #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 testGenerate2(self): 
     numVertices = 20
     graph = SparseGraph(GeneralVertexList(numVertices))
     p = 0.2
     generator = ErdosRenyiGenerator(p)
     graph = generator.generate(graph)
     
     self.assertTrue((graph.getNumEdges() - p*numVertices*numVertices/2) < 8)
예제 #7
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 testSequenceVectorStats(self):
        numFeatures = 1
        numVertices = 10
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

        graph.addEdge(0, 2)
        graph.addEdge(0, 1)

        subgraphIndices = [[0, 1, 3], [0, 1, 2, 3]]

        growthStatistics = GraphStatistics()
        statsList = growthStatistics.sequenceVectorStats(graph, subgraphIndices)
    def cvModelSelection(self, graph, paramList, paramFunc, folds, errorFunc):
        """
        ParamList is a list of lists of parameters and paramFunc
        is a list of the corresponding functions to call with the parameters
        as arguments. Note that a parameter can also be a tuple which is expanded
        out before the function is called. 

        e.g.
        paramList = [[1, 2], [2, 1], [12, 1]]
        paramFunc = [predictor.setC, predictor.setD]
        """

        inds = Sampling.crossValidation(folds, graph.getNumEdges())
        errors = numpy.zeros((len(paramList), folds))
        allEdges = graph.getAllEdges()

        for i in range(len(paramList)):
            paramSet = paramList[i]
            logging.debug("Using paramSet=" + str(paramSet))

            for j in range(len(paramSet)):
                if type(paramSet[j]) == tuple:
                    paramFunc[j](*paramSet[j])
                else: 
                    paramFunc[j](paramSet[j])

            predY = numpy.zeros(0)
            y = numpy.zeros(0)
            j = 0 

            for (trainInds, testInds) in inds:
                trainEdges = allEdges[trainInds, :]
                testEdges = allEdges[testInds, :]

                trainGraph = SparseGraph(graph.getVertexList(), graph.isUndirected())
                trainGraph.addEdges(trainEdges, graph.getEdgeValues(trainEdges))

                testGraph = SparseGraph(graph.getVertexList(), graph.isUndirected())
                testGraph.addEdges(testEdges, graph.getEdgeValues(testEdges))

                self.learnModel(trainGraph)

                predY = self.predictEdges(testGraph, testGraph.getAllEdges())
                y = testGraph.getEdgeValues(testGraph.getAllEdges())
                #Note that the order the edges is different in testGraphs as
                #opposed to graph when calling getAllEdges()

                errors[i, j] = errorFunc(y, predY)
                j = j+1 

            logging.info("Error of current fold: " + str(numpy.mean(errors[i, :])))

        meanErrors = numpy.mean(errors, 1)
        strErrors = numpy.std(errors, 1)

        return meanErrors, strErrors
예제 #10
0
    def testGraphInfuence(self):
        #We test the influence using a real graph 
        numVertices = 5
        numFeatures = 0

        vList = VertexList(numVertices, numFeatures)
        sGraph = SparseGraph(vList, False)

        sGraph.addEdge(0, 1, 0.1)
        sGraph.addEdge(0, 2, 0.5)
        sGraph.addEdge(1, 3, 0.9)
        sGraph.addEdge(2, 3, 0.7)
        sGraph.addEdge(2, 4, 0.8)

        P = sGraph.maxProductPaths()

        self.assertTrue((P[0, :] == numpy.array([0,0.1, 0.5, 0.35, 0.4])).all())
        self.assertTrue((P[1, :] == numpy.array([0,0,0,0.9,0])).all())
        self.assertTrue((P[2, :] == numpy.array([0,0,0,0.7,0.8])).all())
        self.assertTrue((P[3, :] == numpy.array([0,0,0,0,0])).all())
        self.assertTrue((P[4, :] == numpy.array([0,0,0,0,0])).all())

        k = 5
        influence = GreedyInfluence()
        inds = influence.maxInfluence(P, k)
    def testDegreeDistribution(self):
        numFeatures = 0
        numVertices = 100

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

        alpha = 10.0
        p = 0.01
        dim = 2
        generator = GeometricRandomGenerator(graph)
        graph = generator.generateGraph(alpha, p, dim)

        logging.debug((graph.degreeDistribution()))
    def testGenerate2(self):
        """
        Make sure that the generated degree is less than or equal to the given degree
        
        """
        numVertices = 10

        for i in range(10): 
            degSequence = numpy.random.randint(0, 3, numVertices)
            generator = ConfigModelGenerator(degSequence)
            graph = SparseGraph(GeneralVertexList(numVertices))
            graph = generator.generate(graph)

            self.assertTrue((graph.outDegreeSequence()<=degSequence).all())

        #We try to match an evolving degree sequence 
        degSequence1 = numpy.array([0,0,1,1,1,2,2,2,3, 4])
        degSequence2 = numpy.array([2,0,3,1,2,2,2,2,3, 4])
        degSequence3 = numpy.array([2,1,4,1,2,2,2,2,3, 6])

        generator = ConfigModelGenerator(degSequence1)
        graph = SparseGraph(GeneralVertexList(numVertices))
        graph = generator.generate(graph)
        self.assertTrue((degSequence1>= graph.outDegreeSequence()).all())

        deltaSequence = degSequence2 - graph.outDegreeSequence()
        generator = ConfigModelGenerator(deltaSequence)
        graph = generator.generate(graph, False)
        self.assertTrue((degSequence2>= graph.outDegreeSequence()).all())

        deltaSequence = degSequence3 - graph.outDegreeSequence()
        generator = ConfigModelGenerator(deltaSequence)
        graph = generator.generate(graph, False)
        self.assertTrue((degSequence3>= graph.outDegreeSequence()).all())
예제 #13
0
    def findInfoDecayGraph(self, egoTestFileName, alterTestFileName, egoIndicesR, alterIndices, egoIndicesNR, alterIndicesNR, egoFileName, alterFileName, missing=0):
        (egoTestArray, egoTitles) = self.readFile(egoTestFileName, self.egoTestIds, missing=0)
        (alterTestArray, alterTitles) = self.readFile(alterTestFileName, self.alterTestIds, missing=0)

        egoMarks = numpy.zeros(egoTestArray.shape[0])
        alterMarks = numpy.zeros(alterTestArray.shape[0])
        decays = numpy.zeros(egoIndicesR.shape[0])

        correctAnswers = numpy.array([1,2,4,5,8,9,10,12])
        wrongAnswers = numpy.array([3, 6, 7, 11])

        for i in range(egoTestArray.shape[0]):
            egoMarks[i] = numpy.intersect1d(egoTestArray[i], correctAnswers).shape[0]
            egoMarks[i] += wrongAnswers.shape[0] - numpy.intersect1d(egoTestArray[i], wrongAnswers).shape[0]

        for i in range(alterMarks.shape[0]):
            alterMarks[i] = numpy.intersect1d(alterTestArray[i], correctAnswers).shape[0]
            alterMarks[i] += wrongAnswers.shape[0] - numpy.intersect1d(alterTestArray[i], wrongAnswers).shape[0]

        """
        We just return how much the alter understood, since this represents the
        decay in understanding of the ego and transmission.
        """
        for i in range(decays.shape[0]):
            decays[i] = alterMarks[alterIndices[i]]

        #A lot of people could not be bothered to fill the questions and hence
        #get 4 correct by default 
        decays = (decays) / float(self.numTestQuestions)
        defaultDecay = 10**-6

        #Finally, put the data into a graph
        (egoArray, egoTitles) = self.readFile(egoFileName, self.egoQuestionIds, missing)
        (alterArray, alterTitles) = self.readFile(alterFileName, self.alterQuestionIds, missing)

        V = egoArray
        V = numpy.r_[V, alterArray[alterIndices, :]]
        V = numpy.r_[V, egoArray[alterIndicesNR, :]]
        vList = VertexList(V.shape[0], V.shape[1])
        vList.setVertices(V)
        graph = SparseGraph(vList, False)

        edgesR = numpy.c_[egoIndicesR, egoArray.shape[0]+numpy.arange(alterIndices.shape[0])]
        graph.addEdges(edgesR, decays)

        edgesNR = numpy.c_[egoIndicesNR, egoArray.shape[0]+alterIndices.shape[0]+numpy.arange(alterIndicesNR.shape[0])]
        graph.addEdges(edgesNR, numpy.ones(edgesNR.shape[0]) * defaultDecay)

        return graph
        def readFromFile(self, fileName):
            """
            Read vertices and edges of the graph from the given file name. The file
            must have as its first line "Vertices" followed by a list of
            vertex indices (one per line). Then the lines following "Arcs" or "Edges"
            have a list of pairs of vertex indices represented directed or undirected
            edges.
            """
            infile = open(fileName, "r")
            line = infile.readline()
            line = infile.readline()
            ind = 0
            vertexIdDict = {}

            while infile and line != "Edges" and line != "Arcs":
                vertexIdDict[int(line)] = ind
                line = infile.readline().strip()
                ind += 1 

            if line == "Edges":
                undirected = True
            elif line == "Arcs":
                undirected = False
            else:
                raise ValueError("Unknown edge types: " + line)

            numVertices = len(vertexIdDict)
            numFeatures = 0
            vList = VertexList(numVertices, numFeatures)
            sGraph = SparseGraph(vList, undirected)
            line = infile.readline()

            while line:
                s = line.split()
                try:
                    i = vertexIdDict[int(s[0].strip(',').strip())]
                    j = vertexIdDict[int(s[1].strip(',').strip())]
                    k = float(s[2].strip(',').strip())
                except KeyError:
                    print("Vertex not found in list of vertices.")
                    raise 

                sGraph.addEdge(i, j, k)
                line = infile.readline()

            logging.info("Read graph with " + str(numVertices) + " vertices and " + str(sGraph.getNumEdges()) + " edges")

            return sGraph
   def testGenerate(self):
       k = 2
       numVertices = 1000
       numFeatures = 0
 
       vList = VertexList(numVertices, numFeatures)
       initialGraph = SparseGraph(vList)
       initialGraph.addEdge(0, 1)
       initialGraph.addEdge(1, 2)
 
       for i in range(numVertices):
           initialGraph.addEdge(i, i)
 
       generator = KroneckerGenerator(initialGraph, k)
 
       graph = generator.generate()
       print (graph.size)
    def generate(self):
        """
        Generate a Kronecker graph using the adjacency matrix of the input graph.
 
        :returns: The generate graph as a SparseGraph object.
        """
        W = self.initialGraph.adjacencyMatrix()
        Wi = W
 
        for i in range(1, self.k):
            Wi = np.kron(Wi, W)
 
        vList = VertexList(Wi.shape[0], 0)
        graph = SparseGraph(vList, self.initialGraph.isUndirected())
        graph.setWeightMatrix(Wi)
 
        return graph
    def testDegreeDistribution(self):
        #We want to see how the degree distribution changes with kronecker powers


        numVertices = 3
        numFeatures = 0

        vList = VertexList(numVertices, numFeatures)
        initialGraph = SparseGraph(vList)
        initialGraph.addEdge(0, 1)
        initialGraph.addEdge(1, 2)

        for i in range(numVertices):
            initialGraph.addEdge(i, i)

        logging.debug((initialGraph.outDegreeSequence()))
        logging.debug((initialGraph.degreeDistribution()))

        k = 2
        generator = StochasticKroneckerGenerator(initialGraph, k)
        graph = generator.generateGraph()

        logging.debug((graph.outDegreeSequence()))
        logging.debug((graph.degreeDistribution()))

        k = 3
        generator = StochasticKroneckerGenerator(initialGraph, k)
        graph = generator.generateGraph()

        logging.debug((graph.degreeDistribution()))
    def testSequenceScalarStats(self):
        numFeatures = 1
        numVertices = 10
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

        graph.addEdge(0, 2)
        graph.addEdge(0, 1)

        subgraphIndices = [[0, 1, 3], [0, 1, 2, 3]]

        growthStatistics = GraphStatistics()
        statsArray = growthStatistics.sequenceScalarStats(graph, subgraphIndices)

        self.assertTrue(statsArray[0, 0] == 3.0)
        self.assertTrue(statsArray[1, 0] == 4.0)
        self.assertTrue(statsArray[0, 1] == 1.0)
        self.assertTrue(statsArray[1, 1] == 2.0)
    def generateGraph(self):
        """
        Generate a Kronecker graph
        """
        W = self.initialGraph.getWeightMatrix()
        Wi = W

        for i in range(1, self.k):
            Wi = numpy.kron(Wi, W)

        P = numpy.random.rand(Wi.shape[0], Wi.shape[0])
        Wi = numpy.array(P < Wi, numpy.float64)

        vList = VertexList(Wi.shape[0], 0)
        graph = SparseGraph(vList, self.initialGraph.isUndirected())
        graph.setWeightMatrix(Wi)

        return graph
 def setUp(self):    
     numpy.set_printoptions(suppress=True, linewidth=200, precision=5)
     self.numVertices = 10; 
     self.numFeatures = 2; 
     
     self.vList = VertexList(self.numVertices, self.numFeatures)
     self.graph = SparseGraph(self.vList)
     self.p = 0.1 
     self.erg = ErdosRenyiGenerator(self.p)
예제 #21
0
    def testFullTransGraph(self):
        transGraph = self.egoSimulator.fullTransGraph()

        #Create a simple graph and deterministic classifier
        numExamples = 10
        numFeatures = 3

        #Here, the first element is gender (say) with female = 0, male = 1
        vList = VertexList(numExamples, numFeatures)
        vList.setVertex(0, numpy.array([0,0,1]))
        vList.setVertex(1, numpy.array([1,0,0]))
        vList.setVertex(2, numpy.array([1,0,0]))
        vList.setVertex(3, numpy.array([1,0,0]))
        vList.setVertex(4, numpy.array([0,0,1]))
        vList.setVertex(5, numpy.array([0,0,1]))
        vList.setVertex(6, numpy.array([0,0,0]))
        vList.setVertex(7, numpy.array([1,0,0]))
        vList.setVertex(8, numpy.array([0,0,1]))
        vList.setVertex(9, numpy.array([1,0,0]))

        sGraph = SparseGraph(vList)
        sGraph.addEdge(0, 1, 1)
        sGraph.addEdge(0, 2, 1)
        sGraph.addEdge(0, 3, 1)
        sGraph.addEdge(4, 5, 1)
        sGraph.addEdge(4, 6, 1)
        sGraph.addEdge(6, 7, 1)
        sGraph.addEdge(6, 8, 1)
        sGraph.addEdge(6, 9, 1)

        simulator = EgoSimulator(sGraph, self.dc)
        logging.debug("Writing out full transmission graph")
        transGraph = simulator.fullTransGraph()

        self.assertEquals(transGraph.isUndirected(), False)
        self.assertEquals(transGraph.getNumEdges(), 11)
        self.assertEquals(transGraph.getEdge(0,1), 1)
        self.assertEquals(transGraph.getEdge(0,2), 1)
        self.assertEquals(transGraph.getEdge(0,3), 1)
        self.assertEquals(transGraph.getEdge(4,5), 1)
        self.assertEquals(transGraph.getEdge(4,6), 1)
        self.assertEquals(transGraph.getEdge(5,4), 1)
        self.assertEquals(transGraph.getEdge(6,4), 1)
        self.assertEquals(transGraph.getEdge(6,7), 1)
        self.assertEquals(transGraph.getEdge(6,8), 1)
        self.assertEquals(transGraph.getEdge(6,9), 1)
        self.assertEquals(transGraph.getEdge(8,6), 1)

        self.assertEquals(transGraph.getVertexList(), vList)
예제 #22
0
    def testTreeDepth(self):
        numVertices = 4
        numFeatures = 1

        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList, False)
        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 3)
        self.assertEquals(GraphUtils.treeDepth(graph), 2)

        numVertices = 5
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList, False)
        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 3)
        graph.addEdge(3, 4)
        self.assertEquals(GraphUtils.treeDepth(graph), 3)
    def readFromFile(self, fileName):
        inFile = open(fileName,"r")
        numFeatures = 1

        graphList = []
        line = inFile.readline()

        while line != "":
            #First 3 lines are useless
            
            inFile.readline()
            inFile.readline()

            #4th line has edge information
            line = inFile.readline()
            valueList = line.split(None)
            numVertices = int(valueList[0])
            #Not strictly the number of edges, as molecules can have multiple edges
            #between a pair of atoms 
            numEdges = int(valueList[1])

            vList = VertexList(numVertices, numFeatures)

            for i in range(numVertices):
                line = inFile.readline()
                valueList = line.split(None)
                vList.setVertex(i, numpy.array([self.atomDict[valueList[3]]]))

            graph = SparseGraph(vList)

            for i in range(numEdges):
                line = inFile.readline()
                valueList = line.split(None)
                graph.addEdge(int(valueList[0])-1, int(valueList[1])-1)
        
            graphList.append(graph)

            #Ignore next two lines
            inFile.readline()
            inFile.readline()
            line = inFile.readline()

        return graphList 
예제 #24
0
    def fullTransGraph(self):
        """
        This function will return a new graph which contains a directed edge if
        a transmission will occur between two vertices. 
        """
        if self.iteration != 0:
            raise ValueError("Must run fullTransGraph before advanceGraph")

        #First, find all the edges in the graph and create an ExampleList
        numEdges = self.edges.shape[0]
        X = numpy.zeros((numEdges*2, self.numPersonFeatures*2))
        ind = 0 

        for i in range(numEdges):
            vertex1 = self.graph.getVertex(self.edges[i,0])
            vertex2 = self.graph.getVertex(self.edges[i,1])

            X[ind, :] = numpy.r_[vertex1[0:self.numPersonFeatures], vertex2[0:self.numPersonFeatures]]
            X[ind+numEdges, :] = numpy.r_[vertex2[0:self.numPersonFeatures], vertex1[0:self.numPersonFeatures]]
            ind = ind + 1

        name = "X"
        examplesList = ExamplesList(X.shape[0])
        examplesList.addDataField(name, X)
        examplesList.setDefaultExamplesName(name)

        if self.preprocessor != None:
            X = self.preprocessor.process(examplesList.getDataField(examplesList.getDefaultExamplesName()))
            examplesList.overwriteDataField(examplesList.getDefaultExamplesName(), X)

        y = self.egoPairClassifier.classify(examplesList.getSampledDataField(name))
        fullTransmissionGraph = SparseGraph(self.graph.getVertexList(), False)

        transIndices = numpy.nonzero(y==1)[0]

        #Now, write out the transmission graph 
        for i in range(len(transIndices)):
            if transIndices[i] < numEdges:
                fullTransmissionGraph.addEdge(self.edges[transIndices[i],0], self.edges[transIndices[i],1])
            else:
                fullTransmissionGraph.addEdge(self.edges[transIndices[i]-numEdges,1], self.edges[transIndices[i]-numEdges,0])

        return fullTransmissionGraph
예제 #25
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)
예제 #26
0
    def testFitDiscretePowerLaw2(self):
        try:
            import networkx
        except ImportError:
            logging.debug("Networkx not found, can't run test")
            return

        nxGraph = networkx.barabasi_albert_graph(1000, 2)
        graph = SparseGraph.fromNetworkXGraph(nxGraph)
        degreeSeq = graph.outDegreeSequence()

        output = Util.fitDiscretePowerLaw(degreeSeq)
예제 #27
0
    def testLearnModel(self):
        numVertices = 100
        numFeatures = 1

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

        p = 0.2
        generator = ErdosRenyiGenerator(p)
        graph = generator.generate(graph)

        vertexIndices = list(range(0, numVertices))

        k = 2
        learner = GrowthLearner(k)

        tol = 10**-1

        #Lets test the values of alpha on a series of Erdos-Renyi graphs 
        for i in range(1, 6):
            p = float(i)/10
            graph.removeAllEdges()
            graph = generator.generate(graph)

            alpha = learner.learnModel(graph, vertexIndices)
            logging.debug((numpy.linalg.norm(alpha - numpy.array([p, 0]))))
            #self.assertTrue(numpy.linalg.norm(alpha - numpy.array([p, 0])) < tol)


        #Now test the learning on some preferencial attachment graphs
        ell = 10
        m = 8

        vertexIndices = list(range(ell, numVertices))
        graph.removeAllEdges()
        generator = BarabasiAlbertGenerator(ell, m)
        graph = generator.generate(graph)

        alpha = learner.learnModel(graph, vertexIndices)
        logging.debug(alpha)
    def clusterFromIterator(self, graphListIterator, timeIter=False):
        """
        Find a set of clusters for the graphs given by the iterator. 
        """
        clustersList = []
        timeList = [] 

        for subW in graphListIterator:
            logging.debug("Clustering graph of size " + str(subW.shape))
            #Create a SparseGraph
            startTime = time.time()
            graph = SparseGraph(GeneralVertexList(subW.shape[0]))
            graph.setWeightMatrixSparse(subW)
            iGraph = graph.toIGraph()

            vertexCluster = iGraph.community_leading_eigenvector(self.k)
            clustersList.append(vertexCluster.membership)
            timeList.append(time.time()-startTime)

        if timeIter:
            return clustersList, timeList
        else:
            return clustersList
    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 testGenerateGraph(self):
        k = 2
        numVertices = 3
        numFeatures = 0

        vList = VertexList(numVertices, numFeatures)
        initialGraph = SparseGraph(vList)
        initialGraph.addEdge(0, 1)
        initialGraph.addEdge(1, 2)

        for i in range(numVertices):
            initialGraph.addEdge(i, i)

        d = initialGraph.diameter()
        degreeSequence = initialGraph.outDegreeSequence()
        generator = StochasticKroneckerGenerator(initialGraph, k)

        graph = generator.generateGraph()
        d2 = graph.diameter()
        degreeSequence2 = graph.outDegreeSequence()

        self.assertTrue((numpy.kron(degreeSequence, degreeSequence) == degreeSequence2).all())
        self.assertTrue(graph.getNumVertices() == numVertices**k)
        self.assertTrue(graph.getNumDirEdges() == initialGraph.getNumDirEdges()**k)
        self.assertEquals(d, d2)

        #Try different k
        k = 3
        generator.setK(k)
        graph = generator.generateGraph()
        d3 = graph.diameter()
        degreeSequence3 = graph.outDegreeSequence()

        self.assertTrue((numpy.kron(degreeSequence, degreeSequence2) == degreeSequence3).all())
        self.assertTrue(graph.getNumVertices() == numVertices**k)
        self.assertTrue(graph.getNumDirEdges() == initialGraph.getNumDirEdges()**k)
        self.assertEquals(d, d3)

        #Test the multinomial degree distribution
        logging.debug(degreeSequence)
        logging.debug(degreeSequence2)
        logging.debug(degreeSequence3)
    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 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)
    return dia_matrix( (np.array( [np.array([2]*5),np.array([1]*5), np.array([1]*5)] ),np.array([0,1,-1]) ),shape=(5,5))

dx = np.linspace(0,1,5)
x = populatematrix1(len(dx))
print (x.shape, type(x))
y = populatematrix2(len(dx))
print (y.shape, type(y))
z = kron(x,y)
print (z.shape)

# Generating simple graph

numVertices = 5000

weightMatrix = scipy.sparse.lil_matrix((numVertices, numVertices))
graph = SparseGraph(numVertices, W=weightMatrix)
graph[0, 1] = 1
graph[0, 2] = 1

#Output the number of vertices
print(graph.size)

# PLotting Graph
a = np.reshape(np.random.random_integers(0,1,size=100),(10,10))
G= nx.DiGraph(a)
nx.draw(G)

# Computing Matrix Exponetaial 

Mat_Exp = scipy.linalg.expm(a, q=None)
    def testModularity(self):
        numVertices = 6
        graph = SparseGraph(GeneralVertexList(numVertices))

        graph.addEdge(0, 0)
        graph.addEdge(1, 1)
        graph.addEdge(2, 2)
        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 1)

        graph.addEdge(3, 4, 2)
        graph.addEdge(3, 5, 2)
        graph.addEdge(4, 5, 2)
        graph.addEdge(3, 3, 2)
        graph.addEdge(4, 4, 2)
        graph.addEdge(5, 5, 2)

        W = graph.getWeightMatrix()
        clustering = numpy.array([0, 0, 0, 1, 1, 1])

        #This is the same as the igraph result
        Q = GraphUtils.modularity(W, clustering)
        self.assertEquals(Q, 4.0 / 9.0)

        Ws = scipy.sparse.csr_matrix(W)
        Q = GraphUtils.modularity(Ws, clustering)
        self.assertEquals(Q, 4.0 / 9.0)

        W = numpy.ones((numVertices, numVertices))
        Q = GraphUtils.modularity(W, clustering)

        self.assertEquals(Q, 0.0)

        Ws = scipy.sparse.csr_matrix(W)
        Q = GraphUtils.modularity(Ws, clustering)
        self.assertEquals(Q, 0.0)
예제 #35
0
"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

import numpy
import scipy.sparse as sps
from apgl.graph.GeneralVertexList import GeneralVertexList
from apgl.graph.SparseGraph import SparseGraph

numVertices = 10
vList = GeneralVertexList(numVertices)
wght = sps.csc_matrix(numVertices, numVertices)
graph = SparseGraph(vList, W=wght, undirected=False)

graph[0, 1] = 1
graph[0, 2] = 1
graph.setVertex(0, "abc")
graph.setVertex(1, 123)

print(graph)
    def cvModelSelection(self, graph, paramList, paramFunc, folds, errorFunc):
        """
        ParamList is a list of lists of parameters and paramFunc
        is a list of the corresponding functions to call with the parameters
        as arguments. Note that a parameter can also be a tuple which is expanded
        out before the function is called. 

        e.g.
        paramList = [[1, 2], [2, 1], [12, 1]]
        paramFunc = [predictor.setC, predictor.setD]
        """

        inds = Sampling.crossValidation(folds, graph.getNumEdges())
        errors = numpy.zeros((len(paramList), folds))
        allEdges = graph.getAllEdges()

        for i in range(len(paramList)):
            paramSet = paramList[i]
            logging.debug("Using paramSet=" + str(paramSet))

            for j in range(len(paramSet)):
                if type(paramSet[j]) == tuple:
                    paramFunc[j](*paramSet[j])
                else:
                    paramFunc[j](paramSet[j])

            predY = numpy.zeros(0)
            y = numpy.zeros(0)
            j = 0

            for (trainInds, testInds) in inds:
                trainEdges = allEdges[trainInds, :]
                testEdges = allEdges[testInds, :]

                trainGraph = SparseGraph(graph.getVertexList(),
                                         graph.isUndirected())
                trainGraph.addEdges(trainEdges,
                                    graph.getEdgeValues(trainEdges))

                testGraph = SparseGraph(graph.getVertexList(),
                                        graph.isUndirected())
                testGraph.addEdges(testEdges, graph.getEdgeValues(testEdges))

                self.learnModel(trainGraph)

                predY = self.predictEdges(testGraph, testGraph.getAllEdges())
                y = testGraph.getEdgeValues(testGraph.getAllEdges())
                #Note that the order the edges is different in testGraphs as
                #opposed to graph when calling getAllEdges()

                errors[i, j] = errorFunc(y, predY)
                j = j + 1

            logging.info("Error of current fold: " +
                         str(numpy.mean(errors[i, :])))

        meanErrors = numpy.mean(errors, 1)
        strErrors = numpy.std(errors, 1)

        return meanErrors, strErrors
    def testKwayNormalisedCut(self):
        numVertices = 6
        graph = SparseGraph(GeneralVertexList(numVertices))

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 1)

        graph.addEdge(3, 4)
        graph.addEdge(3, 5)
        graph.addEdge(5, 4)

        W = graph.getWeightMatrix()
        clustering = numpy.array([0, 0, 0, 1, 1, 1])

        self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 0.0)

        #Try sparse W
        Ws = scipy.sparse.csr_matrix(W)
        self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 0.0)

        graph.addEdge(2, 3)
        W = graph.getWeightMatrix()
        self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 1.0 / 7)

        Ws = scipy.sparse.csr_matrix(W)
        self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering),
                          1.0 / 7)

        clustering = numpy.array([0, 0, 0, 1, 1, 2])
        self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering),
                          61.0 / 105)

        self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering),
                          61.0 / 105)

        #Test two vertices without any edges
        W = numpy.zeros((2, 2))
        clustering = numpy.array([0, 1])
        self.assertEquals(GraphUtils.kwayNormalisedCut(W, clustering), 0.0)

        Ws = scipy.sparse.csr_matrix(W)
        self.assertEquals(GraphUtils.kwayNormalisedCut(Ws, clustering), 0.0)
예제 #38
0
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.VertexList import VertexList
from apgl.generator.SmallWorldGenerator import SmallWorldGenerator
from apgl.io.PajekWriter import PajekWriter
import unittest
import cProfile
import pstats


profileFileName = "profile.cprof"

p = 0.5
k = 15

numVertices = 200
numFeatures = 5

vList = VertexList(numVertices, numFeatures)
sGraph = SparseGraph(vList)
swg = SmallWorldGenerator(p, k)

cProfile.runctx('swg.generate(sGraph)', globals(), locals(), profileFileName)
stats = pstats.Stats(profileFileName)
stats.strip_dirs().sort_stats("cumulative").print_stats(20)
예제 #39
0
"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

from apgl.graph.GeneralVertexList import GeneralVertexList
from apgl.graph.SparseGraph import SparseGraph

numVertices = 10
graph = SparseGraph(GeneralVertexList(numVertices))

graph[0, 1] = 1
graph[0, 2] = 1

P = graph.floydWarshall()
print("geodesicDistance:", graph.geodesicDistance(P=P))
print("harmonicGeodesicDistance:", graph.harmonicGeodesicDistance(P=P))

    def testConcat(self):
        numVertices = 5
        graph = SparseGraph(GeneralVertexList(numVertices))
        graph.addEdge(1, 1, 0.1)
        graph.addEdge(1, 3, 0.5)
        graph.addEdge(2, 4, 1)
        graph.addEdge(2, 3, 2)
        graph.setVertex(0, "abc")

        graph2 = SparseGraph(GeneralVertexList(numVertices))
        graph2.addEdge(1, 1)
        graph2.addEdge(1, 4)
        graph2.setVertex(1, "def")

        graph3 = graph.concat(graph2)

        self.assertTrue(graph3.getNumVertices, 10)
        self.assertEquals(graph3.getVertex(0), "abc")
        self.assertEquals(graph3.getVertex(6), "def")
        self.assertEquals(graph3.getEdge(1, 1), 0.1)
        self.assertEquals(graph3.getEdge(1, 3), 0.5)
        self.assertEquals(graph3.getEdge(2, 4), 1)
        self.assertEquals(graph3.getEdge(2, 3), 2)

        self.assertEquals(graph3.getEdge(6, 6), 1)
        self.assertEquals(graph3.getEdge(6, 9), 1)
    def testGenerate(self):
        degSequence = numpy.array([2, 1, 3, 0, 0, 0, 0, 0, 0, 1])
        generator = ConfigModelGenerator(degSequence)

        numVertices = 10
        graph = SparseGraph(GeneralVertexList(numVertices))
        graph = generator.generate(graph)

        tol = 3
        self.assertTrue(
            numpy.linalg.norm(degSequence - graph.degreeSequence()) < tol)

        degSequence = numpy.array([2, 1, 3, 0, 2, 1, 4, 0, 0, 1])
        generator.setOutDegSequence(degSequence)
        graph.removeAllEdges()
        graph = generator.generate(graph)

        self.assertTrue(
            numpy.linalg.norm(degSequence - graph.degreeSequence()) < tol)

        #Test using a non-empty graph
        degSequence = numpy.array([0, 0, 0, 2, 0, 0, 0, 1, 1, 0])
        generator.setOutDegSequence(degSequence)
        oldDegSequence = graph.degreeSequence()

        self.assertRaises(ValueError, generator.generate, graph, True)
        graph = generator.generate(graph, False)

        diffSequence = graph.degreeSequence() - oldDegSequence
        self.assertTrue(numpy.linalg.norm(degSequence - diffSequence) < tol)

        #Test the case where we also have an in-degree sequence
        degSequence = numpy.array([2, 1, 3, 0, 0, 0, 0, 0, 0, 1])
        inDegSequence = numpy.array([1, 1, 1, 1, 1, 1, 1, 0, 0, 0])
        generator = ConfigModelGenerator(degSequence, inDegSequence)

        graph = SparseGraph(GeneralVertexList(numVertices))
        self.assertRaises(ValueError, generator.generate, graph)

        graph = SparseGraph(GeneralVertexList(numVertices), False)
        graph = generator.generate(graph)

        self.assertTrue(
            numpy.linalg.norm(degSequence - graph.outDegreeSequence()) < tol)
        self.assertTrue(
            numpy.linalg.norm(inDegSequence - graph.inDegreeSequence()) < tol)

        outDegSequence = numpy.array([2, 1, 3, 0, 2, 1, 4, 0, 0, 1])
        inDegSequence = numpy.array([1, 2, 1, 1, 2, 1, 2, 1, 2, 1])
        generator.setOutDegSequence(outDegSequence)
        generator.setInDegSequence(inDegSequence)
        graph.removeAllEdges()
        graph = generator.generate(graph)

        self.assertTrue(
            numpy.linalg.norm(outDegSequence -
                              graph.outDegreeSequence()) < tol)
        self.assertTrue(
            numpy.linalg.norm(inDegSequence - graph.inDegreeSequence()) < tol)

        #In the case that the in-degree sequence sum larger than that of the out-degree it is
        #not satisfied, but the out-degree should be.
        inDegSequence = numpy.array([1, 2, 1, 1, 2, 1, 2, 1, 5, 6])
        generator.setInDegSequence(inDegSequence)
        graph.removeAllEdges()
        graph = generator.generate(graph)
        self.assertTrue(
            numpy.linalg.norm(outDegSequence -
                              graph.outDegreeSequence()) < tol)

        #Now try the other way around
        generator.setOutDegSequence(inDegSequence)
        generator.setInDegSequence(outDegSequence)
        graph.removeAllEdges()
        graph = generator.generate(graph)
        self.assertTrue(
            numpy.linalg.norm(outDegSequence - graph.inDegreeSequence()) < tol)

        #Test growing graph
        outDegSequence = numpy.array([2, 1, 3, 0, 2, 1, 4, 0, 0, 1])
        inDegSequence = numpy.array([1, 2, 1, 1, 2, 1, 2, 1, 2, 1])

        generator.setOutDegSequence(outDegSequence)
        generator.setInDegSequence(inDegSequence)
        graph.removeAllEdges()
        graph = generator.generate(graph)

        newOutDegreeSequence = numpy.array([2, 1, 3, 5, 2, 1, 4, 0, 0, 1])
        newInDegreeSequence = numpy.array([2, 3, 2, 2, 3, 1, 2, 1, 2, 1])
        diffOutSequence = newOutDegreeSequence - graph.outDegreeSequence()
        diffInSequence = newInDegreeSequence - graph.inDegreeSequence()
        generator.setOutDegSequence(diffOutSequence)
        generator.setInDegSequence(diffInSequence)
        graph = generator.generate(graph, False)

        self.assertTrue(
            numpy.linalg.norm(newOutDegreeSequence -
                              graph.outDegreeSequence()) < tol)
        self.assertTrue(
            numpy.linalg.norm(newInDegreeSequence -
                              graph.inDegreeSequence()) < tol)
예제 #42
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)
예제 #43
0
    def testMatch(self):
        matcher = GraphMatch(algorithm="U", alpha=0.3)
        permutation, distance, time = matcher.match(self.graph1, self.graph2)

        #Checked output file - seems correct

        distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                                   permutation)
        self.assertAlmostEquals(distance[0], distance2)

        #Now test case in which alpha is different
        matcher = GraphMatch(algorithm="U", alpha=0.5)
        permutation, distance, time = matcher.match(self.graph1, self.graph2)
        distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                                   permutation)
        self.assertAlmostEquals(distance[0], distance2)

        #Test normalised distance
        alpha = 0.0
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, self.graph2)
        distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                     permutation, True)
        self.assertAlmostEquals(distance[1], distance2)

        alpha = 1.0
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, self.graph2)
        distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                     permutation, True)
        self.assertAlmostEquals(distance[1], distance2, 5)

        #Test empty graph
        alpha = 0.0
        graph1 = SparseGraph(VertexList(0, 0))
        graph2 = SparseGraph(VertexList(0, 0))

        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     graph1, graph2)

        nptst.assert_array_equal(permutation, numpy.array([], numpy.int))
        self.assertEquals(distance, [0, 0, 0])

        #Test where 1 graph is empty
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     graph1, self.graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
        self.assertEquals(distance[1], 1)
        self.assertEquals(distance[2], 1)

        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
        self.assertEquals(distance[1], 1)
        self.assertEquals(distance[2], 1)

        alpha = 1.0
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     graph1, self.graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])

        V2 = self.graph1.vlist.getVertices()
        V1 = numpy.zeros(V2.shape)
        C = GraphMatch(algorithm="U", alpha=alpha).matrixSimilarity(V1, V2)
        dist = numpy.trace(C) / numpy.linalg.norm(C)

        self.assertAlmostEquals(distance[1], -dist, 4)
        self.assertAlmostEquals(distance[2], -dist, 4)

        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
        self.assertAlmostEquals(distance[1], -dist, 4)
        self.assertAlmostEquals(distance[2], -dist, 4)

        #Test one graph which is a subgraph of another
        p = 0.2
        k = 10
        numVertices = 20
        generator = SmallWorldGenerator(p, k)
        graph = SparseGraph(VertexList(numVertices, 2))
        graph = generator.generate(graph)

        subgraphInds = numpy.random.permutation(numVertices)[0:10]
        subgraph = graph.subgraph(subgraphInds)

        matcher = GraphMatch(algorithm="U", alpha=0.0)
        permutation, distance, time = matcher.match(graph, subgraph)
        distance = matcher.distance(graph, subgraph, permutation, True, True)

        self.assertTrue(distance < 1)
    def testCvModelSelection(self):
        numVertices = 10
        numFeatures = 1

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

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(3, 4)
        graph.addEdge(5, 6)
        graph.addEdge(4, 6)
        graph.addEdge(9, 8)
        graph.addEdge(9, 7)
        graph.addEdge(9, 6)

        windowSize = 3
        predictor = RandomEdgePredictor(windowSize)

        folds = 5
        paramList = [[1, 2], [2, 1], [12, 1]]
        paramFunc = [predictor.setC, predictor.setD]

        errors = predictor.cvModelSelection(graph, paramList, paramFunc, folds)

        self.assertTrue(errors.shape[0] == len(paramList))

        for i in range(errors.shape[0]): 
            self.assertTrue(errors[i]>= 0 and errors[i]<= 1)
예제 #45
0
"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

from apgl.graph.DictGraph import DictGraph
from apgl.graph.SparseGraph import SparseGraph
from apgl.graph.GeneralVertexList import GeneralVertexList

graph = DictGraph()
graph.addEdge("a", "b")
graph.addEdge("a", "c")
graph.addEdge("a", "d")

edgeIndices = graph.getAllEdgeIndices()
graph2 = SparseGraph(GeneralVertexList(graph.getNumVertices()))
graph2.addEdges(edgeIndices)

    def testNativeAdjacencyMatrix(self):
        numVertices = 10
        graph = SparseGraph(GeneralVertexList(numVertices))

        graph.addEdge(1, 1, 0.1)
        graph.addEdge(1, 3, 0.5)
        graph.addEdge(2, 5, 1)
        graph.addEdge(7, 0, 2)

        A = graph.nativeAdjacencyMatrix()
        self.assertEquals(A[0, 7], 1)
        self.assertEquals(A[7, 0], 1)
        self.assertEquals(A[1, 3], 1)
        self.assertEquals(A[3, 1], 1)
        self.assertEquals(A[1, 1], 1)
        self.assertEquals(A[2, 5], 1)
        self.assertEquals(A[5, 2], 1)
        self.assertEquals(A.getnnz(), 7)

        graph = SparseGraph(GeneralVertexList(numVertices), False)
        graph.addEdge(1, 1, 0.1)
        graph.addEdge(1, 3, 0.5)
        graph.addEdge(2, 5, 1)

        A = graph.nativeAdjacencyMatrix()
        self.assertEquals(A[1, 3], 1)
        self.assertEquals(A[1, 1], 1)
        self.assertEquals(A[2, 5], 1)
        self.assertEquals(A.getnnz(), 3)
예제 #47
0
    def testUnNormSpectralClusterer(self):
        numVertices = 10
        numFeatures = 0

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

        #We form two cliques with an edge between then
        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(1, 3)
        graph.addEdge(2, 3)

        graph.addEdge(2, 4)

        graph.addEdge(4, 5)
        graph.addEdge(4, 6)
        graph.addEdge(4, 7)
        graph.addEdge(5, 6)
        graph.addEdge(5, 7)
        graph.addEdge(6, 7)
        graph.addEdge(7, 8)
        graph.addEdge(7, 9)

        #graph.addEdge(0, 4)

        k = 3
        clusterer = SpectralClusterer(k)
        clusters = clusterer.cluster(graph)

        self.assertEquals(clusters.shape[0], numVertices)
        self.assertEquals(numpy.unique(clusters).shape[0], k)
        logging.debug(clusters)

        realClusters = numpy.array([1,1,1,1, 0,0,0,0, 2,2])

        similarityMatrix1 = numpy.zeros((numVertices, numVertices))
        similarityMatrix2 = numpy.zeros((numVertices, numVertices))

        for i in range(numVertices):
            for j in range(numVertices):
                if clusters[i] == clusters[j]:
                    similarityMatrix1[i, j] = 1

                if realClusters[i] == realClusters[j]:
                    similarityMatrix2[i, j] = 1     

        self.assertTrue((similarityMatrix1 == similarityMatrix2).all())
    def testTreeDepth(self):
        numVertices = 4
        numFeatures = 1

        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList, False)
        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 3)
        self.assertEquals(GraphUtils.treeDepth(graph), 2)

        numVertices = 5
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList, False)
        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 3)
        graph.addEdge(3, 4)
        self.assertEquals(GraphUtils.treeDepth(graph), 3)
    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
예제 #50
0
    def testDistance(self):
        permutation = numpy.arange(self.numVertices)
        dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph1,
                                              permutation)
        self.assertEquals(dist, 0.0)

        dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                              permutation)
        self.assertAlmostEquals(dist, 50.0)

        permutation = numpy.arange(self.numVertices)
        permutation[8] = 9
        permutation[9] = 8
        dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                              permutation)
        self.assertAlmostEquals(dist, 54.0)

        #Try graphs of unequal size
        graph3 = self.graph1.subgraph(range(8))
        permutation = numpy.arange(self.numVertices)
        dist1 = GraphMatch(alpha=0.0).distance(self.graph1, graph3,
                                               permutation)
        dist1a = GraphMatch(alpha=0.0).distance(graph3, self.graph1,
                                                permutation)
        self.assertEquals(dist1, dist1a)

        graph3 = self.graph1.subgraph(range(5))
        dist2 = GraphMatch(alpha=0.0).distance(self.graph1, graph3,
                                               permutation)
        dist2a = GraphMatch(alpha=0.0).distance(graph3, self.graph1,
                                                permutation)
        self.assertEquals(dist2, dist2a)
        self.assertTrue(dist1 < dist2)

        #Test case where alpha!=0
        alpha = 1.0
        permutation = numpy.arange(self.numVertices)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                    permutation, False)
        C = GraphMatch(alpha=alpha).vertexSimilarities(self.graph1,
                                                       self.graph2)
        distance2 = -numpy.trace(C)
        self.assertEquals(distance, distance2)

        #Check case where we want non negativve distance even when alpha!=0
        distance = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                    permutation, True, True)
        self.assertTrue(distance >= 0)

        permutation = numpy.arange(self.numVertices)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, self.graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 0)

        #Check case where both graphs are empty
        graph1 = SparseGraph(VertexList(0, 0))
        graph2 = SparseGraph(VertexList(0, 0))

        permutation = numpy.array([], numpy.int)
        distance = GraphMatch(alpha=alpha).distance(graph1, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 0)

        #Now, just one graph is empty
        #Distance is always 1 due to normalisations
        alpha = 0.0
        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph2, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        #distance = GraphMatch(alpha=alpha).distance(self.graph1, graph1, permutation, False, False)
        #self.assertEquals(distance, numpy.linalg.norm(self.graph1.getWeightMatrix())**2)

        alpha = 0.9
        matcher = GraphMatch("U", alpha=alpha)
        permutation, distanceVector, time = matcher.match(self.graph2, graph1)
        distance = matcher.distance(self.graph2, graph1, permutation, True,
                                    True)
        self.assertEquals(distance, 1.0)

        alpha = 1.0
        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph2, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        alpha = 0.5
        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph2, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        #Test on unequal graphs and compare against distance from graphm
        alpha = 0.5
        matcher = GraphMatch(alpha=alpha)
        permutation, distanceVector, time = matcher.match(
            self.graph1, self.graph2)
        distance = matcher.distance(self.graph1, self.graph2, permutation,
                                    True, False)

        self.assertAlmostEquals(distanceVector[1], distance, 3)
예제 #51
0
import time
import numpy
import scipy.sparse.linalg
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
예제 #52
0
class GraphMatchTest(unittest.TestCase):
    def setUp(self):
        numpy.set_printoptions(suppress=True, precision=3)
        numpy.random.seed(21)
        numpy.set_printoptions(threshold=numpy.nan, linewidth=100)

        #Use the example in the document
        self.numVertices = 10
        self.numFeatures = 2
        self.graph1 = SparseGraph(
            VertexList(self.numVertices, self.numFeatures))
        self.graph1.setVertices(
            range(self.numVertices),
            numpy.random.rand(self.numVertices, self.numFeatures))

        edges = numpy.array([[0, 1], [0, 2], [0, 4], [0, 5], [0, 8], [0, 9]])
        self.graph1.addEdges(edges)
        edges = numpy.array([[1, 3], [1, 5], [1, 6], [1, 8], [2, 9], [3, 4],
                             [3, 5], [3, 6], [3, 7], [3, 8], [3, 9]])
        self.graph1.addEdges(edges)
        edges = numpy.array([[4, 2], [4, 7], [4, 9], [5, 8], [6, 7]])
        self.graph1.addEdges(edges)

        self.graph2 = SparseGraph(
            VertexList(self.numVertices, self.numFeatures))
        self.graph2.setVertices(
            range(self.numVertices),
            numpy.random.rand(self.numVertices, self.numFeatures))

        edges = numpy.array([[0, 3], [0, 4], [0, 5], [0, 8], [0, 9], [1, 2]])
        self.graph2.addEdges(edges)
        edges = numpy.array([[1, 3], [1, 5], [1, 7], [1, 8], [1, 9], [2, 3],
                             [2, 5], [3, 5], [4, 5], [4, 6]])
        self.graph2.addEdges(edges)
        edges = numpy.array([[4, 9], [6, 8], [7, 8], [7, 9], [8, 9]])
        self.graph2.addEdges(edges)

    def testMatch(self):
        matcher = GraphMatch(algorithm="U", alpha=0.3)
        permutation, distance, time = matcher.match(self.graph1, self.graph2)

        #Checked output file - seems correct

        distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                                   permutation)
        self.assertAlmostEquals(distance[0], distance2)

        #Now test case in which alpha is different
        matcher = GraphMatch(algorithm="U", alpha=0.5)
        permutation, distance, time = matcher.match(self.graph1, self.graph2)
        distance2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                                   permutation)
        self.assertAlmostEquals(distance[0], distance2)

        #Test normalised distance
        alpha = 0.0
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, self.graph2)
        distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                     permutation, True)
        self.assertAlmostEquals(distance[1], distance2)

        alpha = 1.0
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, self.graph2)
        distance2 = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                     permutation, True)
        self.assertAlmostEquals(distance[1], distance2, 5)

        #Test empty graph
        alpha = 0.0
        graph1 = SparseGraph(VertexList(0, 0))
        graph2 = SparseGraph(VertexList(0, 0))

        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     graph1, graph2)

        nptst.assert_array_equal(permutation, numpy.array([], numpy.int))
        self.assertEquals(distance, [0, 0, 0])

        #Test where 1 graph is empty
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     graph1, self.graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
        self.assertEquals(distance[1], 1)
        self.assertEquals(distance[2], 1)

        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
        self.assertEquals(distance[1], 1)
        self.assertEquals(distance[2], 1)

        alpha = 1.0
        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     graph1, self.graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])

        V2 = self.graph1.vlist.getVertices()
        V1 = numpy.zeros(V2.shape)
        C = GraphMatch(algorithm="U", alpha=alpha).matrixSimilarity(V1, V2)
        dist = numpy.trace(C) / numpy.linalg.norm(C)

        self.assertAlmostEquals(distance[1], -dist, 4)
        self.assertAlmostEquals(distance[2], -dist, 4)

        permutation, distance, time = GraphMatch(algorithm="U",
                                                 alpha=alpha).match(
                                                     self.graph1, graph1)
        self.assertEquals(
            numpy.linalg.norm(self.graph1.getWeightMatrix())**2, distance[0])
        self.assertAlmostEquals(distance[1], -dist, 4)
        self.assertAlmostEquals(distance[2], -dist, 4)

        #Test one graph which is a subgraph of another
        p = 0.2
        k = 10
        numVertices = 20
        generator = SmallWorldGenerator(p, k)
        graph = SparseGraph(VertexList(numVertices, 2))
        graph = generator.generate(graph)

        subgraphInds = numpy.random.permutation(numVertices)[0:10]
        subgraph = graph.subgraph(subgraphInds)

        matcher = GraphMatch(algorithm="U", alpha=0.0)
        permutation, distance, time = matcher.match(graph, subgraph)
        distance = matcher.distance(graph, subgraph, permutation, True, True)

        self.assertTrue(distance < 1)

    def testDistance(self):
        permutation = numpy.arange(self.numVertices)
        dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph1,
                                              permutation)
        self.assertEquals(dist, 0.0)

        dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                              permutation)
        self.assertAlmostEquals(dist, 50.0)

        permutation = numpy.arange(self.numVertices)
        permutation[8] = 9
        permutation[9] = 8
        dist = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                              permutation)
        self.assertAlmostEquals(dist, 54.0)

        #Try graphs of unequal size
        graph3 = self.graph1.subgraph(range(8))
        permutation = numpy.arange(self.numVertices)
        dist1 = GraphMatch(alpha=0.0).distance(self.graph1, graph3,
                                               permutation)
        dist1a = GraphMatch(alpha=0.0).distance(graph3, self.graph1,
                                                permutation)
        self.assertEquals(dist1, dist1a)

        graph3 = self.graph1.subgraph(range(5))
        dist2 = GraphMatch(alpha=0.0).distance(self.graph1, graph3,
                                               permutation)
        dist2a = GraphMatch(alpha=0.0).distance(graph3, self.graph1,
                                                permutation)
        self.assertEquals(dist2, dist2a)
        self.assertTrue(dist1 < dist2)

        #Test case where alpha!=0
        alpha = 1.0
        permutation = numpy.arange(self.numVertices)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                    permutation, False)
        C = GraphMatch(alpha=alpha).vertexSimilarities(self.graph1,
                                                       self.graph2)
        distance2 = -numpy.trace(C)
        self.assertEquals(distance, distance2)

        #Check case where we want non negativve distance even when alpha!=0
        distance = GraphMatch(alpha=alpha).distance(self.graph1, self.graph2,
                                                    permutation, True, True)
        self.assertTrue(distance >= 0)

        permutation = numpy.arange(self.numVertices)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, self.graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 0)

        #Check case where both graphs are empty
        graph1 = SparseGraph(VertexList(0, 0))
        graph2 = SparseGraph(VertexList(0, 0))

        permutation = numpy.array([], numpy.int)
        distance = GraphMatch(alpha=alpha).distance(graph1, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 0)

        #Now, just one graph is empty
        #Distance is always 1 due to normalisations
        alpha = 0.0
        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph2, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        #distance = GraphMatch(alpha=alpha).distance(self.graph1, graph1, permutation, False, False)
        #self.assertEquals(distance, numpy.linalg.norm(self.graph1.getWeightMatrix())**2)

        alpha = 0.9
        matcher = GraphMatch("U", alpha=alpha)
        permutation, distanceVector, time = matcher.match(self.graph2, graph1)
        distance = matcher.distance(self.graph2, graph1, permutation, True,
                                    True)
        self.assertEquals(distance, 1.0)

        alpha = 1.0
        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph1, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph2, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        alpha = 0.5
        permutation = numpy.arange(10, dtype=numpy.int)
        distance = GraphMatch(alpha=alpha).distance(self.graph2, graph1,
                                                    permutation, True, True)
        self.assertEquals(distance, 1.0)

        #Test on unequal graphs and compare against distance from graphm
        alpha = 0.5
        matcher = GraphMatch(alpha=alpha)
        permutation, distanceVector, time = matcher.match(
            self.graph1, self.graph2)
        distance = matcher.distance(self.graph1, self.graph2, permutation,
                                    True, False)

        self.assertAlmostEquals(distanceVector[1], distance, 3)

    def testDistance2(self):
        permutation = numpy.arange(self.numVertices)
        dist = GraphMatch(alpha=0.0).distance2(self.graph1, self.graph1,
                                               permutation)
        self.assertEquals(dist, 0.0)

        dist = GraphMatch(alpha=0.0).distance2(self.graph1, self.graph2,
                                               permutation)
        dist2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                               permutation, True)
        self.assertAlmostEquals(dist, dist2)

        permutation = numpy.arange(self.numVertices)
        permutation[8] = 9
        permutation[9] = 8
        dist = GraphMatch(alpha=0.0).distance2(self.graph1, self.graph2,
                                               permutation)
        dist2 = GraphMatch(alpha=0.0).distance(self.graph1, self.graph2,
                                               permutation, True)
        self.assertAlmostEquals(dist, dist2)

        #Try graphs of unequal size
        graph3 = self.graph1.subgraph(range(8))
        permutation = numpy.arange(self.numVertices)
        dist1 = GraphMatch(alpha=0.0).distance2(self.graph1, graph3,
                                                permutation)
        dist1a = GraphMatch(alpha=0.0).distance2(graph3, self.graph1,
                                                 permutation)
        self.assertEquals(dist1, dist1a)

        graph3 = self.graph1.subgraph(range(5))
        dist2 = GraphMatch(alpha=0.0).distance2(self.graph1, graph3,
                                                permutation)
        dist2a = GraphMatch(alpha=0.0).distance2(graph3, self.graph1,
                                                 permutation)
        self.assertEquals(dist2, dist2a)
        self.assertTrue(dist1 < dist2)

        #Test case where alpha!=0
        alpha = 1.0
        permutation = numpy.arange(self.numVertices)
        distance = GraphMatch(alpha=alpha).distance2(self.graph1, self.graph1,
                                                     permutation)
        self.assertEquals(distance, 0.0)

        #Check distances are between 0 and 1
        for i in range(100):
            alpha = numpy.random.rand()
            permutation = numpy.random.permutation(self.numVertices)

            distance = GraphMatch(alpha=alpha).distance2(
                self.graph1, self.graph1, permutation)
            self.assertTrue(0 <= distance <= 1)

    def testVertexSimilarities(self):
        matcher = GraphMatch(alpha=0.0)
        C = matcher.vertexSimilarities(self.graph1, self.graph1)

        Cdiag = numpy.diag(C)
        nptst.assert_array_almost_equal(Cdiag, numpy.ones(Cdiag.shape[0]))

        #Now compute trace(C)/||C||
        #print(numpy.trace(C)/numpy.linalg.norm(C))

        #Test use of feature inds
        matcher = GraphMatch(alpha=0.0, featureInds=numpy.array([0]))

        C = matcher.vertexSimilarities(self.graph1, self.graph2)

        #Now, let's vary the non-used feature
        self.graph1.vlist[:, 1] = 0
        C2 = matcher.vertexSimilarities(self.graph1, self.graph2)
        nptst.assert_array_equal(C, C2)

        self.graph2.vlist[:, 1] = 0
        C2 = matcher.vertexSimilarities(self.graph1, self.graph2)
        nptst.assert_array_equal(C, C2)

        #Vary used feature
        self.graph1.vlist[:, 0] = 0
        C2 = matcher.vertexSimilarities(self.graph1, self.graph2)
        self.assertTrue((C != C2).any())

    def testMatrixSimilarity(self):
        numExamples = 5
        numFeatures = 3
        V1 = numpy.random.rand(numExamples, numFeatures)

        matcher = GraphMatch(alpha=0.0)
        C = matcher.matrixSimilarity(V1, V1)
        Cdiag = numpy.diag(C)
        nptst.assert_array_almost_equal(Cdiag, numpy.ones(Cdiag.shape[0]))

        V1[:, 2] *= 10
        C2 = matcher.matrixSimilarity(V1, V1)
        Cdiag = numpy.diag(C2)
        nptst.assert_array_almost_equal(Cdiag, numpy.ones(Cdiag.shape[0]))
        nptst.assert_array_almost_equal(C, C2)

        #print("Running match")
        J = numpy.ones((numExamples, numFeatures))
        Z = numpy.zeros((numExamples, numFeatures))

        C2 = matcher.matrixSimilarity(J, Z)
        #This should be 1 ideally

        nptst.assert_array_almost_equal(C2, numpy.ones(C2.shape))

        C2 = matcher.matrixSimilarity(J, J)
        nptst.assert_array_almost_equal(C2, numpy.ones(C2.shape))
    def testIndicesFromScores(self):
        numVertices = 10
        numFeatures = 1

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

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(0, 3)
        graph.addEdge(1, 2)
        graph.addEdge(3, 4)
        graph.addEdge(5, 6)
        graph.addEdge(4, 6)
        graph.addEdge(9, 8)
        graph.addEdge(9, 7)
        graph.addEdge(9, 6)

        windowSize = 10
        predictor = RandomEdgePredictor(windowSize)
        predictor.learnModel(graph)
        scores = numpy.random.randn(numVertices)
        ind = 0 
        p, s = predictor.indicesFromScores(ind , scores)

        self.assertTrue(p.shape[0] == windowSize)
        self.assertTrue(s.shape[0] == windowSize)

        infIndices = p[numpy.nonzero(s==-float('Inf'))]

        self.assertTrue((numpy.sort(infIndices) == numpy.sort(graph.neighbours(ind))).all())
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)
예제 #55
0
"""
Name: Generate Graph:
Author: Jia_qiu Wang(王佳秋)
Data: December, 2016
function:
"""

import numpy

from apgl.graph.VertexList import VertexList
from apgl.graph.SparseGraph import SparseGraph

numVertex = 5
numFeature = 2  # vector labels of size 2
graph = SparseGraph(VertexList(numVertex, numFeature))

# Add some edges to the graph(method 1)
# Vertices are indexed starting from 0
# graph[0, 1] = 0.1
# graph[1, 2] = 1

# Add some edges to the graph(method 2 is identity to method 1)
edges = numpy.array([[0, 1], [1, 2]], numpy.int)
edgeValues = numpy.array([0.1, 1])
graph.addEdges(edges, edgeValues)

# Set the label of 0th vertex to [2, 3]
graph.setVertex(0, numpy.array([2, 3]))

# Displays edge weights
print(graph[1, 2])
    def testInit(self):
        numVertices = 0
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

        numVertices = 10
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)
        self.assertEquals(graph.weightMatrixType(), scipy.sparse.csr_matrix)

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

        #Now test invalid values of W
        W = numpy.zeros((numVertices, numVertices))
        self.assertRaises(ValueError, SparseGraph, vList, True, W)

        W = scipy.sparse.lil_matrix((numVertices + 1, numVertices))
        self.assertRaises(ValueError, SparseGraph, vList, True, W)

        W = scipy.sparse.lil_matrix((numVertices, numVertices))
        W[0, 1] = 1
        self.assertRaises(ValueError, SparseGraph, vList, True, W)

        W = scipy.sparse.lil_matrix((numVertices, numVertices))
        graph = SparseGraph(vList, W=W)

        self.assertEquals(graph.weightMatrixType(), scipy.sparse.lil_matrix)

        #Test intialising with non-empty graph
        numVertices = 10
        W = scipy.sparse.csr_matrix((numVertices, numVertices))
        W[1, 0] = 1.1
        W[0, 1] = 1.1
        graph = SparseGraph(numVertices, W=W)

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

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

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

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

        #Test the different sparse matrix formats
        graph = SparseGraph(numVertices, frmt="lil")
        self.assertEquals(type(graph.W), scipy.sparse.lil_matrix)

        graph = SparseGraph(numVertices, frmt="csr")
        self.assertEquals(type(graph.W), scipy.sparse.csr_matrix)

        graph = SparseGraph(numVertices, frmt="csc")
        self.assertEquals(type(graph.W), scipy.sparse.csc_matrix)
    def testVectorStatistics(self):
        numFeatures = 1
        numVertices = 10
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

        graph.addEdge(0, 2)
        graph.addEdge(0, 1)

        growthStatistics = GraphStatistics()
        statsDict = growthStatistics.vectorStatistics(graph)

        self.assertTrue((statsDict["outDegreeDist"] == numpy.array([7,2,1])).all())
        self.assertTrue((statsDict["inDegreeDist"] == numpy.array([7,2,1])).all())
        self.assertTrue((statsDict["hopCount"] == numpy.array([10,14,16])).all())
        self.assertTrue((statsDict["triangleDist"] == numpy.array([10])).all())

        W = graph.getWeightMatrix()
        W = (W + W.T)/2
        lmbda, V = numpy.linalg.eig(W)
        maxEigVector = V[:, numpy.argmax(lmbda)]
        lmbda = numpy.flipud(numpy.sort(lmbda[lmbda>0]))
        self.assertTrue((statsDict["maxEigVector"] == maxEigVector).all())
        self.assertTrue((statsDict["eigenDist"] == lmbda).all())
        self.assertTrue((statsDict["componentsDist"] == numpy.array([0, 7, 0, 1])).all())

        graph.addEdge(0, 3)
        graph.addEdge(0, 4)
        graph.addEdge(1, 4)

        growthStatistics = GraphStatistics()
        statsDict = growthStatistics.vectorStatistics(graph)

        self.assertTrue((statsDict["outDegreeDist"] == numpy.array([5,2,2,0,1])).all())
        self.assertTrue((statsDict["inDegreeDist"] == numpy.array([5,2,2,0,1])).all())
        self.assertTrue((statsDict["hopCount"] == numpy.array([10,20,30])).all())
        self.assertTrue((statsDict["triangleDist"] == numpy.array([7, 0, 3])).all())

        W = graph.getWeightMatrix()
        W = (W + W.T)/2
        lmbda, V = numpy.linalg.eig(W)
        maxEigVector = V[:, numpy.argmax(lmbda)]
        lmbda = numpy.flipud(numpy.sort(lmbda[lmbda>0]))
        self.assertTrue((statsDict["maxEigVector"] == maxEigVector).all())
        self.assertTrue((statsDict["eigenDist"] == lmbda).all())
        self.assertTrue((statsDict["componentsDist"] == numpy.array([0, 5, 0, 0, 0, 1])).all())

        #Test on a directed graph and generating tree statistics 
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList, False)

        graph.addEdge(0, 1)
        graph.addEdge(0, 2)
        graph.addEdge(2, 3)

        graph.addEdge(4, 5)
        
        statsDict = growthStatistics.vectorStatistics(graph, treeStats=True)

        self.assertTrue(( statsDict["inDegreeDist"] == numpy.array([6, 4]) ).all())
        self.assertTrue(( statsDict["outDegreeDist"] == numpy.array([7, 2, 1]) ).all())
        self.assertTrue(( statsDict["triangleDist"] == numpy.array([10]) ).all())
        self.assertTrue(( statsDict["treeSizesDist"] == numpy.array([0, 4, 1, 0, 1]) ).all())
        self.assertTrue(( statsDict["treeDepthsDist"] == numpy.array([4, 1, 1]) ).all())
    def testScalarStatistics(self):
        numFeatures = 1
        numVertices = 10
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)
        graph.addEdge(0, 1)

        growthStatistics = GraphStatistics()
        statsArray = growthStatistics.scalarStatistics(graph)

        #logging.debug(statsArray)

        self.assertTrue(statsArray[growthStatistics.numVerticesIndex] == 10.0)
        self.assertTrue(statsArray[growthStatistics.numEdgesIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.maxComponentSizeIndex] == 2.0)
        self.assertTrue(statsArray[growthStatistics.maxComponentEdgesIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.numComponentsIndex] == 9.0)
        self.assertEquals(statsArray[growthStatistics.meanComponentSizeIndex], 10.0/9.0)
        self.assertTrue(statsArray[growthStatistics.meanDegreeIndex] == 0.2)
        self.assertTrue(statsArray[growthStatistics.diameterIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.effectiveDiameterIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.densityIndex] == 1.0/45)
        self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 1.0/55)
        self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 55.0)
        self.assertEquals(statsArray[growthStatistics.geodesicDistMaxCompIndex], 1.0/3)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], 1.0)
        self.assertEquals(statsArray[growthStatistics.numTriOrMoreComponentsIndex], 0.0)
        self.assertEquals(statsArray[growthStatistics.secondComponentSizeIndex], 1.0)
        self.assertEquals(statsArray[growthStatistics.maxCompMeanDegreeIndex], 1.0)

        graph.addEdge(0, 2)

        graph.addEdge(3,4)
        graph.addEdge(3,5)
        graph.addEdge(3,6)
        graph.addEdge(7,8)

        statsArray = growthStatistics.scalarStatistics(graph)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], 3.0)
        self.assertEquals(statsArray[growthStatistics.numTriOrMoreComponentsIndex], 2.0)
        self.assertEquals(statsArray[growthStatistics.secondComponentSizeIndex], 3.0)
        self.assertEquals(statsArray[growthStatistics.maxCompMeanDegreeIndex], 1.5)

        #Test on a directed graph 
        graph = SparseGraph(vList, False)
        graph.addEdge(0, 1)

        statsArray = growthStatistics.scalarStatistics(graph, treeStats=True)

        self.assertTrue(statsArray[growthStatistics.numVerticesIndex] == 10.0)
        self.assertTrue(statsArray[growthStatistics.numEdgesIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.maxComponentSizeIndex] == -1)
        self.assertTrue(statsArray[growthStatistics.maxComponentEdgesIndex] == -1)
        self.assertTrue(statsArray[growthStatistics.numComponentsIndex] == -1)
        self.assertTrue(statsArray[growthStatistics.meanComponentSizeIndex] == -1)
        self.assertEquals(statsArray[growthStatistics.meanDegreeIndex], 0.1)
        self.assertTrue(statsArray[growthStatistics.diameterIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.effectiveDiameterIndex] == 1.0)
        self.assertTrue(statsArray[growthStatistics.densityIndex] == 1.0/90)
        self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 1.0/100)
        self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 100)
        self.assertEquals(statsArray[growthStatistics.meanTreeSizeIndex], 10.0/9)
        self.assertEquals(statsArray[growthStatistics.meanTreeDepthIndex], 1.0/9)
        self.assertEquals(statsArray[growthStatistics.maxTreeSizeIndex], 2.0)
        self.assertEquals(statsArray[growthStatistics.maxTreeDepthIndex], 1.0)
        self.assertEquals(statsArray[growthStatistics.numTreesIndex], 9.0)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonTreesIndex], 1.0)
        

        #Test that the max tree is decribed correctly
        graph.addEdge(2, 3)
        graph.addEdge(2, 4)
        graph.addEdge(3, 5)
        graph.addEdge(3, 6)

        statsArray = growthStatistics.scalarStatistics(graph, treeStats=True)
        self.assertEquals(statsArray[growthStatistics.maxTreeSizeIndex], 5.0)
        self.assertEquals(statsArray[growthStatistics.maxTreeDepthIndex], 2.0)
        self.assertEquals(statsArray[growthStatistics.secondTreeSizeIndex], 2.0)
        self.assertEquals(statsArray[growthStatistics.secondTreeDepthIndex], 1.0)
        self.assertEquals(statsArray[growthStatistics.numTreesIndex], 5.0)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonTreesIndex], 2.0)

        #Try a zero size graph
        numFeatures = 0
        numVertices = 0
        vList = VertexList(numVertices, numFeatures)
        graph = SparseGraph(vList)

        statsArray = growthStatistics.scalarStatistics(graph)
        self.assertEquals(statsArray[growthStatistics.numVerticesIndex], 0)
        self.assertEquals(statsArray[growthStatistics.numEdgesIndex], 0)
        self.assertEquals(statsArray[growthStatistics.maxComponentSizeIndex], 0)
        self.assertTrue(statsArray[growthStatistics.maxComponentEdgesIndex] == 0)
        self.assertEquals(statsArray[growthStatistics.numComponentsIndex], 0)
        self.assertEquals(statsArray[growthStatistics.meanComponentSizeIndex], 0)
        self.assertEquals(statsArray[growthStatistics.meanDegreeIndex], 0)
        self.assertEquals(statsArray[growthStatistics.diameterIndex], 0)
        self.assertEquals(statsArray[growthStatistics.effectiveDiameterIndex], 0)
        self.assertEquals(statsArray[growthStatistics.densityIndex], 0)
        self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 0.0)
        self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 0.0)
        self.assertEquals(statsArray[growthStatistics.geodesicDistMaxCompIndex], 0.0)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], 0.0)

        graph = SparseGraph(vList, False)

        statsArray = growthStatistics.scalarStatistics(graph)
        self.assertEquals(statsArray[growthStatistics.numVerticesIndex], 0)
        self.assertEquals(statsArray[growthStatistics.numEdgesIndex], 0)
        self.assertEquals(statsArray[growthStatistics.maxComponentSizeIndex], -1)
        self.assertEquals(statsArray[growthStatistics.numComponentsIndex], -1)
        self.assertEquals(statsArray[growthStatistics.meanComponentSizeIndex], -1)
        self.assertEquals(statsArray[growthStatistics.maxComponentEdgesIndex], -1)
        self.assertEquals(statsArray[growthStatistics.meanDegreeIndex], 0)
        self.assertEquals(statsArray[growthStatistics.diameterIndex], 0)
        self.assertEquals(statsArray[growthStatistics.effectiveDiameterIndex], 0)
        self.assertEquals(statsArray[growthStatistics.densityIndex], 0)
        self.assertEquals(statsArray[growthStatistics.geodesicDistanceIndex], 0.0)
        self.assertEquals(statsArray[growthStatistics.harmonicGeoDistanceIndex], 0.0)
        self.assertEquals(statsArray[growthStatistics.geodesicDistMaxCompIndex], -1)
        self.assertEquals(statsArray[growthStatistics.meanTreeSizeIndex], -1)
        self.assertEquals(statsArray[growthStatistics.meanTreeDepthIndex], -1)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonComponentsIndex], -1)
        self.assertEquals(statsArray[growthStatistics.numTreesIndex], -1)
        self.assertEquals(statsArray[growthStatistics.numNonSingletonTreesIndex], -1)
    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 testGenerateGraph(self):
        numFeatures = 0
        numVertices = 20

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

        alpha1 = 10.0
        alpha2 = 20.0
        p = 0.001
        dim = 2
        generator = GeometricRandomGenerator(graph)

        graph = generator.generateGraph(alpha1, p, dim)
        numEdges1 = graph.getNumEdges()

        #Check no self edges
        for i in range(numVertices):
            self.assertTrue(graph.getEdge(i, i) == None)

        graph.removeAllEdges()
        graph = generator.generateGraph(alpha2, p, dim)
        numEdges2 = graph.getNumEdges()

        #self.assertTrue(numEdges1 >= numEdges2)
        logging.debug(numEdges1)
        logging.debug(numEdges2)

        for i in range(numVertices):
            self.assertTrue(graph.getEdge(i, i) == None)

        #Test case with p=0 and alpha huge 
        p = 0.0
        alpha = 100.0
        graph.removeAllEdges()
        graph = generator.generateGraph(alpha, p, dim)

        self.assertEquals(graph.getNumEdges(),  0)

        #When alpha=0, should get max edges
        alpha = 0.0
        graph.removeAllEdges()
        graph = generator.generateGraph(alpha, p, dim)

        #self.assertEquals(graph.getNumEdges(), int(0.5*(numVertices + numVertices**2) - numVertices))

        #TODO: Test variations in dimension 

        """