Skip to content

Commit fea8ef9

Browse files
committed
Cleanup and add documentation to threshold_arr and thresholded_arr.
1 parent ed07841 commit fea8ef9

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

brainx/util.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -138,78 +138,104 @@ def arr_stat(x,ddof=1):
138138
return m,std
139139

140140

141-
def threshold_arr(cmat,threshold=0.0,threshold2=None):
141+
def threshold_arr(cmat, threshold=0.0, threshold2=None):
142142
"""Threshold values from the input matrix.
143143
144144
Parameters
145145
----------
146-
cmat : array
146+
cmat : array_like
147+
An array of numbers.
147148
148-
threshold : float, optional.
149-
First threshold.
149+
threshold : float, optional
150+
If threshold2 is None, indices and values for elements of cmat
151+
greater than this value (0 by default) will be returned. If
152+
threshold2 is not None, indices and values for elements of cmat
153+
less than this value (or greater than threshold2) will be
154+
returned.
150155
151-
threshold2 : float, optional.
152-
Second threshold.
156+
threshold2 : float, optional
157+
Indices and values for elements of cmat greater than this value
158+
(or less than threshold) will be returned. By default,
159+
threshold2 is set to None and not used.
153160
154161
Returns
155162
-------
156-
indices, values: a tuple with ndim+1
163+
A tuple of length N + 1, where N is the number of dimensions in
164+
cmat. The first N elements of this tuple are arrays with indices in
165+
cmat, for each dimension, corresponding to elements greater than
166+
threshold (if threshold2 is None) or more extreme than the two
167+
thresholds. The last element of the tuple is an array with the
168+
values in cmat corresponding to these indices.
157169
158170
Examples
159171
--------
160-
>>> a = np.linspace(0,0.8,7)
172+
>>> a = np.linspace(0, 0.8, 7)
161173
>>> a
162-
array([ 0. , 0.1333, 0.2667, 0.4 , 0.5333, 0.6667, 0.8 ])
163-
>>> threshold_arr(a,0.3)
164-
(array([3, 4, 5, 6]), array([ 0.4 , 0.5333, 0.6667, 0.8 ]))
174+
array([ 0. , 0.1333, 0.2667, 0.4 , 0.5333,
175+
0.6667, 0.8 ])
176+
>>> threshold_arr(a, 0.3)
177+
(array([3, 4, 5, 6]),
178+
array([ 0.4 , 0.5333, 0.6667, 0.8 ]))
165179
166180
With two thresholds:
167-
>>> threshold_arr(a,0.3,0.6)
168-
(array([0, 1, 2, 5, 6]), array([ 0. , 0.1333, 0.2667, 0.6667, 0.8 ]))
181+
>>> threshold_arr(a, 0.3, 0.6)
182+
(array([0, 1, 2, 5, 6]),
183+
array([ 0. , 0.1333, 0.2667, 0.6667, 0.8 ]))
169184
170185
"""
171-
# Select thresholds
186+
# Select thresholds.
172187
if threshold2 is None:
173188
th_low = -np.inf
174189
th_hi = threshold
175190
else:
176191
th_low = threshold
177192
th_hi = threshold2
178-
179-
# Mask out the values we are actually going to use
180-
idx = np.where( (cmat < th_low) | (cmat > th_hi) )
193+
# Mask out the values we are actually going to use.
194+
idx = np.where((cmat < th_low) | (cmat > th_hi))
181195
vals = cmat[idx]
182-
183196
return idx + (vals,)
184197

185198

186-
def thresholded_arr(arr,threshold=0.0,threshold2=None,fill_val=np.nan):
199+
def thresholded_arr(arr, threshold=0.0, threshold2=None, fill_val=np.nan):
187200
"""Threshold values from the input matrix and return a new matrix.
188201
189202
Parameters
190203
----------
191-
arr : array
204+
arr : array_like
205+
An array of numbers.
192206
193-
threshold : float
194-
First threshold.
207+
threshold : float, optional
208+
If threshold2 is None, elements of arr less than this value (0
209+
by default) will be filled with fill_val. If threshold2 is not
210+
None, elements of arr greater than this value but less than
211+
threshold2 will be filled with fill_val.
195212
196-
threshold2 : float, optional.
197-
Second threshold.
213+
threshold2 : float, optional
214+
Elements of arr less than this value but greater than threshold
215+
will be filled with fill_val. By default, high_thresh is set to
216+
None and not used.
217+
218+
fill_val : float or numpy.nan, optional
219+
Value (np.nan by default) with which to fill elements below
220+
threshold or between threshold and threshold2.
198221
199222
Returns
200223
-------
201-
An array shaped like the input, with the values outside the threshold
202-
replaced with fill_val.
203-
204-
Examples
205-
--------
224+
a2 : array_like
225+
An array with the same shape as arr, but with values below
226+
threshold or between threshold and threshold2 replaced with
227+
fill_val.
228+
229+
Notes
230+
-----
231+
arr itself is not altered.
232+
206233
"""
207234
a2 = np.empty_like(arr)
208235
a2.fill(fill_val)
209-
mth = threshold_arr(arr,threshold,threshold2)
236+
mth = threshold_arr(arr, threshold, threshold2)
210237
idx,vals = mth[:-1], mth[-1]
211238
a2[idx] = vals
212-
213239
return a2
214240

215241

0 commit comments

Comments
 (0)