Skip to content

Commit 5546028

Browse files
author
mb3152
committed
nodal roles docs cleaned up,working on tests
1 parent 31ee50d commit 5546028

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

brainx/nodal_roles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def within_community_degree(weighted_partition, nan = 0.0, catch_edgeless_node=T
1111
Parameters
1212
------
1313
weighted_partition: Louvain Weighted Partition
14-
louvain = weighted_modularity.wm.LouvainCommunityDetection()
14+
louvain = weighted_modularity.LouvainCommunityDetection(graph)
1515
weighted_partitions = louvain.run()
1616
weighted_partition = weighted_partition[0], where index is the partition level
1717
nan : int
@@ -42,10 +42,10 @@ def within_community_degree(weighted_partition, nan = 0.0, catch_edgeless_node=T
4242
for node in community: #get node's within_community-degree z-score
4343
within_community_degree = weighted_partition.node_degree_by_community(node)[c]
4444
std = np.std(community_degrees) # std of community's degrees
45+
mean = np.mean(community_degrees) # mean of community's degrees
4546
if std == 0.0: #so we don't divide by 0
4647
wc_dict[node] = (within_community_degree - mean) #z_score
4748
continue
48-
mean = np.mean(community_degrees) # mean of community's degrees
4949
wc_dict[node] = (within_community_degree - mean / std) #z_score
5050
return wc_dict
5151

@@ -57,7 +57,7 @@ def participation_coefficient(weighted_partition, catch_edgeless_node=True):
5757
Parameters
5858
------
5959
weighted_partition: Louvain Weighted Partition
60-
louvain = weighted_modularity.wm.LouvainCommunityDetection()
60+
louvain = weighted_modularity.LouvainCommunityDetection(graph)
6161
weighted_partitions = louvain.run()
6262
weighted_partition = weighted_partition[0], where index is the partition level
6363
catch_edgeless_node: Boolean

brainx/test_nodal_roles.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#test nodal roles
2+
import unittest
3+
import networkx as nx
4+
import nodal_roles as nr
5+
import weighted_modularity as wm
6+
7+
class TestNodalRoles(unittest.TestCase):
8+
def test_participation_coefficient(self):
9+
graph = nx.Graph([(0,1)])
10+
graph.add_node(2)
11+
louvain = wm.LouvainCommunityDetection(graph)
12+
weighted_partitions = louvain.run()
13+
weighted_partition = weighted_partitions[0]
14+
with self.assertRaises(ValueError):
15+
nr.participation_coefficient(weighted_partition)
16+
graph = nx.Graph([(0,1),(1,2),(2,0),(3,4),(3,5),(4,5)])
17+
louvain = wm.LouvainCommunityDetection(graph)
18+
partition = louvain.run()[0]
19+
def test_within_community_degree(self):
20+
graph = nx.Graph([(0,1)])
21+
graph.add_node(2)
22+
louvain = wm.LouvainCommunityDetection(graph)
23+
weighted_partitions = louvain.run()
24+
weighted_partition = weighted_partitions[0]
25+
with self.assertRaises(ValueError):
26+
nr.within_community_degree(weighted_partition)
27+
def test_disconnected_communites(self):
28+
graph = nx.Graph([(0,1),(1,2),(2,0),(3,4),(3,5),(4,5)])
29+
louvain = wm.LouvainCommunityDetection(graph)
30+
partition = louvain.run()[0]
31+
wcd = nr.within_community_degree(partition)
32+
self.assertEqual(wcd, {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})
33+
pc = nr.participation_coefficient(partition)
34+
self.assertEqual(pc, {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0})

0 commit comments

Comments
 (0)