def saveStats(args): i, theta, startDate, endDate, recordStep = args resultsFileName = outputDir + "SimStats" + str(i) + ".pkl" try: with open(resultsFileName) as f: pass except IOError as e: featureInds= numpy.ones(targetGraph.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, 1.0, matcher, float(endDate)) times, infectedIndices, removedIndices, graph = HIVModelUtils.simulate(thetaArray[i], startDate, endDate, recordStep, M, graphMetrics) times, vertexArray, removedGraphStats = HIVModelUtils.generateStatistics(graph, startDate, endDate, recordStep) stats = times, vertexArray, removedGraphStats, graphMetrics.dists, graphMetrics.graphDists, graphMetrics.labelDists Util.savePickle(stats, resultsFileName)
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) times, vertexArray, removedGraphStats = HIVModelUtils.generateStatistics(graph, startDate, endDate, recordStep) plt.figure(0) plt.plot(times, vertexArray[:, 0]) plt.xlabel("Time") plt.ylabel("Removed") plt.figure(1) plt.plot(times, vertexArray[:, 4]) plt.xlabel("Time") plt.ylabel("Bi Detect") plt.figure(2) plt.plot(times, vertexArray[:, 5]) plt.xlabel("Time") plt.ylabel("Rand Detect")
thetaArray = loadThetaArray(N, resultsDir, t)[0] logging.debug(thetaArray) paramList = [] for i in range(thetaArray.shape[0]): paramList.append((i, thetaArray[i, :], startDate, endDate, recordStep)) 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 stats = HIVModelUtils.generateStatistics(targetGraph, startDate, endDate, recordStep) resultsFileName = outputDir + "IdealStats.pkl" Util.savePickle(stats, resultsFileName) else: j = 0 resultsDir = PathDefaults.getOutputDir() + "viroscopy/real/theta" + str(j) + "/" outputDir = resultsDir + "stats/" endDate = endDates[j] for i in range(maxT): thetaArray, distArray = loadThetaArray(N, resultsDir, i) if thetaArray.shape[0] == N: t = i