Skip to content

Commit 4f5761d

Browse files
author
Vahid Tavanashad
committed
add support for mode median
1 parent c3b50a2 commit 4f5761d

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,9 @@ def pad(array, pad_width, mode="constant", **kwargs):
18741874
"mean"
18751875
Pads with the mean value of all or part of the
18761876
vector along each axis.
1877+
"median"
1878+
Pads with the median value of all or part of the
1879+
vector along each axis.
18771880
"minimum"
18781881
Pads with the minimum value of all or part of the
18791882
vector along each axis.
@@ -1894,10 +1897,10 @@ def pad(array, pad_width, mode="constant", **kwargs):
18941897
Padding function, see Notes.
18951898
Default: ``"constant"``.
18961899
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.
1900+
Used in ``"maximum"``, ``"mean"``, ``"median"``, and ``"minimum"``.
1901+
Number of values at edge of each axis used to calculate the statistic
1902+
value. ``((before_1, after_1), ... (before_N, after_N))`` unique
1903+
statistic lengths for each axis.
19011904
``(before, after)`` or ``((before, after),)`` yields same before
19021905
and after statistic lengths for each axis.
19031906
``(stat_length,)`` or ``int`` is a shortcut for
@@ -1936,11 +1939,6 @@ def pad(array, pad_width, mode="constant", **kwargs):
19361939
Padded array of rank equal to `array` with shape increased
19371940
according to `pad_width`.
19381941
1939-
Limitations
1940-
-----------
1941-
Parameter `mode` as ``"median"`` is not currently supported and
1942-
``NotImplementedError`` exception will be raised.
1943-
19441942
Notes
19451943
-----
19461944
For an array with rank greater than 1, some of the padding of later
@@ -1988,7 +1986,7 @@ def pad(array, pad_width, mode="constant", **kwargs):
19881986
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
19891987
19901988
>>> np.pad(a, (2,), 'median')
1991-
NotImplementedError: Keyword argument `mode` does not support 'median'
1989+
array([3, 3, 1, 2, 3, 4, 5, 3, 3])
19921990
19931991
>>> a = np.array([[1, 2], [3, 4]])
19941992
>>> np.pad(a, ((3, 2), (2, 3)), 'minimum')
@@ -2040,10 +2038,6 @@ def pad(array, pad_width, mode="constant", **kwargs):
20402038
"""
20412039

20422040
dpnp.check_supported_arrays_type(array)
2043-
if mode == "median":
2044-
raise NotImplementedError(
2045-
"Keyword argument `mode` does not support 'median'"
2046-
)
20472041
return dpnp_pad(array, pad_width, mode=mode, **kwargs)
20482042

20492043

dpnp/dpnp_utils/dpnp_utils_pad.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ def dpnp_pad(array, pad_width, mode="constant", **kwargs):
659659
"linear_ramp": ["end_values"],
660660
"maximum": ["stat_length"],
661661
"mean": ["stat_length"],
662-
# "median": ["stat_length"], # TODO: dpnp.median is not implemented
662+
"median": ["stat_length"],
663663
"minimum": ["stat_length"],
664664
"reflect": ["reflect_type"],
665665
"symmetric": ["reflect_type"],
@@ -684,11 +684,11 @@ def dpnp_pad(array, pad_width, mode="constant", **kwargs):
684684
# faster path for 1d arrays or small n-dimensional arrays
685685
return _pad_simple(array, pad_width, 0)[0]
686686

687-
# TODO: add "median": dpnp.median when dpnp.median is implemented
688687
stat_functions = {
689688
"maximum": dpnp.amax,
690689
"minimum": dpnp.amin,
691690
"mean": dpnp.mean,
691+
"median": dpnp.median,
692692
}
693693

694694
# Create array with final shape and original values

tests/test_arraypad.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TestPad:
2222
"linear_ramp": {"end_values": 0},
2323
"maximum": {"stat_length": None},
2424
"mean": {"stat_length": None},
25+
"median": {"stat_length": None},
2526
"minimum": {"stat_length": None},
2627
"reflect": {"reflect_type": "even"},
2728
"symmetric": {"reflect_type": "even"},
@@ -303,7 +304,7 @@ def test_linear_ramp_negative_diff(self, dtype, data, end_values):
303304
assert_array_equal(result, expected)
304305

305306
@pytest.mark.parametrize("pad_width", [5, (25, 20)])
306-
@pytest.mark.parametrize("mode", ["maximum", "minimum", "mean"])
307+
@pytest.mark.parametrize("mode", ["maximum", "minimum", "mean", "median"])
307308
@pytest.mark.parametrize("stat_length", [10, (2, 3)])
308309
def test_stat_func_1d(self, pad_width, mode, stat_length):
309310
a_np = numpy.arange(100)
@@ -315,7 +316,7 @@ def test_stat_func_1d(self, pad_width, mode, stat_length):
315316
assert_array_equal(result, expected)
316317

317318
@pytest.mark.parametrize("pad_width", [((1,), (2,)), ((2, 3), (3, 2))])
318-
@pytest.mark.parametrize("mode", ["maximum", "minimum", "mean"])
319+
@pytest.mark.parametrize("mode", ["maximum", "minimum", "mean", "median"])
319320
@pytest.mark.parametrize("stat_length", [(3,), (2, 3)])
320321
def test_stat_func_2d(self, pad_width, mode, stat_length):
321322
a_np = numpy.arange(30).reshape(6, 5)
@@ -326,7 +327,7 @@ def test_stat_func_2d(self, pad_width, mode, stat_length):
326327
result = dpnp.pad(a_dp, pad_width, mode=mode, stat_length=stat_length)
327328
assert_array_equal(result, expected)
328329

329-
@pytest.mark.parametrize("mode", ["mean", "minimum", "maximum"])
330+
@pytest.mark.parametrize("mode", ["mean", "minimum", "maximum", "median"])
330331
def test_same_prepend_append(self, mode):
331332
"""Test that appended and prepended values are equal"""
332333
a = dpnp.array([-1, 2, -1]) + dpnp.array(
@@ -335,14 +336,15 @@ def test_same_prepend_append(self, mode):
335336
result = dpnp.pad(a, (1, 1), mode)
336337
assert_equal(result[0], result[-1])
337338

338-
def test_mean_with_zero_stat_length(self):
339+
@pytest.mark.parametrize("mode", ["mean", "median"])
340+
def test_zero_stat_length_valid(self):
339341
a_np = numpy.array([1.0, 2.0])
340342
a_dp = dpnp.array(a_np)
341-
expected = numpy.pad(a_np, (1, 2), "mean")
342-
result = dpnp.pad(a_dp, (1, 2), "mean")
343+
expected = numpy.pad(a_np, (1, 2), mode, stat_length=0)
344+
result = dpnp.pad(a_dp, (1, 2), mode, stat_length=0)
343345
assert_array_equal(result, expected)
344346

345-
@pytest.mark.parametrize("mode", ["mean", "minimum", "maximum"])
347+
@pytest.mark.parametrize("mode", ["mean", "minimum", "maximum", "median"])
346348
@pytest.mark.parametrize(
347349
"stat_length", [-2, (-2,), (3, -1), ((5, 2), (-2, 3)), ((-4,), (2,))]
348350
)

0 commit comments

Comments
 (0)