def oneModelRunWithOutput(self, allocationDictionary): import costcov import data from copy import deepcopy as dcp costCov = costcov.Costcov() spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) model, derived, params = self.helper.setupModelConstantsParameters(spreadsheetData) costCoverageInfo = self.getCostCoverageInfo() # run the model modelList = [] timestepsPre = 12 for t in range(timestepsPre): model.moveOneTimeStep() modelThisTimeStep = dcp(model) modelList.append(modelThisTimeStep) # update coverages after 1 year targetPopSize = getTargetPopSizeFromModelInstance(self.dataSpreadsheetName, self.helper.keyList, model) newCoverages = {} for i in range(0, len(spreadsheetData.interventionList)): intervention = spreadsheetData.interventionList[i] newCoverages[intervention] = costCov.function(allocationDictionary[intervention], costCoverageInfo[intervention], targetPopSize[intervention]) / targetPopSize[intervention] model.updateCoverages(newCoverages) for t in range(self.numModelSteps - timestepsPre): model.moveOneTimeStep() modelThisTimeStep = dcp(model) modelList.append(modelThisTimeStep) return modelList
def performCascadeOptimisation(self, optimise, MCSampleSize, filename, cascadeValues): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() initialTargetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, initialTargetPopSize) currentTotalBudget = sum(initialAllocation) xmin = [0.] * len(initialAllocation) for cascade in cascadeValues: totalBudget = currentTotalBudget * cascade args = { 'totalBudget': totalBudget, 'costCoverageInfo': costCoverageInfo, 'optimise': optimise, 'numModelSteps': self.numModelSteps, 'dataSpreadsheetName': self.dataSpreadsheetName, 'data': spreadsheetData } self.runOnce(MCSampleSize, xmin, args, spreadsheetData.interventionList, totalBudget, filename + str(cascade) + '.pkl')
def setUpDataModelConstantsParameters(): helperTests = helper.Helper() testData = data.readSpreadsheet( 'input_spreadsheets/InputForCode_tests.xlsx', helperTests.keyList) testModel, testDerived, testParams = helperTests.setupModelConstantsParameters( testData) return testData, testModel, testDerived, testParams, helperTests.keyList
def generateBOCVectors(self, filenameStem, regionNameList, cascadeValues, outcome): import pickle import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() targetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, targetPopSize) currentTotalBudget = sum(initialAllocation) spendingVector = [] outcomeVector = [] for cascade in cascadeValues: spendingVector.append(cascade * currentTotalBudget) filename = filenameStem + '_cascade_' + str(outcome) + '_' + str( cascade) + '.pkl' infile = open(filename, 'rb') thisAllocation = pickle.load(infile) infile.close() modelOutput = self.oneModelRunWithOutput(thisAllocation) if outcome == 'deaths': outcomeVector.append(modelOutput[self.numModelSteps - 1].getTotalCumulativeDeaths()) if outcome == 'stunting': outcomeVector.append( modelOutput[self.numModelSteps - 1].getCumulativeAgingOutStunted()) return spendingVector, outcomeVector
def oneModelRunWithOutput(self, allocationDictionary): import costcov import data from copy import deepcopy as dcp costCov = costcov.Costcov() spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) model, derived, params = self.helper.setupModelConstantsParameters( spreadsheetData) costCoverageInfo = self.getCostCoverageInfo() # run the model modelList = [] timestepsPre = 12 for t in range(timestepsPre): model.moveOneTimeStep() modelThisTimeStep = dcp(model) modelList.append(modelThisTimeStep) # update coverages after 1 year targetPopSize = getTargetPopSizeFromModelInstance( self.dataSpreadsheetName, self.helper.keyList, model) newCoverages = {} for i in range(0, len(spreadsheetData.interventionList)): intervention = spreadsheetData.interventionList[i] newCoverages[intervention] = costCov.function( allocationDictionary[intervention], costCoverageInfo[intervention], targetPopSize[intervention]) / targetPopSize[intervention] model.updateCoverages(newCoverages) for t in range(self.numModelSteps - timestepsPre): model.moveOneTimeStep() modelThisTimeStep = dcp(model) modelList.append(modelThisTimeStep) return modelList
def performSingleOptimisationForGivenTotalBudget(self, optimise, MCSampleSize, filename, totalBudget): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() xmin = [0.] * len(spreadsheetData.interventionList) args = {'totalBudget':totalBudget, 'costCoverageInfo':costCoverageInfo, 'optimise':optimise, 'numModelSteps':self.numModelSteps, 'dataSpreadsheetName':self.dataSpreadsheetName, 'data':spreadsheetData} self.runOnce(MCSampleSize, xmin, args, spreadsheetData.interventionList, totalBudget, filename+'.pkl')
def performSingleOptimisation(self, optimise, MCSampleSize, filename): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() initialTargetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, initialTargetPopSize) totalBudget = sum(initialAllocation) xmin = [0.] * len(initialAllocation) args = {'totalBudget':totalBudget, 'costCoverageInfo':costCoverageInfo, 'optimise':optimise, 'numModelSteps':self.numModelSteps, 'dataSpreadsheetName':self.dataSpreadsheetName, 'data':spreadsheetData} self.runOnce(MCSampleSize, xmin, args, spreadsheetData.interventionList, totalBudget, filename+'.pkl')
def getCostCoverageInfo(self): import data from copy import deepcopy as dcp spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = {} for intervention in spreadsheetData.interventionList: costCoverageInfo[intervention] = {} costCoverageInfo[intervention]['unitcost'] = dcp(spreadsheetData.costSaturation[intervention]["unit cost"]) costCoverageInfo[intervention]['saturation'] = dcp(spreadsheetData.costSaturation[intervention]["saturation coverage"]) return costCoverageInfo
def getInitialAllocationDictionary(self): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() targetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, targetPopSize) initialAllocationDictionary = {} for i in range(0, len(spreadsheetData.interventionList)): intervention = spreadsheetData.interventionList[i] initialAllocationDictionary[intervention] = initialAllocation[i] return initialAllocationDictionary
def performParallelCascadeOptimisation(self, optimise, MCSampleSize, filename, cascadeValues): import data from joblib import Parallel, delayed spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() initialTargetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, initialTargetPopSize) currentTotalBudget = sum(initialAllocation) xmin = [0.] * len(initialAllocation) # use one core per cascade value nCores = len(cascadeValues) Parallel(n_jobs=nCores)(delayed(self.cascadeFunc)(cascadeValue, currentTotalBudget, costCoverageInfo, optimise, MCSampleSize, xmin, filename) for cascadeValue in cascadeValues)
def getTargetPopSizeFromModelInstance(dataSpreadsheetName, keyList, model): import data spreadsheetData = data.readSpreadsheet(dataSpreadsheetName, keyList) numAgeGroups = len(keyList['ages']) targetPopSize = {} for intervention in spreadsheetData.interventionList: targetPopSize[intervention] = 0. for iAge in range(numAgeGroups): ageName = keyList['ages'][iAge] targetPopSize[intervention] += spreadsheetData.targetPopulation[intervention][ageName] * model.listOfAgeCompartments[iAge].getTotalPopulation() targetPopSize[intervention] += spreadsheetData.targetPopulation[intervention]['pregnant women'] * model.pregnantWomen.populationSize return targetPopSize
def getCostCoverageInfo(self): import data from copy import deepcopy as dcp spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = {} for intervention in spreadsheetData.interventionList: costCoverageInfo[intervention] = {} costCoverageInfo[intervention]['unitcost'] = dcp( spreadsheetData.costSaturation[intervention]["unit cost"]) costCoverageInfo[intervention]['saturation'] = dcp( spreadsheetData.costSaturation[intervention] ["saturation coverage"]) return costCoverageInfo
def getInitialTargetPopSize(self): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) mothers = self.helper.makePregnantWomen(spreadsheetData) numAgeGroups = len(self.helper.keyList['ages']) agePopSizes = self.helper.makeAgePopSizes(spreadsheetData) targetPopSize = {} for intervention in spreadsheetData.interventionList: targetPopSize[intervention] = 0. for iAge in range(numAgeGroups): ageName = self.helper.keyList['ages'][iAge] targetPopSize[intervention] += spreadsheetData.targetPopulation[intervention][ageName] * agePopSizes[iAge] targetPopSize[intervention] += spreadsheetData.targetPopulation[intervention]['pregnant women'] * mothers.populationSize return targetPopSize
def getTotalNationalBudget(self): import optimisation import data regionalBudgets = [] for region in range(0, self.numRegions): thisSpreadsheet = self.regionSpreadsheetList[region] thisOptimisation = optimisation.Optimisation(thisSpreadsheet, self.numModelSteps) spreadsheetData = data.readSpreadsheet(thisSpreadsheet, thisOptimisation.helper.keyList) costCoverageInfo = thisOptimisation.getCostCoverageInfo() initialTargetPopSize = thisOptimisation.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, initialTargetPopSize) regionTotalBudget = sum(initialAllocation) regionalBudgets.append(regionTotalBudget) nationalTotalBudget = sum(regionalBudgets) return nationalTotalBudget
def getTargetPopSizeFromModelInstance(dataSpreadsheetName, keyList, model): import data spreadsheetData = data.readSpreadsheet(dataSpreadsheetName, keyList) numAgeGroups = len(keyList['ages']) targetPopSize = {} for intervention in spreadsheetData.interventionList: targetPopSize[intervention] = 0. for iAge in range(numAgeGroups): ageName = keyList['ages'][iAge] targetPopSize[intervention] += spreadsheetData.targetPopulation[ intervention][ageName] * model.listOfAgeCompartments[ iAge].getTotalPopulation() targetPopSize[ intervention] += spreadsheetData.targetPopulation[intervention][ 'pregnant women'] * model.pregnantWomen.populationSize return targetPopSize
def getTotalNationalBudget(self): import optimisation import data regionalBudgets = [] for region in range(0, self.numRegions): thisSpreadsheet = self.regionSpreadsheetList[region] thisOptimisation = optimisation.Optimisation( thisSpreadsheet, self.numModelSteps) spreadsheetData = data.readSpreadsheet( thisSpreadsheet, thisOptimisation.helper.keyList) costCoverageInfo = thisOptimisation.getCostCoverageInfo() initialTargetPopSize = thisOptimisation.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation( spreadsheetData, costCoverageInfo, initialTargetPopSize) regionTotalBudget = sum(initialAllocation) regionalBudgets.append(regionTotalBudget) nationalTotalBudget = sum(regionalBudgets) return nationalTotalBudget
def performParallelCascadeOptimisation(self, optimise, MCSampleSize, filename, cascadeValues): import data from joblib import Parallel, delayed spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() initialTargetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, initialTargetPopSize) currentTotalBudget = sum(initialAllocation) xmin = [0.] * len(initialAllocation) # use one core per cascade value nCores = len(cascadeValues) Parallel(n_jobs=nCores)(delayed(self.cascadeFunc)( cascadeValue, currentTotalBudget, costCoverageInfo, optimise, MCSampleSize, xmin, filename) for cascadeValue in cascadeValues)
def getInitialTargetPopSize(self): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) mothers = self.helper.makePregnantWomen(spreadsheetData) numAgeGroups = len(self.helper.keyList['ages']) agePopSizes = self.helper.makeAgePopSizes(spreadsheetData) targetPopSize = {} for intervention in spreadsheetData.interventionList: targetPopSize[intervention] = 0. for iAge in range(numAgeGroups): ageName = self.helper.keyList['ages'][iAge] targetPopSize[ intervention] += spreadsheetData.targetPopulation[ intervention][ageName] * agePopSizes[iAge] targetPopSize[intervention] += spreadsheetData.targetPopulation[ intervention]['pregnant women'] * mothers.populationSize return targetPopSize
def performSingleOptimisationForGivenTotalBudget(self, optimise, MCSampleSize, filename, totalBudget): import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() xmin = [0.] * len(spreadsheetData.interventionList) args = { 'totalBudget': totalBudget, 'costCoverageInfo': costCoverageInfo, 'optimise': optimise, 'numModelSteps': self.numModelSteps, 'dataSpreadsheetName': self.dataSpreadsheetName, 'data': spreadsheetData } self.runOnce(MCSampleSize, xmin, args, spreadsheetData.interventionList, totalBudget, filename + '.pkl')
def generateBOCVectors(self, filenameStem, regionNameList, cascadeValues, outcome): import pickle import data spreadsheetData = data.readSpreadsheet(self.dataSpreadsheetName, self.helper.keyList) costCoverageInfo = self.getCostCoverageInfo() targetPopSize = self.getInitialTargetPopSize() initialAllocation = getTotalInitialAllocation(spreadsheetData, costCoverageInfo, targetPopSize) currentTotalBudget = sum(initialAllocation) spendingVector = [] outcomeVector = [] for cascade in cascadeValues: spendingVector.append(cascade * currentTotalBudget) filename = filenameStem + '_cascade_' + str(outcome) + '_' + str(cascade)+'.pkl' infile = open(filename, 'rb') thisAllocation = pickle.load(infile) infile.close() modelOutput = self.oneModelRunWithOutput(thisAllocation) if outcome == 'deaths': outcomeVector.append(modelOutput[self.numModelSteps-1].getTotalCumulativeDeaths()) if outcome == 'stunting': outcomeVector.append(modelOutput[self.numModelSteps-1].getCumulativeAgingOutStunted()) return spendingVector, outcomeVector
import data import helper import output import costcov helper = helper.Helper() costCov = costcov.Costcov() country = 'Bangladesh' startYear = 2016 version = '1604' dataFilename = '../input_spreadsheets/%s/InputForCode_%s.xlsx' % (country, country) #dataFilename = '../input_spreadsheets/%s/Input_%s_%i_%s.xlsx'%(country, country, startYear, version) inputData = data.readSpreadsheet(dataFilename, helper.keyList) numAgeGroups = len(helper.keyList['ages']) numsteps = 180 oldCoverages = {} costCovParams = {} print "Baseline coverage of: " for intervention in inputData.interventionList: print "%s = %g" % (intervention, inputData.coverage[intervention]) oldCoverages[intervention] = inputData.coverage[intervention] # calculate coverage (%) costCovParams[intervention] = {} costCovParams[intervention]['unitcost'] = dcp( inputData.costSaturation[intervention]["unit cost"]) costCovParams[intervention]['saturation'] = dcp(
def setUpDataModelConstantsParameters(): helperTests = helper.Helper() testData = data.readSpreadsheet('input_spreadsheets/InputForCode_tests.xlsx', helperTests.keyList) testModel, testDerived, testParams = helperTests.setupModelConstantsParameters(testData) return testData, testModel, testDerived, testParams, helperTests.keyList
import os, sys moduleDir = os.path.join(os.path.dirname(__file__), "..") sys.path.append(moduleDir) import data import helper import output helper = helper.Helper() country = "Bangladesh" startYear = 2000 dataFilename = "../input_spreadsheets/%s/validation/Input_%s_%i_1604.xlsx" % (country, country, startYear) inputData = data.readSpreadsheet(dataFilename, helper.keyList) numAgeGroups = len(helper.keyList["ages"]) year1 = 3871841 year2 = 3731124 year3 = 3645815 year4 = 3567786 year5 = 3491964 agePopSizes = [year1 / 12.0, year1 * 5.0 / 12.0, year1 * 6.0 / 12.0, year2, year3 + year4 + year5] numsteps = 192 timespan = helper.keyList["timestep"] * float(numsteps) # endYear = startYear + int(timespan) numYears = int(timespan) yearList = list(range(startYear, startYear + numYears)) # initialise