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

Function _set_wrap_both

numpy/lib/arraypad.py:381–455  ·  view source on GitHub ↗

Pad `axis` of `arr` with wrapped values. Parameters ---------- padded : ndarray Input array of arbitrary shape. axis : int Axis along which to pad `arr`. width_pair : (int, int) Pair of widths that mark the pad area on both sides in the given

(padded, axis, width_pair, original_period)

Source from the content-addressed store, hash-verified

379
380
381def _set_wrap_both(padded, axis, width_pair, original_period):
382 """
383 Pad `axis` of `arr` with wrapped values.
384
385 Parameters
386 ----------
387 padded : ndarray
388 Input array of arbitrary shape.
389 axis : int
390 Axis along which to pad `arr`.
391 width_pair : (int, int)
392 Pair of widths that mark the pad area on both sides in the given
393 dimension.
394 original_period : int
395 Original length of data on `axis` of `arr`.
396
397 Returns
398 -------
399 pad_amt : tuple of ints, length 2
400 New index positions of padding to do along the `axis`. If these are
401 both 0, padding is done in this dimension.
402 """
403 left_pad, right_pad = width_pair
404 period = padded.shape[axis] - right_pad - left_pad
405 # Avoid wrapping with only a subset of the original area by ensuring period
406 # can only be a multiple of the original area's length.
407 period = period // original_period * original_period
408
409 # If the current dimension of `arr` doesn't contain enough valid values
410 # (not part of the undefined pad area) we need to pad multiple times.
411 # Each time the pad area shrinks on both sides which is communicated with
412 # these variables.
413 new_left_pad = 0
414 new_right_pad = 0
415
416 if left_pad > 0:
417 # Pad with wrapped values on left side
418 # First slice chunk from left side of the non-pad area.
419 # Use min(period, left_pad) to ensure that chunk is not larger than
420 # pad area.
421 slice_end = left_pad + period
422 slice_start = slice_end - min(period, left_pad)
423 right_slice = _slice_at_axis(slice(slice_start, slice_end), axis)
424 right_chunk = padded[right_slice]
425
426 if left_pad > period:
427 # Chunk is smaller than pad area
428 pad_area = _slice_at_axis(slice(left_pad - period, left_pad), axis)
429 new_left_pad = left_pad - period
430 else:
431 # Chunk matches pad area
432 pad_area = _slice_at_axis(slice(None, left_pad), axis)
433 padded[pad_area] = right_chunk
434
435 if right_pad > 0:
436 # Pad with wrapped values on right side
437 # First slice chunk from right side of the non-pad area.
438 # Use min(period, right_pad) to ensure that chunk is not larger than

Callers 1

padFunction · 0.85

Calls 2

_slice_at_axisFunction · 0.85
minFunction · 0.50

Tested by

no test coverage detected