Skip to content

Commit c6bd993

Browse files
author
CindeeM
committed
RF+TEST: added more tests, cleaned up LouvainCommunityPartition class
1 parent 7a881b6 commit c6bd993

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,26 @@ def test_move_node(self):
200200
newpart = self.louvain._move_node(part, -1, comm)
201201
invalid_communities = len(part.communities) + 1
202202
with self.assertRaises(IndexError):
203-
newpart = self.louvain._move_node(part, node, invalid_communities)
203+
newpart = self.louvain._move_node(part, node, invalid_communities)
204+
205+
def test_gen_dendogram(self):
206+
graph = nx.Graph()
207+
nodeslist = [0,1,2,3,4]
208+
graph.add_nodes_from(nodeslist, weight=True)
209+
louvain = wm.LouvainCommunityDetection(graph)
210+
dendo = louvain.gen_dendogram()
211+
self.assertEqual(len(dendo), 1)
212+
expected = [set([x]) for x in nodeslist]
213+
self.assertEqual(dendo[0].communities, expected)
214+
215+
def test_run(self):
216+
karate = nx.karate_club_graph()
217+
louvain = wm.LouvainCommunityDetection(karate)
218+
final_partitions = louvain.run()
219+
self.assertEqual(final_partitions[-1].modularity() > .38,
220+
True)
221+
self.assertEqual(len(final_partitions), 2)
222+
204223

205224
def test_combine():
206225
first = [set([0,1,2]), set([3,4,5]), set([6,7])]

brainx/weighted_modularity.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,32 @@ def modularity(self):
171171

172172

173173
class LouvainCommunityDetection(object):
174+
""" Uses the Louvain Community Detection algorithm to detect
175+
communities in networks
176+
177+
Parameters
178+
----------
179+
graph : netwrokx Graph object
180+
communities : list of sets, optional
181+
initial identified communties
182+
minthr : float, optional
183+
minimum threshold value for change in modularity
184+
default(0.0000001)
185+
186+
Examples
187+
--------
188+
>>> louvain = LouvainCommunityDetection(graph)
189+
>>> partitions = louvain.run()
190+
>>> ## best partition
191+
>>> partitions[-1].modularity()
192+
193+
References
194+
----------
195+
.. [1] VD Blondel, JL Guillaume, R Lambiotte, E Lefebvre, "Fast
196+
unfolding of communities in large networks", Journal of Statistical
197+
Mechanics: Theory and Experiment vol.10, P10008 2008.
198+
199+
"""
174200

175201
def __init__(self, graph, communities=None, minthr=0.0000001):
176202
self.graph = graph
@@ -180,7 +206,7 @@ def __init__(self, graph, communities=None, minthr=0.0000001):
180206
def run(self):
181207
dendogram = self.gen_dendogram()
182208
partitions = self.partitions_from_dendogram(dendogram)
183-
return partitions
209+
return [WeightedPartition(self.graph, part) for part in partitions]
184210

185211

186212
def gen_dendogram(self):
@@ -191,13 +217,13 @@ def gen_dendogram(self):
191217

192218
#special case, when there is no link
193219
#the best partition is everyone in its communities
194-
if graph.number_of_edges() == 0 :
195-
return WeightedPartition(self.graph)
220+
if self.graph.number_of_edges() == 0 :
221+
return [WeightedPartition(self.graph)]
196222

197223
current_graph = self.graph.copy()
198-
part = WeightedPartition(self.graph, self.communities)
224+
part = WeightedPartition(self.graph, self.initial_communities)
199225
# first pass
200-
mod = modularity(part)
226+
mod = part.modularity()
201227
dendogram = list()
202228
new_part = self._one_level(part, self.minthr)
203229
new_mod = new_part.modularity()
@@ -210,16 +236,15 @@ def gen_dendogram(self):
210236
partition = WeightedPartition(current_graph)
211237
newpart = self._one_level(partition, self.minthr)
212238
new_mod = newpart.modularity()
213-
if new_mod - mod < minthr :
239+
if new_mod - mod < self.minthr :
214240
break
215241

216242
dendogram.append(newpart)
217243
mod = new_mod
218244
current_graph,_ = meta_graph(newpart)
219245
return dendogram
220246

221-
@staticmethod
222-
def _one_level(part, min_modularity= .0000001):
247+
def _one_level(self, part, min_modularity= .0000001):
223248
"""run one level of patitioning"""
224249
curr_mod = part.modularity()
225250
modified = True

0 commit comments

Comments
 (0)