-
Notifications
You must be signed in to change notification settings - Fork 0
/
Modularity.js
316 lines (248 loc) · 9.61 KB
/
Modularity.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright 2008-2011 Gephi
Authors : Patick J. McSweeney <[email protected]>, Sebastien Heymann <[email protected]>
Website : http://www.gephi.org
This file is part of Gephi.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2011 Gephi Consortium. All rights reserved.
The contents of this file are subject to the terms of either the GNU
General Public License Version 3 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with the
License. You can obtain a copy of the License at
http://gephi.org/about/legal/license-notice/
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
specific language governing permissions and limitations under the
License. When distributing the software, include this License Header
Notice in each file and include the License files at
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
License Header, with the fields enclosed by brackets [] replaced by
your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"
If you wish your version of this file to be governed by only the CDDL
or only the GPL Version 3, indicate your decision by adding
"[Contributor] elects to include this software in this distribution
under the [CDDL or GPL Version 3] license." If you do not indicate a
single choice of license, a recipient has the option to distribute
your version of this file under either the CDDL, the GPL Version 3 or
to extend the choice of license to its licensees as provided above.
However, if you add GPL Version 3 code and therefore, elected the GPL
Version 3 license, then the option applies only if the new code is
made subject to such option by the copyright holder.
Contributor(s): Thomas Aynaud <[email protected]>
Portions Copyrighted 2011 Gephi Consortium.
*/
var CommunityStructure = require('./CommunityStructure')
, centrality = require('ngraph.centrality')
;
/**
* @constructor
*/
function Modularity (resolution, useWeight) {
this.isRandomized = false;
this.useWeight = useWeight;
this.resolution = resolution || 1.;
/**
* @type {CommunityStructure}
*/
this.structure = null;
}
/**
* @param {IGraph} graph
*/
Modularity.prototype.execute = function (graph/*, AttributeModel attributeModel*/) {
this.structure = new CommunityStructure(graph, this.useWeight);
var comStructure = new Array(graph.getNodesCount());
var computedModularityMetrics = this.computeModularity(
graph
, this.structure
, comStructure
, this.resolution
, this.isRandomized
, this.useWeight
);
var result = {};
this.structure.map.forEach(function (i, node) {
result[node] = comStructure[i];
});
return result;
};
/**
*
* @param {IGraph} graph
* @param {CommunityStructure} theStructure
* @param {Array.<Number>} comStructure
* @param {Number} currentResolution
* @param {Boolean} randomized
* @param {Boolean} weighted
* @returns {Object.<String, Number>}
*/
Modularity.prototype.computeModularity = function(graph, theStructure, comStructure, currentResolution, randomized, weighted) {
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
var totalWeight = theStructure.graphWeightSum;
var nodeDegrees = theStructure.weights.slice();
var /** @type {Object.<String, Number>} */ results = Object.create(null);
var someChange = true;
while (someChange) {
someChange = false;
var localChange = true;
while (localChange) {
localChange = false;
var start = 0;
if (randomized) {
//start = Math.abs(rand.nextInt()) % theStructure.N;
start = getRandomInt(0,theStructure.N);
}
var step = 0;
for (var i = start; step < theStructure.N; i = (i + 1) % theStructure.N) {
step++;
var bestCommunity = this.updateBestCommunity(theStructure, i, currentResolution);
if ((theStructure.nodeCommunities[i] != bestCommunity) && (bestCommunity != null)) {
theStructure.moveNodeTo(i, bestCommunity);
localChange = true;
}
}
someChange = localChange || someChange;
}
if (someChange) {
theStructure.zoomOut();
}
}
this.fillComStructure(graph, theStructure, comStructure);
/*
//TODO: uncomment when finalQ will be implemented
var degreeCount = this.fillDegreeCount(graph, theStructure, comStructure, nodeDegrees, weighted);
var computedModularity = this._finalQ(comStructure, degreeCount, graph, theStructure, totalWeight, 1., weighted);
var computedModularityResolution = this._finalQ(comStructure, degreeCount, graph, theStructure, totalWeight, currentResolution, weighted);
results["modularity"] = computedModularity;
results["modularityResolution"] = computedModularityResolution;
*/
return results;
};
/**
* @param {CommunityStructure} theStructure
* @param {Number} i
* @param {Number} currentResolution
* @returns {Community}
*/
Modularity.prototype.updateBestCommunity = function(theStructure, i, currentResolution) {
var best = this.q(i, theStructure.nodeCommunities[i], theStructure, currentResolution);
var bestCommunity = theStructure.nodeCommunities[i];
//var /*Set<Community>*/ iter = theStructure.nodeConnectionsWeight[i].keySet();
theStructure.nodeConnectionsWeight[i].forEach(function (_$$val, com) {
var qValue = this.q(i, com, theStructure, currentResolution);
if (qValue > best) {
best = qValue;
bestCommunity = com;
}
}, this);
return bestCommunity;
};
/**
*
* @param {IGraph} graph
* @param {CommunityStructure} theStructure
* @param {Array.<Number>} comStructure
* @returns {Array.<Number>}
*/
Modularity.prototype.fillComStructure = function(graph, theStructure, comStructure) {
var count = 0;
theStructure.communities.forEach(function (com) {
com.nodes.forEach(function (node) {
var hidden = theStructure.invMap.get(node);
hidden.nodes.forEach( function (nodeInt){
comStructure[nodeInt] = count;
});
});
count++;
});
return comStructure;
};
/**
* @param {IGraph} graph
* @param {CommunityStructure} theStructure
* @param {Array.<Number>} comStructure
* @param {Array.<Number>} nodeDegrees
* @param {Boolean} weighted
* @returns {Array.<Number>}
*/
Modularity.prototype.fillDegreeCount = function(graph, theStructure, comStructure, nodeDegrees, weighted) {
var degreeCount = new Array(theStructure.communities.length);
var degreeCentrality = centrality.degree(graph);
graph.forEachNode(function(node){
var index = theStructure.map.get(node);
if (weighted) {
degreeCount[comStructure[index]] += nodeDegrees[index];
} else {
degreeCount[comStructure[index]] += degreeCentrality[node.id];
}
});
return degreeCount;
};
/**
*
* @param {Array.<Number>} struct
* @param {Array.<Number>} degrees
* @param {IGraph} graph
* @param {CommunityStructure} theStructure
* @param {Number} totalWeight
* @param {Number} usedResolution
* @param {Boolean} weighted
* @returns {Number}
*/
Modularity.prototype._finalQ = function(struct, degrees, graph, theStructure, totalWeight, usedResolution, weighted) {
//TODO: rewrite for wighted version of algorithm
throw new Error("not implemented properly");
var res = 0;
var internal = new Array(degrees.length);
graph.forEachNode(function(n){
var n_index = theStructure.map.get(n);
graph.forEachLinkedNode(n.id, function(neighbor){
if (n == neighbor) {
return;
}
var neigh_index = theStructure.map.get(neighbor);
if (struct[neigh_index] == struct[n_index]) {
if (weighted) {
//throw new Error("weighted aren't implemented");
//internal[struct[neigh_index]] += graph.getEdge(n, neighbor).getWeight();
} else {
internal[struct[neigh_index]]++;
}
}
}.bind(this), false);
}.bind(this));
for (var i = 0; i < degrees.length; i++) {
internal[i] /= 2.0;
res += usedResolution * (internal[i] / totalWeight) - Math.pow(degrees[i] / (2 * totalWeight), 2);//HERE
}
return res;
};
/**
*
* @param {Number} nodeId
* @param {Community} community
* @param {CommunityStructure} theStructure
* @param {Number} currentResolution
* @returns {Number}
*/
Modularity.prototype.q = function(nodeId, community, theStructure, currentResolution) {
var edgesToFloat = theStructure.nodeConnectionsWeight[nodeId].get(community);
var edgesTo = 0;
if (edgesToFloat != null) {
edgesTo = edgesToFloat;
}
var weightSum = community.weightSum;
var nodeWeight = theStructure.weights[nodeId];
var qValue = currentResolution * edgesTo - (nodeWeight * weightSum) / (2.0 * theStructure.graphWeightSum);
if ((theStructure.nodeCommunities[nodeId] == community) && (theStructure.nodeCommunities[nodeId].size() > 1)) {
qValue = currentResolution * edgesTo - (nodeWeight * (weightSum - nodeWeight)) / (2.0 * theStructure.graphWeightSum);
}
if ((theStructure.nodeCommunities[nodeId] == community) && (theStructure.nodeCommunities[nodeId].size() == 1)) {
qValue = 0.;
}
return qValue;
};
module.exports = Modularity;