Skip to content

Commit f2c0182

Browse files
authored
Merge pull request #21190 from anntzer/sw
Deprecate mlab.stride_windows.
2 parents fc0ee10 + a507a4d commit f2c0182

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``mlab.stride_windows``
2+
~~~~~~~~~~~~~~~~~~~~~~~
3+
... is deprecated. Use ``np.lib.stride_tricks.sliding_window_view`` instead
4+
(or ``np.lib.stride_tricks.as_strided`` on numpy<1.20).

lib/matplotlib/mlab.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def detrend_linear(y):
215215
return y - (b*x + a)
216216

217217

218+
@_api.deprecated("3.6")
218219
def stride_windows(x, n, noverlap=None, axis=0):
219220
"""
220221
Get all windows of x with length n as a single array,
@@ -246,6 +247,18 @@ def stride_windows(x, n, noverlap=None, axis=0):
246247
"""
247248
if noverlap is None:
248249
noverlap = 0
250+
if np.ndim(x) != 1:
251+
raise ValueError('only 1-dimensional arrays can be used')
252+
return _stride_windows(x, n, noverlap, axis)
253+
254+
255+
def _stride_windows(x, n, noverlap=0, axis=0):
256+
# np>=1.20 provides sliding_window_view, and we only ever use axis=0.
257+
if hasattr(np.lib.stride_tricks, "sliding_window_view") and axis == 0:
258+
if noverlap >= n:
259+
raise ValueError('noverlap must be less than n')
260+
return np.lib.stride_tricks.sliding_window_view(
261+
x, n, axis=0)[::n - noverlap].T
249262

250263
if noverlap >= n:
251264
raise ValueError('noverlap must be less than n')
@@ -254,8 +267,6 @@ def stride_windows(x, n, noverlap=None, axis=0):
254267

255268
x = np.asarray(x)
256269

257-
if x.ndim != 1:
258-
raise ValueError('only 1-dimensional arrays can be used')
259270
if n == 1 and noverlap == 0:
260271
if axis == 0:
261272
return x[np.newaxis]
@@ -370,15 +381,15 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
370381
raise ValueError(
371382
"The window length must match the data's first dimension")
372383

373-
result = stride_windows(x, NFFT, noverlap, axis=0)
384+
result = _stride_windows(x, NFFT, noverlap)
374385
result = detrend(result, detrend_func, axis=0)
375386
result = result * window.reshape((-1, 1))
376387
result = np.fft.fft(result, n=pad_to, axis=0)[:numFreqs, :]
377388
freqs = np.fft.fftfreq(pad_to, 1/Fs)[:numFreqs]
378389

379390
if not same_data:
380391
# if same_data is False, mode must be 'psd'
381-
resultY = stride_windows(y, NFFT, noverlap)
392+
resultY = _stride_windows(y, NFFT, noverlap)
382393
resultY = detrend(resultY, detrend_func, axis=0)
383394
resultY = resultY * window.reshape((-1, 1))
384395
resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :]

lib/matplotlib/tests/test_mlab.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import numpy as np
44
import pytest
55

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

88

99
class TestStride:
@@ -13,6 +13,11 @@ 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+
1621
def calc_window_target(self, x, NFFT, noverlap=0, axis=0):
1722
"""
1823
This is an adaptation of the original window extraction algorithm.

0 commit comments

Comments
 (0)