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

Commit6c2f514

Browse files
authored
Merge pull request#21190 from anntzer/sw
Deprecate mlab.stride_windows.
2 parents5f44c48 +47fb5e8 commit6c2f514

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed
Lines changed: 4 additions & 0 deletions
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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def detrend_linear(y):
215215
returny- (b*x+a)
216216

217217

218+
@_api.deprecated("3.6")
218219
defstride_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
ifnoverlapisNone:
248249
noverlap=0
250+
ifnp.ndim(x)!=1:
251+
raiseValueError('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+
ifhasattr(np.lib.stride_tricks,"sliding_window_view")andaxis==0:
258+
ifnoverlap>=n:
259+
raiseValueError('noverlap must be less than n')
260+
returnnp.lib.stride_tricks.sliding_window_view(
261+
x,n,axis=0)[::n-noverlap].T
249262

250263
ifnoverlap>=n:
251264
raiseValueError('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-
ifx.ndim!=1:
258-
raiseValueError('only 1-dimensional arrays can be used')
259270
ifn==1andnoverlap==0:
260271
ifaxis==0:
261272
returnx[np.newaxis]
@@ -370,15 +381,15 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
370381
raiseValueError(
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
ifnotsame_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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
importnumpyasnp
44
importpytest
55

6-
importmatplotlib.mlabasmlab
6+
frommatplotlibimportmlab,_api
77

88

99
classTestStride:
@@ -13,6 +13,11 @@ def get_base(self, x):
1313
y=y.base
1414
returny
1515

16+
@pytest.fixture(autouse=True)
17+
defstride_is_deprecated(self):
18+
with_api.suppress_matplotlib_deprecation_warning():
19+
yield
20+
1621
defcalc_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)

[8]ページ先頭

©2009-2025 Movatter.jp