From 3f0c4d65855f7932ed3dbcddb25b4b111fa63b92 Mon Sep 17 00:00:00 2001 From: Katelyn Begany Date: Thu, 20 Jun 2013 17:21:46 -0700 Subject: [PATCH 1/4] BF: add test of and fix bug for merges in modularity adjust_partition function --- brainx/modularity.py | 28 +++++++++++++++++++-------- brainx/tests/test_modularity.py | 34 +++++++++++++++++++++------------ 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/brainx/modularity.py b/brainx/modularity.py index 1f98c10..38b3bb7 100644 --- a/brainx/modularity.py +++ b/brainx/modularity.py @@ -388,12 +388,12 @@ def node_update(self, n, m1, m2): #Compute the change in modularity return (e1[m1]-a1[m1]**2) + (e1[m2]-a1[m2]**2) - mod_old - def compute_node_update(self, n, m1, m2): + def compute_node_update(self, node, m1, m2): """Moves a single node within or between modules Parameters ---------- - n : node identifier + node : node identifier The node that will be moved from module m1 to module m2 m1 : module identifier The module that n used to belong to. @@ -408,14 +408,14 @@ def compute_node_update(self, n, m1, m2): n1 = self.index[m1] n2 = self.index[m2] - node_moved_mods = {0: n1 - set([n]),1: n2 | set([n])} - + node_moved_mods = {0 : n1 - set([node]), 1 : n2 | set([node])} + # Before we overwrite the mod vectors, compute the contribution to # modularity from before the change e1 = [0,0] a1 = [0,0] e0, a0 = self.mod_e, self.mod_a - + # The values that change: _edge_info with arguments will update the e, # a vectors only for the modules in index e1, a1 = self._edge_info(e1, a1, node_moved_mods) @@ -424,8 +424,9 @@ def compute_node_update(self, n, m1, m2): delta_q = ( (e1[0]-a1[0]**2) + (e1[1]-a1[1]**2)) - \ ( (e0[m1]-a0[m1]**2) + (e0[m2]-a0[m2]**2) ) + #print n,m1,m2,node_moved_mods,n1,n2 - return node_moved_mods, e1, a1, -delta_q, n, m1, m2 + return node_moved_mods, e1, a1, -delta_q, node, m1, m2 def apply_node_update(self, n, m1, m2, node_moved_mods, e_new, a_new): """Moves a single node within or between modules @@ -1393,7 +1394,7 @@ def adjust_partition(g, partition, max_iter=None): iterations = 0 max_iter = max_iter or np.inf best_modularity = partition.modularity() - + while nodes and no_improvement < 10 and iterations <= max_iter: moves = [] move_modularity = [] @@ -1404,13 +1405,24 @@ def adjust_partition(g, partition, max_iter=None): moves.append((n, node_map[n], p)) M = -partition.compute_node_update(n, node_map[n], p)[3] move_modularity.append(M) - + (n, p0, p1) = moves[np.argmax(move_modularity)] + split_modules, e_new, a_new = partition.compute_node_update(n, p0, p1)[:3] partition.apply_node_update(n, p0, p1, split_modules, e_new, a_new) node_map[n] = p1 nodes.remove(n) + # Perform adjustments after merge instances + if not P == set(range(len(partition))): + # Reset the module labels in the initial partition + P = set(range(len(partition))) + # Recreate the dict that maps nodes to their partition labels + node_map = {} + for p in P: + for node in partition.index[p]: + node_map[node] = p + print '[%d/%d] -> %.4f' % (len(nodes), L, partition.modularity()) M = partition.modularity() diff --git a/brainx/tests/test_modularity.py b/brainx/tests/test_modularity.py index fb93d43..ad5d5d7 100644 --- a/brainx/tests/test_modularity.py +++ b/brainx/tests/test_modularity.py @@ -738,17 +738,28 @@ def test_apply_node_move(): def test_adjust_partition(): - e = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'), + # Check that adjust_partition returns best output + e1 = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'), skiprows=3, dtype=int)[:, :2] - 1 - g = nx.Graph() - g.add_edges_from(e) - - p0 = mod.newman_partition(g) - p1 = mod.adjust_partition(g, p0, max_iter=6) - - npt.assert_(p0 > 0.38) - npt.assert_(p1 > 0.42) - + g1 = nx.Graph() + g1.add_edges_from(e1) + + g1_p0 = mod.newman_partition(g1) + g1_p1 = mod.adjust_partition(g1, g1_p0, max_iter=6) + + npt.assert_(g1_p0 > 0.38) + npt.assert_(g1_p1 > 0.42) + + # Check that adjust_partition handles merges properly + e2 = np.loadtxt(os.path.join(os.path.dirname(__file__), 'simple_net.txt'), + dtype=int) + g2 = nx.Graph() + g2.add_edges_from(e2) + + g2_p0 = {0: {0,1,2}, 1: {3,4,5,6}, 2: {7,8,9}} + g2_p1 = mod.adjust_partition(g2, g2_p0) + + npt.assert_(g2_p0 > 0.39) def test_empty_graphpartition(): g = nx.Graph() @@ -769,7 +780,6 @@ def test_badindex_graphpartition(): npt.assert_raises(TypeError, mod.GraphPartition, g, {0: g.nodes()}) npt.assert_raises(ValueError, mod.GraphPartition, g, {0:set(g.nodes()[:-1])}) npt.assert_raises(TypeError, mod.GraphPartition, g, g.nodes()) - - + if __name__ == "__main__": npt.run_module_suite() From 406dfdb658f34faf879f91b1444ae9929e066c25 Mon Sep 17 00:00:00 2001 From: Katelyn Begany Date: Sun, 23 Jun 2013 15:14:54 -0700 Subject: [PATCH 2/4] RF: Added additional test to test_adjust_modularity --- brainx/tests/simple_net.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 brainx/tests/simple_net.txt diff --git a/brainx/tests/simple_net.txt b/brainx/tests/simple_net.txt new file mode 100644 index 0000000..744d3de --- /dev/null +++ b/brainx/tests/simple_net.txt @@ -0,0 +1,15 @@ +0 1 +0 2 +0 5 +1 2 +2 3 +2 5 +3 4 +3 5 +4 5 +5 6 +6 7 +6 8 +6 9 +7 8 +8 9 From c4e7bf6a832e49e26c733f92dfd2bfbd225d18da Mon Sep 17 00:00:00 2001 From: Katelyn Begany Date: Sun, 23 Jun 2013 15:40:35 -0700 Subject: [PATCH 3/4] RF: Adjustments to simple_net to ensure a merge is always optimal solution in test_adjust_partition --- brainx/tests/simple_net.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/brainx/tests/simple_net.txt b/brainx/tests/simple_net.txt index 744d3de..8fef618 100644 --- a/brainx/tests/simple_net.txt +++ b/brainx/tests/simple_net.txt @@ -1,15 +1,15 @@ 0 1 -0 2 +0 4 0 5 1 2 +1 3 2 3 -2 5 3 4 -3 5 4 5 5 6 6 7 6 8 6 9 7 8 +7 9 8 9 From 286d97230a4cfee07a903ff8a7b40f43bb64113e Mon Sep 17 00:00:00 2001 From: Katelyn Begany Date: Mon, 24 Jun 2013 15:22:35 -0700 Subject: [PATCH 4/4] BF: Fix test in test_adjust_partition --- brainx/tests/test_modularity.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/brainx/tests/test_modularity.py b/brainx/tests/test_modularity.py index ad5d5d7..2b87241 100644 --- a/brainx/tests/test_modularity.py +++ b/brainx/tests/test_modularity.py @@ -756,7 +756,7 @@ def test_adjust_partition(): g2 = nx.Graph() g2.add_edges_from(e2) - g2_p0 = {0: {0,1,2}, 1: {3,4,5,6}, 2: {7,8,9}} + g2_p0 = mod.GraphPartition(g2,{0: {0,1,2}, 1: {3,4,5,6}, 2: {7,8,9}}) g2_p1 = mod.adjust_partition(g2, g2_p0) npt.assert_(g2_p0 > 0.39)