Skip to content

Commit 9fee68f

Browse files
author
CindeeM
committed
NF: change make_cost_thresh_array to return a record array for added clarity
1 parent 28497f1 commit 9fee68f

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

brainx/tests/test_util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ def test_make_cost_thresh_lookup():
4545
adj_mat[ind] = thresholds
4646
lookup = util.make_cost_thresh_lookup(adj_mat)
4747

48-
npt.assert_equal(sorted(thresholds, reverse=True), lookup[0,:])
49-
npt.assert_equal(lookup[1,0] < lookup[1,-1], True)
48+
npt.assert_equal(sorted(thresholds, reverse=True), lookup.weight)
49+
npt.assert_equal(lookup[0].cost < lookup[-1].cost, True)
5050
# costs in ascending order
51-
## last vecore is same as second vector rounded to 2 decimals
52-
npt.assert_almost_equal(lookup[1], lookup[2], decimal=2)
51+
## last vector is same as second vector rounded to 2 decimals
52+
npt.assert_almost_equal(lookup.actual_cost, lookup.cost, decimal=2)
5353

5454
def test_cost_size():
5555
n_nodes = 5

brainx/util.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,34 @@ def make_cost_thresh_lookup(adjacency_matrix):
128128
adjacency matrix, sorts (lowest -> highest)
129129
Returns
130130
-------
131-
lookup : numpy array
132-
3 X number_of_edges, numpy array
133-
row 0 is sorted thresholds (largest -> smallest)
134-
row 1 is cost at each threshold (smallest -> largest)
135-
row 2 is costs rounded to one decimal point
131+
lookup : numpy record array
132+
shape = number of edges
133+
'weight' is sorted weight values (largest -> smallest)
134+
'actual_cost' is cost at each weight (smallest -> largest)
135+
'cost' is 'actual_costs' rounded to two decimal points
136+
Example
137+
-------
138+
lookup = make_cost_thresh_lookup(adj_mat)
139+
lookup[100]
140+
(0.3010111736597483, 0.704225352112676, 0.7)
141+
lookup[100].weight
142+
0.3010111736597483
143+
lookup[100].actual_cost
144+
0.704225352112676
145+
lookup[100].cost
146+
0.70
147+
136148
"""
137149

138150
ind = np.triu_indices_from(adjacency_matrix, k = 1)
139151
edges = adjacency_matrix[ind]
140152
nedges = edges.shape[0]
141-
lookup = np.zeros((3, nedges))
142-
lookup[0,:] = sorted(edges, reverse = True)
143-
lookup[1,:] = np.arange(nedges) / float(nedges)
144-
lookup[2,:] = np.round(lookup[1,:], decimals = 2)
153+
lookup = np.recarray((nedges), dtype = [('weight', float),
154+
('actual_cost', float),
155+
('cost', float)])
156+
lookup['weight'] = sorted(edges, reverse = True)
157+
lookup['actual_cost'] = np.arange(nedges) / float(nedges)
158+
lookup['cost'] = np.round(lookup['actual_cost'], decimals = 2)
145159
return lookup
146160

147161
def cost_size(nnodes):

0 commit comments

Comments
 (0)