本文整理汇总了Python中exp.viroscopy.model.HIVGraph.HIVGraph.getNumVertices方法的典型用法代码示例。如果您正苦于以下问题:Python HIVGraph.getNumVertices方法的具体用法?Python HIVGraph.getNumVertices怎么用?Python HIVGraph.getNumVertices使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类exp.viroscopy.model.HIVGraph.HIVGraph
的用法示例。
在下文中一共展示了HIVGraph.getNumVertices方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testPickle
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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))
示例2: profileSimulate
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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())
示例3: testInfectionProbability
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例4: testRemoveEvent
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例5: createModel
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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
示例6: testContactRates
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例7: testContructor
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
def testContructor(self):
numVertices = 10
graph = HIVGraph(numVertices)
self.assertEquals(numVertices, graph.getNumVertices())
self.assertEquals(8, graph.getVertexList().getNumFeatures())
self.assertTrue(graph.isUndirected() == True)
示例8: testRandomDetectionRates
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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())))
示例9: simulate
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例10: testContactEvent
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例11: testContactRates3
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例12: setUp
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例13: testUpperDetectionRates
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例14: findDerivative
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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)
示例15: runModel
# 需要导入模块: from exp.viroscopy.model.HIVGraph import HIVGraph [as 别名]
# 或者: from exp.viroscopy.model.HIVGraph.HIVGraph import getNumVertices [as 别名]
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