def testSimulate(self): #We want to see if we can get the same simulation twice N, matchAlpha, breakScale, numEpsilons, epsilon, minEpsilon, matchAlg, abcMaxRuns, batchSize, pertScale = HIVModelUtils.toyABCParams() startDate, endDate, recordStep, M, targetGraph = HIVModelUtils.toySimulationParams(test=True) breakSize = (targetGraph.subgraph(targetGraph.removedIndsAt(endDate)).size - targetGraph.subgraph(targetGraph.removedIndsAt(startDate)).size) * breakScale theta, sigmaTheta, pertTheta = HIVModelUtils.toyTheta() model = HIVModelUtils.createModel(theta, targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg) model.setParams(theta) times, infectedIndices, removedIndices, graph, compTimes, graphMetrics = HIVModelUtils.simulate(model) numEdges = graph.getNumEdges() lastRemovedIndices = removedIndices[-1] #Simulate again model = HIVModelUtils.createModel(theta, targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg) model.setParams(theta) times, infectedIndices, removedIndices, graph, compTimes, graphMetrics = HIVModelUtils.simulate(model) numEdges2 = graph.getNumEdges() lastRemovedIndices2 = removedIndices[-1] self.assertEquals(numEdges, numEdges2) self.assertEquals(lastRemovedIndices, lastRemovedIndices2)
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))
def createModel(t): """ The parameter t is the particle index. """ return HIVModelUtils.createModel(targetGraph, startDate, endDate, recordStep, M, matchAlpha, breakSize, matchAlg)