Skip to content

Commit 88bd98d

Browse files
authored
Merge e55e590 into 927d898
2 parents 927d898 + e55e590 commit 88bd98d

File tree

9 files changed

+2037
-2
lines changed

9 files changed

+2037
-2
lines changed

.github/workflows/conda-package.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ env:
5959
third_party/cupy/logic_tests
6060
third_party/cupy/manipulation_tests
6161
third_party/cupy/math_tests
62+
third_party/cupy/padding_tests
6263
third_party/cupy/sorting_tests
6364
third_party/cupy/statistics_tests/test_histogram.py
6465
third_party/cupy/statistics_tests/test_meanvar.py

dpnp/dpnp_iface_manipulation.py

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import dpnp
5050

5151
from .dpnp_array import dpnp_array
52+
from .dpnp_utils.dpnp_utils_pad import dpnp_pad
5253

5354
__all__ = [
5455
"append",
@@ -75,6 +76,7 @@
7576
"hstack",
7677
"moveaxis",
7778
"ndim",
79+
"pad",
7880
"permute_dims",
7981
"ravel",
8082
"repeat",
@@ -1838,6 +1840,213 @@ def ndim(a):
18381840
return numpy.ndim(a)
18391841

18401842

1843+
def pad(array, pad_width, mode="constant", **kwargs):
1844+
"""
1845+
Pad an array.
1846+
1847+
For full documentation refer to :obj:`numpy.pad`.
1848+
1849+
Parameters
1850+
----------
1851+
array : {dpnp.ndarray, usm_ndarray}
1852+
The array of rank ``N`` to pad.
1853+
pad_width : {sequence, array_like, int}
1854+
Number of values padded to the edges of each axis.
1855+
``((before_1, after_1), ... (before_N, after_N))`` unique pad widths
1856+
for each axis.
1857+
``(before, after)`` or ``((before, after),)`` yields same before
1858+
and after pad for each axis.
1859+
``(pad,)`` or ``int`` is a shortcut for ``before = after = pad`` width
1860+
for all axes.
1861+
mode : {str, function}, optional
1862+
One of the following string values or a user supplied function.
1863+
1864+
"constant"
1865+
Pads with a constant value.
1866+
"edge"
1867+
Pads with the edge values of array.
1868+
"linear_ramp"
1869+
Pads with the linear ramp between `end_value` and the
1870+
array edge value.
1871+
"maximum"
1872+
Pads with the maximum value of all or part of the
1873+
vector along each axis.
1874+
"mean"
1875+
Pads with the mean value of all or part of the
1876+
vector along each axis.
1877+
"minimum"
1878+
Pads with the minimum value of all or part of the
1879+
vector along each axis.
1880+
"reflect"
1881+
Pads with the reflection of the vector mirrored on
1882+
the first and last values of the vector along each
1883+
axis.
1884+
"symmetric"
1885+
Pads with the reflection of the vector mirrored
1886+
along the edge of the array.
1887+
"wrap"
1888+
Pads with the wrap of the vector along the axis.
1889+
The first values are used to pad the end and the
1890+
end values are used to pad the beginning.
1891+
"empty"
1892+
Pads with undefined values.
1893+
<function>
1894+
Padding function, see Notes.
1895+
Default: ``"constant"``.
1896+
stat_length : {None, int, sequence of ints}, optional
1897+
Used in ``"maximum"``, ``"mean"``, and ``"minimum"``. Number of
1898+
values at edge of each axis used to calculate the statistic value.
1899+
``((before_1, after_1), ... (before_N, after_N))`` unique statistic
1900+
lengths for each axis.
1901+
``(before, after)`` or ``((before, after),)`` yields same before
1902+
and after statistic lengths for each axis.
1903+
``(stat_length,)`` or ``int`` is a shortcut for
1904+
``before = after = statistic`` length for all axes.
1905+
Default: ``None``, to use the entire axis.
1906+
constant_values : {sequence, scalar}, optional
1907+
Used in ``"constant"``. The values to set the padded values for each
1908+
axis.
1909+
``((before_1, after_1), ... (before_N, after_N))`` unique pad constants
1910+
for each axis.
1911+
``(before, after)`` or ``((before, after),)`` yields same before
1912+
and after constants for each axis.
1913+
``(constant,)`` or ``constant`` is a shortcut for
1914+
``before = after = constant`` for all axes.
1915+
Default: ``0``.
1916+
end_values : {sequence, scalar}, optional
1917+
Used in ``"linear_ramp"``. The values used for the ending value of the
1918+
linear_ramp and that will form the edge of the padded array.
1919+
``((before_1, after_1), ... (before_N, after_N))`` unique end values
1920+
for each axis.
1921+
``(before, after)`` or ``((before, after),)`` yields same before
1922+
and after end values for each axis.
1923+
``(constant,)`` or ``constant`` is a shortcut for
1924+
``before = after = constant`` for all axes.
1925+
Default: ``0``.
1926+
reflect_type : {"even", "odd"}, optional
1927+
Used in ``"reflect"``, and ``"symmetric"``. The ``"even"`` style is the
1928+
default with an unaltered reflection around the edge value. For
1929+
the ``"odd"`` style, the extended part of the array is created by
1930+
subtracting the reflected values from two times the edge value.
1931+
Default: ``"even"``.
1932+
1933+
Returns
1934+
-------
1935+
padded array : dpnp.ndarray
1936+
Padded array of rank equal to `array` with shape increased
1937+
according to `pad_width`.
1938+
1939+
Limitations
1940+
-----------
1941+
Parameter `mode` as ``"median"`` is not currently supported and
1942+
``NotImplementedError`` exception will be raised.
1943+
1944+
Notes
1945+
-----
1946+
For an array with rank greater than 1, some of the padding of later
1947+
axes is calculated from padding of previous axes. This is easiest to
1948+
think about with a rank 2 array where the corners of the padded array
1949+
are calculated by using padded values from the first axis.
1950+
1951+
The padding function, if used, should modify a rank 1 array in-place. It
1952+
has the following signature::
1953+
1954+
padding_func(vector, iaxis_pad_width, iaxis, kwargs)
1955+
1956+
where
1957+
1958+
vector : dpnp.ndarray
1959+
A rank 1 array already padded with zeros. Padded values are
1960+
vector[:iaxis_pad_width[0]] and vector[-iaxis_pad_width[1]:].
1961+
iaxis_pad_width : tuple
1962+
A 2-tuple of ints, iaxis_pad_width[0] represents the number of
1963+
values padded at the beginning of vector where
1964+
iaxis_pad_width[1] represents the number of values padded at
1965+
the end of vector.
1966+
iaxis : int
1967+
The axis currently being calculated.
1968+
kwargs : dict
1969+
Any keyword arguments the function requires.
1970+
1971+
Examples
1972+
--------
1973+
>>> import dpnp as np
1974+
>>> a = np.array([1, 2, 3, 4, 5])
1975+
>>> np.pad(a, (2, 3), 'constant', constant_values=(4, 6))
1976+
array([4, 4, 1, 2, 3, 4, 5, 6, 6, 6])
1977+
1978+
>>> np.pad(a, (2, 3), 'edge')
1979+
array([1, 1, 1, 2, 3, 4, 5, 5, 5, 5])
1980+
1981+
>>> np.pad(a, (2, 3), 'linear_ramp', end_values=(5, -4))
1982+
array([ 5, 3, 1, 2, 3, 4, 5, 2, -1, -4])
1983+
1984+
>>> np.pad(a, (2,), 'maximum')
1985+
array([5, 5, 1, 2, 3, 4, 5, 5, 5])
1986+
1987+
>>> np.pad(a, (2,), 'mean')
1988+
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
1989+
1990+
>>> np.pad(a, (2,), 'median')
1991+
NotImplementedError: Keyword argument `mode` does not support 'median'
1992+
1993+
>>> a = np.array([[1, 2], [3, 4]])
1994+
>>> np.pad(a, ((3, 2), (2, 3)), 'minimum')
1995+
array([[1, 1, 1, 2, 1, 1, 1],
1996+
[1, 1, 1, 2, 1, 1, 1],
1997+
[1, 1, 1, 2, 1, 1, 1],
1998+
[1, 1, 1, 2, 1, 1, 1],
1999+
[3, 3, 3, 4, 3, 3, 3],
2000+
[1, 1, 1, 2, 1, 1, 1],
2001+
[1, 1, 1, 2, 1, 1, 1]])
2002+
2003+
>>> a = np.array([1, 2, 3, 4, 5])
2004+
>>> np.pad(a, (2, 3), 'reflect')
2005+
array([3, 2, 1, 2, 3, 4, 5, 4, 3, 2])
2006+
2007+
>>> np.pad(a, (2, 3), 'reflect', reflect_type='odd')
2008+
array([-1, 0, 1, 2, 3, 4, 5, 6, 7, 8])
2009+
2010+
>>> np.pad(a, (2, 3), 'symmetric')
2011+
array([2, 1, 1, 2, 3, 4, 5, 5, 4, 3])
2012+
2013+
>>> np.pad(a, (2, 3), 'symmetric', reflect_type='odd')
2014+
array([0, 1, 1, 2, 3, 4, 5, 5, 6, 7])
2015+
2016+
>>> np.pad(a, (2, 3), 'wrap')
2017+
array([4, 5, 1, 2, 3, 4, 5, 1, 2, 3])
2018+
2019+
>>> def pad_width(vector, pad_width, iaxis, kwargs):
2020+
... pad_value = kwargs.get('padder', 10)
2021+
... vector[:pad_width[0]] = pad_value
2022+
... vector[-pad_width[1]:] = pad_value
2023+
>>> a = np.arange(6)
2024+
>>> a = a.reshape((2, 3))
2025+
>>> np.pad(a, 2, pad_width)
2026+
array([[10, 10, 10, 10, 10, 10, 10],
2027+
[10, 10, 10, 10, 10, 10, 10],
2028+
[10, 10, 0, 1, 2, 10, 10],
2029+
[10, 10, 3, 4, 5, 10, 10],
2030+
[10, 10, 10, 10, 10, 10, 10],
2031+
[10, 10, 10, 10, 10, 10, 10]])
2032+
>>> np.pad(a, 2, pad_width, padder=100)
2033+
array([[100, 100, 100, 100, 100, 100, 100],
2034+
[100, 100, 100, 100, 100, 100, 100],
2035+
[100, 100, 0, 1, 2, 100, 100],
2036+
[100, 100, 3, 4, 5, 100, 100],
2037+
[100, 100, 100, 100, 100, 100, 100],
2038+
[100, 100, 100, 100, 100, 100, 100]])
2039+
2040+
"""
2041+
2042+
dpnp.check_supported_arrays_type(array)
2043+
if mode == "median":
2044+
raise NotImplementedError(
2045+
"Keyword argument `mode` does not support 'median'"
2046+
)
2047+
return dpnp_pad(array, pad_width, mode=mode, **kwargs)
2048+
2049+
18412050
def ravel(a, order="C"):
18422051
"""
18432052
Return a contiguous flattened array.

0 commit comments

Comments
 (0)