generated from puzzlef/pagerank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
208 lines (168 loc) · 4.82 KB
/
process.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const fs = require('fs');
const os = require('os');
const path = require('path');
const RGRAPH = /^Loading graph .*\/(.*?)\.mtx \.\.\./m;
const RORDER = /^order: (\d+) size: (\d+) {}$/m;
const RBATCH = /^# Batch size ([\d\.e+-]+)/;
const RFIELD = /^- ([\w\-]+): (.+)/m;
const RRESLT = /^order: (\d+) size: (\d+) \{\} \[(.*?) ms; (\d+) iters\.\] \[(.*?) err\.\] (.*)/m;
// *-ARRAY
// -------
function sumArray(x) {
var a = 0;
for (var i=0, I=x.length; i<I; i++)
a += x[i];
return a;
}
function avgArray(x) {
return x.length? sumArray(x)/x.length : 0;
}
// *-FILE
// ------
function readFile(pth) {
var d = fs.readFileSync(pth, 'utf8');
return d.replace(/\r?\n/g, '\n');
}
function writeFile(pth, d) {
d = d.replace(/\r?\n/g, os.EOL);
fs.writeFileSync(pth, d);
}
// *-CSV
// -----
function writeCsv(pth, rows) {
var cols = Object.keys(rows[0]);
var a = cols.join()+'\n';
for (var r of rows)
a += [...Object.values(r)].map(v => `"${v}"`).join()+'\n';
writeFile(pth, a);
}
// *-LOG
// -----
function readLogLine(ln, data, state) {
if (RGRAPH.test(ln)) {
var [, graph] = RGRAPH.exec(ln);
if (!data.has(graph)) data.set(graph, []);
state = {graph};
}
else if (RORDER.test(ln)) {
var [, order, size] = RORDER.exec(ln);
state.order = parseFloat(order);
state.size = parseFloat(size);
}
else if (RBATCH.test(ln)) {
var [, batch_size] = RBATCH.exec(ln);
state.batch_size = parseFloat(batch_size);
}
else if (RFIELD.test(ln)) {
var [, key, value] = RFIELD.exec(ln);
state[key] = parseFloat(value);
}
else if (RRESLT.test(ln)) {
var [, order, size, time, iters, err, technique] = RRESLT.exec(ln);
data.get(state.graph).push(Object.assign({}, state, {
order: parseFloat(order),
size: parseFloat(size),
time: parseFloat(time),
iterations: parseFloat(iters),
error: parseFloat(err),
technique
}));
}
return state;
}
function readLog(pth) {
var text = readFile(pth);
var lines = text.split('\n');
var data = new Map();
var state = null;
for (var ln of lines)
state = readLogLine(ln, data, state);
return data;
}
// PROCESS-*
// ---------
function processBatchAverage(rows) {
var a = [];
var graph = rows[0].graph;
var order = Math.max(...rows.map(r => r.order));
var size = Math.max(...rows.map(r => r.size));
var batch_size = rows[0].batch_size;
var techniques = new Set(rows.map(r => r.technique));
for (var technique of techniques) {
var rows_filt = rows.filter(r => r.technique===technique);
var time = avgArray(rows_filt.map(r => r.time));
var iterations = avgArray(rows_filt.map(r => r.iterations));
var error = avgArray(rows_filt.map(r => r.error));
a.push({graph, order, size, batch_size, technique, time, iterations, error});
}
return a;
}
function processCsv(data) {
var a = [];
for (var rows of data.values())
a.push(...rows);
return a;
}
function processShortCsv(data) {
var a = [];
for (var rows of data.values()) {
var batch_sizes = new Set(rows.map(r => r.batch_size));
for (var batch_size of batch_sizes) {
var rows_filt = rows.filter(r => r.batch_size===batch_size);
a.push(...processBatchAverage(rows_filt));
}
}
return a;
}
function processShortLog(data) {
var a = '';
for (var rows of data.values()) {
var order = Math.max(...rows.map(r => r.order));
var size = Math.max(...rows.map(r => r.size));
a += `Using graph ${rows[0].graph} ...\n`;
a += `Temporal edges: ${rows[0].temporal_edges}\n`;
a += `order: ${order} size: ${size} {}\n\n`;
var batch_sizes = new Set(rows.map(r => r.batch_size));
for (var batch_size of batch_sizes) {
var rows_filt = rows.filter(r => r.batch_size===batch_size);
a += `# Batch size ${batch_size.toExponential(0)}\n`;
for (var r of processBatchAverage(rows_filt)) {
var time = r.time.toFixed(3).padStart(9, '0');
var iterations = r.iterations.toFixed(0).padStart(3, '0');
var error = r.error.toExponential(4);
a += `[${time} ms; ${iterations} iters.] [${error} err.] ${r.technique}\n`;
}
a += `\n`;
}
a += '\n';
}
return a.trim()+'\n';
}
// MAIN
// ----
function main(cmd, log, out) {
var data = readLog(log);
if (path.extname(out)==='') cmd += '-dir';
switch (cmd) {
case 'csv':
var rows = processCsv(data);
writeCsv(out, rows);
break;
case 'csv-dir':
for (var [graph, rows] of data)
writeCsv(path.join(out, graph+'.csv'), rows);
break;
case 'short-csv':
var rows = processShortCsv(data);
writeCsv(out, rows);
break;
case 'short-log':
var text = processShortLog(data);
writeFile(out, text);
break;
default:
console.error(`error: "${cmd}"?`);
break;
}
}
main(...process.argv.slice(2));