Skip to content

Commit 125f804

Browse files
author
CindeeM
committed
RF: Partition changed to WeightedPartition
1 parent fb464c5 commit 125f804

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def setUp(self):
4444
self.community = community
4545

4646
def test_init(self):
47-
part = wm.Partition(self.graph)
47+
part = wm.WeightedPartition(self.graph)
4848
self.assertEqual(type(part.degrees), type({}))
4949
npt.assert_array_almost_equal(part.total_edge_weight, 1500.5653444)
5050
# generated communities
@@ -53,14 +53,14 @@ def test_init(self):
5353

5454
def test_community_degree(self):
5555
## if no community, method will raise error
56-
part = wm.Partition(self.graph)
57-
part = wm.Partition(self.graph, self.community)
56+
part = wm.WeightedPartition(self.graph)
57+
part = wm.WeightedPartition(self.graph, self.community)
5858
cdegree = part.community_degree()
5959
self.assertEqual(round(cdegree[0]), 1462.0)
6060

6161

6262
def test_set_community(self):
63-
part = wm.Partition(self.graph, self.community)
63+
part = wm.WeightedPartition(self.graph, self.community)
6464
self.assertEqual(part.community, self.community)
6565
with self.assertRaises(TypeError):
6666
# raise error if not list of sets
@@ -75,49 +75,49 @@ def test_set_community(self):
7575
def test_allnodes_in_community(self):
7676
"""checks communities contain all nodes
7777
with no repetition"""
78-
part = wm.Partition(self.graph)
78+
part = wm.WeightedPartition(self.graph)
7979
self.assertTrue(part._allnodes_in_community(self.community))
8080
self.assertFalse(part._allnodes_in_community([self.community[0]]))
8181

8282

8383
def test_get_node_community(self):
84-
part = wm.Partition(self.graph, self.community)
84+
part = wm.WeightedPartition(self.graph, self.community)
8585
self.assertEqual(part.get_node_community(0), 0)
8686
self.assertEqual(part.get_node_community(self.graph.nodes()[-1]),1)
8787
with self.assertRaises(ValueError):
8888
part.get_node_community(-1)
89-
part = wm.Partition(self.graph)
89+
part = wm.WeightedPartition(self.graph)
9090
self.assertEqual(part.get_node_community(0), 0)
9191

9292

9393
def test_modularity():
9494
graph, comm = get_test_data()
95-
part = wm.Partition(graph, comm)
95+
part = wm.WeightedPartition(graph, comm)
9696
npt.assert_almost_equal(wm.modularity(part), 0.0555463)
9797

9898

9999
def test_total_links():
100100
graph, community = get_test_data()
101-
part = wm.Partition(graph) # one comm per node
101+
part = wm.WeightedPartition(graph) # one comm per node
102102
## summ of all links in or out of community
103103
## since one per scommunity, just weighted degree of each node
104104
tot_per_comm = wm.total_links(part)
105105
degw = graph.degree(weight='weight').values()
106106
npt.assert_equal(tot_per_comm, degw)
107107
## This isnt true of we have communities with multiple nodes
108-
part_2comm = wm.Partition(graph, community)
108+
part_2comm = wm.WeightedPartition(graph, community)
109109
npt.assert_equal(part_2comm == degw, False)
110110

111111
def test_internal_links():
112112
graph, community = get_test_data()
113-
part = wm.Partition(graph) # one comm per node
113+
part = wm.WeightedPartition(graph) # one comm per node
114114
weights = wm.internal_links(part)
115115
## this inlcudes seld links so
116116

117117

118118
def test_dnodecom():
119119
graph, community = get_test_data()
120-
part = wm.Partition(graph) # one comm per node
120+
part = wm.WeightedPartition(graph) # one comm per node
121121
node = 0
122122
node2comm_weights = wm.dnodecom(node, part)
123123
# self loops not added to weight
@@ -127,18 +127,18 @@ def test_dnodecom():
127127
neighbor = 1
128128
expected = graph[node][neighbor]['weight']
129129
npt.assert_equal(node2comm_weights[neighbor],expected)
130-
part = wm.Partition(graph, community)
130+
part = wm.WeightedPartition(graph, community)
131131
node2comm_weights = wm.dnodecom(node, part)
132132
npt.assert_equal(len(node2comm_weights), 2)
133133

134134
def test_meta_graph():
135135
graph, community = get_test_data()
136-
part = wm.Partition(graph)
136+
part = wm.WeightedPartition(graph)
137137
metagraph,_ = wm.meta_graph(part)
138138
## each node is a comm, so no change to metagraph
139139
npt.assert_equal(metagraph.nodes(), graph.nodes())
140140
## two communitties
141-
part = wm.Partition(graph, community)
141+
part = wm.WeightedPartition(graph, community)
142142
metagraph,mapping = wm.meta_graph(part)
143143
npt.assert_equal(metagraph.nodes(), [0,1])
144144
npt.assert_equal(metagraph.edges(), [(0,0),(0,1), (1,1)])
@@ -151,24 +151,24 @@ def test_meta_graph():
151151

152152
def test_communities_without_node():
153153
graph, community = get_test_data()
154-
part = wm.Partition(graph) # one comm per node
154+
part = wm.WeightedPartition(graph) # one comm per node
155155
node = 0
156156
updated_comm = wm._communities_without_node(part, node )
157157
npt.assert_equal(updated_comm[0], set([]))
158-
part = wm.Partition(graph, community)
158+
part = wm.WeightedPartition(graph, community)
159159
updated_comm = wm._communities_without_node(part, node )
160160
## make sure we dont break community from original partition
161161
npt.assert_equal(part.community, community)
162162
npt.assert_equal(0 not in updated_comm[0], True)
163163

164164
def test_community_nodes_alledgesw():
165165
graph, community = get_test_data()
166-
part = wm.Partition(graph, community)
166+
part = wm.WeightedPartition(graph, community)
167167
node = 0
168168
weights = wm._community_nodes_alledgesw(part, node)
169169
npt.assert_almost_equal(weights[0], 1424.0220362)
170170
## test with possible empty node set
171-
part = wm.Partition(graph)
171+
part = wm.WeightedPartition(graph)
172172
weights = wm._community_nodes_alledgesw(part, node)
173173
npt.assert_equal(weights[0], 0)
174174
# other communities are made up of just one node
@@ -179,7 +179,7 @@ def test_community_nodes_alledgesw():
179179

180180
def test_node_degree():
181181
graph, community = get_test_data()
182-
part = wm.Partition(graph) # one comm per node
182+
part = wm.WeightedPartition(graph) # one comm per node
183183
node = 0
184184
res = wm.node_degree(graph, node)
185185
npt.assert_almost_equal(res, 37.94151675 )
@@ -194,7 +194,7 @@ def test_combine():
194194

195195
def test_calc_delta_modularity():
196196
graph, community = get_test_data()
197-
part = wm.Partition(graph) # one comm per node
197+
part = wm.WeightedPartition(graph) # one comm per node
198198
node = 0
199199
change = wm._calc_delta_modularity(node, part)
200200
npt.assert_equal(len(change), len(part.community))
@@ -208,7 +208,7 @@ def test_calc_delta_modularity():
208208

209209
def test_move_node():
210210
graph, community = get_test_data()
211-
part = wm.Partition(graph) # one comm per node
211+
part = wm.WeightedPartition(graph) # one comm per node
212212
#move first node to second community
213213
node = 0
214214
comm = 1

brainx/weighted_modularity.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from . import util
77

88

9-
class Partition(object):
9+
class WeightedPartition(object):
1010
"""Represent a weighted Graph Partition
1111
1212
The main object keeping track of the nodes in each partition is the
@@ -240,10 +240,10 @@ def gen_dendogram(graph, community=None, minthr=0.0000001):
240240
#special case, when there is no link
241241
#the best partition is everyone in its community
242242
if graph.number_of_edges() == 0 :
243-
return Partition(graph)
243+
return WeightedPartition(graph)
244244

245245
current_graph = graph.copy()
246-
part = Partition(graph, community)
246+
part = WeightedPartition(graph, community)
247247
# first pass
248248
mod = modularity(part)
249249
dendogram = list()
@@ -255,7 +255,7 @@ def gen_dendogram(graph, community=None, minthr=0.0000001):
255255
current_graph, _ = meta_graph(new_part)
256256

257257
while True :
258-
partition = Partition(current_graph)
258+
partition = WeightedPartition(current_graph)
259259
newpart = _one_level(partition)
260260
new_mod = modularity(newpart)
261261
if new_mod - mod < minthr :
@@ -303,7 +303,7 @@ def _move_node(part, node, new_comm):
303303
new_community[curr_node_comm].remove(node)
304304
new_community[new_comm].add(node)
305305
new_comm = [x for x in new_community if len(x) > 0]
306-
return Partition(part.graph, new_comm)
306+
return WeightedPartition(part.graph, new_comm)
307307

308308

309309
def _one_level(part, min_modularity= .0000001):

0 commit comments

Comments
 (0)