Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dstansby merged 1 commit intomatplotlib:masterfromanntzer:sw
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsdoc/api/next_api_changes/deprecations/ZZZZZ-AL.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/matplotlib/mlab.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionlib/matplotlib/tests/test_mlab.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp