@@ -138,78 +138,104 @@ def arr_stat(x,ddof=1):
138
138
return m ,std
139
139
140
140
141
- def threshold_arr (cmat ,threshold = 0.0 ,threshold2 = None ):
141
+ def threshold_arr (cmat , threshold = 0.0 , threshold2 = None ):
142
142
"""Threshold values from the input matrix.
143
143
144
144
Parameters
145
145
----------
146
- cmat : array
146
+ cmat : array_like
147
+ An array of numbers.
147
148
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.
150
155
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.
153
160
154
161
Returns
155
162
-------
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.
157
169
158
170
Examples
159
171
--------
160
- >>> a = np.linspace(0,0.8,7)
172
+ >>> a = np.linspace(0, 0.8, 7)
161
173
>>> 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 ]))
165
179
166
180
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 ]))
169
184
170
185
"""
171
- # Select thresholds
186
+ # Select thresholds.
172
187
if threshold2 is None :
173
188
th_low = - np .inf
174
189
th_hi = threshold
175
190
else :
176
191
th_low = threshold
177
192
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 ))
181
195
vals = cmat [idx ]
182
-
183
196
return idx + (vals ,)
184
197
185
198
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 ):
187
200
"""Threshold values from the input matrix and return a new matrix.
188
201
189
202
Parameters
190
203
----------
191
- arr : array
204
+ arr : array_like
205
+ An array of numbers.
192
206
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.
195
212
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.
198
221
199
222
Returns
200
223
-------
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
+
206
233
"""
207
234
a2 = np .empty_like (arr )
208
235
a2 .fill (fill_val )
209
- mth = threshold_arr (arr ,threshold ,threshold2 )
236
+ mth = threshold_arr (arr , threshold , threshold2 )
210
237
idx ,vals = mth [:- 1 ], mth [- 1 ]
211
238
a2 [idx ] = vals
212
-
213
239
return a2
214
240
215
241
0 commit comments