Skip to content

Commit 3e071d6

Browse files
author
CindeeM
committed
RF: Partition inherits from object to handle community property properly, add doc strings
1 parent 4b35367 commit 3e071d6

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

brainx/weighted_modularity.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11

22

33
import copy
4-
import itertools
54
import numpy as np
65
import networkx as nx
76
from . import util
87

98

10-
class Partition:
9+
class Partition(object):
1110
"""Represent a weighted Graph Partition
1211
1312
The main object keeping track of the nodes in each partition is the
@@ -40,6 +39,7 @@ def __init__(self, graph, community=None):
4039

4140
@property
4241
def community(self):
42+
"""list of sets decribing the communities"""
4343
return self._community
4444

4545
@community.setter
@@ -86,7 +86,7 @@ def _allnodes_in_community(self, community):
8686
duplicate nodes"""
8787
if not (isinstance(community, list) and \
8888
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 '\
9090
'{}'.format(community))
9191
## simple count to check for all nodes
9292
return len(self.graph.nodes()) == \
@@ -199,9 +199,6 @@ def _community_nodes_alledgesw(part, removed_node):
199199
return weights
200200

201201

202-
203-
204-
205202
def node_degree(graph, node):
206203
""" find the summed weight to node
207204
Ki in Blondel paper"""
@@ -220,7 +217,9 @@ def dnodecom(node, part):
220217

221218

222219

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+
224223
if type(graph) != nx.Graph :
225224
raise TypeError("Bad graph type, use only non directed graph")
226225

@@ -245,7 +244,7 @@ def gen_dendogram(graph, community = None, MIN = 0.0000001):
245244
partition = Partition(current_graph)
246245
newpart = _one_level(partition)
247246
new_mod = modularity(newpart)
248-
if new_mod - mod < MIN :
247+
if new_mod - mod < min :
249248
break
250249

251250
dendogram.append(newpart)
@@ -267,17 +266,17 @@ def partitions_from_dendogram(dendo):
267266

268267

269268
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
271270
communities
272271
deltamod = inC - totc * ki / total_weight"""
273272
noded = node_degree(part.graph, node)
274-
dnc = dnodecom(node,part)
273+
dnc = dnodecom(node, part)
275274
totc = _community_nodes_alledgesw(part, node)
276275
total_weight = part.total_edge_weight
277276
# cast to arrays to improve calc
278277
dnc = np.array(dnc)
279278
totc = np.array(totc)
280-
return dnc - totc * noded / (total_weight*2)
279+
return dnc - totc*noded / (total_weight*2)
281280

282281

283282
def _move_node(part, node, new_comm):
@@ -294,6 +293,7 @@ def _move_node(part, node, new_comm):
294293

295294

296295
def _one_level(part, min_modularity= .0000001):
296+
"""run one level of patitioning"""
297297
curr_mod = modularity(part)
298298
modified = True
299299
while modified:
@@ -322,8 +322,9 @@ def _one_level(part, min_modularity= .0000001):
322322
def _combine(prev, next):
323323
"""combines nodes in set based on next level
324324
community partition
325+
325326
Parameters
326-
====== ===
327+
==========
327328
prev : list of sets
328329
community partition
329330
next : list of sets

0 commit comments

Comments
 (0)