22
22
class NodalMetricsTestCase (TestCase ):
23
23
24
24
def setUp (self ):
25
+ # Distances for all node pairs:
26
+ # 0-1: 1 1-2: 1 2-3: 1 3-4: 1
27
+ # 0-2: 1 1-3: 2 2-4: 2
28
+ # 0-3: 2 1-4: 3
29
+ # 0-4: 3
25
30
self .corr_mat = np .array ([[0.0 , 0.0 , 0.0 , 0.0 , 0.0 ],
26
31
[0.5 , 0.0 , 0.0 , 0.0 , 0.0 ],
27
32
[0.3 , 0.4 , 0.0 , 0.0 , 0.0 ],
@@ -30,19 +35,40 @@ def setUp(self):
30
35
self .n_nodes = self .corr_mat .shape [0 ]
31
36
self .g = nx .from_numpy_matrix (self .corr_mat )
32
37
33
- def test_nodal_pathlengths_conn (self ):
34
- path_lengths = metrics .nodal_pathlengths (self .g , self .n_nodes )
38
+ def test_inter_node_distances_conn (self ):
39
+ distances = metrics .inter_node_distances (self .g , self .n_nodes )
40
+ desired = {0 : {1 : 1 , 2 : 1 , 3 : 2 , 4 : 3 },
41
+ 1 : {0 : 1 , 2 : 1 , 3 : 2 , 4 : 3 },
42
+ 2 : {0 : 1 , 1 : 1 , 3 : 1 , 4 : 2 },
43
+ 3 : {0 : 2 , 1 : 2 , 2 : 1 , 4 : 1 },
44
+ 4 : {0 : 3 , 1 : 3 , 2 : 2 , 3 : 1 }}
45
+ self .assertEqual (distances , desired )
46
+
47
+ def test_inter_node_distances_disconn (self ):
48
+ self .g .remove_edge (2 , 3 )
49
+ # Now all nodes still have at least one edge, but not all nodes are
50
+ # reachable from all others.
51
+ distances = metrics .inter_node_distances (self .g , self .n_nodes )
35
52
# Distances for all node pairs:
36
- # 0-1: 1 1-2: 1 2-3: 1 3-4: 1
37
- # 0-2: 1 1-3: 2 2-4: 2
38
- # 0-3: 2 1-4: 3
39
- # 0-4: 3
53
+ # 0-1: 1 1-2: 1 2-3: Inf 3-4: 1
54
+ # 0-2: 1 1-3: Inf 2-4: Inf
55
+ # 0-3: Inf 1-4: Inf
56
+ # 0-4: Inf
57
+ desired = {0 : {1 : 1 , 2 : 1 , 3 : np .inf , 4 : np .inf },
58
+ 1 : {0 : 1 , 2 : 1 , 3 : np .inf , 4 : np .inf },
59
+ 2 : {0 : 1 , 1 : 1 , 3 : np .inf , 4 : np .inf },
60
+ 3 : {0 : np .inf , 1 : np .inf , 2 : np .inf , 4 : 1 },
61
+ 4 : {0 : np .inf , 1 : np .inf , 2 : np .inf , 3 : 1 }}
62
+ self .assertEqual (distances , desired )
63
+
64
+ def test_nodal_pathlengths_conn (self ):
65
+ mean_path_lengths = metrics .nodal_pathlengths (self .g , self .n_nodes )
40
66
desired = 1.0 / (self .n_nodes - 1 ) * np .array ([1 + 1 + 2 + 3 ,
41
67
1 + 1 + 2 + 3 ,
42
68
1 + 1 + 1 + 2 ,
43
69
2 + 2 + 1 + 1 ,
44
70
3 + 3 + 2 + 1 ])
45
- npt .assert_array_almost_equal (path_lengths , desired )
71
+ npt .assert_array_almost_equal (mean_path_lengths , desired )
46
72
47
73
def test_nodal_pathlengths_disconn (self ):
48
74
self .g .remove_edge (2 , 3 )
@@ -64,11 +90,6 @@ def test_nodal_pathlengths_disconn(self):
64
90
65
91
def test_nodal_efficiency_conn (self ):
66
92
n_eff_array = metrics .nodal_efficiency (self .g , self .n_nodes )
67
- # Distances for all node pairs:
68
- # 0-1: 1 1-2: 1 2-3: 1 3-4: 1
69
- # 0-2: 1 1-3: 2 2-4: 2
70
- # 0-3: 2 1-4: 3
71
- # 0-4: 3
72
93
desired = (1.0 / (self .n_nodes - 1 ) *
73
94
np .array ([1 + 1 + 1 / 2.0 + 1 / 3.0 ,
74
95
1 + 1 + 1 / 2.0 + 1 / 3.0 ,
0 commit comments