Skip to content

Commit dd08691

Browse files
author
CindeeM
committed
NF: add function to find true cost associated with thresholded array (and tests), doc cleaning
1 parent e4ce721 commit dd08691

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

brainx/tests/test_util.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ def test_threshold_adjacency_matrix(self):
115115
npt.assert_equal(mask.sum(), 1840)
116116
npt.assert_equal(real_cost, 0.9)
117117

118+
def test_find_true_cost(self):
119+
adj_matrix = self.data_5d[0,0,0].squeeze()
120+
mask, real_cost = util.threshold_adjacency_matrix(adj_matrix, 0.2)
121+
true_cost = util.find_true_cost(mask)
122+
npt.assert_equal(real_cost, true_cost)
123+
## test on rounded array
124+
adj_matrix = self.data_5d[0,0,0].squeeze().round(decimals = 1)
125+
mask, expected_cost = util.threshold_adjacency_matrix(adj_matrix, 0.2)
126+
true_cost = util.find_true_cost(mask)
127+
## the cost of the thresholded matrix will be less than expected
128+
npt.assert_equal(real_cost > true_cost, True)
129+
130+
118131

119132

120133
def test_apply_cost():

brainx/util.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,37 @@ def format_matrix2(data, s, sc, c, lk, co, idc=[],
117117
return cmat
118118

119119
def threshold_adjacency_matrix(adj_matrix, cost):
120-
"""docstring for threshold_adjacency_matrix(adj_matrix, cost"""
120+
"""threshold adj_matrix at cost
121+
122+
Parameters
123+
----------
124+
adj_matrix : numpy array
125+
graph adjacency matrix
126+
cost : float
127+
user specified cost
128+
Returns
129+
-------
130+
thresholded : array of bools
131+
binary matrix thresholded to result in cost
132+
expected_cost : float
133+
the real cost value (closest to cost)
134+
"""
121135
nnodes, _ = adj_matrix.shape
122136
ind = np.triu_indices(nnodes, 1)
123137
nedges = adj_matrix[ind].shape[0]
124138
lookup = make_cost_thresh_lookup(adj_matrix)
125139
cost_index = np.round(cost * float(nedges))
126-
thresh, actual_cost, round_cost = lookup[cost_index]
127-
return adj_matrix > thresh, actual_cost
128-
140+
thresh, expected_cost, round_cost = lookup[cost_index]
141+
return adj_matrix > thresh, expected_cost
142+
143+
def find_true_cost(boolean_matrix):
144+
""" when passed a boolean matrix, presumably from thresholding to
145+
achieve a specific cost, this calculates the actual cost for
146+
this thresholded array"""
147+
ind = np.triu_indices_from( boolean_matrix, 1)
148+
alledges = np.array(boolean_matrix)[ind].shape[0]
149+
found_edges = boolean_matrix[ind].sum()
150+
return float(found_edges) / alledges
129151

130152
def all_positive(adjacency_matrix):
131153
""" checks if edge values in adjacency matrix are all positive

0 commit comments

Comments
 (0)