Skip to content

Commit fa1958b

Browse files
committed
Clean up and add documentation to cost2thresh.
1 parent 19fb592 commit fa1958b

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

brainx/util.py

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#-----------------------------------------------------------------------------
55
# Imports
66
#-----------------------------------------------------------------------------
7+
from __future__ import print_function
78

89
import numpy as np
910

@@ -446,7 +447,7 @@ def replace_diag(arr,val=0):
446447
return arr
447448

448449

449-
def cost2thresh(cost, sub, bl, lk, last, idc=[], costlist=[]):
450+
def cost2thresh(cost, sub, bl, lk, idc=[], costlist=[]):
450451
"""Return the threshold associated with a particular cost.
451452
452453
The cost is assessed with regard to block 'bl' and subject 'sub'.
@@ -463,50 +464,63 @@ def cost2thresh(cost, sub, bl, lk, last, idc=[], costlist=[]):
463464
Block number.
464465
465466
lk: numpy array
466-
Lookup table with blocks X subjects X 2 (threshold or cost) X #
467-
costs/thresholds.
468-
469-
last:
470-
last threshold value
471-
472-
idc:
473-
474-
costlist:
475-
467+
Lookup table with blocks X subjects X 2 (threshold or cost, in
468+
that order) X thresholds/costs. Each threshold is a value
469+
representing the lowest correlation value accepted. They are
470+
ordered from least to greatest. Each cost is the fraction of
471+
all possible edges that exists in an undirected graph made from
472+
this block's correlations (thresholded with the corresponding
473+
threshold).
474+
475+
idc: integer or empty list, optional
476+
Index in costlist corresponding to cost currently being
477+
processed. By default, idc is an empty list.
478+
479+
costlist: array_like
480+
List of costs that are being queried with the current function
481+
in order.
476482
477483
Returns
478484
-------
479-
th:
480-
threshold value for this cost
485+
th: float
486+
Threshold value in lk corresponding to the supplied cost. If
487+
multiple entries matching cost exist, the smallest threshold
488+
corresponding to these is returned. If no entries matching cost
489+
are found, return the threshold corresponding to the previous
490+
cost in costlist.
491+
492+
Notes
493+
-----
494+
The supplied cost must exactly match an entry in lk for a match to
495+
be registered.
481496
482497
"""
483-
ind=np.where(lk[bl][sub][1]==cost)
484-
th=lk[bl][sub][0][ind]
485-
486-
if len(th)>1:
487-
th=th[0] #if there are multiple thresholds, go down to the lower cost ####Is this right?!!!####
488-
print 'multiple thresh'
489-
elif len(th)<1:
490-
done = 1
491-
while done:
492-
idc = idc-1
493-
newcost = costlist[idc]
494-
print idc,newcost
495-
ind=np.where(lk[bl][sub][1]==newcost)
496-
th=lk[bl][sub][0][ind]
497-
if len(th) > 1:
498-
th = th[0]
499-
done = 0
500-
501-
#th=last #if there is no associated thresh value because of repeats, just use the previous one
502-
print 'use previous thresh'
503-
498+
# For this subject and block, find the indices corresponding to this cost.
499+
# Note there may be more than one such index. There will be no such
500+
# indices if cost is not a value in the array.
501+
ind = np.where(lk[bl][sub][1] == cost)
502+
# The possibility of multiple (or no) indices implies multiple (or no)
503+
# thresholds may be acquired here.
504+
th = lk[bl][sub][0][ind]
505+
n_thresholds = len(th)
506+
if n_thresholds > 1:
507+
th=th[0]
508+
print(''.join(['Subject %s has multiple thresholds in block %d ',
509+
'corresponding to a cost of %f. The smallest is being',
510+
' used.']) % (sub, bl, cost))
511+
elif n_thresholds < 1:
512+
idc = idc - 1
513+
newcost = costlist[idc]
514+
th = cost2thresh(newcost, sub, bl, lk, idc, costlist)
515+
print(''.join(['Subject %s does not have a threshold in block %d ',
516+
'corresponding to a cost of %f. The threshold ',
517+
'matching the nearest previous cost in costlist is ',
518+
'being used.']) % (sub, block, cost))
504519
else:
505520
th=th[0]
506-
507-
#print th
508521
return th
509522

523+
510524
def cost2thresh2(cost,sub,sc,c,lk,last,idc = [],costlist=[]):
511525
"""A definition for loading the lookup table and finding the threshold associated with a particular cost for a particular subject in a particular block
512526

0 commit comments

Comments
 (0)