Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 12f8111

Browse files
committed
Remove stride_windows
1 parent e3d2eb7 commit 12f8111

File tree

2 files changed

+16
-75
lines changed

2 files changed

+16
-75
lines changed

lib/matplotlib/mlab.py

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ def detrend_linear(y):
213213
return y - (b*x + a)
214214

215215

216-
@_api.deprecated("3.6")
217-
def stride_windows(x, n, noverlap=None, axis=0):
216+
def _stride_windows(x, n, noverlap=0):
218217
"""
219218
Get all windows of *x* with length *n* as a single array,
220219
using strides to avoid data duplication.
@@ -233,8 +232,6 @@ def stride_windows(x, n, noverlap=None, axis=0):
233232
The number of data points in each window.
234233
noverlap : int, default: 0 (no overlap)
235234
The overlap between adjacent windows.
236-
axis : int
237-
The axis along which the windows will run.
238235
239236
References
240237
----------
@@ -243,49 +240,10 @@ def stride_windows(x, n, noverlap=None, axis=0):
243240
`stackoverflow: Using strides for an efficient moving average filter
244241
<https://stackoverflow.com/a/4947453>`_
245242
"""
246-
if noverlap is None:
247-
noverlap = 0
248-
if np.ndim(x) != 1:
249-
raise ValueError('only 1-dimensional arrays can be used')
250-
return _stride_windows(x, n, noverlap, axis)
251-
252-
253-
def _stride_windows(x, n, noverlap=0, axis=0):
254-
# np>=1.20 provides sliding_window_view, and we only ever use axis=0.
255-
if hasattr(np.lib.stride_tricks, "sliding_window_view") and axis == 0:
256-
if noverlap >= n:
257-
raise ValueError('noverlap must be less than n')
258-
return np.lib.stride_tricks.sliding_window_view(
259-
x, n, axis=0)[::n - noverlap].T
260-
261243
if noverlap >= n:
262244
raise ValueError('noverlap must be less than n')
263-
if n < 1:
264-
raise ValueError('n cannot be less than 1')
265-
266-
x = np.asarray(x)
267-
268-
if n == 1 and noverlap == 0:
269-
if axis == 0:
270-
return x[np.newaxis]
271-
else:
272-
return x[np.newaxis].T
273-
if n > x.size:
274-
raise ValueError('n cannot be greater than the length of x')
275-
276-
# np.lib.stride_tricks.as_strided easily leads to memory corruption for
277-
# non integer shape and strides, i.e. noverlap or n. See #3845.
278-
noverlap = int(noverlap)
279-
n = int(n)
280-
281-
step = n - noverlap
282-
if axis == 0:
283-
shape = (n, (x.shape[-1]-noverlap)//step)
284-
strides = (x.strides[0], step*x.strides[0])
285-
else:
286-
shape = ((x.shape[-1]-noverlap)//step, n)
287-
strides = (step*x.strides[0], x.strides[0])
288-
return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
245+
return np.lib.stride_tricks.sliding_window_view(
246+
x, n, axis=0)[::n - noverlap].T
289247

290248

291249
def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,

lib/matplotlib/tests/test_mlab.py

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

6-
from matplotlib import mlab, _api
6+
from matplotlib import mlab
77

88

99
class TestStride:
@@ -13,12 +13,7 @@ def get_base(self, x):
1313
y = y.base
1414
return y
1515

16-
@pytest.fixture(autouse=True)
17-
def stride_is_deprecated(self):
18-
with _api.suppress_matplotlib_deprecation_warning():
19-
yield
20-
21-
def calc_window_target(self, x, NFFT, noverlap=0, axis=0):
16+
def calc_window_target(self, x, NFFT, noverlap=0):
2217
"""
2318
This is an adaptation of the original window extraction algorithm.
2419
This is here to test to make sure the new implementation has the same
@@ -32,55 +27,43 @@ def calc_window_target(self, x, NFFT, noverlap=0, axis=0):
3227
# do the ffts of the slices
3328
for i in range(n):
3429
result[:, i] = x[ind[i]:ind[i]+NFFT]
35-
if axis == 1:
36-
result = result.T
3730
return result
3831

39-
@pytest.mark.parametrize('shape', [(), (10, 1)], ids=['0D', '2D'])
40-
def test_stride_windows_invalid_input_shape(self, shape):
41-
x = np.arange(np.prod(shape)).reshape(shape)
42-
with pytest.raises(ValueError):
43-
mlab.stride_windows(x, 5)
44-
4532
@pytest.mark.parametrize('n, noverlap',
46-
[(0, None), (11, None), (2, 2), (2, 3)],
47-
ids=['n less than 1', 'n greater than input',
48-
'noverlap greater than n',
33+
[(2, 2), (2, 3)],
34+
ids=['noverlap greater than n',
4935
'noverlap equal to n'])
5036
def test_stride_windows_invalid_params(self, n, noverlap):
5137
x = np.arange(10)
5238
with pytest.raises(ValueError):
53-
mlab.stride_windows(x, n, noverlap)
39+
mlab._stride_windows(x, n, noverlap)
5440

55-
@pytest.mark.parametrize('axis', [0, 1], ids=['axis0', 'axis1'])
5641
@pytest.mark.parametrize('n, noverlap',
5742
[(1, 0), (5, 0), (15, 2), (13, -3)],
5843
ids=['n1-noverlap0', 'n5-noverlap0',
5944
'n15-noverlap2', 'n13-noverlapn3'])
60-
def test_stride_windows(self, n, noverlap, axis):
45+
def test_stride_windows(self, n, noverlap):
6146
x = np.arange(100)
62-
y = mlab.stride_windows(x, n, noverlap=noverlap, axis=axis)
47+
y = mlab._stride_windows(x, n, noverlap=noverlap)
6348

6449
expected_shape = [0, 0]
65-
expected_shape[axis] = n
66-
expected_shape[1 - axis] = 100 // (n - noverlap)
67-
yt = self.calc_window_target(x, n, noverlap=noverlap, axis=axis)
50+
expected_shape[0] = n
51+
expected_shape[1] = 100 // (n - noverlap)
52+
yt = self.calc_window_target(x, n, noverlap=noverlap)
6853

6954
assert yt.shape == y.shape
7055
assert_array_equal(yt, y)
7156
assert tuple(expected_shape) == y.shape
7257
assert self.get_base(y) is x
7358

74-
@pytest.mark.parametrize('axis', [0, 1], ids=['axis0', 'axis1'])
75-
def test_stride_windows_n32_noverlap0_unflatten(self, axis):
59+
def test_stride_windows_n32_noverlap0_unflatten(self):
7660
n = 32
7761
x = np.arange(n)[np.newaxis]
7862
x1 = np.tile(x, (21, 1))
7963
x2 = x1.flatten()
80-
y = mlab.stride_windows(x2, n, axis=axis)
64+
y = mlab._stride_windows(x2, n)
8165

82-
if axis == 0:
83-
x1 = x1.T
66+
x1 = x1.T
8467
assert y.shape == x1.shape
8568
assert_array_equal(y, x1)
8669

0 commit comments

Comments
 (0)