29
29
# Our own modules
30
30
import util
31
31
32
- #-----------------------------------------------------------------------------
33
- # Functions
34
- #-----------------------------------------------------------------------------
35
- def empty_module (msg = 'Empty module' ):
36
- """Raise an exception if an empty module is found"""
37
-
38
- raise ValueError (msg )
39
-
40
32
41
33
#-----------------------------------------------------------------------------
42
34
# Class declarations
43
35
#-----------------------------------------------------------------------------
44
36
37
+ class EmptyModuleError (ValueError ):
38
+ pass
39
+
40
+
45
41
class GraphPartition (object ):
46
42
"""Represent a graph partition.
47
43
@@ -283,9 +279,13 @@ def compute_module_split(self, m, n1, n2):
283
279
284
280
Returns
285
281
-------
282
+
286
283
The change in modularity resulting from the change
287
284
(Q_final-Q_initial)"""
288
285
286
+ # FIXME : docstring is wrong (the partition is modified in-place), and
287
+ # we shouldn't be returning split_modules at all from this.
288
+
289
289
# create a dict that contains the new modules 0 and 1 that have the
290
290
# sets n1 and n2 of nodes from module m.
291
291
split_modules = {0 : n1 , 1 : n2 }
@@ -318,19 +318,25 @@ def apply_module_split(self, m, n1, n2, split_modules, e_new, a_new):
318
318
The two sets of nodes in which the nodes originally in module m will
319
319
be split. Note: It is the responsibility of the caller to ensure
320
320
that the set n1+n2 is the full set of nodes originally in module m.
321
+ split_modules : dict DEPRECATED - will be removed soon.
322
+ The dict ``{0: n1, 1: n2}``.
323
+ e_new : array
324
+ The e vector for the resulting partition after the split has been applied.
325
+ a_new : array
326
+ The a vector for the resulting partition after the split has been applied.
321
327
322
328
Returns
323
329
-------
324
- The change in modularity resulting from the change
325
- (Q_final-Q_initial) """
330
+ None : the partition is modified in-place.
331
+ """
326
332
327
333
# To reuse slicing code, use m1/m2 lables like in merge code
328
334
m1 = m
329
335
m2 = len (self )
330
336
331
- #Add a new module to the end of the index dictionary
332
- self .index [m1 ] = split_modules [ 0 ] #replace m1 with n1
333
- self .index [m2 ] = split_modules [ 1 ] #add in new module, fill with n2
337
+ # Add a new module to the end of the index dictionary
338
+ self .index [m1 ] = n1
339
+ self .index [m2 ] = n2
334
340
335
341
self .mod_e [m1 ] = e_new [0 ]
336
342
self .mod_a [m1 ] = a_new [0 ]
@@ -351,13 +357,9 @@ def apply_module_split(self, m, n1, n2, split_modules, e_new, a_new):
351
357
# If there are empty modules after the operation, remove them from the
352
358
# index and rename the partition labels
353
359
if len (self .index [m1 ])< 1 :
354
- self .index .pop (m1 )
355
- rename_keys (self .index , m1 )
356
- return
360
+ EmptyModuleError ('Empty module after module split, old mod' )
357
361
if len (self .index [m2 ])< 1 :
358
- self .index .pop (m2 )
359
- rename_keys (self .index , m2 )
360
- return
362
+ EmptyModuleError ('Empty module after module split, old mod' )
361
363
362
364
def node_update (self , n , m1 , m2 ):
363
365
"""Moves a single node within or between modules
@@ -468,7 +470,7 @@ def apply_node_update(self, n, m1, m2, node_moved_mods, e_new, a_new):
468
470
469
471
# This checks whether there is an empty module. If so, renames the keys.
470
472
if len (self .index [m1 ]) < 1 :
471
- #empty_module ('Empty module after node move')
473
+ #EmptyModuleError ('Empty module after node move')
472
474
self .index .pop (m1 )
473
475
rename_keys (self .index , m1 )
474
476
# Once the index structure changes, the labeling of E and A arrays
@@ -973,9 +975,9 @@ def mutual_information(d1, d2):
973
975
# the entire partitions, we look for this problem at this stage, and bail
974
976
# if there was an empty module.
975
977
## if (nsum_row==0).any():
976
- ## empty_module ("Empty module in second partition.")
978
+ ## EmptyModuleError ("Empty module in second partition.")
977
979
## if (nsum_col==0).any():
978
- ## empty_module ("Empty module in first partition.")
980
+ ## EmptyModuleError ("Empty module in first partition.")
979
981
980
982
# nn is the total number of nodes
981
983
nn = nsum_row .sum ()
@@ -1104,7 +1106,7 @@ def simulated_annealing(g, p0=None, temperature = 50, temp_scaling = 0.995, tmin
1104
1106
graph_partition .modularity (), 11 )
1105
1107
for mod in graph_partition .index :
1106
1108
if len (graph_partition .index [mod ]) < 1 :
1107
- empty_module ('Empty module after module %s,SA' % (movetype ))
1109
+ EmptyModuleError ('Empty module after module %s,SA' % (movetype ))
1108
1110
1109
1111
1110
1112
#maybe store the best one here too?
@@ -1174,7 +1176,7 @@ def simulated_annealing(g, p0=None, temperature = 50, temp_scaling = 0.995, tmin
1174
1176
1175
1177
for mod in graph_partition .index :
1176
1178
if len (graph_partition .index [mod ]) < 1 :
1177
- empty_module ('Empty module after ndoe move,SA' )
1179
+ EmptyModuleError ('Empty module after node move,SA' )
1178
1180
1179
1181
1180
1182
#else:
0 commit comments