Repository: neherlab/covid19_scenarios
File: src/algorithms/cli.ts (TypeScript)

Code before:

process.on('unhandledRejection', handleRejection)

function readJsonFromFile<T>(arg: number) {
  const inputFilename = process.argv[arg]
  if (!inputFilename) {
    usage()
  }
  console.log(`Reading data from file ${inputFilename}`)
  const inputData = fs.readFileSync(inputFilename, 'utf8')
  const inputJson = JSON.parse(inputData) as T
  console.log(`Read input data`)
  return inputJson
}

async function main() {
  const scenarioData = readJsonFromFile<ScenarioData>(2)
  const outputFile = process.argv[3]
  if (!outputFile) {
    usage()
  }
  console.log(`Will write model output to ${outputFile}`)

  const scenario = toInternal(scenarioData.data)

  const params: ScenarioFlat = {
    ...scenario.population,
    ...scenario.epidemiological,
    ...scenario.simulation,
    ...scenario.mitigation,
  }
  // Read data from severityDistributions.json and perform JSON validation.
  // Use the model's default (and only) severity distribution.
  console.log(`Reading severity distribution data`)
  const DEFAULT_SEVERITY_DISTRIBUTION = 'China CDC'
  const severity = getSeverityDistribution(DEFAULT_SEVERITY_DISTRIBUTION)
  // Read data from ageDistribution.json and perform JSON validation.
  console.log(`Reading age distribution data`)
  const ageDistribution = getAgeDistribution(params.ageDistributionName)

  try {
    console.log('Running the model')
    const result = await run({ params, severity, ageDistribution })
    console.log('Run complete')
    console.log(result)
    console.log(`Writing output to ${outputFile}`)
    fs.writeFileSync(outputFile, JSON.stringify(result))
  } catch (error) {
    console.error(`Run failed: ${error}`)
    process.exit(1)
  }

Review comment (suggestion):
I think you can type this as `SeverityDistributionData` instead of `any`. That type can be added to the import from `./types/Param.types`.

Code after:
  process.exit(1)
}

process.on('unhandledRejection', handleRejection)

/**
 * Read a file in JSON format.
 *
 * @param inputFilename The path to the file.
 */
function readJsonFromFile<T>(inputFilename: string) {
  console.log('Reading data from file ' + inputFilename)
  const inputData = fs.readFileSync(inputFilename, 'utf8')
  const inputJson = JSON.parse(inputData) as T
  return inputJson
}

/**
 * Get severity distribution data. If a file is specified on the command
 * line, give priority to its contents, else load a default distribution.
 *
 * @param inputFilename The path to the file.
 */
function getSeverity(inputFilename: string | undefined) {
  if (inputFilename) {
    const data = readJsonFromFile<any>(inputFilename)
    return data.data
  }
  else {
    const DEFAULT_SEVERITY_DISTRIBUTION = 'China CDC'
    return getSeverityDistribution(DEFAULT_SEVERITY_DISTRIBUTION)
  }
}

/**
 * Get age distribution data. If a file is specified on the command
 * line, give priority to its contents, else load the distribution
 * name as specified in the scenario parameters.
 *
 * @param inputFilename The path to the file.
 * @param name The age distribution name to use if no file is
 *             specified.
 */
function getAge(inputFilename: string | undefined, name: string) {
  if (inputFilename) {
    const data = readJsonFromFile<any>(inputFilename)
    return data.data
  }
  else {
    return getAgeDistribution(name)
  }