def saveStats(args): i, theta = args resultsFileName = outputDir + "SimStats" + str(i) + ".pkl" lock = FileLock(resultsFileName) if not lock.fileExists() and not lock.isLocked(): lock.lock() model = HIVModelUtils.createModel(targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg, theta=thetaArray[i]) times, infectedIndices, removedIndices, graph, compTimes, graphMetrics = HIVModelUtils.simulate(model) times = numpy.arange(startDate, endDate+1, recordStep) vertexArray, infectedIndices, removedIndices, contactGraphStats, removedGraphStats, finalRemovedDegrees = HIVModelUtils.generateStatistics(graph, times) stats = times, vertexArray, infectedIndices, removedGraphStats, finalRemovedDegrees, graphMetrics.objectives, compTimes Util.savePickle(stats, resultsFileName) lock.unlock() else: logging.debug("Results already computed: " + str(resultsFileName))
thetaArray = loadThetaArray(N, resultsDir, t)[0] logging.debug(thetaArray) paramList = [] for i in range(thetaArray.shape[0]): paramList.append((i, thetaArray[i, :])) pool = multiprocessing.Pool(multiprocessing.cpu_count()) resultIterator = pool.map(saveStats, paramList) #resultIterator = map(saveStats, paramList) pool.terminate() #Now save the statistics on the target graph times = numpy.arange(startDate, endDate+1, recordStep) vertexArray, infectedIndices, removedIndices, contactGraphStats, removedGraphStats, finalRemovedDegrees = HIVModelUtils.generateStatistics(targetGraph, times) stats = vertexArray, infectedIndices, removedIndices, contactGraphStats, removedGraphStats, finalRemovedDegrees resultsFileName = outputDir + "IdealStats.pkl" Util.savePickle(stats, resultsFileName) else: import matplotlib matplotlib.use("GTK3Agg") import matplotlib.pyplot as plt plotStyles = ['k-', 'kx-', 'k+-', 'k.-', 'k*-'] N, resultsDir, outputDir, recordStep, startDate, endDate, prefix, targetGraph, breakSize, numEpsilons, M, matchAlpha, matchAlg, numInds = loadParams(0) inds = range(numInds) numRecordSteps = int((endDate-startDate)/recordStep)+1
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)) times, infectedIndices, removedIndices, graph = model.simulate(True) statistics = GraphStatistics() vertexArray, infectedIndices, removedIndices, contactGraphStats, removedGraphStats = HIVModelUtils.generateStatistics(graph, times) numInfectedIndices = [len(x) for x in infectedIndices] plt.figure(0) plt.plot(times, numInfectedIndices) plt.xlabel("Time") plt.ylabel("Infected") plt.figure(1) plt.plot(times, contactGraphStats[:, statistics.numVerticesIndex]) plt.xlabel("Time") plt.ylabel("Contact graph size") plt.show()
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]
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, epsilon, matcher, float(endDate)) graphMetrics.breakDist = 1.0 rates = HIVRates(graph, hiddenDegSeq) model = HIVEpidemicModel(graph, rates, T=float(endDate), T0=float(startDate), metrics=graphMetrics) model.setRecordStep(recordStep) model.setParams(meanTheta) numpy.random.seed(i) times, infectedIndices, removedIndices, graph = model.simulate(True) print(times) times2, vertexArray, removedGraphStats = HIVModelUtils.generateStatistics(graph, startDate, endDate, recordStep) print(graphMetrics.dists) graphDists.append(graphMetrics.dists) removedArray.append(vertexArray[:, 0]) maleArray.append(vertexArray[:, 1]) femaleArray.append(vertexArray[:, 2]) biArray.append(vertexArray[:, 4]) graphDists = numpy.array(graphDists) removedArray = numpy.array(removedArray) maleArray = numpy.array(maleArray) femaleArray = numpy.array(femaleArray) biArray = numpy.array(biArray) graphDistsMean = graphDists.mean(0)