|
4 | 4 | # Imports
|
5 | 5 | #-----------------------------------------------------------------------------
|
6 | 6 |
|
| 7 | +from unittest import TestCase |
| 8 | + |
7 | 9 | # Third party
|
8 | 10 | import networkx as nx
|
9 | 11 | import nose.tools as nt
|
|
17 | 19 | # Functions
|
18 | 20 | #-----------------------------------------------------------------------------
|
19 | 21 |
|
20 |
| -def test_nodal_pathlengths(): |
21 |
| - corr_mat = np.array([[0.0, 0.0, 0.0, 0.0, 0.0], |
22 |
| - [0.5, 0.0, 0.0, 0.0, 0.0], |
23 |
| - [0.3, 0.4, 0.0, 0.0, 0.0], |
24 |
| - [0.0, 0.0, 0.7, 0.0, 0.0], |
25 |
| - [0.0, 0.0, 0.0, 0.4, 0.0]]) |
26 |
| - n_nodes = 5 |
27 |
| - g = nx.from_numpy_matrix(corr_mat) |
28 |
| - path_lengths = metrics.nodal_pathlengths(g, n_nodes) |
29 |
| - # Distances for all node pairs: |
30 |
| - # 0-1: 1 1-2: 1 2-3: 1 3-4: 1 |
31 |
| - # 0-2: 1 1-3: 2 2-4: 2 |
32 |
| - # 0-3: 2 1-4: 3 |
33 |
| - # 0-4: 3 |
34 |
| - desired = 1.0 / (n_nodes - 1) * np.array([1 + 1 + 2 + 3, |
35 |
| - 1 + 1 + 2 + 3, |
36 |
| - 1 + 1 + 1 + 2, |
37 |
| - 2 + 2 + 1 + 1, |
38 |
| - 3 + 3 + 2 + 1]) |
39 |
| - npt.assert_array_almost_equal(path_lengths, desired) |
40 |
| - # Check how unreachability is handled. |
41 |
| - g.remove_edge(2, 3) |
42 |
| - # Now all nodes have at least one edge, but not all nodes are reachable |
43 |
| - # from all others. |
44 |
| - path_lengths = metrics.nodal_pathlengths(g, n_nodes) |
45 |
| - # Distances for all node pairs: |
46 |
| - # 0-1: 1 1-2: 1 2-3: Inf 3-4: 1 |
47 |
| - # 0-2: 1 1-3: Inf 2-4: Inf |
48 |
| - # 0-3: Inf 1-4: Inf |
49 |
| - # 0-4: Inf |
50 |
| - desired = 1.0 / (n_nodes - 1) * np.array([1 + 1 + np.inf + np.inf, |
51 |
| - 1 + 1 + np.inf + np.inf, |
52 |
| - 1 + 1 + np.inf + np.inf, |
53 |
| - np.inf + np.inf + np.inf + 1, |
54 |
| - np.inf + np.inf + np.inf + 1]) |
55 |
| - npt.assert_array_almost_equal(path_lengths, desired) |
| 22 | +class NodalMetricsTestCase(TestCase): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self.corr_mat = np.array([[0.0, 0.0, 0.0, 0.0, 0.0], |
| 26 | + [0.5, 0.0, 0.0, 0.0, 0.0], |
| 27 | + [0.3, 0.4, 0.0, 0.0, 0.0], |
| 28 | + [0.0, 0.0, 0.7, 0.0, 0.0], |
| 29 | + [0.0, 0.0, 0.0, 0.4, 0.0]]) |
| 30 | + self.n_nodes = self.corr_mat.shape[0] |
| 31 | + self.g = nx.from_numpy_matrix(self.corr_mat) |
| 32 | + |
| 33 | + def test_nodal_pathlengths_conn(self): |
| 34 | + path_lengths = metrics.nodal_pathlengths(self.g, self.n_nodes) |
| 35 | + # 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 |
| 40 | + desired = 1.0 / (self.n_nodes - 1) * np.array([1 + 1 + 2 + 3, |
| 41 | + 1 + 1 + 2 + 3, |
| 42 | + 1 + 1 + 1 + 2, |
| 43 | + 2 + 2 + 1 + 1, |
| 44 | + 3 + 3 + 2 + 1]) |
| 45 | + npt.assert_array_almost_equal(path_lengths, desired) |
| 46 | + |
| 47 | + def test_nodal_pathlengths_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 | + path_lengths = metrics.nodal_pathlengths(self.g, self.n_nodes) |
| 52 | + # Distances for all node pairs: |
| 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 = (1.0 / (self.n_nodes - 1) * |
| 58 | + np.array([1 + 1 + np.inf + np.inf, |
| 59 | + 1 + 1 + np.inf + np.inf, |
| 60 | + 1 + 1 + np.inf + np.inf, |
| 61 | + np.inf + np.inf + np.inf + 1, |
| 62 | + np.inf + np.inf + np.inf + 1])) |
| 63 | + npt.assert_array_almost_equal(path_lengths, desired) |
56 | 64 |
|
| 65 | + def test_nodal_efficiency_conn(self): |
| 66 | + 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 | + desired = (1.0 / (self.n_nodes - 1) * |
| 73 | + np.array([1 + 1 + 1 / 2.0 + 1 / 3.0, |
| 74 | + 1 + 1 + 1 / 2.0 + 1 / 3.0, |
| 75 | + 1 + 1 + 1 + 1 / 2.0, |
| 76 | + 1 / 2.0 + 1 / 2.0 + 1 + 1, |
| 77 | + 1 / 3.0 + 1 / 3.0 + 1 / 2.0 + 1])) |
| 78 | + npt.assert_array_almost_equal(n_eff_array, desired) |
57 | 79 |
|
58 |
| -def test_nodal_efficiency(): |
59 |
| - corr_mat = np.array([[0.0, 0.0, 0.0, 0.0, 0.0], |
60 |
| - [0.5, 0.0, 0.0, 0.0, 0.0], |
61 |
| - [0.3, 0.4, 0.0, 0.0, 0.0], |
62 |
| - [0.0, 0.0, 0.7, 0.0, 0.0], |
63 |
| - [0.0, 0.0, 0.0, 0.4, 0.0]]) |
64 |
| - n_nodes = 5 |
65 |
| - g = nx.from_numpy_matrix(corr_mat) |
66 |
| - n_eff_array = metrics.nodal_efficiency(g, 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 |
| - desired = 1.0 / (n_nodes - 1) * np.array([1 + 1 + 1 / 2.0 + 1 / 3.0, |
73 |
| - 1 + 1 + 1 / 2.0 + 1 / 3.0, |
74 |
| - 1 + 1 + 1 + 1 / 2.0, |
75 |
| - 1 / 2.0 + 1 / 2.0 + 1 + 1, |
76 |
| - 1 / 3.0 + 1 / 3.0 + 1 / 2.0 + 1]) |
77 |
| - npt.assert_array_almost_equal(n_eff_array, desired) |
78 |
| - # Check how unreachability is handled. |
79 |
| - g.remove_edge(2, 3) |
80 |
| - # Now all nodes have at least one edge, but not all nodes are reachable |
81 |
| - # from all others. |
82 |
| - n_eff_array = metrics.nodal_efficiency(g, n_nodes) |
83 |
| - # Distances for all node pairs: |
84 |
| - # 0-1: 1 1-2: 1 2-3: Inf 3-4: 1 |
85 |
| - # 0-2: 1 1-3: Inf 2-4: Inf |
86 |
| - # 0-3: Inf 1-4: Inf |
87 |
| - # 0-4: Inf |
88 |
| - desired = (1.0 / (n_nodes - 1) * |
89 |
| - np.array([1 + 1 + 1 / np.inf + 1 / np.inf, |
90 |
| - 1 + 1 + 1 / np.inf + 1 / np.inf, |
91 |
| - 1 + 1 + 1 / np.inf + 1 / np.inf, |
92 |
| - 1 / np.inf + 1 / np.inf + 1 / np.inf + 1, |
93 |
| - 1 / np.inf + 1 / np.inf + 1 / np.inf + 1])) |
94 |
| - npt.assert_array_almost_equal(n_eff_array, desired) |
| 80 | + def test_nodal_efficiency_disconn(self): |
| 81 | + self.g.remove_edge(2, 3) |
| 82 | + # Now all nodes still have at least one edge, but not all nodes are |
| 83 | + # reachable from all others. |
| 84 | + n_eff_array = metrics.nodal_efficiency(self.g, self.n_nodes) |
| 85 | + # Distances for all node pairs: |
| 86 | + # 0-1: 1 1-2: 1 2-3: Inf 3-4: 1 |
| 87 | + # 0-2: 1 1-3: Inf 2-4: Inf |
| 88 | + # 0-3: Inf 1-4: Inf |
| 89 | + # 0-4: Inf |
| 90 | + desired = (1.0 / (self.n_nodes - 1) * |
| 91 | + np.array([1 + 1 + 1 / np.inf + 1 / np.inf, |
| 92 | + 1 + 1 + 1 / np.inf + 1 / np.inf, |
| 93 | + 1 + 1 + 1 / np.inf + 1 / np.inf, |
| 94 | + 1 / np.inf + 1 / np.inf + 1 / np.inf + 1, |
| 95 | + 1 / np.inf + 1 / np.inf + 1 / np.inf + 1])) |
| 96 | + npt.assert_array_almost_equal(n_eff_array, desired) |
95 | 97 |
|
96 | 98 |
|
97 | 99 | def test_path_lengths():
|
|
0 commit comments