-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommunity_test.html
More file actions
executable file
·200 lines (147 loc) · 5.28 KB
/
community_test.html
File metadata and controls
executable file
·200 lines (147 loc) · 5.28 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jLouvain Example</title>
<script src="lib/d3.min.js" charset="utf-8"></script>
<script src="js/jquery-2.1.3.js"></script>
<script type="text/javascript" src="lib/jLouvain.js"></script>
<script src="js/xsltTransform.js"></script>
<script src="js/importFriends2.js"></script>
<style>
.node {
stroke: #fff;
stroke-width: 1.5px;
}
.link {
stroke: #999;
stroke-opacity: .6;
}
</style>
</head>
<body>
<div id="content_wrapper">
<input type="button" value="Reset" id='reset_btn' />
<input type="button" value="Run Community Detection" id='comm_detect' />
</div>
</body>
<script>
var initFriends = importFriends2();
//console.log(initFriends)
var node_data = initFriends.map(function(d,i,a){
console.log(+d.id)
return i //+d.id//i * 2
})
//Original node and edge data
//var node_data = [0, 1, 2, 3, 4, 5,6,7,8,9, 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
console.log('Input Node Data', node_data);
/* var edge_data = [{"source":0,"target":1,"weight":50},
{"source":1,"target":2,"weight":4},
{"source":3,"target":4,"weight":50},
{"source":0,"target":4,"weight":2},
{"source":0,"target":5,"weight":2}];*/
//var edge_data = [{"source":0,"target":1,"weight":50}]
console.log(initFriends[0].books)
console.log(initFriends)
var edge_data = [];
initFriends.forEach(function(d,i,a){
for(var j = i + 1; j < a.length; j ++){
e = a[j]
var inter = 0
e.books.forEach(function(b1){
d.books.forEach(function(b2){
if(b1.id == b2.id){
inter ++
}
})
})
if(inter != 0 ){
//inter = Math.pow(inter,2)/1000;
console.log("comparing " +d.name + " to "+ e.name + " inter " + inter)
//edge_data.push({"source":i,"target":j,"weight":Math.random()*100})
//edge_data.push({"source":i,"target":j,"weight":inter})
}
edge_data.push({"source":i,"target":j,"weight":inter+1})
}
})
console.log('Input Edge Data', edge_data);
var community = jLouvain().nodes(node_data).edges(edge_data);
console.log(community)
//Drawing code
var width = 600,
height = 600;
var original_node_data = d3.entries(node_data);
var max_weight = d3.max(edge_data, function(d){ return d.weight});
var weight_scale = d3.scale.linear().domain([0, max_weight]).range([1,5]);
var force = d3.layout.force()
.charge(-50)
.linkDistance(250)
.size([width, height]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
force.nodes(original_node_data)
.links(edge_data)
.start();
var link = svg.selectAll(".link")
.data(edge_data)
.enter().append("line")
.attr("class", "link")
.style("stroke-width", function(d) { return weight_scale(d.weight); });
var node = svg.selectAll(".node")
.data(force.nodes())
.enter().append("circle")
.attr("class", "node")
.attr("r", 5)
.style("fill", '#a30500')
.call(force.drag);
force.on("tick", function() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
d3.select('#comm_detect').on('click', function(){
//Communnity detection on click event
var community_assignment_result = community();
var node_ids = Object.keys(community_assignment_result);
node_ids.forEach(function(d,i,a){
console.log("d "+d + " " +community_assignment_result[d]);
initFriends[d]["community"] = community_assignment_result[d];
});
console.log('Resulting Community Data', community_assignment_result);
var max_community_number = 0;
node_ids.forEach(function(d){
original_node_data[d].community = community_assignment_result[d];
max_community_number = max_community_number < community_assignment_result[d] ? community_assignment_result[d]: max_community_number;
});
console.log(original_node_data);
console.log("max community number "+ max_community_number)
console.log(initFriends) // This initfriends has the list of all the friends with the community to which they belong to.
initFriends.forEach(function(d,i,a){
var filteredFriends = initFriends.filter(function( object ) {
return object.community == d.community
})
if(filteredFriends.length == 1){
d.community = -1;
}
})
var nested_friends = d3.nest()
.key(function(d) { return d.community; })
.entries(initFriends);
console.log(nested_friends);
var color = d3.scale.category20().domain(d3.range([0, max_community_number]));
d3.selectAll('.node')
.data(original_node_data)
.style('fill', function(d){ return color(d.community);})
});
d3.select('#reset_btn').on('click', function(){
d3.selectAll('.node')
.data(original_node_data)
.style('fill', '#a30500');
});
</script>
</html>
<!-- trying shizzle -->