Skip to content

Commit 7c8998f

Browse files
committed
Change empty_module to being EmptyModuleError, a real exception.
1 parent 2c2179d commit 7c8998f

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

brainx/modularity.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,15 @@
2929
# Our own modules
3030
import util
3131

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-
4032

4133
#-----------------------------------------------------------------------------
4234
# Class declarations
4335
#-----------------------------------------------------------------------------
4436

37+
class EmptyModuleError(ValueError):
38+
pass
39+
40+
4541
class GraphPartition(object):
4642
"""Represent a graph partition.
4743
@@ -283,9 +279,13 @@ def compute_module_split(self, m, n1, n2):
283279
284280
Returns
285281
-------
282+
286283
The change in modularity resulting from the change
287284
(Q_final-Q_initial)"""
288285

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+
289289
# create a dict that contains the new modules 0 and 1 that have the
290290
# sets n1 and n2 of nodes from module m.
291291
split_modules = {0: n1, 1: n2}
@@ -318,19 +318,25 @@ def apply_module_split(self, m, n1, n2, split_modules, e_new, a_new):
318318
The two sets of nodes in which the nodes originally in module m will
319319
be split. Note: It is the responsibility of the caller to ensure
320320
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.
321327
322328
Returns
323329
-------
324-
The change in modularity resulting from the change
325-
(Q_final-Q_initial)"""
330+
None : the partition is modified in-place.
331+
"""
326332

327333
# To reuse slicing code, use m1/m2 lables like in merge code
328334
m1 = m
329335
m2 = len(self)
330336

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
334340

335341
self.mod_e[m1] = e_new[0]
336342
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):
351357
# If there are empty modules after the operation, remove them from the
352358
# index and rename the partition labels
353359
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')
357361
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')
361363

362364
def node_update(self, n, m1, m2):
363365
"""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):
468470

469471
# This checks whether there is an empty module. If so, renames the keys.
470472
if len(self.index[m1]) < 1:
471-
#empty_module('Empty module after node move')
473+
#EmptyModuleError('Empty module after node move')
472474
self.index.pop(m1)
473475
rename_keys(self.index, m1)
474476
# Once the index structure changes, the labeling of E and A arrays
@@ -973,9 +975,9 @@ def mutual_information(d1, d2):
973975
# the entire partitions, we look for this problem at this stage, and bail
974976
# if there was an empty module.
975977
## if (nsum_row==0).any():
976-
## empty_module("Empty module in second partition.")
978+
## EmptyModuleError("Empty module in second partition.")
977979
## if (nsum_col==0).any():
978-
## empty_module("Empty module in first partition.")
980+
## EmptyModuleError("Empty module in first partition.")
979981

980982
# nn is the total number of nodes
981983
nn = nsum_row.sum()
@@ -1104,7 +1106,7 @@ def simulated_annealing(g, p0=None, temperature = 50, temp_scaling = 0.995, tmin
11041106
graph_partition.modularity(), 11)
11051107
for mod in graph_partition.index:
11061108
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))
11081110

11091111

11101112
#maybe store the best one here too?
@@ -1174,7 +1176,7 @@ def simulated_annealing(g, p0=None, temperature = 50, temp_scaling = 0.995, tmin
11741176

11751177
for mod in graph_partition.index:
11761178
if len(graph_partition.index[mod]) < 1:
1177-
empty_module('Empty module after ndoe move,SA')
1179+
EmptyModuleError('Empty module after node move,SA')
11781180

11791181

11801182
#else:

0 commit comments

Comments
 (0)