示例#1
0
 def createModel(targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg, theta=None): 
     alpha = 2
     zeroVal = 0.9
     numpy.random.seed(21)
     
     graph = targetGraph.subgraph(targetGraph.removedIndsAt(startDate)) 
     graph.addVertices(M-graph.size)
     logging.debug("Created graph: " + str(graph))   
     
     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(matchAlg, alpha=matchAlpha, featureInds=featureInds, useWeightM=False)
     graphMetrics = HIVGraphMetrics2(targetGraph, breakSize, matcher, startDate)
     
     rates = HIVRates(graph, hiddenDegSeq)
     model = HIVEpidemicModel(graph, rates, T=float(endDate), T0=float(startDate), metrics=graphMetrics)
     model.setRecordStep(recordStep)
     if theta != None: 
         model.setParams(theta)
             
     return model 
示例#2
0
    def testSimulate2(self):    
        alpha = 2
        zeroVal = 0.9
        startDate = 0.0 
        endDate = 200.0
        M = 1000 
        undirected = True
        
        theta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta()        
                
        
        numpy.random.seed(21)
        graph = HIVGraph(M, undirected)
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    
        rates = HIVRates(graph, hiddenDegSeq)
        model = HIVEpidemicModel(graph, rates, endDate, startDate, metrics=None)
        #model.setRecordStep(recordStep)
        model.setParams(theta)
        times, infectedIndices, removedIndices, graph =  model.simulate(True)
        
        numVertices = graph.size
        numEdges = graph.getNumEdges()
        
        #Try again 
        numpy.random.seed(21)
        graph = HIVGraph(M, undirected)
        p = Util.powerLawProbs(alpha, zeroVal)
        hiddenDegSeq = Util.randomChoice(p, graph.getNumVertices())
    
        rates = HIVRates(graph, hiddenDegSeq)
        model = HIVEpidemicModel(graph, rates, endDate, startDate, metrics=None)
        model.setParams(theta)
        times, infectedIndices, removedIndices, graph =  model.simulate(True)
        
        numVertices2 = graph.size
        numEdges2 = graph.getNumEdges()

        self.assertEquals(numVertices2, numVertices)
        self.assertEquals(numEdges2, numEdges)
示例#3
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)
示例#4
0
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  
    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)