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