Skip to content

Instantly share code, notes, and snippets.

@null-a
Created May 16, 2017 09:04
Show Gist options
  • Save null-a/078bc0ee7df1af6995874508e82438cb to your computer and use it in GitHub Desktop.
Save null-a/078bc0ee7df1af6995874508e82438cb to your computer and use it in GitHub Desktop.
// Converts parameters serialized with WebPPL prior to PR #837 so that
// they work with more recent versions.
// https://github.com/probmods/webppl/pull/837
// Output is written to standard out and can be redirected to a file
// as required. For example:
// node convertParams.js old.json > new.json
var fs = require('fs');
var argv = process.argv;
if (argv.length < 3) {
console.log('Usage: node convertParams.js <json-parameter-file>');
process.exit();
}
var inFilename = argv[2];
try {
var inData = JSON.parse(fs.readFileSync(inFilename));
console.warn('Processing ' + inFilename + '...');
} catch (e) {
console.warn('Error reading ' + inFilename);
process.exit();
}
var outData = {};
for (var k in inData) {
var arr = inData[k];
if (!Array.isArray(arr) || arr.length !== 1) {
throw new Error('Unexpected input, cannot convert these parameters.');
}
outData[k] = arr[0];
}
console.log(JSON.stringify(outData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment