1
1
2
2
3
3
import copy
4
- import itertools
5
4
import numpy as np
6
5
import networkx as nx
7
6
from . import util
8
7
9
8
10
- class Partition :
9
+ class Partition ( object ) :
11
10
"""Represent a weighted Graph Partition
12
11
13
12
The main object keeping track of the nodes in each partition is the
@@ -40,6 +39,7 @@ def __init__(self, graph, community=None):
40
39
41
40
@property
42
41
def community (self ):
42
+ """list of sets decribing the communities"""
43
43
return self ._community
44
44
45
45
@community .setter
@@ -86,7 +86,7 @@ def _allnodes_in_community(self, community):
86
86
duplicate nodes"""
87
87
if not (isinstance (community , list ) and \
88
88
util ._contains_only (community , set )):
89
- raise TypeError ('community should be list of sets, not' \
89
+ raise TypeError ('community should be list of sets, not ' \
90
90
'{}' .format (community ))
91
91
## simple count to check for all nodes
92
92
return len (self .graph .nodes ()) == \
@@ -199,9 +199,6 @@ def _community_nodes_alledgesw(part, removed_node):
199
199
return weights
200
200
201
201
202
-
203
-
204
-
205
202
def node_degree (graph , node ):
206
203
""" find the summed weight to node
207
204
Ki in Blondel paper"""
@@ -220,7 +217,9 @@ def dnodecom(node, part):
220
217
221
218
222
219
223
- def gen_dendogram (graph , community = None , MIN = 0.0000001 ):
220
+ def gen_dendogram (graph , community = None , min = 0.0000001 ):
221
+ """generate dendogram based on muti-levels of partitioning"""
222
+
224
223
if type (graph ) != nx .Graph :
225
224
raise TypeError ("Bad graph type, use only non directed graph" )
226
225
@@ -245,7 +244,7 @@ def gen_dendogram(graph, community = None, MIN = 0.0000001):
245
244
partition = Partition (current_graph )
246
245
newpart = _one_level (partition )
247
246
new_mod = modularity (newpart )
248
- if new_mod - mod < MIN :
247
+ if new_mod - mod < min :
249
248
break
250
249
251
250
dendogram .append (newpart )
@@ -267,17 +266,17 @@ def partitions_from_dendogram(dendo):
267
266
268
267
269
268
def _calc_delta_modularity (node , part ):
270
- """calculate the increase in modularity if node is moved to other
269
+ """calculate the increase(s) in modularity if node is moved to other
271
270
communities
272
271
deltamod = inC - totc * ki / total_weight"""
273
272
noded = node_degree (part .graph , node )
274
- dnc = dnodecom (node ,part )
273
+ dnc = dnodecom (node , part )
275
274
totc = _community_nodes_alledgesw (part , node )
276
275
total_weight = part .total_edge_weight
277
276
# cast to arrays to improve calc
278
277
dnc = np .array (dnc )
279
278
totc = np .array (totc )
280
- return dnc - totc * noded / (total_weight * 2 )
279
+ return dnc - totc * noded / (total_weight * 2 )
281
280
282
281
283
282
def _move_node (part , node , new_comm ):
@@ -294,6 +293,7 @@ def _move_node(part, node, new_comm):
294
293
295
294
296
295
def _one_level (part , min_modularity = .0000001 ):
296
+ """run one level of patitioning"""
297
297
curr_mod = modularity (part )
298
298
modified = True
299
299
while modified :
@@ -322,8 +322,9 @@ def _one_level(part, min_modularity= .0000001):
322
322
def _combine (prev , next ):
323
323
"""combines nodes in set based on next level
324
324
community partition
325
+
325
326
Parameters
326
- ====== ===
327
+ ======= ===
327
328
prev : list of sets
328
329
community partition
329
330
next : list of sets
0 commit comments