3d-interface/analysis/analyse.js

236 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-10-07 14:13:20 +02:00
// http://codereview.stackexchange.com/questions/37028/grouping-elements-in-array-by-multiple-properties
function groupBy(array, f)
{
var groups = {};
array.forEach(function(o) {
var group = JSON.stringify(f(o));
groups[group] = groups[group] || [];
groups[group].push(o);
});
return Object.keys(groups).map(function(group) {
return groups[group];
})
}
function compareRecommendationStyle(style1, style2) {
if (style1.recommendation_style[4] === 'B' && style2.recommendation_style[4] !== 'B') {
return -1;
}
if (style2.recommendation_style[4] === 'B' && style1.recommendation_style[4] !== 'B') {
return 1;
}
if (style1.recommendation_style[4] === 'V' && style2.recommendation_style[4] === 'A') {
return -1;
}
if (style2.recommendation_style[4] === 'V' && style1.recommendation_style[4] === 'A') {
return 1;
}
return 0;
}
function main(path) {
2015-09-14 16:35:16 +02:00
var db = JSON.parse(require('fs').readFileSync(path, 'utf8'));
2015-09-14 16:35:16 +02:00
console.log(`There were ${db.users.length} users for ${db.experiments.length} experiments`);
2015-09-28 09:36:49 +02:00
var meanTimeNoReco = 0;
var meanTimeArrow = 0;
var meanTimeViewport = 0;
2015-10-07 14:13:20 +02:00
var groups = makeGroups(db);
2015-10-07 14:13:20 +02:00
groups.forEach(function(elt) {
var ok = true;
2015-09-28 09:36:49 +02:00
2015-10-07 14:13:20 +02:00
// console.log(elt);
elt.sort(compareRecommendationStyle);
// console.log(elt);
2015-09-28 09:36:49 +02:00
2015-10-07 14:13:20 +02:00
elt.forEach(function(subElt) {
if (subElt.coin_combination_id !== elt[0].coin_combination_id) {
ok = false;
}
});
2015-09-28 09:36:49 +02:00
2015-10-07 14:13:20 +02:00
if (!ok) {
process.stderr.write('Error : assertion failed');
process.exit(-1);
2015-09-28 09:36:49 +02:00
}
2015-10-07 14:13:20 +02:00
console.log(elt.length + ' -> ' + elt[0].coin_combination_id);
});
2015-10-07 14:13:20 +02:00
console.log('-----------------');
2015-10-07 14:13:20 +02:00
var value = minDifferences(groups);
console.log(value);
2015-10-07 14:13:20 +02:00
// for (var i = 0; i < db.experiments.length; i++) {
2015-10-07 14:13:20 +02:00
// var exp = db.experiments[i];
// var events = exp.elements.events;
2015-10-07 14:13:20 +02:00
// if (events.length === 0 || exp.user.worker_id === null) {
// continue;
// }
// var coins = [];
// for (var j = 0; j < exp.elements.events.length; j++) {
// if (exp.elements.events[j].type === 'coin') {
// if (coins.find(function(elt) { return elt.id === exp.elements.events[j].id; }) === undefined) {
// coins.push(exp.elements.events[j]);
// }
// }
// }
// console.log(`${exp.id} -> ${coins.length} (on ${exp.coinCombination.scene_id} )`);
// }
// console.log();
// for (var i = 0; i < db.users.length; i++) {
// var user = db.users[i];
// console.log(`${user.worker_id} has done ${user.experiments.length} experiments with rating ${user.rating}`);
// }
}
function minDifference(exps) {
var dict = {};
exps.forEach(function(subElt) {
dict[subElt.recommendation_style[4]] = subElt.duration;
});
2015-10-07 14:13:20 +02:00
if (dict.B !== undefined) {
if (dict.A !== undefined && dict.V !== undefined) {
return (
Math.min(dict.A - dict.B, dict.V - dict.B)
);
}
if (dict.A !== undefined) {
return dict.A - dict.B;
}
if (dict.V !== undefined) {
return dict.V - dict.B;
}
}
2015-10-07 14:13:20 +02:00
}
function minDifferences(groups) {
var differences = [];
groups.forEach(function(elt) {
2015-10-07 14:13:20 +02:00
var timesMean = 0;
2015-10-07 14:13:20 +02:00
elt.forEach(function(elt) {
var duration = experimentDuration(elt);
timesMean += experimentDuration(elt);
});
2015-09-28 09:36:49 +02:00
2015-10-07 14:13:20 +02:00
timesMean /= elt.length;
var normalizedElt = [];
elt.forEach(function(subElt) {
normalizedElt.push({
recommendation_style : subElt.recommendation_style,
duration : experimentDuration(subElt) / timesMean
});
});
var diff = minDifference(normalizedElt);
if (diff !== undefined)
differences.push(diff);
});
var sum = 0;
differences.forEach(function(elt) {
sum += elt;
});
return sum / differences.length;
}
function experimentDuration(exp) {
var lastCoin = null;
for (var i = exp.elements.events.length - 1; i >= 0; i--) {
if (exp.elements.events[i].type === 'coin') {
lastCoin = exp.elements.events[i];
break;
}
2015-09-28 09:36:49 +02:00
}
2015-10-07 14:13:20 +02:00
return timeDifference(exp.elements.events[0].time, lastCoin.time);
2015-09-28 09:36:49 +02:00
}
function timeDifference(time1, time2) {
return new Date(time2).getTime() - new Date(time1).getTime();
}
function timeToString(_time) {
var time = _time / 1000;
return Math.floor(time / 3600) + 'h' + Math.floor((time % 3600) / 60) + 'm' + Math.floor(time % 60);
}
2015-10-07 14:13:20 +02:00
function makeGroups(db) {
var elements = [];
for (var i = 0; i < db.experiments.length; i++) {
if (db.experiments[i].coinCombination.scene_id !== 1 && db.experiments[i].elements.events.length !== 0) {
elements.push(db.experiments[i]);
}
}
return groupBy(elements, function(item) {
return item.coin_combination_id;//, item.user.rating];
});
}
if (process.argv.length !== 3) {
process.stderr.write('Error : please give me a JSON file to work on\n');
process.exit(-1);
}
main(process.argv[2])