Beispiel #1
0
 def testPickle(self): 
     numVertices = 10
     graph = HIVGraph(numVertices)  
     graph[0, 0] = 1
     graph[3, 5] = 0.1
     
     output = pickle.dumps(graph)
     newGraph = pickle.loads(output)
     
     graph[2, 2] = 1
     
     self.assertEquals(newGraph[0, 0], 1)
     self.assertEquals(newGraph[3, 5], 0.1)
     self.assertEquals(newGraph[2, 2], 0.0)
     self.assertEquals(newGraph.getNumEdges(), 2)
     self.assertEquals(newGraph.getNumVertices(), numVertices)
     self.assertEquals(newGraph.isUndirected(), True)
     
     self.assertEquals(graph[0, 0], 1)
     self.assertEquals(graph[3, 5], 0.1)
     self.assertEquals(graph[2, 2], 1)
     self.assertEquals(graph.getNumEdges(), 3)
     self.assertEquals(graph.getNumVertices(), numVertices)
     self.assertEquals(graph.isUndirected(), True)        
     
     for i in range(numVertices): 
         nptst.assert_array_equal(graph.getVertex(i), newGraph.getVertex(i))
    def profileSimulate(self):
        startDate, endDate, recordStep, printStep, M, targetGraph = HIVModelUtils.realSimulationParams()
        meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta()
        meanTheta = numpy.array([337,        1.4319,    0.211,     0.0048,    0.0032,    0.5229,    0.042,     0.0281,    0.0076,    0.0293])

        
        undirected = True
        graph = HIVGraph(M, undirected)
        logging.info("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.setT0(startDate)
        model.setT(startDate+100)
        model.setRecordStep(recordStep)
        model.setPrintStep(printStep)
        model.setParams(meanTheta)
        
        logging.debug("MeanTheta=" + str(meanTheta))

        ProfileUtils.profile('model.simulate()', globals(), locals())
Beispiel #3
0
    def testInfectionProbability(self):
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)
        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        t = 0.1

        graph.getVertex(0)[HIVVertices.stateIndex] = HIVVertices.infected
        graph.getVertex(1)[HIVVertices.stateIndex] = HIVVertices.removed
        graph.getVertex(2)[HIVVertices.stateIndex] = HIVVertices.infected

        for vertexInd1 in range(numVertices):
            for vertexInd2 in range(numVertices): 
                vertex1 = graph.getVertex(vertexInd1)
                vertex2 = graph.getVertex(vertexInd2)

                if vertex1[HIVVertices.stateIndex]!=HIVVertices.infected or vertex2[HIVVertices.stateIndex]!=HIVVertices.susceptible:
                    self.assertEquals(rates.infectionProbability(vertexInd1, vertexInd2, t), 0.0)
                elif vertex1[HIVVertices.genderIndex] == HIVVertices.female and vertex2[HIVVertices.genderIndex] == HIVVertices.male:
                    self.assertEquals(rates.infectionProbability(vertexInd1, vertexInd2, t), rates.infectProb) 
                elif vertex1[HIVVertices.genderIndex] == HIVVertices.male and vertex2[HIVVertices.genderIndex] == HIVVertices.female:
                    self.assertEquals(rates.infectionProbability(vertexInd1, vertexInd2, t), rates.infectProb)
                elif vertex1[HIVVertices.genderIndex] == HIVVertices.male and vertex2[HIVVertices.orientationIndex]==HIVVertices.bi:
                    self.assertEquals(rates.infectionProbability(vertexInd1, vertexInd2, t), rates.infectProb)
                else:
                    self.assertEquals(rates.infectionProbability(vertexInd1, vertexInd2, t), 0.0)
Beispiel #4
0
    def testRemoveEvent(self):
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)
        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        t = 0.1

        V = graph.getVertexList().getVertices()
        femaleInds = V[:, HIVVertices.genderIndex]==HIVVertices.female
        maleInds = V[:, HIVVertices.genderIndex]==HIVVertices.male
        biMaleInds = numpy.logical_and(maleInds, V[:, HIVVertices.orientationIndex]==HIVVertices.bi)

        self.assertEquals(rates.expandedDegSeqFemales.shape[0], hiddenDegSeq[femaleInds].sum()*rates.p)
        self.assertEquals(rates.expandedDegSeqMales.shape[0], hiddenDegSeq[maleInds].sum()*rates.p)
        self.assertEquals(rates.expandedDegSeqBiMales.shape[0], hiddenDegSeq[biMaleInds].sum()*rates.p)

        graph.getVertexList().setInfected(4, t)
        graph.getVertexList().setInfected(7, t)
        graph.getVertexList().setInfected(8, t)
        rates.removeEvent(4, HIVVertices.randomDetect, t)
        rates.removeEvent(7, HIVVertices.randomDetect, t)
        
        removedInds= list(graph.getRemovedSet())    
        
        hiddenDegSeq[removedInds] = 0 
        
        #Check the new degree sequences are correct 
        self.assertEquals(rates.expandedDegSeqFemales.shape[0], hiddenDegSeq[femaleInds].sum()*rates.p)
        self.assertEquals(rates.expandedDegSeqMales.shape[0], hiddenDegSeq[maleInds].sum()*rates.p)
        self.assertEquals(rates.expandedDegSeqBiMales.shape[0], hiddenDegSeq[biMaleInds].sum()*rates.p)
def createModel(t):
    """
    The parameter t is the particle index. 
    """
    undirected = True
    graph = HIVGraph(M, undirected)
    
    alpha = 2
    zeroVal = 0.9
    p = Util.powerLawProbs(alpha, zeroVal)
    hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    
    featureInds= numpy.ones(graph.vlist.getNumFeatures(), numpy.bool)
    featureInds[HIVVertices.dobIndex] = False 
    featureInds[HIVVertices.infectionTimeIndex] = False 
    featureInds[HIVVertices.hiddenDegreeIndex] = False 
    featureInds[HIVVertices.stateIndex] = False
    featureInds = numpy.arange(featureInds.shape[0])[featureInds]
    matcher = GraphMatch("PATH", alpha=0.5, featureInds=featureInds, useWeightM=False)
    graphMetrics = HIVGraphMetrics2(targetGraph, breakDist, matcher, endDate)
    graphMetrics.breakDist = 0.0 

    rates = HIVRates(graph, hiddenDegSeq)
    model = HIVEpidemicModel(graph, rates, T=float(endDate), T0=float(startDate), metrics=graphMetrics)
    model.setRecordStep(recordStep)

    return model
Beispiel #6
0
    def testContactRates(self):
        undirected = True 
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)

        t = 0.2

        contactList = range(numVertices)

        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        contactRateInds, contactRates = rates.contactRates([0, 5, 7], contactList, t)
        self.assertEquals(contactRates.shape[0], 3)

        #Now we have that 0 had contact with another
        rates.contactEvent(0, 3, 0.2)
        rates.contactEvent(1, 9, 0.1)
        
        infectedInds = numpy.arange(numVertices)
        contactRateInds, contactRates = rates.contactRates(infectedInds, contactList, t)

        #Note that in some cases an infected has no contacted as the persons do not match 
        for i in range(infectedInds.shape[0]): 
            if contactRateInds[i] != -1: 
                if graph.getVertex(infectedInds[i])[HIVVertices.genderIndex]==graph.getVertex(contactRateInds[i])[HIVVertices.genderIndex]:
                    self.assertEquals(contactRates[i], rates.heteroContactRate)
                elif graph.getVertex(infectedInds[i])[HIVVertices.genderIndex]!=graph.getVertex(contactRateInds[1])[HIVVertices.genderIndex] and graph.getVertex(infectedInds[i])[HIVVertices.orientationIndex]==HIVVertices.bi and graph.getVertex(contactRateInds[i])[HIVVertices.orientationIndex]==HIVVertices.bi:
                    self.assertEquals(contactRates[i],rates.biContactRate)
Beispiel #7
0
    def testContructor(self):
        numVertices = 10
        graph = HIVGraph(numVertices)

        
        self.assertEquals(numVertices, graph.getNumVertices())
        self.assertEquals(8, graph.getVertexList().getNumFeatures())
        self.assertTrue(graph.isUndirected() == True)
Beispiel #8
0
    def testRandomDetectionRates(self):
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)

        t = 0.1
        graph.getVertexList().setInfected(0, t)

        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        infectedList = [0, 2, 9]

        rdRates = rates.randomDetectionRates(infectedList, float(graph.size - len(graph.getRemovedSet())))

        nptst.assert_array_almost_equal(rdRates, numpy.ones(len(infectedList))*rates.randDetectRate*len(infectedList)/float(graph.size - len(graph.getRemovedSet())))
Beispiel #9
0
 def simulate(theta, startDate, endDate, recordStep, M, graphMetrics=None): 
     undirected = True
     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, endDate, startDate, metrics=graphMetrics)
     model.setRecordStep(recordStep)
     model.setParams(theta)
     
     logging.debug("Theta = " + str(theta))
     
     return model.simulate(True)
Beispiel #10
0
    def testContactEvent(self):
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)

        #for i in range(numVertices):
        #    logging.debug(graph.getVertex(i))

        t = 0.2
        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)

        V = graph.getVertexList().getVertices()
        femaleInds = V[:, HIVVertices.genderIndex]==HIVVertices.female
        maleInds = V[:, HIVVertices.genderIndex]==HIVVertices.male
        biMaleInds = numpy.logical_and(maleInds, V[:, HIVVertices.orientationIndex]==HIVVertices.bi)

        self.assertEquals(rates.expandedDegSeqFemales.shape[0], hiddenDegSeq[femaleInds].sum()*rates.p)
        self.assertEquals(rates.expandedDegSeqMales.shape[0], hiddenDegSeq[maleInds].sum()*rates.p)
        self.assertEquals(rates.expandedDegSeqBiMales.shape[0], hiddenDegSeq[biMaleInds].sum()*rates.p)

        for i in range(numVertices):
            self.assertEquals(rates.contactTimesArr[i], -1)

        rates.contactEvent(0, 9, 0.1)
        rates.contactEvent(0, 3, 0.2)
        
        self.assertEquals(graph.getEdge(0, 3), 0.2)
        self.assertEquals(graph.getEdge(0, 9), 0.1)

        self.assertTrue((rates.contactTimesArr[0] == numpy.array([3])).all())
        self.assertTrue((rates.contactTimesArr[9] == numpy.array([0])).all())
        self.assertTrue((rates.contactTimesArr[3] == numpy.array([0])).all())

        for i in range(numVertices):
            self.assertTrue((rates.neighboursList[i] == graph.neighbours(i)).all())

        #Check that the degree sequence is correct
        degSequence = graph.outDegreeSequence()
        r = rates.q-rates.p 

        self.assertEquals(rates.expandedDegSeqFemales.shape[0], hiddenDegSeq[femaleInds].sum()*rates.p + degSequence[femaleInds].sum()*r)
        self.assertEquals(rates.expandedDegSeqMales.shape[0], hiddenDegSeq[maleInds].sum()*rates.p + degSequence[maleInds].sum()*r)
        self.assertEquals(rates.expandedDegSeqBiMales.shape[0], hiddenDegSeq[biMaleInds].sum()*rates.p + degSequence[biMaleInds].sum()*r)
Beispiel #11
0
 def testContactRates3(self): 
     #Figure out why infection does not explode when we set infection probability 
     #to a high value and do not detect 
     
     undirected = True
     numVertices = 20
     graph = HIVGraph(numVertices, undirected)
     hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
     rates = HIVRates(graph, hiddenDegSeq)
     t = 0.1
     
     for i in range(10): 
         graph.getVertexList().setInfected(i, t)
     
     t = 0.2
     infectedList = graph.infectedIndsAt(t)
     contactList = range(0, numVertices)
     contactRateInds, contactRates = rates.contactRates(infectedList, contactList, t)
     
     print(contactRateInds, contactRates)
    def setUp(self):
        numpy.seterr(invalid='raise')
        logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
        numpy.set_printoptions(suppress=True, precision=4, linewidth=100)
        numpy.random.seed(21)

        M = 1000
        undirected = True

        graph = HIVGraph(M, undirected)
        alpha = 2
        zeroVal = 0.9
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)

        self.numParams = 6
        self.graph = graph
        self.meanTheta = numpy.array([100, 0.9, 0.05, 0.001, 0.1, 0.005])
        self.hivAbcParams = HIVABCParameters(self.meanTheta, self.meanTheta/2)
Beispiel #13
0
    def testUpperDetectionRates(self): 
        """
        See if the upper bound on detection rates is correct 
        """
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)
        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        t = 0.1
        
        graph.getVertexList().setInfected(0, t)
        graph.getVertexList().setInfected(1, t)
        graph.getVertexList().setInfected(8, t)
        
        t = 0.2
        rates.removeEvent(8, HIVVertices.randomDetect, t)
        rates.infectionProbability = 1.0
        
        infectedList = graph.infectedIndsAt(t)
        removedList = graph.removedIndsAt(t)
        n = graph.size-removedList
        self.assertEquals(rates.upperDetectionRates(infectedList, n), rates.randomDetectionRates(infectedList, n, seed=21).sum()) 
        
        t = 0.3
        rates.contactEvent(0, 2, t)
        graph.vlist.setInfected(2, t)
        
        t = 0.4
        rates.removeEvent(0, HIVVertices.randomDetect, t)
        
        infectedList = graph.infectedIndsAt(t)
        removedSet = graph.removedIndsAt(t)
        removedSet = set(removedSet.tolist())

        nptst.assert_array_almost_equal(rates.contactTracingRates(infectedList, removedSet, t + rates.ctStartTime + 1), numpy.array([0, rates.ctRatePerPerson]))
        
        upperDetectionRates = rates.ctRatePerPerson + rates.randomDetectionRates(infectedList, n, seed=21).sum()
        self.assertEquals(rates.upperDetectionRates(infectedList, n), upperDetectionRates) 
def findDerivative(args):
    pertScale, startDate, endDate, recordStep, M, targetGraph, seed = args
    numpy.random.seed(seed)
    meanTheta, sigmaTheta = HIVModelUtils.toyTheta()  
    
    epsilon = 5.0
    undirected = True
    
    alpha = 2
    zeroVal = 0.9
    p = Util.powerLawProbs(alpha, zeroVal)
    
    graph = HIVGraph(M, undirected)
    
    featureInds= numpy.ones(graph.vlist.getNumFeatures(), numpy.bool)
    featureInds[HIVVertices.dobIndex] = False 
    featureInds[HIVVertices.infectionTimeIndex] = False 
    featureInds[HIVVertices.hiddenDegreeIndex] = False 
    featureInds[HIVVertices.stateIndex] = False
    featureInds = numpy.arange(featureInds.shape[0])[featureInds]
    matcher = GraphMatch("PATH", alpha=0.5, featureInds=featureInds, useWeightM=False)    
        
    abcParams = HIVABCParameters(meanTheta, sigmaTheta, pertScale)
    newTheta = abcParams.perturbationKernel(meanTheta)
    
    undirected = True
    graph = HIVGraph(M, undirected)
    graphMetrics = HIVGraphMetrics2(targetGraph, epsilon, matcher, float(endDate))
    graphMetrics.breakDist = 1.0     
    
    hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    rates = HIVRates(graph, hiddenDegSeq)
    model = HIVEpidemicModel(graph, rates, T=float(endDate), T0=float(startDate), metrics=graphMetrics)
    model.setRecordStep(recordStep)
    model.setParams(meanTheta)
    
    times, infectedIndices, removedIndices, graph = model.simulate(True)
    
    return abs(0.7 - graphMetrics.distance())/numpy.linalg.norm(newTheta-meanTheta)
def runModel(theta, endDate=100.0, M=1000): 
    numpy.random.seed(21)
    undirected= True
    recordStep = 10 
    printStep = 10 
    startDate = 0
    alpha = 2
    zeroVal = 0.9
    p = Util.powerLawProbs(alpha, zeroVal)
    graph = HIVGraph(M, undirected)
    hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    logging.debug("MeanTheta=" + str(theta))
    
    rates = HIVRates(graph, hiddenDegSeq)
    model = HIVEpidemicModel(graph, rates, endDate, startDate)
    model.setRecordStep(recordStep)
    model.setPrintStep(printStep)
    model.setParams(theta)
    
    times, infectedIndices, removedIndices, graph = model.simulate(True)            
    
    return times, infectedIndices, removedIndices, graph, model  
Beispiel #16
0
class HIVRatesProfile():
    def __init__(self):
        #Total number of people in population
        self.M = 10000
        numInitialInfected = 5

        #The graph is one in which edges represent a contact
        undirected = True
        self.graph = HIVGraph(self.M, undirected)

        for i in range(self.M):
            vertex = self.graph.getVertex(i)

            #Set the infection time of a number of individuals to 0
            if i < numInitialInfected:
                vertex[HIVVertices.stateIndex] = HIVVertices.infected

        outputDirectory = PathDefaults.getOutputDir()
        directory = outputDirectory + "test/"
        self.profileFileName = directory + "profile.cprof"


    def profileContactRate(self):
        susceptibleList = list(range(1, self.graph.getNumVertices()))
        t = 10

        s = 3
        gen = scipy.stats.zipf(s)
        hiddenDegSeq = gen.rvs(size=self.graph.getNumVertices())
        rates = HIVRates(self.graph, hiddenDegSeq)

        numContactEvents = 5000
        for i in range(numContactEvents):
            vertexInd1 = numpy.random.randint(0, self.graph.getNumVertices())
            vertexInd2 = numpy.random.randint(0, self.graph.getNumVertices())
            rates.contactEvent(vertexInd1, vertexInd2, 5)

        print((self.graph.getNumEdges()))

        infectedList = range(0, 100)
        contactList = range(100, self.M)
        t = 10

        def runContactRates():
            for i in range(100):
                rates.contactRates(infectedList, contactList, t)

        ProfileUtils.profile('runContactRates()', globals(), locals())


    def profileInfectionProbability(self):
        s = 3
        gen = scipy.stats.zipf(s)
        hiddenDegSeq = gen.rvs(size=self.graph.getNumVertices())
        rates = HIVRates(self.graph, hiddenDegSeq)
        t = 5

        #Getting vertices and checking parameters takes the most time 
        def runInfectionProbs():
            for i in range(10000):
                vertexInd1 = numpy.random.randint(0, self.graph.getNumVertices())
                vertexInd2 = numpy.random.randint(0, self.graph.getNumVertices())
                rates.infectionProbability(vertexInd1, vertexInd2, t)

        ProfileUtils.profile('runInfectionProbs()', globals(), locals())

    def profileContactTracingRate(self):
        s = 3
        gen = scipy.stats.zipf(s)
        hiddenDegSeq = gen.rvs(size=self.graph.getNumVertices())
        rates = HIVRates(self.graph, hiddenDegSeq)

        #Create a network of sexual contacts 
        numContactEvents = 10000
        for i in range(numContactEvents):
            vertexInd1 = numpy.random.randint(0, self.graph.getNumVertices())
            vertexInd2 = numpy.random.randint(0, self.graph.getNumVertices())
            rates.contactEvent(vertexInd1, vertexInd2, 5)

        print((self.graph))
        print((self.graph.degreeDistribution()))

        #Choose some individuals as being infected and then detected 
        p = 0.3
        q = 0.4
        for i in range(self.graph.getNumVertices()):
            if numpy.random.rand() < p and not self.graph.getVertex(i)[HIVVertices.stateIndex] == HIVVertices.infected:
                self.graph.getVertexList().setInfected(i, 5.0)

                if numpy.random.rand() < q:
                    self.graph.getVertexList().setDetected(i, 6.0, HIVVertices.randomDetect)

        infectedSet = self.graph.getInfectedSet()
        print((len(infectedSet)))
        print((len(self.graph.getRemovedSet())))

        removedSet = self.graph.getRemovedSet()

        t = 200
        def runContactTracingRate():
            for j in range(2000):
                rates.contactTracingRates(list(infectedSet), removedSet, t)

        ProfileUtils.profile('runContactTracingRate()', globals(), locals())
Beispiel #17
0
    def testContactTracingRate(self):
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)

        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        t = 0.1
        graph.getVertexList().setInfected(0, t)
        rates.contactEvent(0, 3, 0.2)
        rates.contactEvent(0, 9, 0.1)

        t = 0.3
        graph.getVertexList().setInfected(3, t)
        graph.getVertexList().setInfected(9, t)

        t = 0.4
        rates.removeEvent(0, HIVVertices.randomDetect, t)

        removedSet = graph.getRemovedSet()
        infectedList = [3, 9]
        ctRates = rates.contactTracingRates(infectedList, removedSet, t)
        self.assertTrue((ctRates==numpy.array([0.0, 0.0])).all())

        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctStartTime)
        self.assertTrue((ctRates == numpy.array([rates.ctRatePerPerson, rates.ctRatePerPerson])).all())

        #Test contact tracing is within correct time period
        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctEndTime-0.01)
        self.assertTrue((ctRates == numpy.array([rates.ctRatePerPerson, rates.ctRatePerPerson])).all())

        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctEndTime+1)
        self.assertTrue((ctRates == numpy.array([0, 0])).all())

        rates.contactEvent(3, 5, t)
        graph.getVertexList().setInfected(5, t)
        rates.removeEvent(5, HIVVertices.randomDetect, t)
        removedSet = graph.getRemovedSet()
        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctStartTime)

        self.assertTrue((ctRates == numpy.array([rates.ctRatePerPerson, rates.ctRatePerPerson])).all())
        
        rates.contactEvent(3, 6, t)
        graph.getVertexList().setInfected(6, t)
        infectedList = [3, 6, 9]
        removedSet = graph.getRemovedSet()
 
        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctStartTime)
        self.assertTrue((ctRates == numpy.array([rates.ctRatePerPerson, 0, rates.ctRatePerPerson])).all())

        #Now make removedSet bigger than infectedList
        graph.getVertexList().setInfected(4, t)
        graph.getVertexList().setInfected(7, t)
        graph.getVertexList().setInfected(8, t)
        graph.getVertexList().setDetected(4, t, HIVVertices.randomDetect)
        graph.getVertexList().setDetected(7, t, HIVVertices.randomDetect)
        graph.getVertexList().setDetected(8, t, HIVVertices.randomDetect)

        #Note: InfectedList is out of order 
        infectedList = list(graph.getInfectedSet())
        sortInds = numpy.argsort(numpy.array(infectedList))
        removedSet = graph.getRemovedSet()

        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctStartTime)
        ctRates2 = numpy.array([rates.ctRatePerPerson, 0, rates.ctRatePerPerson])
        self.assertTrue((ctRates[sortInds] == ctRates2).all())

        #Test the case where InfectedList is out of order and removedSet is small
        graph.getVertexList().setInfected(4, t)
        graph.getVertex(7)[HIVVertices.stateIndex] = HIVVertices.susceptible
        graph.getVertex(8)[HIVVertices.stateIndex] = HIVVertices.susceptible

        infectedList = list(graph.getInfectedSet())
        sortInds = numpy.argsort(numpy.array(infectedList))
        removedSet = graph.getRemovedSet()

        ctRates = rates.contactTracingRates(infectedList, removedSet, t+rates.ctStartTime)
        ctRates2 = numpy.array([rates.ctRatePerPerson, 0, 0, rates.ctRatePerPerson])
        self.assertTrue((ctRates[sortInds] == ctRates2).all())
Beispiel #18
0
    def testContactRates2(self):
        undirected = True
        numVertices = 10
        graph = HIVGraph(numVertices, undirected)

        maleVertex = graph.getVertex(0)
        maleVertex[HIVVertices.genderIndex] = HIVVertices.male
        femaleVertex = maleVertex.copy()
        femaleVertex[HIVVertices.genderIndex] = HIVVertices.female

        for i in range(5): 
            graph.setVertex(i, maleVertex)
            graph.setVertex(i+5, femaleVertex)

        V = graph.getVertexList().getVertices()

        contactList = range(numVertices)

        #Test that the parameters alpha and C do the right thing
        hiddenDegSeq = self.gen.rvs(size=graph.getNumVertices())
        rates = HIVRates(graph, hiddenDegSeq)
        t = 0.2
        logging.debug("Rates with no existing contacts")
        contactRateInds, contactRates = rates.contactRates(range(numVertices), contactList, t)

        #When there are no contacts the choice is easy and some random new contacts
        #are chosen.
        #Now test differences in choice between existing and new contact.
        t = 0.3
        for i in range(5):
            rates.contactEvent(i, i+5, t)

        rates.alpha = 1.0
        logging.debug("Rates with default alpha=" + str(rates.alpha))
        contactRateInds, contactRates = rates.contactRates(range(numVertices), contactList, 0.4)


        for i in range(5):
            self.assertTrue(contactRates[i] == rates.contactRate)
            self.assertTrue(contactRateInds[i] == i+5)

        #Now try changing alpha
        logging.debug("Rates with alpha=0.5")
        rates.setAlpha(0.5)
        contactRateInds, contactRates = rates.contactRates(range(numVertices), contactList, 0.4)
        #Observed probabilities change as expected


        #Now increase time and observe probabilities
        logging.debug("Rates with t=20")
        contactRateInds, contactRates = rates.contactRates(range(numVertices), contactList, 20)


        #Test we don't pick from removed
        graph.getVertexList().setInfected(0, t)
        graph.getVertexList().setInfected(4, t)
        graph.getVertexList().setInfected(7, t)
        graph.getVertexList().setInfected(8, t)
        #graph.getVertexList().setDetected(4, t, HIVVertices.randomDetect)
        #graph.getVertexList().setDetected(7, t, HIVVertices.randomDetect)
        rates.removeEvent(4, HIVVertices.randomDetect, t)
        rates.removeEvent(7, HIVVertices.randomDetect, t)

        infectedSet = graph.getInfectedSet()
        susceptibleSet = graph.getSusceptibleSet()
        removedSet = graph.getRemovedSet()
        contactSet = infectedSet.union(susceptibleSet)

        infectedList = list(infectedSet)
        removedList = list(removedSet)
        contactList = list(contactSet)

        contactRateInds, contactRates = rates.contactRates(infectedList, contactList, 20)
        
        #Contacts cannot be in removed set 
        self.assertTrue(numpy.intersect1d(contactRateInds, numpy.array(removedList)).shape[0]==0)        
numpy.set_printoptions(suppress=True, precision=4, linewidth=100)

startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.realSimulationParams()
endDate = startDate + 10000
meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta()
meanTheta = numpy.array([ 50,        0.5131,    0.3242,    0.1,    0.0001,    0.0,  325,        0.34,      0.001,     0.1,     0.1,    0.1])
outputDir = PathDefaults.getOutputDir() + "viroscopy/"

undirected = True
graph = HIVGraph(M, undirected)
logging.info("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.setT0(startDate)
model.setT(endDate)
model.setRecordStep(recordStep)
model.setParams(meanTheta)

logging.debug("MeanTheta=" + str(meanTheta))

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

times, vertexArray, removedGraphStats = HIVModelUtils.generateStatistics(graph, startDate, endDate, recordStep)

plt.figure(0)
    def testSimulate2(self):    
        startDate = 0.0 
        endDate = 100.0 
        M = 1000 
        meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta()
        
        undirected = True
        graph = HIVGraph(M, undirected)
        
        alpha = 2
        zeroVal = 0.9
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
        
        meanTheta[4] = 0.1        
        
        recordStep = 10 
        printStep = 10
        rates = HIVRates(graph, hiddenDegSeq)
        model = HIVEpidemicModel(graph, rates, endDate, startDate)
        model.setRecordStep(recordStep)
        model.setPrintStep(printStep)
        model.setParams(meanTheta)
        
        initialInfected = graph.getInfectedSet()
        
        times, infectedIndices, removedIndices, graph = model.simulate(True)
        
        #Now test the final graph 
        edges = graph.getAllEdges()
        
        for i, j in edges:
            if graph.vlist.V[i, HIVVertices.genderIndex] == graph.vlist.V[j, HIVVertices.genderIndex] and (graph.vlist.V[i, HIVVertices.orientationIndex] != HIVVertices.bi or graph.vlist.V[j, HIVVertices.orientationIndex] != HIVVertices.bi): 
                self.fail()
                      
        finalInfected = graph.getInfectedSet()
        finalRemoved = graph.getRemovedSet()
        
        self.assertEquals(numpy.intersect1d(initialInfected, finalRemoved).shape[0], len(initialInfected))
        
        #Test case where there is no contact  
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, 0, 0, 0, 0, 0], numpy.float)
        
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)

        self.assertEquals(len(graph.getInfectedSet()), 100)
        self.assertEquals(len(graph.getRemovedSet()), 0)
        self.assertEquals(graph.getNumEdges(), 0)
        
        heteroContactRate = 0.1
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        
        self.assertEquals(len(graph.getInfectedSet()), 100)
        self.assertEquals(len(graph.getRemovedSet()), 0)
        
        edges = graph.getAllEdges()
        
        for i, j in edges:
            self.assertNotEqual(graph.vlist.V[i, HIVVertices.genderIndex], graph.vlist.V[j, HIVVertices.genderIndex]) 
            
        #Number of conacts = rate*people*time
        infectedSet = graph.getInfectedSet()
        numHetero = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.hetero).sum()
        self.assertTrue(abs(numHetero*endDate*heteroContactRate- model.getNumContacts()) < 100)
        
        heteroContactRate = 0.01
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        infectedSet = graph.getInfectedSet()
        numHetero = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.hetero).sum()
        self.assertAlmostEqual(numHetero*endDate*heteroContactRate/100, model.getNumContacts()/100.0, 0)      
class  HIVEpidemicModelTest(unittest.TestCase):
    def setUp(self):
        numpy.random.seed(21)
        numpy.set_printoptions(suppress=True, precision=4)
        logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

        M = 100
        undirected = True
        self.graph = HIVGraph(M, undirected)
        s = 3
        self.gen = scipy.stats.zipf(s)
        hiddenDegSeq = self.gen.rvs(size=self.graph.getNumVertices())
        rates = HIVRates(self.graph, hiddenDegSeq)
        self.model = HIVEpidemicModel(self.graph, rates)
     
    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, 0.95)

        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)

        times, infectedIndices, removedIndices, graph = model.simulate(verboseOut=True)
        self.assertTrue((times == numpy.array([0, 10, 20], numpy.int)).all())
        self.assertEquals(len(infectedIndices), 3)
        self.assertEquals(len(removedIndices), 3)

        #TODO: Much better testing
        
    def testSimulate2(self):    
        startDate = 0.0 
        endDate = 100.0 
        M = 1000 
        meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta()
        
        undirected = True
        graph = HIVGraph(M, undirected)
        
        alpha = 2
        zeroVal = 0.9
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
        
        meanTheta[4] = 0.1        
        
        recordStep = 10 
        printStep = 10
        rates = HIVRates(graph, hiddenDegSeq)
        model = HIVEpidemicModel(graph, rates, endDate, startDate)
        model.setRecordStep(recordStep)
        model.setPrintStep(printStep)
        model.setParams(meanTheta)
        
        initialInfected = graph.getInfectedSet()
        
        times, infectedIndices, removedIndices, graph = model.simulate(True)
        
        #Now test the final graph 
        edges = graph.getAllEdges()
        
        for i, j in edges:
            if graph.vlist.V[i, HIVVertices.genderIndex] == graph.vlist.V[j, HIVVertices.genderIndex] and (graph.vlist.V[i, HIVVertices.orientationIndex] != HIVVertices.bi or graph.vlist.V[j, HIVVertices.orientationIndex] != HIVVertices.bi): 
                self.fail()
                      
        finalInfected = graph.getInfectedSet()
        finalRemoved = graph.getRemovedSet()
        
        self.assertEquals(numpy.intersect1d(initialInfected, finalRemoved).shape[0], len(initialInfected))
        
        #Test case where there is no contact  
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, 0, 0, 0, 0, 0], numpy.float)
        
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)

        self.assertEquals(len(graph.getInfectedSet()), 100)
        self.assertEquals(len(graph.getRemovedSet()), 0)
        self.assertEquals(graph.getNumEdges(), 0)
        
        heteroContactRate = 0.1
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        
        self.assertEquals(len(graph.getInfectedSet()), 100)
        self.assertEquals(len(graph.getRemovedSet()), 0)
        
        edges = graph.getAllEdges()
        
        for i, j in edges:
            self.assertNotEqual(graph.vlist.V[i, HIVVertices.genderIndex], graph.vlist.V[j, HIVVertices.genderIndex]) 
            
        #Number of conacts = rate*people*time
        infectedSet = graph.getInfectedSet()
        numHetero = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.hetero).sum()
        self.assertTrue(abs(numHetero*endDate*heteroContactRate- model.getNumContacts()) < 100)
        
        heteroContactRate = 0.01
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        infectedSet = graph.getInfectedSet()
        numHetero = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.hetero).sum()
        self.assertAlmostEqual(numHetero*endDate*heteroContactRate/100, model.getNumContacts()/100.0, 0)      
        
    def testSimulateBis(self): 
        #Play with bi rate 
        biContactRate = 0.1
        endDate = 100.0
        meanTheta = numpy.array([200, 0.95, 1, 1, 0, 0, 0, biContactRate, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta, endDate=endDate)
        infectedSet = graph.getInfectedSet()
        numBi = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.bi).sum()
        susceptibleSet = graph.getSusceptibleSet()
        self.assertTrue(abs(numBi*endDate*biContactRate- model.getNumContacts()) < 10)
        
        numContacts = model.getNumContacts()
        edges = graph.getAllEdges()        
        numMSM = 0         
        
        for i, j in edges:
            self.assertTrue(graph.vlist.V[i, HIVVertices.orientationIndex]==HIVVertices.bi or graph.vlist.V[j, HIVVertices.orientationIndex]==HIVVertices.bi) 
            
            if graph.vlist.V[i, HIVVertices.genderIndex] == graph.vlist.V[j, HIVVertices.genderIndex] and graph.vlist.V[i, HIVVertices.genderIndex]==HIVVertices.male: 
                numMSM += 1 
            
        biContactRate = 0.2
        meanTheta = numpy.array([200, 0.95, 1, 1, 0, 0, 0, biContactRate, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        
        infectedSet = graph.getInfectedSet()
        numBi = (graph.vlist.V[list(infectedSet), HIVVertices.orientationIndex] == HIVVertices.bi).sum()
        
        numContacts2 = model.getNumContacts()
        self.assertTrue(abs(numContacts*2-numContacts2) < 15)
        
        #Try infection between men only 
        biContactRate = 0.2
        manBiInfectProb = 1.0 
        meanTheta = numpy.array([300, 0.95, 1, 1, 0, 0, 0, biContactRate, 0, 0, manBiInfectProb], numpy.float)
        
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        #print(numpy.logical_and(graph.vlist.V[:, HIVVertices.orientationIndex] == HIVVertices.bi, graph.vlist.V[:, HIVVertices.genderIndex] == HIVVertices.male).sum())
        
        newInfects = numpy.setdiff1d(graph.getInfectedSet(), numpy.array(infectedIndices[0]))
        
        self.assertTrue((graph.vlist.V[newInfects, HIVVertices.orientationIndex] == HIVVertices.bi).all())
        self.assertTrue((graph.vlist.V[newInfects, HIVVertices.genderIndex] == HIVVertices.male).all())


    def testSimulateInfects(self): 
        #Test varying infection probabilities 
        
        heteroContactRate = 0.1
        manWomanInfectProb = 1.0 
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, manWomanInfectProb, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        
        newInfects = numpy.setdiff1d(graph.getInfectedSet(), numpy.array(infectedIndices[0]))
        
        self.assertTrue((graph.vlist.V[newInfects, HIVVertices.genderIndex] == HIVVertices.female).all())
        
        manWomanInfectProb = 0.1
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, 0, manWomanInfectProb, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        newInfects2 = numpy.setdiff1d(graph.getInfectedSet(), numpy.array(infectedIndices[0]))
        
        self.assertTrue((graph.vlist.V[newInfects2, HIVVertices.genderIndex] == HIVVertices.female).all())
        self.assertTrue(newInfects.shape[0] > newInfects2.shape[0])
        
        
        #Now only women infect 
        heteroContactRate = 0.1
        womanManInfectProb = 1.0 
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, womanManInfectProb, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        
        newInfects = numpy.setdiff1d(graph.getInfectedSet(), numpy.array(infectedIndices[0]))
        
        self.assertTrue((graph.vlist.V[newInfects, HIVVertices.genderIndex] == HIVVertices.male).all())
        
        womanManInfectProb = 0.1
        meanTheta = numpy.array([100, 0.95, 1, 1, 0, 0, heteroContactRate, 0, womanManInfectProb, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        newInfects2 = numpy.setdiff1d(graph.getInfectedSet(), numpy.array(infectedIndices[0]))
        
        self.assertTrue((graph.vlist.V[newInfects2, HIVVertices.genderIndex] == HIVVertices.male).all())
        self.assertTrue(newInfects.shape[0] > newInfects2.shape[0])
  

    def testSimulateDetects(self): 
        heteroContactRate = 0.05
        endDate = 100
        
        randDetectRate = 0
        meanTheta = numpy.array([100, 0.95, 1, 1, randDetectRate, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        detectedSet = graph.getRemovedSet()    
        self.assertEquals(len(detectedSet), 0)
        
        heteroContactRate = 0.0
        randDetectRate = 0.01
        meanTheta = numpy.array([100, 0.95, 1, 1, randDetectRate, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        detectedSet = graph.getRemovedSet()
        
        self.assertTrue(len(detectedSet) < 100*randDetectRate*endDate)
        
        randDetectRate = 0.005
        meanTheta = numpy.array([100, 0.95, 1, 1, randDetectRate, 0, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        detectedSet2 = graph.getRemovedSet()
    
        print(len(detectedSet), len(detectedSet2))
        self.assertTrue(abs(len(detectedSet)*2 - len(detectedSet2))<15)   
        
        removedGraph = graph.subgraph(list(graph.getRemovedSet())) 
        edges = removedGraph.getAllEdges()        
        
        for edge in edges: 
            i, j = edge
            self.assertEquals(removedGraph.vlist.V[i, HIVVertices.detectionTimeIndex]. HIVVertices.randomDetect)
            self.assertEquals(removedGraph.vlist.V[j, HIVVertices.detectionTimeIndex]. HIVVertices.randomDetect)
               
        #Test contact tracing 
        randDetectRate = 0
        setCtRatePerPerson = 0.1
        meanTheta = numpy.array([100, 0.95, 1, 1, randDetectRate, setCtRatePerPerson, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta)
        detectedSet = graph.getRemovedSet()   
        self.assertEquals(len(detectedSet), 0)
        
        randDetectRate = 0.001
        setCtRatePerPerson = 0.1
        meanTheta = numpy.array([100, 0.95, 1, 1, randDetectRate, setCtRatePerPerson, heteroContactRate, 0, 0, 0, 0], numpy.float)
        times, infectedIndices, removedIndices, graph, model = runModel(meanTheta, endDate=500.0)
        detectedSet = graph.getRemovedSet()   
              
        removedGraph = graph.subgraph(list(graph.getRemovedSet())) 
        edges = removedGraph.getAllEdges()
        
        for i in removedGraph.getAllVertexIds(): 
            if removedGraph.vlist.V[i, HIVVertices.detectionTypeIndex] == HIVVertices.contactTrace: 
                self.assertTrue(removedGraph.vlist.V[i, HIVVertices.detectionTimeIndex] >= 180)

        
    
    @unittest.skip("")
    def testFindStandardResults(self):
        times = [3, 12, 22, 25, 40, 50]
        infectedIndices = [[1], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
        removedIndices = [[1], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]

        self.model.setT(51.0)
        self.model.setRecordStep(10)

        times, infectedIndices, removedIndices = self.model.findStandardResults(times, infectedIndices, removedIndices)

        self.assertTrue((numpy.array(times)==numpy.arange(0, 60, 10)).all())


        #Now try case where simulation is slightly longer than T and infections = 0
        numpy.random.seed(21)
        times = [3, 12, 22, 25, 40, 50]
        infectedIndices = [[1], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]
        removedIndices = [[1], [1, 2], [1, 2], [1, 2], [1, 2], [1, 2]]

        self.model.setT(51.0)
        self.model.setRecordStep(10)

        times, infectedIndices, removedIndices = self.model.findStandardResults(times, infectedIndices, removedIndices)
        logging.debug(times)