Pad array on all sides with either a single value or undefined values. Parameters ---------- array : ndarray Array to grow. pad_width : sequence of tuple[int, int] Pad width on both sides for each dimension in `arr`. fill_value : scalar, optional If
(array, pad_width, fill_value=None)
| 85 | |
| 86 | |
| 87 | def _pad_simple(array, pad_width, fill_value=None): |
| 88 | """ |
| 89 | Pad array on all sides with either a single value or undefined values. |
| 90 | |
| 91 | Parameters |
| 92 | ---------- |
| 93 | array : ndarray |
| 94 | Array to grow. |
| 95 | pad_width : sequence of tuple[int, int] |
| 96 | Pad width on both sides for each dimension in `arr`. |
| 97 | fill_value : scalar, optional |
| 98 | If provided the padded area is filled with this value, otherwise |
| 99 | the pad area left undefined. |
| 100 | |
| 101 | Returns |
| 102 | ------- |
| 103 | padded : ndarray |
| 104 | The padded array with the same dtype as`array`. Its order will default |
| 105 | to C-style if `array` is not F-contiguous. |
| 106 | original_area_slice : tuple |
| 107 | A tuple of slices pointing to the area of the original array. |
| 108 | """ |
| 109 | # Allocate grown array |
| 110 | new_shape = tuple( |
| 111 | left + size + right |
| 112 | for size, (left, right) in zip(array.shape, pad_width) |
| 113 | ) |
| 114 | order = 'F' if array.flags.fnc else 'C' # Fortran and not also C-order |
| 115 | padded = np.empty(new_shape, dtype=array.dtype, order=order) |
| 116 | |
| 117 | if fill_value is not None: |
| 118 | padded.fill(fill_value) |
| 119 | |
| 120 | # Copy old array into correct space |
| 121 | original_area_slice = tuple( |
| 122 | slice(left, left + size) |
| 123 | for size, (left, right) in zip(array.shape, pad_width) |
| 124 | ) |
| 125 | padded[original_area_slice] = array |
| 126 | |
| 127 | return padded, original_area_slice |
| 128 | |
| 129 | |
| 130 | def _set_pad_area(padded, axis, width_pair, value_pair): |