Skip to content

Commit 980a65c

Browse files
authored
Merge 1e68bee into 48515c8
2 parents 48515c8 + 1e68bee commit 980a65c

File tree

8 files changed

+2025
-2
lines changed

8 files changed

+2025
-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: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import dpnp
4949

5050
from .dpnp_array import dpnp_array
51+
from .dpnp_utils.dpnp_utils_pad import dpnp_pad
5152

5253
__all__ = [
5354
"append",
@@ -74,6 +75,7 @@
7475
"hstack",
7576
"moveaxis",
7677
"ndim",
78+
"pad",
7779
"permute_dims",
7880
"ravel",
7981
"repeat",
@@ -1837,6 +1839,217 @@ def ndim(a):
18371839
return numpy.ndim(a)
18381840

18391841

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

0 commit comments

Comments
 (0)