Skip to content

Commit 43107f9

Browse files
author
CindeeM
committed
RF: change community to communities to clarify code readability
1 parent 125f804 commit 43107f9

File tree

2 files changed

+96
-96
lines changed

2 files changed

+96
-96
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,65 @@ def get_test_data():
2323
Returns
2424
=======
2525
graph : networkx graph
26-
community : list of sets
26+
communities : list of sets
2727
"""
2828
pth, _ = os.path.split(__file__)
2929
testdir = os.path.join(pth, 'tdata_corr_txt')
3030
data_file = os.path.join(testdir, '101_Block01.txt')
3131
mat = np.loadtxt(data_file)
3232
mat[mat<0] = 0
3333
graph = nx.from_numpy_matrix(mat)
34-
# graph has 85 nodes, make generic community
35-
community = [set(range(42)), set(range(42,86))]
36-
return graph, community
34+
# graph has 85 nodes, make generic communities
35+
communities = [set(range(42)), set(range(42,86))]
36+
return graph, communities
3737

3838
class TestPartition(unittest.TestCase):
3939

4040
def setUp(self):
41-
## generate a default graph and community
42-
graph, community = get_test_data()
41+
## generate a default graph and communities
42+
graph, communities = get_test_data()
4343
self.graph = graph
44-
self.community = community
44+
self.communities = communities
4545

4646
def test_init(self):
4747
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
5151
comm = [set([node]) for node in self.graph.nodes()]
52-
self.assertEqual(part.community, comm)
52+
self.assertEqual(part.communities, comm)
5353

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

6161

62-
def test_set_community(self):
63-
part = wm.WeightedPartition(self.graph, self.community)
64-
self.assertEqual(part.community, self.community)
62+
def test_set_communities(self):
63+
part = wm.WeightedPartition(self.graph, self.communities)
64+
self.assertEqual(part.communities, self.communities)
6565
with self.assertRaises(TypeError):
6666
# raise error if not list of sets
67-
part.set_community(part.community[0])
67+
part.set_communities(part.communities[0])
6868
with self.assertRaises(TypeError):
69-
part.set_community('a')
69+
part.set_communities('a')
7070
with self.assertRaises(ValueError):
7171
## missing nodes
7272
comm = self.graph.nodes()[:-3]
73-
part.set_community([set(comm)])
73+
part.set_communities([set(comm)])
7474

75-
def test_allnodes_in_community(self):
75+
def test_allnodes_in_communities(self):
7676
"""checks communities contain all nodes
7777
with no repetition"""
7878
part = wm.WeightedPartition(self.graph)
79-
self.assertTrue(part._allnodes_in_community(self.community))
80-
self.assertFalse(part._allnodes_in_community([self.community[0]]))
79+
self.assertTrue(part._allnodes_in_communities(self.communities))
80+
self.assertFalse(part._allnodes_in_communities([self.communities[0]]))
8181

8282

8383
def test_get_node_community(self):
84-
part = wm.WeightedPartition(self.graph, self.community)
84+
part = wm.WeightedPartition(self.graph, self.communities)
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):
@@ -97,79 +97,79 @@ def test_modularity():
9797

9898

9999
def test_total_links():
100-
graph, community = get_test_data()
100+
graph, communities = get_test_data()
101101
part = wm.WeightedPartition(graph) # one comm per node
102-
## summ of all links in or out of community
102+
## summ of all links in or out of communities
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.WeightedPartition(graph, community)
108+
part_2comm = wm.WeightedPartition(graph, communities)
109109
npt.assert_equal(part_2comm == degw, False)
110110

111111
def test_internal_links():
112-
graph, community = get_test_data()
112+
graph, communities = get_test_data()
113113
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():
119-
graph, community = get_test_data()
119+
graph, communities = get_test_data()
120120
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
124-
# so community made only of node should be zero
124+
# so communities made only of node should be zero
125125
npt.assert_equal(node2comm_weights[0],0)
126126
# this should be equal to weight between two nodes
127127
neighbor = 1
128128
expected = graph[node][neighbor]['weight']
129129
npt.assert_equal(node2comm_weights[neighbor],expected)
130-
part = wm.WeightedPartition(graph, community)
130+
part = wm.WeightedPartition(graph, communities)
131131
node2comm_weights = wm.dnodecom(node, part)
132132
npt.assert_equal(len(node2comm_weights), 2)
133133

134134
def test_meta_graph():
135-
graph, community = get_test_data()
135+
graph, communities = get_test_data()
136136
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.WeightedPartition(graph, community)
141+
part = wm.WeightedPartition(graph, communities)
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)])
145-
# mapping should map new node 0 to community[0]
146-
npt.assert_equal(mapping[0], community[0])
145+
# mapping should map new node 0 to communities[0]
146+
npt.assert_equal(mapping[0], communities[0])
147147
## weight should not be lost between graphs
148148
npt.assert_almost_equal(metagraph.size(weight='weight'),
149149
graph.size(weight='weight'))
150150

151151

152152
def test_communities_without_node():
153-
graph, community = get_test_data()
153+
graph, communities = get_test_data()
154154
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.WeightedPartition(graph, community)
158+
part = wm.WeightedPartition(graph, communities)
159159
updated_comm = wm._communities_without_node(part, node )
160-
## make sure we dont break community from original partition
161-
npt.assert_equal(part.community, community)
160+
## make sure we dont break communities from original partition
161+
npt.assert_equal(part.communities, communities)
162162
npt.assert_equal(0 not in updated_comm[0], True)
163163

164-
def test_community_nodes_alledgesw():
165-
graph, community = get_test_data()
166-
part = wm.WeightedPartition(graph, community)
164+
def test_communities_nodes_alledgesw():
165+
graph, communities = get_test_data()
166+
part = wm.WeightedPartition(graph, communities)
167167
node = 0
168-
weights = wm._community_nodes_alledgesw(part, node)
168+
weights = wm._communities_nodes_alledgesw(part, node)
169169
npt.assert_almost_equal(weights[0], 1424.0220362)
170170
## test with possible empty node set
171171
part = wm.WeightedPartition(graph)
172-
weights = wm._community_nodes_alledgesw(part, node)
172+
weights = wm._communities_nodes_alledgesw(part, node)
173173
npt.assert_equal(weights[0], 0)
174174
# other communities are made up of just one node
175175
npt.assert_equal(weights[1], graph.degree(weight='weight')[1])
@@ -178,7 +178,7 @@ def test_community_nodes_alledgesw():
178178

179179

180180
def test_node_degree():
181-
graph, community = get_test_data()
181+
graph, communities = get_test_data()
182182
part = wm.WeightedPartition(graph) # one comm per node
183183
node = 0
184184
res = wm.node_degree(graph, node)
@@ -193,30 +193,30 @@ def test_combine():
193193

194194

195195
def test_calc_delta_modularity():
196-
graph, community = get_test_data()
196+
graph, communities = get_test_data()
197197
part = wm.WeightedPartition(graph) # one comm per node
198198
node = 0
199199
change = wm._calc_delta_modularity(node, part)
200-
npt.assert_equal(len(change), len(part.community))
200+
npt.assert_equal(len(change), len(part.communities))
201201
# change is an array
202-
npt.assert_equal(change.shape[0], len(part.community))
202+
npt.assert_equal(change.shape[0], len(part.communities))
203203
npt.assert_equal(change[0] < change[1], True)
204204
# this is one comm per node, so once removed from own
205205
# comm, this delta_weight will be zero
206206
npt.assert_equal(change[node] , 0)
207207

208208

209209
def test_move_node():
210-
graph, community = get_test_data()
210+
graph, communities = get_test_data()
211211
part = wm.WeightedPartition(graph) # one comm per node
212212
#move first node to second community
213213
node = 0
214214
comm = 1
215215
newpart = wm._move_node(part, node, comm )
216-
npt.assert_equal(set([0,1]) in newpart.community, True)
216+
npt.assert_equal(set([0,1]) in newpart.communities, True)
217217
## what happens if node or comm missing
218218
with npt.assert_raises(ValueError):
219219
newpart = wm._move_node(part, -1, comm)
220-
invalid_community = len(part.community) + 1
220+
invalid_communities = len(part.communities) + 1
221221
with npt.assert_raises(IndexError):
222-
newpart = wm._move_node(part, node, invalid_community)
222+
newpart = wm._move_node(part, node, invalid_communities)

0 commit comments

Comments
 (0)