Skip to content

Commit 1be910e

Browse files
author
CindeeM
committed
RF: move dnodecom and node_degree into WeightedPartition object
1 parent d835c55 commit 1be910e

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

brainx/tests/test_weighted_modularity.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ def test_get_node_community(self):
8989
part = wm.WeightedPartition(self.graph)
9090
self.assertEqual(part.get_node_community(0), 0)
9191

92+
def test_node_degree(self):
93+
part = wm.WeightedPartition(self.graph) # one comm per node
94+
node = 0
95+
res = part.node_degree(node)
96+
npt.assert_almost_equal(res, 37.94151675 )
9297

9398
def test_modularity(self):
9499
part = wm.WeightedPartition(self.graph, self.communities)
@@ -114,21 +119,20 @@ def test_internal_links(self):
114119

115120

116121

117-
def test_dnodecom():
118-
graph, communities = get_test_data()
119-
part = wm.WeightedPartition(graph) # one comm per node
120-
node = 0
121-
node2comm_weights = wm.dnodecom(node, part)
122-
# self loops not added to weight
123-
# so communities made only of node should be zero
124-
npt.assert_equal(node2comm_weights[0],0)
125-
# this should be equal to weight between two nodes
126-
neighbor = 1
127-
expected = graph[node][neighbor]['weight']
128-
npt.assert_equal(node2comm_weights[neighbor],expected)
129-
part = wm.WeightedPartition(graph, communities)
130-
node2comm_weights = wm.dnodecom(node, part)
131-
npt.assert_equal(len(node2comm_weights), 2)
122+
def test_dnodecom(self):
123+
part = wm.WeightedPartition(self.graph) # one comm per node
124+
node = 0
125+
node2comm_weights = part.dnodecom(node)
126+
# self loops not added to weight
127+
# so communities made only of node should be zero
128+
npt.assert_equal(node2comm_weights[0],0)
129+
# this should be equal to weight between two nodes
130+
neighbor = 1
131+
expected = self.graph[node][neighbor]['weight']
132+
npt.assert_equal(node2comm_weights[neighbor],expected)
133+
part = wm.WeightedPartition(self.graph, self.communities)
134+
node2comm_weights = part.dnodecom(node)
135+
npt.assert_equal(len(node2comm_weights), 2)
132136

133137
def test_meta_graph():
134138
graph, communities = get_test_data()
@@ -176,12 +180,6 @@ def test_communities_nodes_alledgesw():
176180

177181

178182

179-
def test_node_degree():
180-
graph, communities = get_test_data()
181-
part = wm.WeightedPartition(graph) # one comm per node
182-
node = 0
183-
res = wm.node_degree(graph, node)
184-
npt.assert_almost_equal(res, 37.94151675 )
185183

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

brainx/weighted_modularity.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,23 @@ def _allnodes_in_communities(self, communities):
9191
return len(self.graph.nodes()) == \
9292
len([item for com in communities for item in com])
9393

94+
def node_degree(self, node):
95+
""" find the summed weight of all node edges
96+
"""
97+
return self.graph.degree(weight='weight')[node]
98+
99+
100+
def dnodecom(self, node):
101+
""" Find the number of links from node to each community"""
102+
comm_weights = [0] * len(self.communities)
103+
for neighbor, data in self.graph[node].items():
104+
if neighbor == node:
105+
continue
106+
tmpcomm = self.get_node_community(neighbor)
107+
comm_weights[tmpcomm] += data.get('weight', 1)
108+
return comm_weights
109+
110+
94111
def total_links(self):
95112
""" sum of all links inside or outside community
96113
no nodes are missing"""
@@ -207,22 +224,8 @@ def _communities_nodes_alledgesw(part, removed_node):
207224
return weights
208225

209226

210-
def node_degree(graph, node):
211-
""" find the summed weight to node
212-
Ki in Blondel paper"""
213-
return graph.degree(weight='weight')[node]
214227

215228

216-
def dnodecom(node, part):
217-
""" Find the number of links from node to each community"""
218-
comm_weights = [0] * len(part.communities)
219-
for neighbor, data in part.graph[node].items():
220-
if neighbor == node:
221-
continue
222-
tmpcomm = part.get_node_community(neighbor)
223-
comm_weights[tmpcomm] += data.get('weight', 1)
224-
return comm_weights
225-
226229

227230

228231
def gen_dendogram(graph, communities=None, minthr=0.0000001):
@@ -285,8 +288,8 @@ def _calc_delta_modularity(node, part):
285288
"""calculate the increase(s) in modularity if node is moved to other
286289
communities
287290
deltamod = inC - totc * ki / total_weight"""
288-
noded = node_degree(part.graph, node)
289-
dnc = dnodecom(node, part)
291+
noded = part.node_degree(node)
292+
dnc = part.dnodecom(node)
290293
totc = _communities_nodes_alledgesw(part, node)
291294
total_weight = part.total_edge_weight
292295
# cast to arrays to improve calc

0 commit comments

Comments
 (0)