Skip to content
This repository was archived by the owner on Nov 24, 2021. It is now read-only.

Commit 8469567

Browse files
committed
CovidSim connector: Refactor param file lookup and fix Canada population density path
Use the US/Canada population density file for Canada, not the European one. Factor out helper functions to obtain the path for each parameter file, based on the input region. Add population density lookup for US territories + Alaska and Hawaii, although these are not yet supported by the UI and runner.
1 parent a3f2767 commit 8469567

File tree

1 file changed

+75
-47
lines changed

1 file changed

+75
-47
lines changed

packages/mrc-ide-covidsim/src/imperial.ts

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,63 +45,91 @@ export class ImperialModel implements Model {
4545
this.threadCount = threadCount
4646
}
4747

48+
/** Gets the path to the administrative units parameter file for the given region. */
49+
private getAdminPath(region: string): string {
50+
if (region === 'US') {
51+
return path.join(this.dataDir, 'admin_units', 'United_States_admin.txt')
52+
} else if (COUNTRY_PARAMS_BY_ISO_CODE[region]) {
53+
const { adminFileName } = COUNTRY_PARAMS_BY_ISO_CODE[region]
54+
return path.join(this.dataDir, 'admin_units', adminFileName)
55+
} else {
56+
throw new Error(`Could not find admin file for region ${region}`)
57+
}
58+
}
59+
60+
/**
61+
* Gets the path to the population density parameter file for the given region.
62+
* Europe is used as the default.
63+
*/
64+
private getPopulationDensityPath(region: string, subregion?: string): string {
65+
let populationDensityFileName: string
66+
if (
67+
['AS', 'GU', 'PR', 'VI'].includes(region) ||
68+
(region === 'US' &&
69+
['US-AK', 'US-HI', 'US-AS', 'US-GU', 'US-PR', 'US-VI'].includes(
70+
subregion
71+
))
72+
) {
73+
populationDensityFileName = 'wpop_us_terr.txt'
74+
} else if (['US', 'CA'].includes(region)) {
75+
populationDensityFileName = 'wpop_usacan.txt'
76+
} else {
77+
populationDensityFileName = 'wpop_eur.txt'
78+
}
79+
return path.join(this.dataDir, 'populations', populationDensityFileName)
80+
}
81+
82+
/**
83+
* Gets the path to the pre-parameters template file for the given region.
84+
* The UK is used as the default for known regions.
85+
*/
86+
private getPreParametersTemplatePath(region: string): string {
87+
let preParamsFileName: string
88+
if (region === 'US') {
89+
preParamsFileName = 'preUS_R0=2.0.txt'
90+
} else if (COUNTRY_PARAMS_BY_ISO_CODE[region]) {
91+
preParamsFileName =
92+
COUNTRY_PARAMS_BY_ISO_CODE[region].preParamsFileName ??
93+
'preUK_R0=2.0.txt'
94+
} else {
95+
throw new Error(
96+
`Could not find pre-parameters template file for region ${region}`
97+
)
98+
}
99+
return path.join(this.dataDir, 'param_files', preParamsFileName)
100+
}
101+
102+
private getSubregionName(region: string, subregion: string): string {
103+
if (region === 'US') {
104+
return US_SUBREGIONS[subregion]
105+
} else if (COUNTRY_PARAMS_BY_ISO_CODE[region]) {
106+
const { subregions } = COUNTRY_PARAMS_BY_ISO_CODE[region]
107+
return subregions[subregion]
108+
} else {
109+
throw new Error(`Could not find subregions for region ${region}`)
110+
}
111+
}
112+
48113
inputs(input: input.ModelInput): ImperialRunnerModelInput {
49114
const inputFiles = []
50-
// Select the correct executable and static inputs based on the region.
51-
let adminPath,
52-
populationDensityPath,
53-
preParametersTemplatePath,
54-
subregionName
55115

56116
const modelPath = path.join(this.binDir, 'CovidSim')
57117

118+
// Select the correct static inputs based on the region.
119+
let adminPath = this.getAdminPath(input.region)
120+
const populationDensityPath = this.getPopulationDensityPath(
121+
input.region,
122+
input.subregion
123+
)
58124
const parametersTemplatePath = path.join(
59125
this.dataDir,
60126
'param_files',
61127
'p_NoInt.txt'
62128
)
63-
64-
// The US has its own executable, population density file, and pre-params file.
65-
if (input.region === 'US') {
66-
adminPath = path.join(
67-
this.dataDir,
68-
'admin_units',
69-
'United_States_admin.txt'
70-
)
71-
populationDensityPath = path.join(
72-
this.dataDir,
73-
'populations',
74-
'wpop_usacan.txt'
75-
)
76-
preParametersTemplatePath = path.join(
77-
this.dataDir,
78-
'param_files',
79-
'preUS_R0=2.0.txt'
80-
)
81-
subregionName = US_SUBREGIONS[input.subregion]
82-
} else if (COUNTRY_PARAMS_BY_ISO_CODE[input.region]) {
83-
const { adminFileName, subregions } = COUNTRY_PARAMS_BY_ISO_CODE[
84-
input.region
85-
]
86-
87-
adminPath = path.join(this.dataDir, 'admin_units', adminFileName)
88-
populationDensityPath = path.join(
89-
this.dataDir,
90-
'populations',
91-
'wpop_eur.txt'
92-
)
93-
94-
const preParamsFileName =
95-
COUNTRY_PARAMS_BY_ISO_CODE[input.region].preParamsFileName ??
96-
'preUK_R0=2.0.txt'
97-
98-
preParametersTemplatePath = path.join(
99-
this.dataDir,
100-
'param_files',
101-
preParamsFileName
102-
)
103-
subregionName = subregions[input.subregion]
104-
}
129+
const preParametersTemplatePath = this.getPreParametersTemplatePath(
130+
input.region
131+
)
132+
const subregionName = this.getSubregionName(input.region, input.subregion)
105133

106134
// Generate the intervention-related pre-parameters based on the input.
107135
inputFiles.push(preParametersTemplatePath)

0 commit comments

Comments
 (0)