core.utils.array_pad
- gridr.core.utils.array_pad.pad_inplace(array, src_win, pad_width, mode='constant', strict_size=True, **kwargs)[source]
Pad an array inplace using a source window as original area.
Derived from method numpy.pad but acting inplace with mode limited to constant, edge, reflect, symmetric and wrap
- Parameters:
array (array_like of rank N) – The array to pad inplace. It’s shape must be so that it can hold the full padded array.
src_win (tuple of slices) – Defines the source window within array from which padding values are derived. For a 2D array, use: (slice(row_start, row_end), slice(col_start, col_end)) For a 1D array, use: (slice(start, end),) The window defines the “original data” region, and the outside of this window will be filled according to the padding mode.
pad_width ({sequence, array_like, int, dict}) – Number of values padded to the edges of each axis.
((before_1, after_1), ... (before_N, after_N))unique pad widths for each axis.(before, after)or((before, after),)yields same before and after pad for each axis.(pad,)orintis a shortcut for before = after = pad width for all axes.mode (str, optional) –
One of the following string values or a user supplied function.
- ’constant’ (default)
Pads with a constant value.
- ’edge’
Pads with the edge values of array.
- ’reflect’
Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.
- ’symmetric’
Pads with the reflection of the vector mirrored along the edge of the array.
- ’wrap’
Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.
strict_size (bool, optional) – If True the input array must exactly match the required shape for padding.
constant_values (sequence or scalar, optional) –
Used in ‘constant’. The values to set the padded values for each axis.
((before_1, after_1), ... (before_N, after_N))unique pad constants for each axis.(before, after)or((before, after),)yields same before and after constants for each axis.(constant,)orconstantis a shortcut forbefore = after = constantfor all axes.Default is 0.
reflect_type ({'even', 'odd'}, optional) – Used in ‘reflect’, and ‘symmetric’. The ‘even’ style is the default with an unaltered reflection around the edge value. For the ‘odd’ style, the extended part of the array is created by subtracting the reflected values from two times the edge value.
- Returns:
This function modifies the array in-place and returns None.
- Return type:
None
Notes
For an array with rank greater than 1, some of the padding of later axes is calculated from padding of previous axes. This is easiest to think about with a rank 2 array where the corners of the padded array are calculated by using padded values from the first axis.
The padding function, if used, should modify a rank 1 array in-place. It has the following signature:
padding_func(vector, iaxis_pad_width, iaxis, kwargs)
where
- vectorndarray
A rank 1 array already padded with zeros. Padded values are vector[:iaxis_pad_width[0]] and vector[-iaxis_pad_width[1]:].
- iaxis_pad_widthtuple
A 2-tuple of ints, iaxis_pad_width[0] represents the number of values padded at the beginning of vector where iaxis_pad_width[1] represents the number of values padded at the end of vector.
- iaxisint
The axis currently being calculated.
- kwargsdict
Any keyword arguments the function requires.
Examples
>>> import numpy as np >>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'constant', constant_values=(4, 6)) >>> a array([4, 4, 1, ..., 6, 6, 6])
>>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'edge') >>> a array([1, 1, 1, ..., 5, 5, 5])
>>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'reflect') >>> a array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])
>>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'reflect', reflect_type='odd') >>> a array([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'symmetric') >>> a array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])
>>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'symmetric', reflect_type='odd') >>> a array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])
>>> a = [1, 2, 3, 4, 5] >>> np.pad(a, (2, 3), 'wrap') >>> a array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])
- gridr.core.utils.array_pad.pad_inplace_fallback(array, src_win, pad_width, mode='constant', strict_size=True, **kwargs)[source]
A fallback method in case the import of numpy internal library functions fail (ie. numpy version < 2, or not yet known refactoring)
See pad_inplace for arguments definitions.
This fallback directly calls numpy.pad. With doing so it does allocate a non necessary buffer.
- Return type:
NoReturn