def testAddVertices(self):
        numFeatures = 5
        vList = VertexList(10, numFeatures)
        vList.setVertex(1, numpy.ones(numFeatures) * 0.1)
        self.assertEquals(vList.getNumVertices(), 10)
        nptst.assert_array_equal(vList[1], numpy.ones(numFeatures) * 0.1)

        vList.addVertices(5)
        self.assertEquals(vList.getNumVertices(), 15)
        vList.setVertex(11, numpy.ones(numFeatures) * 2)
        nptst.assert_array_equal(vList[1], numpy.ones(numFeatures) * 0.1)
        nptst.assert_array_equal(vList[11], numpy.ones(numFeatures) * 2)
 def testAddVertices(self): 
     numFeatures = 5
     vList = VertexList(10, numFeatures)
     vList.setVertex(1, numpy.ones(numFeatures)*0.1)
     self.assertEquals(vList.getNumVertices(), 10)
     nptst.assert_array_equal(vList[1], numpy.ones(numFeatures)*0.1)
     
     vList.addVertices(5)
     self.assertEquals(vList.getNumVertices(), 15)
     vList.setVertex(11, numpy.ones(numFeatures)*2)
     nptst.assert_array_equal(vList[1], numpy.ones(numFeatures)*0.1)
     nptst.assert_array_equal(vList[11], numpy.ones(numFeatures)*2)
Example #3
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)
    def testSaveLoad(self):
        try:
            vList = VertexList(self.numVertices, self.numFeatures)
            vList.setVertex(0, numpy.array([1, 2, 3]))
            vList.setVertex(1, numpy.array([4, 5, 6]))
            vList.setVertex(2, numpy.array([7, 8, 9]))

            tempDir = PathDefaults.getTempDir()
            fileName = tempDir + "vList"

            vList.save(fileName)
            vList2 = VertexList.load(fileName)

            self.assertTrue((vList.getVertices() == vList2.getVertices()).all())
        except IOError as e:
            logging.warn(e)
            pass 
    def testSaveLoad(self):
        try:
            vList = VertexList(self.numVertices, self.numFeatures)
            vList.setVertex(0, numpy.array([1, 2, 3]))
            vList.setVertex(1, numpy.array([4, 5, 6]))
            vList.setVertex(2, numpy.array([7, 8, 9]))

            tempDir = PathDefaults.getTempDir()
            fileName = tempDir + "vList"

            vList.save(fileName)
            vList2 = VertexList.load(fileName)

            self.assertTrue(
                (vList.getVertices() == vList2.getVertices()).all())
        except IOError as e:
            logging.warn(e)
            pass
Example #6
0
    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
    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 
Example #8
0
    def generateIndicatorVertices(self, numVertices, mu, sigma, p):
        """ 
        Generate a set of vertices from the means and variances of Y using
        the multivariate normal distribution. Also add at
        the end an indicator variables which is 1 with probability p.
        """
        if not (sigma.T == sigma).all():
            raise ValueError("Must use symmetric sigma matrix: " + str(sigma))

        n = mu.shape[0]
        X = numpy.zeros((numVertices, n+1), numpy.int32)
        X[:, 0:n] = numpy.round(random.multivariate_normal(mu, sigma, numVertices)).astype(numpy.int32)
        X[:, n] = (random.rand(numVertices) < p).astype(numpy.int32)
        
        vList = VertexList(numVertices, n+1)
        
        for i in range(0, numVertices): 
            vList.setVertex(i, X[i, :])
            
        return vList 
    def readFromFile(self, fileName):
        X = numpy.loadtxt(fileName, skiprows=1, converters=self.converters)
        vertexIds = numpy.zeros(X.shape[0] * 2)

        #First, we will map the vertex Ids to a set of numbers
        for i in range(0, X.shape[0]):
            vertexIds[2 * i] = X[i, self.vertex1IdIndex]
            vertexIds[2 * i + 1] = X[i, self.vertex2IdIndex]

        vertexIds = numpy.unique(vertexIds)

        numVertices = vertexIds.shape[0]
        numFeatures = len(self.vertex1Indices)

        vList = VertexList(numVertices, numFeatures)
        sGraph = SparseGraph(vList, self.undirected)

        for i in range(0, X.shape[0]):
            vertex1Id = X[i, self.vertex1IdIndex]
            vertex2Id = X[i, self.vertex2IdIndex]

            vertex1 = X[i, self.vertex1Indices]
            vertex2 = X[i, self.vertex2Indices]

            vertex1VListId = numpy.nonzero(vertexIds == vertex1Id)[0]
            vertex2VListId = numpy.nonzero(vertexIds == vertex2Id)[0]

            vertex1VListId = int(vertex1VListId)
            vertex2VListId = int(vertex2VListId)
            vList.setVertex(vertex1VListId, vertex1)
            vList.setVertex(vertex2VListId, vertex2)

            sGraph.addEdge(vertex1VListId, vertex2VListId, self.edgeWeight)

        logging.info("Read " + fileName + " with " +
                     str(sGraph.getNumVertices()) + " vertices and " +
                     str(sGraph.getNumEdges()) + " edges")

        return sGraph
    def readFromFile(self, fileName):
        X = numpy.loadtxt(fileName, skiprows=1, converters=self.converters)
        vertexIds = numpy.zeros(X.shape[0]*2)
        
        #First, we will map the vertex Ids to a set of numbers
        for i in range(0, X.shape[0]):
            vertexIds[2*i] = X[i, self.vertex1IdIndex]
            vertexIds[2*i+1] = X[i, self.vertex2IdIndex]

        vertexIds = numpy.unique(vertexIds)

        numVertices = vertexIds.shape[0]
        numFeatures = len(self.vertex1Indices)
        
        vList = VertexList(numVertices, numFeatures)
        sGraph = SparseGraph(vList, self.undirected)

        for i in range(0, X.shape[0]):
            vertex1Id = X[i, self.vertex1IdIndex]
            vertex2Id = X[i, self.vertex2IdIndex]
            
            vertex1 = X[i, self.vertex1Indices]
            vertex2 = X[i, self.vertex2Indices]

            vertex1VListId = numpy.nonzero(vertexIds==vertex1Id)[0]
            vertex2VListId = numpy.nonzero(vertexIds==vertex2Id)[0]

            vertex1VListId = int(vertex1VListId)
            vertex2VListId = int(vertex2VListId)
            vList.setVertex(vertex1VListId, vertex1)
            vList.setVertex(vertex2VListId, vertex2)

            sGraph.addEdge(vertex1VListId, vertex2VListId, self.edgeWeight)

        logging.info("Read " + fileName + " with " + str(sGraph.getNumVertices()) + " vertices and " + str(sGraph.getNumEdges()) + " edges")

        return sGraph 
Example #11
0
    def testAdvanceGraph2(self):
        #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)
        simulator.advanceGraph()
        
        
        
        self.assertEquals(simulator.getNumIterations(), 1)
        
        self.assertEquals(sGraph.getVertex(0)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(1)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(2)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(3)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(4)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(5)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(6)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(7)[numFeatures-1], 0)
        self.assertEquals(sGraph.getVertex(8)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(9)[numFeatures-1], 0)
        
        
        #Advance again and all egos have information 
        simulator.advanceGraph()
        
        self.assertEquals(simulator.getNumIterations(), 2)

        self.assertEquals(sGraph.getVertex(0)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(1)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(2)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(3)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(4)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(5)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(6)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(7)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(8)[numFeatures-1], 1)
        self.assertEquals(sGraph.getVertex(9)[numFeatures-1], 1)
        
        #Should be no change 
        simulator.advanceGraph()
        
        self.assertEquals(simulator.getNumIterations(), 3)
        
        #Check the correct alters are added at each step 
        self.assertTrue((simulator.getAlters(0) == numpy.array([1,2,3,6])).all())
        self.assertTrue((simulator.getAlters(1) == numpy.array([7,9])).all())
        self.assertTrue((simulator.getAlters(2) == numpy.array([])).all())

        #Check that the transmission graph is okay
        transGraph = simulator.getTransmissionGraph()
        self.assertEquals(transGraph.getNumVertices(), 9)
        self.assertEquals(transGraph.getNumEdges(), 7)
        self.assertEquals(transGraph.getAllVertexIds(), [0, 1, 2, 3, 4, 6, 7, 8, 9])

        for i in transGraph.getAllVertexIds():
            self.assertTrue((transGraph.getVertex(i) == sGraph.getVertex(i)).all())
    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 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)