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

Function _set_reflect_both

numpy/lib/arraypad.py:296–378  ·  view source on GitHub ↗

Pad `axis` of `arr` with reflection. 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 dim

(padded, axis, width_pair, method, include_edge=False)

Source from the content-addressed store, hash-verified

294
295
296def _set_reflect_both(padded, axis, width_pair, method, include_edge=False):
297 """
298 Pad `axis` of `arr` with reflection.
299
300 Parameters
301 ----------
302 padded : ndarray
303 Input array of arbitrary shape.
304 axis : int
305 Axis along which to pad `arr`.
306 width_pair : (int, int)
307 Pair of widths that mark the pad area on both sides in the given
308 dimension.
309 method : str
310 Controls method of reflection; options are 'even' or 'odd'.
311 include_edge : bool
312 If true, edge value is included in reflection, otherwise the edge
313 value forms the symmetric axis to the reflection.
314
315 Returns
316 -------
317 pad_amt : tuple of ints, length 2
318 New index positions of padding to do along the `axis`. If these are
319 both 0, padding is done in this dimension.
320 """
321 left_pad, right_pad = width_pair
322 old_length = padded.shape[axis] - right_pad - left_pad
323
324 if include_edge:
325 # Edge is included, we need to offset the pad amount by 1
326 edge_offset = 1
327 else:
328 edge_offset = 0 # Edge is not included, no need to offset pad amount
329 old_length -= 1 # but must be omitted from the chunk
330
331 if left_pad > 0:
332 # Pad with reflected values on left side:
333 # First limit chunk size which can't be larger than pad area
334 chunk_length = min(old_length, left_pad)
335 # Slice right to left, stop on or next to edge, start relative to stop
336 stop = left_pad - edge_offset
337 start = stop + chunk_length
338 left_slice = _slice_at_axis(slice(start, stop, -1), axis)
339 left_chunk = padded[left_slice]
340
341 if method == "odd":
342 # Negate chunk and align with edge
343 edge_slice = _slice_at_axis(slice(left_pad, left_pad + 1), axis)
344 left_chunk = 2 * padded[edge_slice] - left_chunk
345
346 # Insert chunk into padded area
347 start = left_pad - chunk_length
348 stop = left_pad
349 pad_area = _slice_at_axis(slice(start, stop), axis)
350 padded[pad_area] = left_chunk
351 # Adjust pointer to left edge for next iteration
352 left_pad -= chunk_length
353

Callers 1

padFunction · 0.85

Calls 2

_slice_at_axisFunction · 0.85
minFunction · 0.50

Tested by

no test coverage detected