Esempio n. 1
0
def runModel(meanTheta):
    startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.toySimulationParams()
    endDate = 1000.0
    recordStep = 50
    undirected = True

    logging.debug("MeanTheta=" + str(meanTheta))
    numReps = 10
    numInfectedIndices = []
    numRemovedIndices = []
    numRemovedEdges = []
    numContactEdges = []

    statistics = GraphStatistics()
    statsTimes = numpy.arange(0, endDate, recordStep)

    for i in range(numReps):
        graph = HIVGraph(M, undirected)
        logging.info("Created graph at index " + str(i) + ": " + 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)
        times, infectedIndices, removedIndices, graph = model.simulate(True)

        vertexArray, infectedIndices, removedIndices, contactGraphStats, removedGraphStats = HIVModelUtils.generateStatistics(
            graph, statsTimes
        )

        numInfectedIndices.append([len(x) for x in infectedIndices])
        numRemovedIndices.append([len(x) for x in removedIndices])

        numContactEdges.append(contactGraphStats[:, statistics.numVerticesIndex])
        numRemovedEdges.append(removedGraphStats[:, statistics.numVerticesIndex])

    numInfectedIndices = numpy.array(numInfectedIndices)
    numInfectedIndices = numpy.mean(numInfectedIndices, 0)

    numRemovedIndices = numpy.array(numRemovedIndices)
    numRemovedIndices = numpy.mean(numRemovedIndices, 0)

    numContactEdges = numpy.array(numContactEdges)
    numContactEdges = numpy.mean(numContactEdges, 0)

    numRemovedEdges = numpy.array(numRemovedEdges)
    numRemovedEdges = numpy.mean(numRemovedEdges, 0)

    return statsTimes, numInfectedIndices, numRemovedIndices, numContactEdges, numRemovedEdges, vertexArray[:, 6]
    def testTimings(self): 
        numVertices = 100
        graph = HIVGraph(numVertices)
        graph = ErdosRenyiGenerator(0.1).generate(graph)        

        graph.vList.V[:, HIVVertices.stateIndex] = HIVVertices.removed
        graph.vList.V[:, HIVVertices.detectionTimeIndex] = 0
        graph.endEventTime = 0
        graph.W = scipy.sparse.csr_matrix(graph.W)
        maxSize = 500
        metrics = HIVGraphMetrics2(graph, maxSize)
        
        metrics.addGraph(graph)
def runModel(theta, endDate=100.0, M=1000): 
    numpy.random.seed(21)
    undirected= True
    recordStep = 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.setParams(theta)
    
    times, infectedIndices, removedIndices, graph = model.simulate(True)            
    
    return times, infectedIndices, removedIndices, graph, model  
Esempio n. 4
0
 def readSimulationHIVGraph(self): 
     """
     We want to read the HIV network into a HIVGraph object. 
     """
     hivReader = HIVGraphReader()
     graph = hivReader.readHIVGraph()        
     
     edgeTypeIndex1 = 0
     edgeTypeIndex2 = 1
     sGraphContact = graph.getSparseGraph(edgeTypeIndex1)
     sGraphInfect = graph.getSparseGraph(edgeTypeIndex2)
     sGraphContact = sGraphContact.union(sGraphInfect)
     sGraph = sGraphContact
     
     fInds = hivReader.getIndicatorFeatureIndices()
     
     hivGraph = HIVGraph(graph.getNumVertices())
     hivGraph.setWeightMatrixSparse(sGraph.getSparseWeightMatrix())
     
     vertices = hivGraph.getVertexList()
     
     for i in range(sGraph.getNumVertices()): 
         vertices.V[i, vertices.dobIndex] = sGraph.getVertex(i)[fInds["birthDate"]]
         vertices.V[i, vertices.genderIndex] = sGraph.getVertex(i)[fInds["gender"]]
         vertices.V[i, vertices.orientationIndex] = sGraph.getVertex(i)[fInds["orient"]]
         vertices.V[i, vertices.stateIndex] = vertices.removed
         #vertices.V[i, self.infectionTimeIndex]
         vertices.V[i, vertices.detectionTimeIndex] = sGraph.getVertex(i)[fInds["detectDate"]]
         
         if sGraph.getVertex(i)[fInds["contactTrace"]]: 
             vertices.V[i, vertices.detectionTypeIndex] = vertices.contactTrace
         else:
             vertices.V[i, vertices.detectionTypeIndex] = vertices.randomDetect
         
         #Not sure about this since the number of contacts changes over time 
         #Possibly ignore when we compare graphs 
         vertices.V[i, vertices.hiddenDegreeIndex] = sGraph.getVertex(i)[fInds["numContacts"]]
         
     return hivGraph 
    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 profileSimulate(self):
        startDate, endDates, numRecordSteps, M, targetGraph = HIVModelUtils.realSimulationParams()
        meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta()
        
        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+1000)
        model.setRecordStep(10)
        model.setParams(meanTheta)
        
        logging.debug("MeanTheta=" + str(meanTheta))

        ProfileUtils.profile('model.simulate()', globals(), locals())
    def setUp(self):
        numpy.random.seed(21)
        numpy.set_printoptions(linewidth=100, suppress=True, precision=3)
        
        
        numVertices = 10
        self.graph = HIVGraph(numVertices)

        self.graph.vlist.setInfected(1, 0.0)
        self.graph.vlist.setDetected(1, 0.1, 0)
        self.graph.vlist.setInfected(2, 2.0)
        self.graph.vlist.setDetected(2, 2.0, 0)
        self.graph.vlist.setInfected(7, 3.0)
        self.graph.vlist.setDetected(7, 3.0, 0)
        self.graph.endEventTime = 3.0
Esempio n. 8
0
 def toySimulationParams(loadTarget=True, test=False): 
     
     if loadTarget: 
         resultsDir = PathDefaults.getOutputDir() + "viroscopy/toy/" 
         graphFile = resultsDir + "ToyEpidemicGraph0"
         targetGraph = HIVGraph.load(graphFile)        
     
     startDate = 0.0        
     endDate = 1000.0
     recordStep = 100
     M = 5000
     testPeriod = 300
     
     if test:
         endDate += testPeriod   
     
     if loadTarget: 
         return startDate, endDate, recordStep, M, targetGraph
     else: 
         return startDate, endDate, recordStep, M
    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)
Esempio n. 10
0
    def testAddGraph(self): 
        maxSize = 100
        metrics = HIVGraphMetrics2(self.graph, maxSize)
        
        metrics.addGraph(self.graph)

        self.assertAlmostEquals(metrics.objectives[0], -0.834, 3)
        self.assertAlmostEquals(metrics.meanObjective(), -0.834, 3)
        
        #Start a new graph 
        #Compute distances directly 
        matcher = GraphMatch("QCV")
        graph =  HIVGraph(self.graph.size)
        dists = [] 
        metrics = HIVGraphMetrics2(self.graph, maxSize)
        
        graph.vlist.setInfected(1, 0.0)
        graph.vlist.setDetected(1, 0.1, 0)
        graph.endEventTime = 1
        metrics.addGraph(graph)
        
        
        t = graph.endTime()
        subgraph1 = graph.subgraph(graph.removedIndsAt(t))
        subgraph2 = self.graph.subgraph(graph.removedIndsAt(t)) 
        permutation, distance, time = matcher.match(subgraph1, subgraph2)
        lastObj, lastGraphObj, lastLabelObj = matcher.distance(subgraph1, subgraph2, permutation, True, False, True)
        self.assertEquals(metrics.objectives[-1], lastObj)

        self.assertFalse(metrics.shouldBreak())
        
        graph.vlist.setInfected(2, 2.0)
        graph.vlist.setDetected(2, 2.0, 0)
        metrics.addGraph(graph)
        
        t = graph.endTime()
        subgraph1 = graph.subgraph(graph.removedIndsAt(t))
        subgraph2 = self.graph.subgraph(graph.removedIndsAt(t)) 
        permutation, distance, time = matcher.match(subgraph1, subgraph2)
        lastObj, lastGraphObj, lastLabelObj = matcher.distance(subgraph1, subgraph2, permutation, True, False, True)
        self.assertEquals(metrics.objectives[-1], lastObj)   
        self.assertFalse(metrics.shouldBreak())
        
        graph.vlist.setInfected(7, 3.0)
        graph.vlist.setDetected(7, 3.0, 0)
        metrics.addGraph(graph)
        
        t = graph.endTime()
        subgraph1 = graph.subgraph(graph.removedIndsAt(t))
        subgraph2 = self.graph.subgraph(graph.removedIndsAt(t)) 
        permutation, distance, time = matcher.match(subgraph1, subgraph2)
        lastObj, lastGraphObj, lastLabelObj = matcher.distance(subgraph1, subgraph2, permutation, True, False, True)
        self.assertEquals(metrics.objectives[-1], lastObj) 
        self.assertFalse(metrics.shouldBreak())
        
        #Test case where one graph has zero size 
        graph1 = HIVGraph(10)
        graph2 = HIVGraph(10)
        
        graph1.vlist[:, HIVVertices.stateIndex] = HIVVertices.removed
        metrics = HIVGraphMetrics2(graph2, maxSize)
        metrics.addGraph(graph1)
        
        #Problem is that distance is 1 when one graph is zero
        self.assertEquals(len(metrics.objectives), 0) 
Esempio n. 11
0
class  HIVGraphMetrics2Test(unittest.TestCase):
    def setUp(self):
        numpy.random.seed(21)
        numpy.set_printoptions(linewidth=100, suppress=True, precision=3)
        
        
        numVertices = 10
        self.graph = HIVGraph(numVertices)

        self.graph.vlist.setInfected(1, 0.0)
        self.graph.vlist.setDetected(1, 0.1, 0)
        self.graph.vlist.setInfected(2, 2.0)
        self.graph.vlist.setDetected(2, 2.0, 0)
        self.graph.vlist.setInfected(7, 3.0)
        self.graph.vlist.setDetected(7, 3.0, 0)
        self.graph.endEventTime = 3.0

    def testAddGraph(self): 
        maxSize = 100
        metrics = HIVGraphMetrics2(self.graph, maxSize)
        
        metrics.addGraph(self.graph)

        self.assertAlmostEquals(metrics.objectives[0], -0.834, 3)
        self.assertAlmostEquals(metrics.meanObjective(), -0.834, 3)
        
        #Start a new graph 
        #Compute distances directly 
        matcher = GraphMatch("QCV")
        graph =  HIVGraph(self.graph.size)
        dists = [] 
        metrics = HIVGraphMetrics2(self.graph, maxSize)
        
        graph.vlist.setInfected(1, 0.0)
        graph.vlist.setDetected(1, 0.1, 0)
        graph.endEventTime = 1
        metrics.addGraph(graph)
        
        
        t = graph.endTime()
        subgraph1 = graph.subgraph(graph.removedIndsAt(t))
        subgraph2 = self.graph.subgraph(graph.removedIndsAt(t)) 
        permutation, distance, time = matcher.match(subgraph1, subgraph2)
        lastObj, lastGraphObj, lastLabelObj = matcher.distance(subgraph1, subgraph2, permutation, True, False, True)
        self.assertEquals(metrics.objectives[-1], lastObj)

        self.assertFalse(metrics.shouldBreak())
        
        graph.vlist.setInfected(2, 2.0)
        graph.vlist.setDetected(2, 2.0, 0)
        metrics.addGraph(graph)
        
        t = graph.endTime()
        subgraph1 = graph.subgraph(graph.removedIndsAt(t))
        subgraph2 = self.graph.subgraph(graph.removedIndsAt(t)) 
        permutation, distance, time = matcher.match(subgraph1, subgraph2)
        lastObj, lastGraphObj, lastLabelObj = matcher.distance(subgraph1, subgraph2, permutation, True, False, True)
        self.assertEquals(metrics.objectives[-1], lastObj)   
        self.assertFalse(metrics.shouldBreak())
        
        graph.vlist.setInfected(7, 3.0)
        graph.vlist.setDetected(7, 3.0, 0)
        metrics.addGraph(graph)
        
        t = graph.endTime()
        subgraph1 = graph.subgraph(graph.removedIndsAt(t))
        subgraph2 = self.graph.subgraph(graph.removedIndsAt(t)) 
        permutation, distance, time = matcher.match(subgraph1, subgraph2)
        lastObj, lastGraphObj, lastLabelObj = matcher.distance(subgraph1, subgraph2, permutation, True, False, True)
        self.assertEquals(metrics.objectives[-1], lastObj) 
        self.assertFalse(metrics.shouldBreak())
        
        #Test case where one graph has zero size 
        graph1 = HIVGraph(10)
        graph2 = HIVGraph(10)
        
        graph1.vlist[:, HIVVertices.stateIndex] = HIVVertices.removed
        metrics = HIVGraphMetrics2(graph2, maxSize)
        metrics.addGraph(graph1)
        
        #Problem is that distance is 1 when one graph is zero
        self.assertEquals(len(metrics.objectives), 0) 

    def testMeanObjective(self): 
        metrics = HIVGraphMetrics2(self.graph, matcher=None, T=1000)
        metrics.objectives = [0.5, 0.5, 0.5]
        
        self.assertEquals(metrics.meanObjective(), 0.5)
        
        metrics.objectives = [1.0, 0.5, 0.5]
        self.assertEquals(metrics.meanObjective(), 7.0/12)
        
        
        metrics.objectives = [1.0, 1.0, 0.5]
        self.assertEquals(metrics.meanObjective(), 3.0/4)
        
        #self.assertTrue(metrics.shouldBreak())
        
    def testTimings(self): 
        numVertices = 100
        graph = HIVGraph(numVertices)
        graph = ErdosRenyiGenerator(0.1).generate(graph)        

        graph.vList.V[:, HIVVertices.stateIndex] = HIVVertices.removed
        graph.vList.V[:, HIVVertices.detectionTimeIndex] = 0
        graph.endEventTime = 0
        graph.W = scipy.sparse.csr_matrix(graph.W)
        maxSize = 500
        metrics = HIVGraphMetrics2(graph, maxSize)
        
        metrics.addGraph(graph)
Esempio n. 12
0
numpy.random.seed(24)
numpy.set_printoptions(suppress=True, precision=4, linewidth=100)

startDate, endDate, recordStep, M = HIVModelUtils.toySimulationParams(False, test=True)

numRepetitions = 10
undirected = True
outputDir = PathDefaults.getOutputDir() + "viroscopy/toy/"
theta, sigmaTheta, purtTheta = HIVModelUtils.toyTheta()

graphList = []
numInfected = numpy.zeros(numRepetitions)
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))
Esempio n. 13
0
assert False, "Must run with -O flag"

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
numpy.seterr(all='raise')
numpy.random.seed(24)
numpy.set_printoptions(suppress=True, precision=4, linewidth=100)

startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.realSimulationParams()
M = 100
endDate = startDate + 1000
meanTheta, sigmaTheta = HIVModelUtils.estimatedRealTheta()
meanTheta = numpy.array([ 1,   0.1,    0.0,    0.00,         0.5,      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))
Esempio n. 14
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())
Esempio n. 15
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)
     
    @unittest.skip("")
    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())
        

    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)


        

        
  

    @unittest.skip("")
    def testSimulateInfects(self): 
        #Test varying infection probabilities 
        
        heteroContactRate = 0.1
        manWomanInfectProb = 1.0 
        meanTheta = numpy.array([100, 1, 1, 0, 0, heteroContactRate, manWomanInfectProb], 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])
  
    @unittest.skip("")
    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)