forked from Kishanjay/LacunaV2-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todomvc_getavgstatistics.js
92 lines (78 loc) · 2.84 KB
/
todomvc_getavgstatistics.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* @author Kishan Nirghin
*
* @version 2
*
* @description Generates the average statistics of the different analyser
* combinations
*/
require("./prototype_extension");
const startTime = Date.now();
const fs = require("fs");
const path = require("path");
const csv = require("fast-csv");
const STATISTICS_FOLDER = "statistics";
const STATISTICS_OUTPUT = "_statistics.csv";
function parseStatistics() {
fs.readdir(STATISTICS_FOLDER, function (err, files) {
var results = {};
files.forEach((file) => {
if (file == STATISTICS_OUTPUT) { return; }
var csv_file = fs.readFileSync(path.join(STATISTICS_FOLDER, file));
var lines = csv_file.toString().split('\n');
// skip first line
lines.shift();
lines.forEach((line) => {
var lineData = line.split(",");
if (!lineData) { return; }
var key = lineData[0].replace(/ /g, "+");
if (!key) { return; }
var accuracy = lineData[8];
var precision = lineData[9];
var recall = lineData[10];
var fscore = lineData[11];
if (!results.hasOwnProperty(key)) {
results[key] = {
accuracy: [],
precision: [],
recall: [],
fscore: []
}
}
results[key].accuracy.push(accuracy);
results[key].precision.push(precision);
results[key].recall.push(recall);
results[key].fscore.push(fscore);
});
});
generateAverages(results);
});
}
function generateAverages(results) {
var csvData = "Analyzer,Accuracy,Precision,Recall,Fscore\n";
for (var analyzer in results) {
if (!results.hasOwnProperty(analyzer)) { continue; }
var analyzerData = results[analyzer];
var avgAccuracy = calculateAverage(analyzerData.accuracy).toFixed(3);
var avgPrecision = calculateAverage(analyzerData.precision).toFixed(3);
var avgRecall = calculateAverage(analyzerData.recall).toFixed(3);
var avgFscore = calculateAverage(analyzerData.fscore).toFixed(3);
csvData += `${analyzer},${avgAccuracy},${avgPrecision},${avgRecall},${avgFscore}\n`;
}
fs.writeFileSync(path.join(__dirname, STATISTICS_FOLDER, STATISTICS_OUTPUT), csvData, 'utf8');
const endTime = Date.now();
const dateDiff = endTime - startTime;
console.log("Execution time: " + startTime + "-" + endTime + " = " + dateDiff);
}
function calculateAverage(arr) {
if (!arr) { return null; }
return sum(arr) / arr.length;
}
function sum(arr) {
var sum = 0;
arr.forEach((item) => {
sum += parseFloat(item);
});
return sum;
}
parseStatistics();