Skip to content

Commit 5678c0b

Browse files
lagrucharris
authored andcommitted
BUG: Replace _linear_ramp with linspace
linspace supports non-scalar values since version 1.16. This can replace the former _linear_ramp method. This removes a bug in the old implementation where certain edge and end values resulted in an integer underflow for unsigned dtypes (see numpygh-14191). Protect against this regression with a new test for all numeric dtypes.
1 parent 18cbe82 commit 5678c0b

2 files changed

Lines changed: 41 additions & 76 deletions

File tree

‎numpy/lib/arraypad.py‎

Lines changed: 16 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -17,66 +17,6 @@
1717
# Private utility functions.
1818

1919

20-
def _linear_ramp(ndim, axis, start, stop, size, reverse=False):
21-
"""
22-
Create a linear ramp of `size` in `axis` with `ndim`.
23-
24-
This algorithm behaves like a vectorized version of `numpy.linspace`.
25-
The resulting linear ramp is broadcastable to any array that matches the
26-
ramp in `shape[axis]` and `ndim`.
27-
28-
Parameters
29-
----------
30-
ndim : int
31-
Number of dimensions of the resulting array. All dimensions except
32-
the one specified by `axis` will have the size 1.
33-
axis : int
34-
The dimension that contains the linear ramp of `size`.
35-
start : int or ndarray
36-
The starting value(s) of the linear ramp. If given as an array, its
37-
size must match `size`.
38-
stop : int or ndarray
39-
The stop value(s) (not included!) of the linear ramp. If given as an
40-
array, its size must match `size`.
41-
size : int
42-
The number of elements in the linear ramp. If this argument is 0 the
43-
dimensions of `ramp` will all be of length 1 except for the one given
44-
by `axis` which will be 0.
45-
reverse : bool
46-
If False, increment in a positive fashion, otherwise decrement.
47-
48-
Returns
49-
-------
50-
ramp : ndarray
51-
Output array of dtype np.float64 that in- or decrements along the given
52-
`axis`.
53-
54-
Examples
55-
--------
56-
>>> _linear_ramp(ndim=2, axis=0, start=np.arange(3), stop=10, size=2)
57-
array([[0. , 1. , 2. ],
58-
[5. , 5.5, 6. ]])
59-
>>> _linear_ramp(ndim=3, axis=0, start=2, stop=0, size=0)
60-
array([], shape=(0, 1, 1), dtype=float64)
61-
"""
62-
# Create initial ramp
63-
ramp = np.arange(size, dtype=np.float64)
64-
if reverse:
65-
ramp = ramp[::-1]
66-
67-
# Make sure, that ramp is broadcastable
68-
init_shape = (1,) * axis + (size,) + (1,) * (ndim - axis - 1)
69-
ramp = ramp.reshape(init_shape)
70-
71-
if size != 0:
72-
# And scale to given start and stop values
73-
gain = (stop - start) / float(size)
74-
ramp = ramp * gain
75-
ramp += start
76-
77-
return ramp
78-
79-
8020
def _round_if_needed(arr, dtype):
8121
"""
8222
Rounds arr inplace if destination dtype is integer.
@@ -269,17 +209,25 @@ def _get_linear_ramps(padded, axis, width_pair, end_value_pair):
269209
"""
270210
edge_pair = _get_edges(padded, axis, width_pair)
271211

272-
left_ramp = _linear_ramp(
273-
padded.ndim, axis, start=end_value_pair[0], stop=edge_pair[0],
274-
size=width_pair[0], reverse=False
212+
left_ramp = np.linspace(
213+
start=end_value_pair[0],
214+
stop=edge_pair[0].squeeze(axis), # Dimensions is replaced by linspace
215+
num=width_pair[0],
216+
endpoint=False,
217+
dtype=padded.dtype,
218+
axis=axis,
275219
)
276-
_round_if_needed(left_ramp, padded.dtype)
277220

278-
right_ramp = _linear_ramp(
279-
padded.ndim, axis, start=end_value_pair[1], stop=edge_pair[1],
280-
size=width_pair[1], reverse=True
221+
right_ramp = np.linspace(
222+
start=end_value_pair[1],
223+
stop=edge_pair[1].squeeze(axis), # Dimension is replaced by linspace
224+
num=width_pair[1],
225+
endpoint=False,
226+
dtype=padded.dtype,
227+
axis=axis,
281228
)
282-
_round_if_needed(right_ramp, padded.dtype)
229+
# Reverse linear space in appropriate dimension
230+
right_ramp = right_ramp[_slice_at_axis(slice(None, None, -1), axis)]
283231

284232
return left_ramp, right_ramp
285233

‎numpy/lib/tests/test_arraypad.py‎

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
"""
44
from __future__ import division, absolute_import, print_function
5-
from itertools import chain
65

76
import pytest
87

@@ -11,6 +10,12 @@
1110
from numpy.lib.arraypad import _as_pairs
1211

1312

13+
_numeric_dtypes = (
14+
np.sctypes["uint"]
15+
+ np.sctypes["int"]
16+
+ np.sctypes["float"]
17+
+ np.sctypes["complex"]
18+
)
1419
_all_modes = {
1520
'constant': {'constant_values': 0},
1621
'edge': {},
@@ -715,6 +720,24 @@ def test_end_values(self):
715720
assert_equal(a[0, :], 0.)
716721
assert_equal(a[-1, :], 0.)
717722

723+
@pytest.mark.parametrize("dtype", _numeric_dtypes)
724+
def test_negative_difference(self, dtype):
725+
"""
726+
Check correct behavior of unsigned dtypes if there is a negative
727+
difference between the edge to pad and `end_values`. Check both cases
728+
to be independent of implementation. Test behavior for all other dtypes
729+
in case dtype casting interferes with complex dtypes. See gh-14191.
730+
"""
731+
x = np.array([3], dtype=dtype)
732+
result = np.pad(x, 3, mode="linear_ramp", end_values=0)
733+
expected = np.array([0, 1, 2, 3, 2, 1, 0], dtype=dtype)
734+
assert_equal(result, expected)
735+
736+
x = np.array([0], dtype=dtype)
737+
result = np.pad(x, 3, mode="linear_ramp", end_values=3)
738+
expected = np.array([3, 2, 1, 0, 1, 2, 3], dtype=dtype)
739+
assert_equal(result, expected)
740+
718741

719742
class TestReflect(object):
720743
def test_check_simple(self):
@@ -1307,13 +1330,7 @@ def test_memory_layout_persistence(mode):
13071330
assert np.pad(x, 5, mode).flags["F_CONTIGUOUS"]
13081331

13091332

1310-
@pytest.mark.parametrize("dtype", chain(
1311-
# Skip "other" dtypes as they are not supported by all modes
1312-
np.sctypes["int"],
1313-
np.sctypes["uint"],
1314-
np.sctypes["float"],
1315-
np.sctypes["complex"]
1316-
))
1333+
@pytest.mark.parametrize("dtype", _numeric_dtypes)
13171334
@pytest.mark.parametrize("mode", _all_modes.keys())
13181335
def test_dtype_persistence(dtype, mode):
13191336
arr = np.zeros((3, 2, 1), dtype=dtype)

0 commit comments

Comments
 (0)