Skip to content

Commit 8fba2a0

Browse files
author
CindeeM
committed
RF: create LouvainCommunityDetection class, move functions into new class
1 parent 1be910e commit 8fba2a0

File tree

2 files changed

+192
-168
lines changed

2 files changed

+192
-168
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def get_test_data():
3535
communities = [set(range(42)), set(range(42,86))]
3636
return graph, communities
3737

38-
class TestPartition(unittest.TestCase):
38+
class TestWeightedPartition(unittest.TestCase):
3939

4040
def setUp(self):
4141
## generate a default graph and communities
@@ -134,23 +134,21 @@ def test_dnodecom(self):
134134
node2comm_weights = part.dnodecom(node)
135135
npt.assert_equal(len(node2comm_weights), 2)
136136

137-
def test_meta_graph():
138-
graph, communities = get_test_data()
139-
part = wm.WeightedPartition(graph)
140-
metagraph,_ = wm.meta_graph(part)
141-
## each node is a comm, so no change to metagraph
142-
npt.assert_equal(metagraph.nodes(), graph.nodes())
143-
## two communitties
144-
part = wm.WeightedPartition(graph, communities)
145-
metagraph,mapping = wm.meta_graph(part)
146-
npt.assert_equal(metagraph.nodes(), [0,1])
147-
npt.assert_equal(metagraph.edges(), [(0,0),(0,1), (1,1)])
148-
# mapping should map new node 0 to communities[0]
149-
npt.assert_equal(mapping[0], communities[0])
150-
## weight should not be lost between graphs
151-
npt.assert_almost_equal(metagraph.size(weight='weight'),
152-
graph.size(weight='weight'))
153137

138+
class TestLouvainCommunityDetection(unittest.TestCase):
139+
140+
def setUp(self):
141+
## generate a default graph and communities
142+
graph, communities = get_test_data()
143+
self.graph = graph
144+
self.communities = communities
145+
146+
def test_init(self):
147+
louvain = wm.LouvainCommunityDetection(self.graph)
148+
self.assertEqual(louvain.graph, self.graph)
149+
self.assertEqual(louvain.initial_communities, None)
150+
self.assertEqual(louvain.minthr, 0.0000001)
151+
"""
154152
155153
def test_communities_without_node():
156154
graph, communities = get_test_data()
@@ -181,12 +179,6 @@ def test_communities_nodes_alledgesw():
181179
182180
183181
184-
def test_combine():
185-
first = [set([0,1,2]), set([3,4,5]), set([6,7])]
186-
second = [set([0,2]), set([1])]
187-
npt.assert_raises(ValueError, wm._combine, second, first)
188-
res = wm._combine(first, second)
189-
npt.assert_equal(res, [set([0,1,2,6,7]), set([3,4,5])])
190182
191183
192184
def test_calc_delta_modularity():
@@ -216,4 +208,29 @@ def test_move_node():
216208
newpart = wm._move_node(part, -1, comm)
217209
invalid_communities = len(part.communities) + 1
218210
with npt.assert_raises(IndexError):
219-
newpart = wm._move_node(part, node, invalid_communities)
211+
newpart = wm._move_node(part, node, invalid_communities)
212+
213+
"""
214+
def test_combine():
215+
first = [set([0,1,2]), set([3,4,5]), set([6,7])]
216+
second = [set([0,2]), set([1])]
217+
npt.assert_raises(ValueError, wm._combine, second, first)
218+
res = wm._combine(first, second)
219+
npt.assert_equal(res, [set([0,1,2,6,7]), set([3,4,5])])
220+
221+
def test_meta_graph():
222+
graph, communities = get_test_data()
223+
part = wm.WeightedPartition(graph)
224+
metagraph,_ = wm.meta_graph(part)
225+
## each node is a comm, so no change to metagraph
226+
npt.assert_equal(metagraph.nodes(), graph.nodes())
227+
## two communitties
228+
part = wm.WeightedPartition(graph, communities)
229+
metagraph,mapping = wm.meta_graph(part)
230+
npt.assert_equal(metagraph.nodes(), [0,1])
231+
npt.assert_equal(metagraph.edges(), [(0,0),(0,1), (1,1)])
232+
# mapping should map new node 0 to communities[0]
233+
npt.assert_equal(mapping[0], communities[0])
234+
## weight should not be lost between graphs
235+
npt.assert_almost_equal(metagraph.size(weight='weight'),
236+
graph.size(weight='weight'))

0 commit comments

Comments
 (0)