Skip to content

Deprecate mlab.stride_windows. #21190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/next_api_changes/deprecations/ZZZZZ-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
``mlab.stride_windows``
~~~~~~~~~~~~~~~~~~~~~~~
... is deprecated. Use ``np.lib.stride_tricks.sliding_window_view`` instead
(or ``np.lib.stride_tricks.as_strided`` on numpy<1.20).
19 changes: 15 additions & 4 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ def detrend_linear(y):
return y - (b*x + a)


@_api.deprecated("3.6")
def stride_windows(x, n, noverlap=None, axis=0):
"""
Get all windows of x with length n as a single array,
Expand Down Expand Up @@ -246,6 +247,18 @@ def stride_windows(x, n, noverlap=None, axis=0):
"""
if noverlap is None:
noverlap = 0
if np.ndim(x) != 1:
raise ValueError('only 1-dimensional arrays can be used')
return _stride_windows(x, n, noverlap, axis)


def _stride_windows(x, n, noverlap=0, axis=0):
# np>=1.20 provides sliding_window_view, and we only ever use axis=0.
if hasattr(np.lib.stride_tricks, "sliding_window_view") and axis == 0:
if noverlap >= n:
raise ValueError('noverlap must be less than n')
return np.lib.stride_tricks.sliding_window_view(
x, n, axis=0)[::n - noverlap].T

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

x = np.asarray(x)

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

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

if not same_data:
# if same_data is False, mode must be 'psd'
resultY = stride_windows(y, NFFT, noverlap)
resultY = _stride_windows(y, NFFT, noverlap)
resultY = detrend(resultY, detrend_func, axis=0)
resultY = resultY * window.reshape((-1, 1))
resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :]
Expand Down
7 changes: 6 additions & 1 deletion lib/matplotlib/tests/test_mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

import matplotlib.mlab as mlab
from matplotlib import mlab, _api


class TestStride:
Expand All @@ -13,6 +13,11 @@ def get_base(self, x):
y = y.base
return y

@pytest.fixture(autouse=True)
def stride_is_deprecated(self):
with _api.suppress_matplotlib_deprecation_warning():
yield

def calc_window_target(self, x, NFFT, noverlap=0, axis=0):
"""
This is an adaptation of the original window extraction algorithm.
Expand Down