8
8
9
9
10
10
class Partition :
11
+ """Represent a weighted Graph Partition
12
+
13
+ The main object keeping track of the nodes in each partition is the
14
+ community attribute.
15
+ """
11
16
def __init__ (self , graph , community = None ):
12
17
""" initialize partition of graph, with optional community
13
- defined
14
18
15
19
Parameters
16
20
==========
17
21
graph : networkx graph
22
+
18
23
community : list of sets
19
24
a list of sets with nodes in each set
20
25
if community is None, will initialize with
21
26
one community per node
22
27
"""
23
- # make sure graph has edge weights, even if binary, and no neg weights
28
+ # assert graph has edge weights, and no negative weights
24
29
mat = nx .adjacency_matrix (graph )
25
30
if mat .min () < 0 :
26
- raise ValueError ('Graph has invalid neg weights' )
31
+ raise ValueError ('Graph has invalid negative weights' )
27
32
28
33
self .graph = nx .from_numpy_matrix (mat )
29
34
if community is None :
30
35
self ._community = self ._init_communities_from_nodes ()
31
36
else :
32
37
self .set_community (community )
33
38
self .total_edge_weight = graph .size (weight = 'weight' )
34
- self .degrees = graph .degree (weight = 'weight' )
39
+ self .degrees = graph .degree (weight = 'weight' )
35
40
36
41
@property
37
42
def community (self ):
@@ -157,7 +162,7 @@ def meta_graph(partition):
157
162
node2_community = partition .get_node_community (node2 )
158
163
try :
159
164
tmpw = metagraph [node1_community ][node2_community ]['weight' ]
160
- except :
165
+ except KeyError :
161
166
tmpw = 0
162
167
metagraph .add_edge (
163
168
node1_community ,
@@ -174,7 +179,6 @@ def _communities_without_node(part, node):
174
179
newpart = copy .deepcopy (part .community )
175
180
newpart [node_comm ].remove (node )
176
181
return newpart
177
- mmunity_nodes_self
178
182
179
183
180
184
def _community_nodes_alledgesw (part , removed_node ):
0 commit comments