Ejemplo n.º 1
0
    def testSimulate2(self):    
        alpha = 2
        zeroVal = 0.9
        startDate = 0.0 
        endDate = 200.0
        M = 1000 
        undirected = True
        
        theta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta()        
                
        
        numpy.random.seed(21)
        graph = HIVGraph(M, undirected)
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    
        rates = HIVRates(graph, hiddenDegSeq)
        model = HIVEpidemicModel(graph, rates, endDate, startDate, metrics=None)
        #model.setRecordStep(recordStep)
        model.setParams(theta)
        times, infectedIndices, removedIndices, graph =  model.simulate(True)
        
        numVertices = graph.size
        numEdges = graph.getNumEdges()
        
        #Try again 
        numpy.random.seed(21)
        graph = HIVGraph(M, undirected)
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    
        rates = HIVRates(graph, hiddenDegSeq)
        model = HIVEpidemicModel(graph, rates, endDate, startDate, metrics=None)
        model.setParams(theta)
        times, infectedIndices, removedIndices, graph =  model.simulate(True)
        
        numVertices2 = graph.size
        numEdges2 = graph.getNumEdges()

        self.assertEquals(numVertices2, numVertices)
        self.assertEquals(numEdges2, numEdges)
Ejemplo n.º 2
0
    def testSimulate(self):
        T = 1.0

        self.graph.getVertexList().setInfected(0, 0.0)
        self.model.setT(T)

        times, infectedIndices, removedIndices, graph = self.model.simulate(verboseOut=True)

        numInfects = 0
        for i in range(graph.getNumVertices()):
            if graph.getVertex(i)[HIVVertices.stateIndex] == HIVVertices==infected:
                numInfects += 1

        self.assertTrue(numInfects == 0 or times[len(times)-1] >= T)

        #Test with a larger population as there seems to be an error when the
        #number of infectives becomes zero.
        M = 100
        undirected = True
        graph = HIVGraph(M, undirected)
        graph.setRandomInfected(10)

        self.graph.removeAllEdges()

        T = 21.0
        hiddenDegSeq = self.gen.rvs(size=self.graph.getNumVertices())
        rates = HIVRates(self.graph, hiddenDegSeq)
        model = HIVEpidemicModel(self.graph, rates)
        model.setRecordStep(10)
        model.setT(T)

        #Test detection rates
        print("Starting test")

        T = 1000.0
        graph = HIVGraph(M, undirected)
        graph.setRandomInfected(10)
        rates = HIVRates(graph, hiddenDegSeq)
        rates.contactRate = 0
        rates.randDetectRate = 0.1
        model = HIVEpidemicModel(graph, rates)
        model.setT(T)
        times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
        print(times)
        self.assertEquals(len(infectedIndices[0]), 10)
        self.assertEquals(len(removedIndices[0]), 0)
        
        T = 10.0
        graph.removeAllEdges()
        graph = HIVGraph(M, undirected)
        graph.setRandomInfected(10)
        rates = HIVRates(graph, hiddenDegSeq)  
        rates.randDetectRate = 0.0
        model = HIVEpidemicModel(graph, rates)
        model.setT(T)
        times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
        self.assertEquals(len(removedIndices[-1]), 0)
        
        T = 100.0
        graph.removeAllEdges()
        graph = HIVGraph(M, undirected)
        graph.setRandomInfected(10)
        rates = HIVRates(graph, hiddenDegSeq)  
        rates.randDetectRate = 10.0
        model = HIVEpidemicModel(graph, rates)
        model.setT(T)
        times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
        self.assertEquals(len(removedIndices[-1]), 10)
        
        #Test contact tracing 
        T = 1000.0
        graph = HIVGraph(M, undirected)
        graph.setRandomInfected(10)
        rates = HIVRates(graph, hiddenDegSeq)  
        rates.randDetectRate = 0.01
        rates.ctRatePerPerson = 0.5 
        model = HIVEpidemicModel(graph, rates)
        model.setT(T)
        times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
        self.assertTrue((graph.vlist.V[:, HIVVertices.detectionTypeIndex] == HIVVertices.contactTrace).sum() > 0) 
        
        #Test contact rate 
        print("Testing contact rate")
        contactRates = [0.5, 1, 2, 4]     
        numContacts = numpy.zeros(len(contactRates))
        
        for i, contactRate in enumerate(contactRates): 
            T = 100.0
            graph = HIVGraph(M, undirected)
            graph.setRandomInfected(1)
            print(i, graph.vlist.V[graph.getInfectedSet().pop(), :]) 
            rates = HIVRates(graph, hiddenDegSeq)  
            rates.contactRate = contactRate 
            rates.infectProb = 0.0
            model = HIVEpidemicModel(graph, rates)
            model.setT(T)
            times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
            numContacts[i] = model.numContacts

    
        lastN = -1
        
        for i, n in enumerate(numContacts):
            #This is an odd case in which we have a bisexual woman, there are no contacts 
            #since they are not modelled 
            if n != 0: 
                self.assertTrue(n > lastN)    
                
                
        #Test infection rate 
        print("Testing infection probability")
        infectProbs = [0.01, 0.1, 0.2, 0.5]     
        numInfects = numpy.zeros(len(contactRates))
        
        for i, infectProb in enumerate(infectProbs): 
            T = 100.0
            graph = HIVGraph(M, undirected)
            graph.setRandomInfected(10)
            rates = HIVRates(graph, hiddenDegSeq)  
            rates.contactRate = 0.5
            rates.infectProb = infectProb 
            model = HIVEpidemicModel(graph, rates)
            model.setT(T)
            times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
            numInfects[i] = len(graph.getInfectedSet())
        
        for n in numInfects:
            self.assertTrue(n > lastN)   
        
        
        print("Testing contact paramters")
        alphas = 1-numpy.array([0.01, 0.1, 0.2, 0.5, 0.99])     
        edges = numpy.zeros(len(alphas))
        
        for i, alpha in enumerate(alphas): 
            T = 100.0
            graph = HIVGraph(M, undirected)
            graph.setRandomInfected(1)
            rates = HIVRates(graph, hiddenDegSeq)  
            rates.setAlpha(alpha)
            rates.infectProb = 0 
            model = HIVEpidemicModel(graph, rates)
            model.setT(T)
            times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
            edges[i] = graph.getNumEdges()
            

        self.assertEquals(edges[0], 1)        
        self.assertTrue((numpy.diff(edges) > 0).all())