@@ -23,65 +23,65 @@ def get_test_data():
23
23
Returns
24
24
=======
25
25
graph : networkx graph
26
- community : list of sets
26
+ communities : list of sets
27
27
"""
28
28
pth , _ = os .path .split (__file__ )
29
29
testdir = os .path .join (pth , 'tdata_corr_txt' )
30
30
data_file = os .path .join (testdir , '101_Block01.txt' )
31
31
mat = np .loadtxt (data_file )
32
32
mat [mat < 0 ] = 0
33
33
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
37
37
38
38
class TestPartition (unittest .TestCase ):
39
39
40
40
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 ()
43
43
self .graph = graph
44
- self .community = community
44
+ self .communities = communities
45
45
46
46
def test_init (self ):
47
47
part = wm .WeightedPartition (self .graph )
48
48
self .assertEqual (type (part .degrees ), type ({}))
49
49
npt .assert_array_almost_equal (part .total_edge_weight , 1500.5653444 )
50
50
# generated communities
51
51
comm = [set ([node ]) for node in self .graph .nodes ()]
52
- self .assertEqual (part .community , comm )
52
+ self .assertEqual (part .communities , comm )
53
53
54
54
def test_community_degree (self ):
55
55
## if no community, method will raise error
56
56
part = wm .WeightedPartition (self .graph )
57
- part = wm .WeightedPartition (self .graph , self .community )
57
+ part = wm .WeightedPartition (self .graph , self .communities )
58
58
cdegree = part .community_degree ()
59
59
self .assertEqual (round (cdegree [0 ]), 1462.0 )
60
60
61
61
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 )
65
65
with self .assertRaises (TypeError ):
66
66
# raise error if not list of sets
67
- part .set_community (part .community [0 ])
67
+ part .set_communities (part .communities [0 ])
68
68
with self .assertRaises (TypeError ):
69
- part .set_community ('a' )
69
+ part .set_communities ('a' )
70
70
with self .assertRaises (ValueError ):
71
71
## missing nodes
72
72
comm = self .graph .nodes ()[:- 3 ]
73
- part .set_community ([set (comm )])
73
+ part .set_communities ([set (comm )])
74
74
75
- def test_allnodes_in_community (self ):
75
+ def test_allnodes_in_communities (self ):
76
76
"""checks communities contain all nodes
77
77
with no repetition"""
78
78
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 ]]))
81
81
82
82
83
83
def test_get_node_community (self ):
84
- part = wm .WeightedPartition (self .graph , self .community )
84
+ part = wm .WeightedPartition (self .graph , self .communities )
85
85
self .assertEqual (part .get_node_community (0 ), 0 )
86
86
self .assertEqual (part .get_node_community (self .graph .nodes ()[- 1 ]),1 )
87
87
with self .assertRaises (ValueError ):
@@ -97,79 +97,79 @@ def test_modularity():
97
97
98
98
99
99
def test_total_links ():
100
- graph , community = get_test_data ()
100
+ graph , communities = get_test_data ()
101
101
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
103
103
## since one per scommunity, just weighted degree of each node
104
104
tot_per_comm = wm .total_links (part )
105
105
degw = graph .degree (weight = 'weight' ).values ()
106
106
npt .assert_equal (tot_per_comm , degw )
107
107
## This isnt true of we have communities with multiple nodes
108
- part_2comm = wm .WeightedPartition (graph , community )
108
+ part_2comm = wm .WeightedPartition (graph , communities )
109
109
npt .assert_equal (part_2comm == degw , False )
110
110
111
111
def test_internal_links ():
112
- graph , community = get_test_data ()
112
+ graph , communities = get_test_data ()
113
113
part = wm .WeightedPartition (graph ) # one comm per node
114
114
weights = wm .internal_links (part )
115
115
## this inlcudes seld links so
116
116
117
117
118
118
def test_dnodecom ():
119
- graph , community = get_test_data ()
119
+ graph , communities = get_test_data ()
120
120
part = wm .WeightedPartition (graph ) # one comm per node
121
121
node = 0
122
122
node2comm_weights = wm .dnodecom (node , part )
123
123
# 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
125
125
npt .assert_equal (node2comm_weights [0 ],0 )
126
126
# this should be equal to weight between two nodes
127
127
neighbor = 1
128
128
expected = graph [node ][neighbor ]['weight' ]
129
129
npt .assert_equal (node2comm_weights [neighbor ],expected )
130
- part = wm .WeightedPartition (graph , community )
130
+ part = wm .WeightedPartition (graph , communities )
131
131
node2comm_weights = wm .dnodecom (node , part )
132
132
npt .assert_equal (len (node2comm_weights ), 2 )
133
133
134
134
def test_meta_graph ():
135
- graph , community = get_test_data ()
135
+ graph , communities = get_test_data ()
136
136
part = wm .WeightedPartition (graph )
137
137
metagraph ,_ = wm .meta_graph (part )
138
138
## each node is a comm, so no change to metagraph
139
139
npt .assert_equal (metagraph .nodes (), graph .nodes ())
140
140
## two communitties
141
- part = wm .WeightedPartition (graph , community )
141
+ part = wm .WeightedPartition (graph , communities )
142
142
metagraph ,mapping = wm .meta_graph (part )
143
143
npt .assert_equal (metagraph .nodes (), [0 ,1 ])
144
144
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 ])
147
147
## weight should not be lost between graphs
148
148
npt .assert_almost_equal (metagraph .size (weight = 'weight' ),
149
149
graph .size (weight = 'weight' ))
150
150
151
151
152
152
def test_communities_without_node ():
153
- graph , community = get_test_data ()
153
+ graph , communities = get_test_data ()
154
154
part = wm .WeightedPartition (graph ) # one comm per node
155
155
node = 0
156
156
updated_comm = wm ._communities_without_node (part , node )
157
157
npt .assert_equal (updated_comm [0 ], set ([]))
158
- part = wm .WeightedPartition (graph , community )
158
+ part = wm .WeightedPartition (graph , communities )
159
159
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 )
162
162
npt .assert_equal (0 not in updated_comm [0 ], True )
163
163
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 )
167
167
node = 0
168
- weights = wm ._community_nodes_alledgesw (part , node )
168
+ weights = wm ._communities_nodes_alledgesw (part , node )
169
169
npt .assert_almost_equal (weights [0 ], 1424.0220362 )
170
170
## test with possible empty node set
171
171
part = wm .WeightedPartition (graph )
172
- weights = wm ._community_nodes_alledgesw (part , node )
172
+ weights = wm ._communities_nodes_alledgesw (part , node )
173
173
npt .assert_equal (weights [0 ], 0 )
174
174
# other communities are made up of just one node
175
175
npt .assert_equal (weights [1 ], graph .degree (weight = 'weight' )[1 ])
@@ -178,7 +178,7 @@ def test_community_nodes_alledgesw():
178
178
179
179
180
180
def test_node_degree ():
181
- graph , community = get_test_data ()
181
+ graph , communities = get_test_data ()
182
182
part = wm .WeightedPartition (graph ) # one comm per node
183
183
node = 0
184
184
res = wm .node_degree (graph , node )
@@ -193,30 +193,30 @@ def test_combine():
193
193
194
194
195
195
def test_calc_delta_modularity ():
196
- graph , community = get_test_data ()
196
+ graph , communities = get_test_data ()
197
197
part = wm .WeightedPartition (graph ) # one comm per node
198
198
node = 0
199
199
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 ))
201
201
# change is an array
202
- npt .assert_equal (change .shape [0 ], len (part .community ))
202
+ npt .assert_equal (change .shape [0 ], len (part .communities ))
203
203
npt .assert_equal (change [0 ] < change [1 ], True )
204
204
# this is one comm per node, so once removed from own
205
205
# comm, this delta_weight will be zero
206
206
npt .assert_equal (change [node ] , 0 )
207
207
208
208
209
209
def test_move_node ():
210
- graph , community = get_test_data ()
210
+ graph , communities = get_test_data ()
211
211
part = wm .WeightedPartition (graph ) # one comm per node
212
212
#move first node to second community
213
213
node = 0
214
214
comm = 1
215
215
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 )
217
217
## what happens if node or comm missing
218
218
with npt .assert_raises (ValueError ):
219
219
newpart = wm ._move_node (part , - 1 , comm )
220
- invalid_community = len (part .community ) + 1
220
+ invalid_communities = len (part .communities ) + 1
221
221
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