generated from puzzlef/louvain-communities-dynamic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.js
145 lines (117 loc) · 3.83 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
const fs = require('fs');
const os = require('os');
const path = require('path');
const ROMPTH = /^OMP_NUM_THREADS=(\d+)/;
const RGRAPH = /^Loading graph .*\/(.*?)\.txt \.\.\./m;
const RORDER = /^order: (\d+) size: (\d+) (?:\[\w+\] )?\{\}/m;
const RBATCH = /^order: (\d+) size: (\d+) \[directed\] \{\} \(insertions=(\d+)\)/m;
const RRESLT = /^\{-(.+?)\/\+(.+?) batchf, (.+?) threads\} -> \{(.+?)ms, (.+?)ms mark, (.+?)ms init, (.+?)ms firstpass, (.+?)ms locmove, (.+?)ms aggr, (.+?) aff, (.+?) iters, (.+?) passes, (.+?) modularity\} (.+)/m;
// *-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) {
ln = ln.replace(/^\d+-\d+-\d+ \d+:\d+:\d+ /, '');
if (ROMPTH.test(ln)) {
var [, omp_num_threads] = ROMPTH.exec(ln);
state.omp_num_threads = parseFloat(omp_num_threads);
}
else if (RGRAPH.test(ln)) {
var [, graph] = RGRAPH.exec(ln);
if (!data.has(graph)) data.set(graph, []);
state.graph = graph;
}
else if (RBATCH.test(ln)) {
var [, order, size] = RBATCH.exec(ln);
state.order = parseFloat(order);
state.size = parseFloat(size);
state.batch_deletions_fraction = 0;
state.batch_insertions_fraction = 0;
state.batch_index = state.batch_index || 0;
state.batch_index = ++state.batch_index;
}
else if (RORDER.test(ln)) {
var [, order, size] = RORDER.exec(ln);
state.order = parseFloat(order);
state.size = parseFloat(size);
state.batch_deletions_fraction = 0;
state.batch_insertions_fraction = 0;
state.batch_index = -1;
}
else if (RRESLT.test(ln)) {
var [, batch_deletions_fraction, batch_insertions_fraction, num_threads, time, marking_time, initialization_time, first_pass_time, local_moving_phase_time, aggregation_phase_time, affected_vertices, iterations, passes, modularity, technique] = RRESLT.exec(ln);
data.get(state.graph).push(Object.assign({}, state, {
batch_deletions_fraction: parseFloat(batch_deletions_fraction),
batch_insertions_fraction: parseFloat(batch_insertions_fraction),
num_threads: parseFloat(num_threads),
time: parseFloat(time),
marking_time: parseFloat(marking_time),
initialization_time: parseFloat(initialization_time),
first_pass_time: parseFloat(first_pass_time),
local_moving_phase_time: parseFloat(local_moving_phase_time),
aggregation_phase_time: parseFloat(aggregation_phase_time),
affected_vertices: parseFloat(affected_vertices),
iterations: parseFloat(iterations),
passes: parseFloat(passes),
modularity: parseFloat(modularity),
technique,
}));
}
return state;
}
function readLog(pth) {
var text = readFile(pth);
var lines = text.split('\n');
var data = new Map();
var state = {};
for (var ln of lines)
state = readLogLine(ln, data, state);
return data;
}
// PROCESS-*
// ---------
function processCsv(data) {
var a = [];
for (var rows of data.values()) {
for (var row of rows)
a.push(row);
}
return a;
}
// 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;
default:
console.error(`error: "${cmd}"?`);
break;
}
}
main(...process.argv.slice(2));