Skip to content

Commit fe7ce47

Browse files
committed
Merge tests for nodal metrics into a TestCase.
1 parent 54a1c0d commit fe7ce47

File tree

1 file changed

+75
-73
lines changed

1 file changed

+75
-73
lines changed

brainx/tests/test_metrics.py

Lines changed: 75 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# Imports
55
#-----------------------------------------------------------------------------
66

7+
from unittest import TestCase
8+
79
# Third party
810
import networkx as nx
911
import nose.tools as nt
@@ -17,81 +19,81 @@
1719
# Functions
1820
#-----------------------------------------------------------------------------
1921

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)
5664

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)
5779

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)
9597

9698

9799
def test_path_lengths():

0 commit comments

Comments
 (0)