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