MCPcopy Create free account
hub / github.com/numpy/numpy / _get_stats

Function _get_stats

numpy/lib/arraypad.py:230–293  ·  view source on GitHub ↗

Calculate statistic for the empty-padded array in given dimension. Parameters ---------- padded : ndarray Empty-padded array. axis : int Dimension in which the statistic is calculated. width_pair : (int, int) Pair of widths that mark the pad area on

(padded, axis, width_pair, length_pair, stat_func)

Source from the content-addressed store, hash-verified

228
229
230def _get_stats(padded, axis, width_pair, length_pair, stat_func):
231 """
232 Calculate statistic for the empty-padded array in given dimension.
233
234 Parameters
235 ----------
236 padded : ndarray
237 Empty-padded array.
238 axis : int
239 Dimension in which the statistic is calculated.
240 width_pair : (int, int)
241 Pair of widths that mark the pad area on both sides in the given
242 dimension.
243 length_pair : 2-element sequence of None or int
244 Gives the number of values in valid area from each side that is
245 taken into account when calculating the statistic. If None the entire
246 valid area in `padded` is considered.
247 stat_func : function
248 Function to compute statistic. The expected signature is
249 ``stat_func(x: ndarray, axis: int, keepdims: bool) -> ndarray``.
250
251 Returns
252 -------
253 left_stat, right_stat : ndarray
254 Calculated statistic for both sides of `padded`.
255 """
256 # Calculate indices of the edges of the area with original values
257 left_index = width_pair[0]
258 right_index = padded.shape[axis] - width_pair[1]
259 # as well as its length
260 max_length = right_index - left_index
261
262 # Limit stat_lengths to max_length
263 left_length, right_length = length_pair
264 if left_length is None or max_length < left_length:
265 left_length = max_length
266 if right_length is None or max_length < right_length:
267 right_length = max_length
268
269 if (left_length == 0 or right_length == 0) \
270 and stat_func in {np.amax, np.amin}:
271 # amax and amin can't operate on an empty array,
272 # raise a more descriptive warning here instead of the default one
273 raise ValueError("stat_length of 0 yields no value for padding")
274
275 # Calculate statistic for the left side
276 left_slice = _slice_at_axis(
277 slice(left_index, left_index + left_length), axis)
278 left_chunk = padded[left_slice]
279 left_stat = stat_func(left_chunk, axis=axis, keepdims=True)
280 _round_if_needed(left_stat, padded.dtype)
281
282 if left_length == right_length == max_length:
283 # return early as right_stat must be identical to left_stat
284 return left_stat, left_stat
285
286 # Calculate statistic for the right side
287 right_slice = _slice_at_axis(

Callers 1

padFunction · 0.85

Calls 2

_slice_at_axisFunction · 0.85
_round_if_neededFunction · 0.85

Tested by

no test coverage detected