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
|
/* "Optimizes" nodes and edges by dropping uninteresting ones. (for example,
* nodes with no edges).
*/
/* jshint devel:true */
'use strict';
/* find user nodes and remove invalid edges */
function filterEdges(data) {
// map userID to nodes
var users = {};
data.nodes.forEach(function (user, i) {
users[user.group] = user;
});
var ratelimit_count = 0, ratelimit_max = 10;
function ratelimit() {
return ratelimit_count <= ratelimit_max;
}
// filter away invalid edges
data.edges = data.edges.filter(function (link, i) {
var invalid = false;
if (!(link.source in users)) {
if (ratelimit()) console.warn('Dropping invalid source user',
link.source, 'at line', (i + 1), link);
invalid = true;
}
if (!(link.target in users)) {
if (ratelimit()) console.warn('Dropping invalid target user',
link.target, 'at line', (i + 1), link);
invalid = true;
}
if (link.source === link.target) {
if (ratelimit()) console.warn('Dropping self-referencing user',
link.target, 'at line', (i + 1), link);
invalid = true;
}
return !invalid;
});
if (ratelimit_count > ratelimit_max) {
console.log('Supressed', ratelimit_count, 'messages');
}
}
function preprocess(data, options) {
console.log('Initial nodes count:', data.nodes.length);
console.log('Initial edges count:', data.edges.length);
filterEdges(data);
console.log('Valid edges count:', data.edges.length);
// find all related users by userID
var hasRelations = {};
data.edges.forEach(function (link) {
hasRelations[link.target] = 1;
hasRelations[link.source] = 1;
});
if (options.kill_loners) {
var hasRelated = {};
data.nodes = data.nodes.filter(function (d) {
return d.group in hasRelations;
});
console.log('Nodes count (after dropping loners):', data.nodes.length);
}
// prepare data for force layout: map user IDs to indices
var userIds_indices = {};
data.nodes.forEach(function (user, i) {
userIds_indices[user.group] = i;
});
console.log('UserID to index map:', userIds_indices);
// change userID of relation edges to indices
data.edges.map(function (link) {
link.source = userIds_indices[link.source];
link.target = userIds_indices[link.target];
});
}
|