|
16 | 16 | #-----------------------------------------------------------------------------
|
17 | 17 | # Functions
|
18 | 18 | #-----------------------------------------------------------------------------
|
| 19 | +def test_apply_cost(): |
| 20 | + corr_mat = np.array([[0.0, 0.5, 0.3, 0.2, 0.1], |
| 21 | + [0.5, 0.0, 0.4, 0.1, 0.2], |
| 22 | + [0.3, 0.4, 0.0, 0.7, 0.2], |
| 23 | + [0.2, 0.1, 0.7, 0.0, 0.4], |
| 24 | + [0.1, 0.2, 0.2, 0.4, 0.0]]) |
| 25 | + # A five-node undirected graph has ten possible edges. Thus, the result |
| 26 | + # here should be a graph with five edges. |
| 27 | + possible_edges = 10 |
| 28 | + cost = 0.5 |
| 29 | + thresholded_corr_mat, threshold = util.apply_cost(corr_mat, cost, |
| 30 | + possible_edges) |
| 31 | + nt.assert_true(np.allclose(thresholded_corr_mat, |
| 32 | + np.array([[0.0, 0.0, 0.0, 0.0, 0.0], |
| 33 | + [0.5, 0.0, 0.0, 0.0, 0.0], |
| 34 | + [0.3, 0.4, 0.0, 0.0, 0.0], |
| 35 | + [0.0, 0.0, 0.7, 0.0, 0.0], |
| 36 | + [0.0, 0.0, 0.0, 0.4, 0.0]]))) |
| 37 | + nt.assert_almost_equal(threshold, 0.3) |
| 38 | + # Check the case in which cost requires that one of several identical edges |
| 39 | + # be kept and the others removed. apply_cost should keep all of these |
| 40 | + # identical edges. |
| 41 | + # |
| 42 | + # To test this, I need to update only a value in the lower triangle. The |
| 43 | + # function zeroes out the upper triangle immediately. |
| 44 | + corr_mat[2, 0] = 0.2 |
| 45 | + thresholded_corr_mat, threshold = util.apply_cost(corr_mat, cost, |
| 46 | + possible_edges) |
| 47 | + nt.assert_true(np.allclose(thresholded_corr_mat, |
| 48 | + np.array([[0.0, 0.0, 0.0, 0.0, 0.0], |
| 49 | + [0.5, 0.0, 0.0, 0.0, 0.0], |
| 50 | + [0.2, 0.4, 0.0, 0.0, 0.0], |
| 51 | + [0.2, 0.0, 0.7, 0.0, 0.0], |
| 52 | + [0.0, 0.2, 0.2, 0.4, 0.0]]))) |
| 53 | + nt.assert_almost_equal(threshold, 0.2) |
| 54 | + |
19 | 55 |
|
20 | 56 | def assert_graphs_equal(g,h):
|
21 | 57 | """Trivial 'equality' check for graphs"""
|
|
0 commit comments