@@ -10,15 +10,15 @@ class WeightedPartition(object):
10
10
"""Represent a weighted Graph Partition
11
11
12
12
The main object keeping track of the nodes in each partition is the
13
- communities attribute.
13
+ communities attribute.
14
14
"""
15
15
def __init__ (self , graph , communities = None ):
16
16
""" initialize partition of graph, with optional communities
17
17
18
18
Parameters
19
19
----------
20
20
graph : networkx graph
21
-
21
+
22
22
communities : list of sets, optional
23
23
a list of sets with nodes in each set
24
24
if communities is None, will initialize with
@@ -86,7 +86,7 @@ def _allnodes_in_communities(self, communities):
86
86
if not (isinstance (communities , list ) and \
87
87
util ._contains_only (communities , set )):
88
88
raise TypeError ('communities should be list of sets, not ' \
89
- '{}' .format (communities ))
89
+ '{}' .format (communities ))
90
90
## simple count to check for all nodes
91
91
return len (self .graph .nodes ()) == \
92
92
len ([item for com in communities for item in com ])
@@ -140,7 +140,7 @@ def internal_links(self):
140
140
141
141
142
142
def modularity (self ):
143
- """Calculates the proportion of within community edges compared to
143
+ """Calculates the proportion of within community edges compared to
144
144
between community edges for all nodes in graph with given partition
145
145
146
146
Parameters
@@ -157,12 +157,12 @@ def modularity(self):
157
157
References
158
158
----------
159
159
.. [1] M. Newman, "Fast algorithm for detecting community structure
160
- in networks", Physical Review E vol. 69(6), 2004.
160
+ in networks", Physical Review E vol. 69(6), 2004.
161
161
162
162
"""
163
163
if self .graph .is_directed ():
164
164
raise TypeError ('only valid on non directed graphs' )
165
-
165
+
166
166
m2 = self .total_edge_weight
167
167
internal_connect = np .array (self .internal_links ())
168
168
total = np .array (self .total_links ())
@@ -178,7 +178,7 @@ class LouvainCommunityDetection(object):
178
178
----------
179
179
graph : netwrokx Graph object
180
180
communities : list of sets, optional
181
- initial identified communties
181
+ initial identified communties
182
182
minthr : float, optional
183
183
minimum threshold value for change in modularity
184
184
default(0.0000001)
@@ -192,9 +192,9 @@ class LouvainCommunityDetection(object):
192
192
193
193
References
194
194
----------
195
- .. [1] VD Blondel, JL Guillaume, R Lambiotte, E Lefebvre, "Fast
196
- unfolding of communities in large networks", Journal of Statistical
197
- Mechanics: Theory and Experiment vol.10, P10008 2008.
195
+ .. [1] VD Blondel, JL Guillaume, R Lambiotte, E Lefebvre, "Fast
196
+ unfolding of communities in large networks", Journal of Statistical
197
+ Mechanics: Theory and Experiment vol.10, P10008 2008.
198
198
199
199
"""
200
200
@@ -215,11 +215,11 @@ def gen_dendogram(self):
215
215
if type (self .graph ) != nx .Graph :
216
216
raise TypeError ("Bad graph type, use only non directed graph" )
217
217
218
- #special case, when there is no link
218
+ #special case, when there is no link
219
219
#the best partition is everyone in its communities
220
220
if self .graph .number_of_edges () == 0 :
221
- return [ WeightedPartition ( self . graph )]
222
-
221
+ raise IOError ( ' graph has no edges why do you want a partition?' )
222
+
223
223
current_graph = self .graph .copy ()
224
224
part = WeightedPartition (self .graph , self .initial_communities )
225
225
# first pass
@@ -231,7 +231,7 @@ def gen_dendogram(self):
231
231
dendogram .append (new_part )
232
232
mod = new_mod
233
233
current_graph , _ = meta_graph (new_part )
234
-
234
+
235
235
while True :
236
236
partition = WeightedPartition (current_graph )
237
237
newpart = self ._one_level (partition , self .minthr )
@@ -260,7 +260,7 @@ def _one_level(self, part, min_modularity= .0000001):
260
260
# no increase by moving this node
261
261
continue
262
262
best_comm = delta_mod .argmax ()
263
- if not best_comm == node_comm :
263
+ if not best_comm == node_comm :
264
264
new_part = self ._move_node (part , node , best_comm )
265
265
part = new_part
266
266
modified = True
@@ -307,7 +307,7 @@ def _communities_nodes_alledgesw(self, part, removed_node):
307
307
if len (node_index )< 1 :
308
308
continue
309
309
weights [val ] = np .sum (all_degree_weights [node_index ])
310
- return weights
310
+ return weights
311
311
312
312
@staticmethod
313
313
def _move_node (part , node , new_comm ):
@@ -337,17 +337,17 @@ def partitions_from_dendogram(dendo):
337
337
return all_partitions
338
338
339
339
340
-
340
+
341
341
def meta_graph (partition ):
342
342
""" takes weighted partition communities and creates a new meta graph where
343
- communities are now the nodes, the new edges are created based on the
343
+ communities are now the nodes, the new edges are created based on the
344
344
node to node connections from original graph, and weighted accordingly,
345
345
this includes self-loops"""
346
346
metagraph = nx .Graph ()
347
347
# new nodes are communities
348
348
newnodes = [val for val ,_ in enumerate (partition .communities )]
349
349
mapping = {val :nodes for val ,nodes in enumerate (partition .communities )}
350
- metagraph .add_nodes_from (newnodes , weight = 0.0 )
350
+ metagraph .add_nodes_from (newnodes , weight = 0.0 )
351
351
352
352
for node1 , node2 , data in partition .graph .edges_iter (data = True ):
353
353
node1_community = partition .get_node_community (node1 )
0 commit comments