Skip to content

Commit 7a881b6

Browse files
author
CindeeM
committed
TEST: finish refactoring tests for new LouvainCommunityPartition class
1 parent c362106 commit 7a881b6

File tree

2 files changed

+38
-53
lines changed

2 files changed

+38
-53
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -162,56 +162,46 @@ def test_communities_without_node(self):
162162
## make sure we dont break communities from original partition
163163
self.assertEqual(part.communities, self.communities)
164164
self.assertEqual(0 not in updated_comm[0], True)
165-
"""
166-
def test_communities_nodes_alledgesw():
167-
graph, communities = get_test_data()
168-
part = wm.WeightedPartition(graph, communities)
169-
node = 0
170-
weights = wm._communities_nodes_alledgesw(part, node)
171-
npt.assert_almost_equal(weights[0], 1424.0220362)
172-
## test with possible empty node set
173-
part = wm.WeightedPartition(graph)
174-
weights = wm._communities_nodes_alledgesw(part, node)
175-
npt.assert_equal(weights[0], 0)
176-
# other communities are made up of just one node
177-
npt.assert_equal(weights[1], graph.degree(weight='weight')[1])
178-
179-
180-
181165

166+
def test_communities_nodes_alledgesw(self):
167+
part = wm.WeightedPartition(self.graph, self.communities)
168+
node = 0
169+
weights = self.louvain_comm._communities_nodes_alledgesw(part, node)
170+
npt.assert_almost_equal(weights[0], 1424.0220362)
171+
## test with possible empty node set
172+
part = wm.WeightedPartition(self.graph)
173+
weights = self.louvain._communities_nodes_alledgesw(part, node)
174+
self.assertEqual(weights[0], 0)
175+
# other communities are made up of just one node
176+
self.assertEqual(weights[1], self.graph.degree(weight='weight')[1])
182177

183178

179+
def test_calc_delta_modularity(self):
180+
part = wm.WeightedPartition(self.graph) # one comm per node
181+
node = 0
182+
change = self.louvain._calc_delta_modularity(node, part)
183+
self.assertEqual(len(change), len(part.communities))
184+
# change is an array
185+
self.assertEqual(change.shape[0], len(part.communities))
186+
self.assertEqual(change[0] < change[1], True)
187+
# this is one comm per node, so once removed from own
188+
# comm, this delta_weight will be zero
189+
self.assertEqual(change[node] , 0)
190+
191+
def test_move_node(self):
192+
part = wm.WeightedPartition(self.graph) # one comm per node
193+
#move first node to second community
194+
node = 0
195+
comm = 1
196+
newpart = self.louvain._move_node(part, node, comm)
197+
self.assertEqual(set([0,1]) in newpart.communities, True)
198+
## what happens if node or comm missing
199+
with self.assertRaises(ValueError):
200+
newpart = self.louvain._move_node(part, -1, comm)
201+
invalid_communities = len(part.communities) + 1
202+
with self.assertRaises(IndexError):
203+
newpart = self.louvain._move_node(part, node, invalid_communities)
184204

185-
def test_calc_delta_modularity():
186-
graph, communities = get_test_data()
187-
part = wm.WeightedPartition(graph) # one comm per node
188-
node = 0
189-
change = wm._calc_delta_modularity(node, part)
190-
npt.assert_equal(len(change), len(part.communities))
191-
# change is an array
192-
npt.assert_equal(change.shape[0], len(part.communities))
193-
npt.assert_equal(change[0] < change[1], True)
194-
# this is one comm per node, so once removed from own
195-
# comm, this delta_weight will be zero
196-
npt.assert_equal(change[node] , 0)
197-
198-
199-
def test_move_node():
200-
graph, communities = get_test_data()
201-
part = wm.WeightedPartition(graph) # one comm per node
202-
#move first node to second community
203-
node = 0
204-
comm = 1
205-
newpart = wm._move_node(part, node, comm )
206-
npt.assert_equal(set([0,1]) in newpart.communities, True)
207-
## what happens if node or comm missing
208-
with npt.assert_raises(ValueError):
209-
newpart = wm._move_node(part, -1, comm)
210-
invalid_communities = len(part.communities) + 1
211-
with npt.assert_raises(IndexError):
212-
newpart = wm._move_node(part, node, invalid_communities)
213-
214-
"""
215205
def test_combine():
216206
first = [set([0,1,2]), set([3,4,5]), set([6,7])]
217207
second = [set([0,2]), set([1])]

brainx/weighted_modularity.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ def _one_level(part, min_modularity= .0000001):
245245
return part
246246
return part
247247

248-
@staticmethod
249-
def _calc_delta_modularity(node, part):
248+
def _calc_delta_modularity(self, node, part):
250249
"""calculate the increase(s) in modularity if node is moved to other
251250
communities
252251
deltamod = inC - totc * ki / total_weight"""
@@ -268,8 +267,7 @@ def _communities_without_node(part, node):
268267
newpart[node_comm].remove(node)
269268
return newpart
270269

271-
@staticmethod
272-
def _communities_nodes_alledgesw(part, removed_node):
270+
def _communities_nodes_alledgesw(self, part, removed_node):
273271
""" returns the sum of all weighted edges to nodes in each
274272
community, once the removed_node is removed
275273
this refers to totc in Blondel paper"""
@@ -314,9 +312,6 @@ def partitions_from_dendogram(dendo):
314312
return all_partitions
315313

316314

317-
318-
319-
320315

321316
def meta_graph(partition):
322317
""" takes weighted partition communities and creates a new meta graph where

0 commit comments

Comments
 (0)