Skip to content

Commit 1349304

Browse files
author
CindeeM
committed
no edges now will raise an error when trying to partition using Louvain
1 parent c6bd993 commit 1349304

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ def test_get_node_community(self):
9090
self.assertEqual(part.get_node_community(0), 0)
9191

9292
def test_node_degree(self):
93-
part = wm.WeightedPartition(self.graph) # one comm per node
94-
node = 0
93+
part = wm.WeightedPartition(self.graph) # one comm per node
94+
node = 0
9595
res = part.node_degree(node)
9696
npt.assert_almost_equal(res, 37.94151675 )
9797

@@ -113,8 +113,8 @@ def test_total_links(self):
113113

114114
def test_internal_links(self):
115115
part = wm.WeightedPartition(self.graph) # one comm per node
116-
weights = part.internal_links()
117-
## this inlcudes self links so
116+
weights = part.internal_links()
117+
## this inlcudes self links so
118118
self.assertEqual(weights[0], 1.0)
119119

120120

@@ -123,7 +123,7 @@ def test_dnodecom(self):
123123
part = wm.WeightedPartition(self.graph) # one comm per node
124124
node = 0
125125
node2comm_weights = part.dnodecom(node)
126-
# self loops not added to weight
126+
# self loops not added to weight
127127
# so communities made only of node should be zero
128128
npt.assert_equal(node2comm_weights[0],0)
129129
# this should be equal to weight between two nodes
@@ -132,7 +132,7 @@ def test_dnodecom(self):
132132
npt.assert_equal(node2comm_weights[neighbor],expected)
133133
part = wm.WeightedPartition(self.graph, self.communities)
134134
node2comm_weights = part.dnodecom(node)
135-
npt.assert_equal(len(node2comm_weights), 2)
135+
npt.assert_equal(len(node2comm_weights), 2)
136136

137137

138138
class TestLouvainCommunityDetection(unittest.TestCase):
@@ -153,7 +153,7 @@ def test_init(self):
153153

154154

155155
def test_communities_without_node(self):
156-
part = wm.WeightedPartition(self.graph) # one comm per node
156+
part = wm.WeightedPartition(self.graph) # one comm per node
157157
node = 0
158158
updated_comm = self.louvain._communities_without_node(part, node)
159159
self.assertEqual(updated_comm[0], set([]))
@@ -164,7 +164,7 @@ def test_communities_without_node(self):
164164
self.assertEqual(0 not in updated_comm[0], True)
165165

166166
def test_communities_nodes_alledgesw(self):
167-
part = wm.WeightedPartition(self.graph, self.communities)
167+
part = wm.WeightedPartition(self.graph, self.communities)
168168
node = 0
169169
weights = self.louvain_comm._communities_nodes_alledgesw(part, node)
170170
npt.assert_almost_equal(weights[0], 1424.0220362)
@@ -186,31 +186,28 @@ def test_calc_delta_modularity(self):
186186
self.assertEqual(change[0] < change[1], True)
187187
# this is one comm per node, so once removed from own
188188
# comm, this delta_weight will be zero
189-
self.assertEqual(change[node] , 0)
189+
self.assertEqual(change[node] , 0)
190190

191191
def test_move_node(self):
192-
part = wm.WeightedPartition(self.graph) # one comm per node
193-
#move first node to second community
192+
part = wm.WeightedPartition(self.graph) # one comm per node
193+
#move first node to second community
194194
node = 0
195195
comm = 1
196196
newpart = self.louvain._move_node(part, node, comm)
197197
self.assertEqual(set([0,1]) in newpart.communities, True)
198198
## what happens if node or comm missing
199199
with self.assertRaises(ValueError):
200-
newpart = self.louvain._move_node(part, -1, comm)
200+
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)
204204

205205
def test_gen_dendogram(self):
206-
graph = nx.Graph()
206+
graph = nx.Graph()
207207
nodeslist = [0,1,2,3,4]
208208
graph.add_nodes_from(nodeslist, weight=True)
209209
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)
210+
self.assertRaises(IOError, louvain.gen_dendogram)
214211

215212
def test_run(self):
216213
karate = nx.karate_club_graph()

brainx/weighted_modularity.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ class WeightedPartition(object):
1010
"""Represent a weighted Graph Partition
1111
1212
The main object keeping track of the nodes in each partition is the
13-
communities attribute.
13+
communities attribute.
1414
"""
1515
def __init__(self, graph, communities=None):
1616
""" initialize partition of graph, with optional communities
1717
1818
Parameters
1919
----------
2020
graph : networkx graph
21-
21+
2222
communities : list of sets, optional
2323
a list of sets with nodes in each set
2424
if communities is None, will initialize with
@@ -86,7 +86,7 @@ def _allnodes_in_communities(self, communities):
8686
if not (isinstance(communities, list) and \
8787
util._contains_only(communities, set)):
8888
raise TypeError('communities should be list of sets, not '\
89-
'{}'.format(communities))
89+
'{}'.format(communities))
9090
## simple count to check for all nodes
9191
return len(self.graph.nodes()) == \
9292
len([item for com in communities for item in com])
@@ -140,7 +140,7 @@ def internal_links(self):
140140

141141

142142
def modularity(self):
143-
"""Calculates the proportion of within community edges compared to
143+
"""Calculates the proportion of within community edges compared to
144144
between community edges for all nodes in graph with given partition
145145
146146
Parameters
@@ -157,12 +157,12 @@ def modularity(self):
157157
References
158158
----------
159159
.. [1] M. Newman, "Fast algorithm for detecting community structure
160-
in networks", Physical Review E vol. 69(6), 2004.
160+
in networks", Physical Review E vol. 69(6), 2004.
161161
162162
"""
163163
if self.graph.is_directed():
164164
raise TypeError('only valid on non directed graphs')
165-
165+
166166
m2 = self.total_edge_weight
167167
internal_connect = np.array(self.internal_links())
168168
total = np.array(self.total_links())
@@ -178,7 +178,7 @@ class LouvainCommunityDetection(object):
178178
----------
179179
graph : netwrokx Graph object
180180
communities : list of sets, optional
181-
initial identified communties
181+
initial identified communties
182182
minthr : float, optional
183183
minimum threshold value for change in modularity
184184
default(0.0000001)
@@ -192,9 +192,9 @@ class LouvainCommunityDetection(object):
192192
193193
References
194194
----------
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.
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.
198198
199199
"""
200200

@@ -215,11 +215,11 @@ def gen_dendogram(self):
215215
if type(self.graph) != nx.Graph :
216216
raise TypeError("Bad graph type, use only non directed graph")
217217

218-
#special case, when there is no link
218+
#special case, when there is no link
219219
#the best partition is everyone in its communities
220220
if self.graph.number_of_edges() == 0 :
221-
return [WeightedPartition(self.graph)]
222-
221+
raise IOError('graph has no edges why do you want a partition?')
222+
223223
current_graph = self.graph.copy()
224224
part = WeightedPartition(self.graph, self.initial_communities)
225225
# first pass
@@ -231,7 +231,7 @@ def gen_dendogram(self):
231231
dendogram.append(new_part)
232232
mod = new_mod
233233
current_graph, _ = meta_graph(new_part)
234-
234+
235235
while True :
236236
partition = WeightedPartition(current_graph)
237237
newpart = self._one_level(partition, self.minthr)
@@ -260,7 +260,7 @@ def _one_level(self, part, min_modularity= .0000001):
260260
# no increase by moving this node
261261
continue
262262
best_comm = delta_mod.argmax()
263-
if not best_comm == node_comm:
263+
if not best_comm == node_comm:
264264
new_part = self._move_node(part, node, best_comm)
265265
part = new_part
266266
modified = True
@@ -307,7 +307,7 @@ def _communities_nodes_alledgesw(self, part, removed_node):
307307
if len(node_index)<1:
308308
continue
309309
weights[val] = np.sum(all_degree_weights[node_index])
310-
return weights
310+
return weights
311311

312312
@staticmethod
313313
def _move_node(part, node, new_comm):
@@ -337,17 +337,17 @@ def partitions_from_dendogram(dendo):
337337
return all_partitions
338338

339339

340-
340+
341341
def meta_graph(partition):
342342
""" takes weighted partition communities and creates a new meta graph where
343-
communities are now the nodes, the new edges are created based on the
343+
communities are now the nodes, the new edges are created based on the
344344
node to node connections from original graph, and weighted accordingly,
345345
this includes self-loops"""
346346
metagraph = nx.Graph()
347347
# new nodes are communities
348348
newnodes = [val for val,_ in enumerate(partition.communities)]
349349
mapping = {val:nodes for val,nodes in enumerate(partition.communities)}
350-
metagraph.add_nodes_from(newnodes, weight=0.0)
350+
metagraph.add_nodes_from(newnodes, weight=0.0)
351351

352352
for node1, node2, data in partition.graph.edges_iter(data=True):
353353
node1_community = partition.get_node_community(node1)

0 commit comments

Comments
 (0)