@@ -56,20 +56,17 @@ def __init__(self, graph, index):
56
56
Graph to which the partition index refers to.
57
57
58
58
index : dict
59
- A dict that maps module labels to sets of nodes, this describes the
60
- partition in full.
59
+ A dict of sets that maps module/partition labels to sets of
60
+ nodes, this describes the partition in full.
61
61
62
62
Note
63
63
----
64
- The values in the index dict MUST be real sets, not lists. No checks
65
- are made of this fact, but later the code relies on them being sets and
66
- may break in strange manners if the values were stored in non-set
67
- objects.
64
+ The values in the index dict MUST be real sets, not lists.
68
65
"""
69
66
# Store references to the original graph and label dict
70
67
if not type (index ) == type ({}):
71
- raise TypeError ('index should be of type dict(), not %s' % type ( index ))
72
-
68
+ raise TypeError ('index should be of type dict(),' \
69
+ 'not %s' % type ( index ))
73
70
self .index = copy .deepcopy (index )
74
71
75
72
## add quick check to make sure the passed index is
@@ -78,6 +75,11 @@ def __init__(self, graph, index):
78
75
79
76
# We'll need the graph's adjacency matrix often, so store it once
80
77
self .graph_adj_matrix = nx .adj_matrix (graph )
78
+ #make sure adj_matrix is binary otherwise raise exception
79
+ if not self .graph_adj_matrix .sum () == \
80
+ self .graph_adj_matrix .astype (bool ).sum ():
81
+ raise ValueError ('Adjacency matrix is weighted, need binary matrix' )
82
+
81
83
82
84
# Just to be sure, we don't want to count self-links, so we zero out the
83
85
# diagonal.
@@ -175,6 +177,12 @@ def modularity_newman(self):
175
177
modularity = modularity_newman
176
178
177
179
180
+ def find_unconnected_nodes (self ):
181
+ """ checks for nodes in graph with no edges """
182
+ graph = nx .from_numpy_matrix (self .graph_adj_matrix )
183
+ unconnected = [ n for n ,d in graph .degree_iter () if d == 0 ]
184
+ return unconnected
185
+
178
186
def compute_module_merge (self , m1 , m2 ):
179
187
"""Merges two modules in a given partition.
180
188
@@ -1368,7 +1376,14 @@ def _divide_partition(p, max_div=np.inf):
1368
1376
1369
1377
# Compute the increase in modularity due to this partitioning.
1370
1378
# If it is less than zero, we should rather not have partitioned.
1371
- q = s [None , :].dot (B_ ).dot (s )
1379
+ Bc_mask = np .ones_like (B_ )
1380
+ Bc_mask [s == 1 , :] = 0
1381
+ Bc_mask [:, s == 1 ] = 0
1382
+ Bc = (B_ * Bc_mask ).sum (axis = 0 )
1383
+ Bc = B_ - Bc
1384
+ q = s [None , :].dot (Bc ).dot (s ) / (4.0 * graph_A_ .number_of_edges ())
1385
+ q2 = s [None , :].dot (B_ ).dot (s ) / (4.0 * graph_A_ .number_of_edges ())
1386
+ print 'orig delta q' , q2 , 'new delta q' , q
1372
1387
if q <= 0 :
1373
1388
return [p ]
1374
1389
@@ -1393,6 +1408,8 @@ def _divide_partition(p, max_div=np.inf):
1393
1408
def adjust_partition (g , partition , max_iter = None ):
1394
1409
"""Adjust partition, using the heuristic method described in Newman (2006),
1395
1410
to have higher modularity.
1411
+ ## TODO BROKEN FIX ME
1412
+
1396
1413
1397
1414
Parameters
1398
1415
----------
0 commit comments