Ejemplo n.º 1
0
    def testVertexLabelPairs(self):
        numVertices = 6
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        vList.setVertices(numpy.array([numpy.arange(0, 6)]).T)

        graph = DenseGraph(vList, True)
        graph.addEdge(0, 1, 0.1)
        graph.addEdge(1, 3, 0.1)
        graph.addEdge(0, 2, 0.2)
        graph.addEdge(2, 3, 0.5)
        graph.addEdge(0, 4, 0.1)
        graph.addEdge(3, 4, 0.1)

        tol = 10**-6
        edges = graph.getAllEdges()

        X = GraphUtils.vertexLabelPairs(graph, edges)
        self.assertTrue(numpy.linalg.norm(X - edges) < tol )


        X = GraphUtils.vertexLabelPairs(graph, edges[[5, 2, 1], :])
        self.assertTrue(numpy.linalg.norm(X - edges[[5,2,1], :]) < tol )

        #Try a bigger graph
        numVertices = 6
        numFeatures = 2
        vList = VertexList(numVertices, numFeatures)
        vList.setVertices(numpy.random.randn(numVertices, numFeatures))

        graph = DenseGraph(vList, True)
        graph.addEdge(0, 1, 0.1)
        graph.addEdge(1, 3, 0.1)

        edges = graph.getAllEdges()

        X = GraphUtils.vertexLabelPairs(graph, edges)
        self.assertTrue(numpy.linalg.norm(X[0, 0:numFeatures] - vList.getVertex(1)) < tol )
        self.assertTrue(numpy.linalg.norm(X[0, numFeatures:numFeatures*2] - vList.getVertex(0)) < tol )
        self.assertTrue(numpy.linalg.norm(X[1, 0:numFeatures] - vList.getVertex(3)) < tol )
        self.assertTrue(numpy.linalg.norm(X[1, numFeatures:numFeatures*2] - vList.getVertex(1)) < tol )

        #Try directed graphs
        graph = DenseGraph(vList, False)
        graph.addEdge(0, 1, 0.1)
        graph.addEdge(1, 3, 0.1)

        edges = graph.getAllEdges()

        X = GraphUtils.vertexLabelPairs(graph, edges)
        self.assertTrue(numpy.linalg.norm(X[0, 0:numFeatures] - vList.getVertex(0)) < tol )
        self.assertTrue(numpy.linalg.norm(X[0, numFeatures:numFeatures*2] - vList.getVertex(1)) < tol )
        self.assertTrue(numpy.linalg.norm(X[1, 0:numFeatures] - vList.getVertex(1)) < tol )
        self.assertTrue(numpy.linalg.norm(X[1, numFeatures:numFeatures*2] - vList.getVertex(3)) < tol )
Ejemplo n.º 2
0
    def learnModel(self, graph):
        """
        Learn a prediction model based on considering all ego-alter pairs. 

        :param graph: The input graph to learn from.
        :type graph: class:`apgl.graph.AbstractSingleGraph`
        """

        logging.info("Learning model on graph of size " +
                     str(graph.getNumVertices()))
        logging.info("Regressor: " + str(self.predictor))

        edges = graph.getAllEdges()

        if graph.isUndirected():
            edges2 = numpy.c_[edges[:, 1], edges[:, 0]]
            edges = numpy.r_[edges, edges2]

        X = GraphUtils.vertexLabelPairs(graph, edges)
        y = graph.getEdgeValues(edges)

        #Now we need to solve least to find regressor of X onto y
        logging.info("Number of vertex pairs " + str(X.shape))
        gc.collect()
        self.predictor.learnModel(X, y)
Ejemplo n.º 3
0
    def predictEdges(self, graph, edges):
        """
        Make prediction  given the edges and given graph.

        :param edges: A numpy array consisting of the edges to make predictions over.
        """
        Parameter.checkInt(graph.getVertexList().getNumFeatures(), 1, float('inf'))
        logging.info("Making prediction over " + str(edges.shape[0]) + " edges")

        X = GraphUtils.vertexLabelPairs(graph, edges)
        predY = self.predictor.predict(X)

        return predY 
Ejemplo n.º 4
0
    def predictEdges(self, graph, edges):
        """
        Make prediction  given the edges and given graph.

        :param edges: A numpy array consisting of the edges to make predictions over.
        """
        Parameter.checkInt(graph.getVertexList().getNumFeatures(), 1,
                           float('inf'))
        logging.info("Making prediction over " + str(edges.shape[0]) +
                     " edges")

        X = GraphUtils.vertexLabelPairs(graph, edges)
        predY = self.predictor.predict(X)

        return predY
Ejemplo n.º 5
0
    def learnModel(self, graph):
        """
        Learn a prediction model based on considering all ego-alter pairs. 

        :param graph: The input graph to learn from.
        :type graph: class:`apgl.graph.AbstractSingleGraph`
        """

        logging.info("Learning model on graph of size " + str(graph.getNumVertices()))
        logging.info("Regressor: " + str(self.predictor))

        edges = graph.getAllEdges()

        if graph.isUndirected():
            edges2 = numpy.c_[edges[:, 1], edges[:, 0]]
            edges = numpy.r_[edges, edges2]

        X = GraphUtils.vertexLabelPairs(graph, edges)
        y = graph.getEdgeValues(edges)

        #Now we need to solve least to find regressor of X onto y
        logging.info("Number of vertex pairs " + str(X.shape))
        gc.collect()
        self.predictor.learnModel(X, y)
    def testVertexLabelPairs(self):
        numVertices = 6
        numFeatures = 1
        vList = VertexList(numVertices, numFeatures)
        vList.setVertices(numpy.array([numpy.arange(0, 6)]).T)

        graph = DenseGraph(vList, True)
        graph.addEdge(0, 1, 0.1)
        graph.addEdge(1, 3, 0.1)
        graph.addEdge(0, 2, 0.2)
        graph.addEdge(2, 3, 0.5)
        graph.addEdge(0, 4, 0.1)
        graph.addEdge(3, 4, 0.1)

        tol = 10**-6
        edges = graph.getAllEdges()

        X = GraphUtils.vertexLabelPairs(graph, edges)
        self.assertTrue(numpy.linalg.norm(X - edges) < tol)

        X = GraphUtils.vertexLabelPairs(graph, edges[[5, 2, 1], :])
        self.assertTrue(numpy.linalg.norm(X - edges[[5, 2, 1], :]) < tol)

        #Try a bigger graph
        numVertices = 6
        numFeatures = 2
        vList = VertexList(numVertices, numFeatures)
        vList.setVertices(numpy.random.randn(numVertices, numFeatures))

        graph = DenseGraph(vList, True)
        graph.addEdge(0, 1, 0.1)
        graph.addEdge(1, 3, 0.1)

        edges = graph.getAllEdges()

        X = GraphUtils.vertexLabelPairs(graph, edges)
        self.assertTrue(
            numpy.linalg.norm(X[0, 0:numFeatures] - vList.getVertex(1)) < tol)
        self.assertTrue(
            numpy.linalg.norm(X[0, numFeatures:numFeatures * 2] -
                              vList.getVertex(0)) < tol)
        self.assertTrue(
            numpy.linalg.norm(X[1, 0:numFeatures] - vList.getVertex(3)) < tol)
        self.assertTrue(
            numpy.linalg.norm(X[1, numFeatures:numFeatures * 2] -
                              vList.getVertex(1)) < tol)

        #Try directed graphs
        graph = DenseGraph(vList, False)
        graph.addEdge(0, 1, 0.1)
        graph.addEdge(1, 3, 0.1)

        edges = graph.getAllEdges()

        X = GraphUtils.vertexLabelPairs(graph, edges)
        self.assertTrue(
            numpy.linalg.norm(X[0, 0:numFeatures] - vList.getVertex(0)) < tol)
        self.assertTrue(
            numpy.linalg.norm(X[0, numFeatures:numFeatures * 2] -
                              vList.getVertex(1)) < tol)
        self.assertTrue(
            numpy.linalg.norm(X[1, 0:numFeatures] - vList.getVertex(1)) < tol)
        self.assertTrue(
            numpy.linalg.norm(X[1, numFeatures:numFeatures * 2] -
                              vList.getVertex(3)) < tol)