-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #689 from adityasharad/unified-ui-cli
Add a simple CLI for use in unified model interface
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* eslint-disable no-console, unicorn/no-process-exit */ | ||
import * as fs from 'fs' | ||
import { run } from './run' | ||
import { ScenarioFlat, ScenarioData } from './types/Param.types' | ||
import { getAgeDistribution } from '../components/Main/state/getAgeDistribution' | ||
import { getSeverityDistribution } from '../components/Main/state/getSeverityDistribution' | ||
import { toInternal } from '../components/Main/state/getScenario' | ||
|
||
const handleRejection: NodeJS.UnhandledRejectionListener = (err) => { | ||
console.error(err) | ||
process.exit(1) | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
function usage() { | ||
console.log( | ||
` | ||
Usage: | ||
cli <input-file> <output-file> | ||
Manually perform a single model run with the given input, writing the results to the given output file. | ||
`.trim(), | ||
) | ||
|
||
process.exit(1) | ||
} | ||
|
||
main() |
b0ee9b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: