Beispiel #1
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)
Beispiel #2
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) 
Beispiel #3
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)        
Beispiel #4
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())