Exemplo n.º 1
0
numRemoved = numpy.zeros(numRepetitions)

for j in range(numRepetitions):
    graph = HIVGraph(M, undirected)
    logging.debug("Created graph: " + str(graph))

    alpha = 2
    zeroVal = 0.9
    p = Util.powerLawProbs(alpha, zeroVal)
    hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())

    rates = HIVRates(graph, hiddenDegSeq)
    model = HIVEpidemicModel(graph, rates)
    model.setT(endDate)
    model.setRecordStep(recordStep)
    model.setParams(theta)

    logging.debug("Theta = " + str(theta))

    times, infectedIndices, removedIndices, graph = model.simulate(True)
    graphFileName = outputDir + "ToyEpidemicGraph" + str(j)
    graph.save(graphFileName)

    graphList.append(graph)
    numInfected[j] = len(graph.getInfectedSet())
    numRemoved[j] = len(graph.getRemovedSet())

logging.debug("Infected (mean, std): " + str((numpy.mean(numInfected), numpy.std(numInfected))))
logging.debug("Removed (mean, std): " + str((numpy.mean(numRemoved), numpy.std(numRemoved))))
logging.debug("All done.")
Exemplo 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())