summaryrefslogtreecommitdiff
path: root/preprocess.js
blob: 7de2717ff1735a332210cac86fde73f8b0dd6427 (plain)
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
/* "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, ratelimit_max) {
    // map userID to nodes
    var users = {};
    data.nodes.forEach(function (user, i) {
        users[user.group] = user;
    });

    var ratelimit_count = 0;
    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_max > 0 && 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, 10);
    console.log('Valid edges count:', data.edges.length);

    if (options.minTweetCount > 0) {
        /* filter away users with almost no tweets */
        data.nodes = data.nodes.filter(function (node) {
            return node.tweetCount >= options.minTweetCount;
        });
        console.log('Nodes count (ignoring users with fewer than',
            options.minTweetCount, 'tweets):', data.nodes.length);
        filterEdges(data, 0);
    }

    // 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];
        // for faster lookup, store neighboring nodes per node
        data.nodes[link.source].relatedTo.push(link.target);
        data.nodes[link.target].relatedFrom.push(link.source);
    });
}