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)
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)
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))
class HIVGraphMetricsProfile(): def __init__(self): #Total number of people in population self.M = 1000 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 p = 0.01 generator = ErdosRenyiGenerator(p) self.graph = generator.generate(self.graph) perm1 = numpy.random.permutation(self.M) perm2 = numpy.random.permutation(self.M) sizes = [200, 300, 500, 1000] self.summary1 = [] self.summary2 = [] for size in sizes: self.summary1.append(self.graph.subgraph(perm1[0:size])) self.summary2.append(self.graph.subgraph(perm2[0:int(size/10)])) print(self.graph) def profileDistance(self): times = numpy.arange(len(self.summary1)) #metrics = HIVGraphMetrics2(times, GraphMatch("RANK")) metrics = HIVGraphMetrics2(times, GraphMatch("U")) #Can try RANK and Umeyama algorithm - Umeyama is faster self.summary2 = self.summary2[0:2] ProfileUtils.profile('metrics.distance(self.summary1, self.summary2)', globals(), locals())
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)
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())
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())