comparative-judgement
Version:
Comparative Judgement Algorithms
126 lines (121 loc) • 3.54 kB
JavaScript
// Read in a decisions file
// Read in an optional player anchor file
// Estimate the model for players
// Write out the players file
/* jshint -W024, expr:true */
/*jslint node: true */
/*global expect, fx, sinon*/
/*jshint -W083 */
;
var _ = require('underscore');
var csv = require('fast-csv');
var statutils = require('./statutils');
var estimation = require('./estimation');
var byjudge = require('./byjudge');
var async = require('async');
var fs = require("fs");
var estimateFromCsv = function(decisionsCsv, playersCsv ,callback){
var decisions = [];
var anchors = [];
var players = [];
var start;
// Read in the decisions csv
async.series([
function(callback){
csv
.fromPath(decisionsCsv, {headers:true})
.on("data", function(data){
decisions.push(data);
})
.on("end", function(){
callback();
});
},
function(callback){
if(playersCsv){
csv
.fromPath(playersCsv, {headers:true})
.on("data", function(data){
anchors.push(data);
})
.on("end", function(){
callback();
});
} else {
callback();
}
},
function(callback){
var chosen = _.uniq(_.pluck(decisions, 'Candidate Chosen'));
var notChosen = _.uniq(_.pluck(decisions, 'Candidate Not Chosen'));
var playerIds = _.union(chosen, notChosen);
var myPlayers = [];
for(var i=0; i<playerIds.length; i++){
var anchor = _.findWhere(anchors, {Id: playerIds[i]});
if(anchor) {
myPlayers.push({
_id: playerIds[i],
comparisons: anchor.Comparisons,
observedScore: anchor['Raw Score'],
trueScore: anchor['True Score'],
seTrueScore: anchor['True Score SE'],
selected: 0,
infit: anchor.Infit,
decisions: [],
opponents: [],
anchor: true,
});
} else {
myPlayers.push({
_id: playerIds[i],
comparisons: 0,
observedScore: 0,
trueScore: 0,
seTrueScore: 0,
selected: 0,
infit: 0,
decisions: [],
opponents: []
});
}
}
byjudge.rebuildPlayers(myPlayers, decisions);
start = new Date().getTime();
estimation.estimateCJ('',myPlayers, 4, function(task, estPlayers){
var end = new Date().getTime();
var time = end - start;
console.log('Execution time: ' + time);
for(var k=0; k<estPlayers.length; k++){
players.push(estPlayers[k]);
}
callback();
});
},
function(callback){
//save scripts collection to csv
var fn = Date.now() + '-players.csv';
var ws = fs.createWriteStream(fn);
csv
.write(players, {
headers: true,
transform: function(row){
return {
'Id': row._id,
'Comparisons': row.comparisons,
'Observed Score': row.observedScore,
'True Score': row.trueScore,
'True Score SE': row.seTrueScore,
};
}
})
.pipe(ws);
callback();
}
], function(err){
if(err) return callback(err);
callback(null, 'estimation complete');
});
};
module.exports = {
estimateFromCsv: estimateFromCsv,
};