55from .. import weighted_modularity as wm
66
77class TestNodalRoles (unittest .TestCase ):
8- def test_participation_coefficient (self ):
9- graph = nx .Graph ()
10- graph .add_node (1 )
11- partition = wm .WeightedPartition (graph )
8+ def test_participation_coefficient_edgeless (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 ]
1214 with self .assertRaises (ValueError ):
13- nr .participation_coefficient (partition )
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_edgeless (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 })
35+ def test_high_low_pc (self ):
36+ graph = nx .Graph ([(0 ,1 ),(1 ,2 ),(2 ,0 ),(0 ,3 ),(3 ,4 ),(3 ,5 ),(4 ,5 )])
37+ louvain = wm .LouvainCommunityDetection (graph )
38+ partition = louvain .run ()[0 ]
39+ pc = nr .participation_coefficient (partition )
40+ self .assertEqual (pc ,{0 : 0.8888888888888888 , 1 : 0.0 , 2 : 0.0 , 3 : 0.8888888888888888 , 4 : 0.0 , 5 : 0.0 })
41+ def test_high_low_wcd (self ):
42+ graph = nx .Graph ([(0 ,1 ),(0 ,2 ),(0 ,3 ),(0 ,4 ),(0 ,5 ),(6 ,7 ),(7 ,8 ),(8 ,6 )])
43+ louvain = wm .LouvainCommunityDetection (graph )
44+ partition = louvain .run ()[0 ]
45+ wcd = nr .within_community_degree (partition )
46+ self .assertEqual (wcd , {0 : 3.8819660112501051 , 1 : - 0.11803398874989512 , 2 : - 0.11803398874989512 , 3 : - 0.11803398874989512 ,4 : - 0.11803398874989512 , 5 : - 0.11803398874989512 , 6 : 0.0 , 7 : 0.0 , 8 : 0.0 })
47+
48+
0 commit comments