Skip to content

Commit 845a3e8

Browse files
committed
More graceful handling of empty partitions.
1 parent 4788304 commit 845a3e8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

brainx/modularity.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def __init__(self, graph, index):
7171
self.num_nodes = graph.number_of_nodes()
7272
self.num_edges = graph.number_of_edges()
7373

74+
if self.num_edges == 0:
75+
raise ValueError("TODO: Cannot create a graph partition of only one node.")
76+
7477
# Store the nodes as a set, needed for many operations
7578
self._node_set = set(graph.nodes())
7679

@@ -1339,11 +1342,12 @@ def _divide_partition(p, max_div=np.inf):
13391342
only `pp` is returned.
13401343
13411344
"""
1342-
if max_div <= 0:
1345+
p = np.asarray(p)
1346+
1347+
if max_div <= 0 or p.size == 1:
13431348
return [p]
13441349

13451350
# Construct the subgraph modularity matrix
1346-
p = np.asarray(p)
13471351
A_ = A[p, p[:, None]]
13481352
k_ = np.sum(A_, axis=0)
13491353
M_ = np.sum(k_)

brainx/tests/test_modularity.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,13 @@ def test_adjust_partition():
762762

763763
npt.assert_(p0 > 0.38)
764764
npt.assert_(p1 > 0.42)
765+
766+
767+
def test_empty_graphpartition():
768+
g = nx.Graph()
769+
g.add_node(1)
770+
npt.assert_raises(ValueError, mod.GraphPartition, g, {1: g.nodes()})
771+
772+
773+
if __name__ == "__main__":
774+
npt.run_module_suite()

0 commit comments

Comments
 (0)