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

Skip to content

Commit b57c39a

Browse files
committed
Deprecate mlab.{apply_window,stride_repeat}.
Altogether, they take more than 100 lines of implementation and many hundred lines of tests, for what amounts to an array multiplication optionally preceded by calling a user-supplied callable (see how apply_window is inlined in _spectral_helper; stride_repeat is strictly a helper for apply_window).
1 parent a3e2897 commit b57c39a

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Deprecations
2+
````````````
3+
4+
``mlab.apply_window`` and ``mlab.stride_repeat`` are deprecated.

lib/matplotlib/mlab.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def window_none(x):
8484
return x
8585

8686

87+
@cbook.deprecated("3.2")
8788
def apply_window(x, window, axis=0, return_window=None):
8889
'''
8990
Apply the given window to the given 1D or 2D array along the given axis.
@@ -376,6 +377,7 @@ def stride_windows(x, n, noverlap=None, axis=0):
376377
return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
377378

378379

380+
@cbook.deprecated("3.2")
379381
def stride_repeat(x, n, axis=0):
380382
'''
381383
Repeat the values in an array in a memory-efficient manner. Array x is
@@ -520,29 +522,34 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
520522
numFreqs = pad_to//2 + 1
521523
scaling_factor = 2.
522524

525+
if not np.iterable(window):
526+
window = window(np.ones(NFFT, x.dtype))
527+
if len(window) != NFFT:
528+
raise ValueError(
529+
"The window length must match the data's first dimension")
530+
523531
result = stride_windows(x, NFFT, noverlap, axis=0)
524532
result = detrend(result, detrend_func, axis=0)
525-
result, windowVals = apply_window(result, window, axis=0,
526-
return_window=True)
533+
result = result * window.reshape((-1, 1))
527534
result = np.fft.fft(result, n=pad_to, axis=0)[:numFreqs, :]
528535
freqs = np.fft.fftfreq(pad_to, 1/Fs)[:numFreqs]
529536

530537
if not same_data:
531538
# if same_data is False, mode must be 'psd'
532539
resultY = stride_windows(y, NFFT, noverlap)
533540
resultY = detrend(resultY, detrend_func, axis=0)
534-
resultY = apply_window(resultY, window, axis=0)
541+
resultY = resultY * window.reshape((-1, 1))
535542
resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :]
536543
result = np.conj(result) * resultY
537544
elif mode == 'psd':
538545
result = np.conj(result) * result
539546
elif mode == 'magnitude':
540-
result = np.abs(result) / np.abs(windowVals).sum()
547+
result = np.abs(result) / np.abs(window).sum()
541548
elif mode == 'angle' or mode == 'phase':
542549
# we unwrap the phase later to handle the onesided vs. twosided case
543550
result = np.angle(result)
544551
elif mode == 'complex':
545-
result /= np.abs(windowVals).sum()
552+
result /= np.abs(window).sum()
546553

547554
if mode == 'psd':
548555

@@ -566,10 +573,10 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
566573
result /= Fs
567574
# Scale the spectrum by the norm of the window to compensate for
568575
# windowing loss; see Bendat & Piersol Sec 11.5.2.
569-
result /= (np.abs(windowVals)**2).sum()
576+
result /= (np.abs(window)**2).sum()
570577
else:
571578
# In this case, preserve power in the segment, not amplitude
572-
result /= np.abs(windowVals).sum()**2
579+
result /= np.abs(window).sum()**2
573580

574581
t = np.arange(NFFT/2, len(x) - NFFT/2 + 1, NFFT - noverlap)/Fs
575582

lib/matplotlib/tests/test_mlab.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
'''
1919

2020

21+
def _stride_repeat(*args, **kwargs):
22+
with pytest.warns(MatplotlibDeprecationWarning):
23+
return mlab.stride_repeat(*args, **kwargs)
24+
25+
2126
class TestStride(object):
2227
def get_base(self, x):
2328
y = x
@@ -61,26 +66,26 @@ def test_stride_windows_invalid_params(self, n, noverlap):
6166
def test_stride_repeat_invalid_input_shape(self, shape):
6267
x = np.arange(np.prod(shape)).reshape(shape)
6368
with pytest.raises(ValueError):
64-
mlab.stride_repeat(x, 5)
69+
_stride_repeat(x, 5)
6570

6671
@pytest.mark.parametrize('axis', [-1, 2],
6772
ids=['axis less than 0',
6873
'axis greater than input shape'])
6974
def test_stride_repeat_invalid_axis(self, axis):
7075
x = np.array(0)
7176
with pytest.raises(ValueError):
72-
mlab.stride_repeat(x, 5, axis=axis)
77+
_stride_repeat(x, 5, axis=axis)
7378

7479
def test_stride_repeat_n_lt_1_ValueError(self):
7580
x = np.arange(10)
7681
with pytest.raises(ValueError):
77-
mlab.stride_repeat(x, 0)
82+
_stride_repeat(x, 0)
7883

7984
@pytest.mark.parametrize('axis', [0, 1], ids=['axis0', 'axis1'])
8085
@pytest.mark.parametrize('n', [1, 5], ids=['n1', 'n5'])
8186
def test_stride_repeat(self, n, axis):
8287
x = np.arange(10)
83-
y = mlab.stride_repeat(x, n, axis=axis)
88+
y = _stride_repeat(x, n, axis=axis)
8489

8590
expected_shape = [10, 10]
8691
expected_shape[axis] = n
@@ -137,7 +142,7 @@ def test_stride_ensure_integer_type(self):
137142
# even previous to #3845 could not find any problematic
138143
# configuration however, let's be sure it's not accidentally
139144
# introduced
140-
y_strided = mlab.stride_repeat(y, n=33.815)
145+
y_strided = _stride_repeat(y, n=33.815)
141146
assert_array_equal(y_strided, 0.3)
142147

143148

@@ -187,6 +192,11 @@ def test_csv2rec_dates(tempcsv, input, kwargs):
187192
assert_array_equal(array['a'].tolist(), expected)
188193

189194

195+
def _apply_window(*args, **kwargs):
196+
with pytest.warns(MatplotlibDeprecationWarning):
197+
return mlab.apply_window(*args, **kwargs)
198+
199+
190200
class TestWindow(object):
191201
def setup(self):
192202
np.random.seed(0)
@@ -238,31 +248,31 @@ def test_apply_window_1D_axis1_ValueError(self):
238248
x = self.sig_rand
239249
window = mlab.window_hanning
240250
with pytest.raises(ValueError):
241-
mlab.apply_window(x, window, axis=1, return_window=False)
251+
_apply_window(x, window, axis=1, return_window=False)
242252

243253
def test_apply_window_1D_els_wrongsize_ValueError(self):
244254
x = self.sig_rand
245255
window = mlab.window_hanning(np.ones(x.shape[0]-1))
246256
with pytest.raises(ValueError):
247-
mlab.apply_window(x, window)
257+
_apply_window(x, window)
248258

249259
def test_apply_window_0D_ValueError(self):
250260
x = np.array(0)
251261
window = mlab.window_hanning
252262
with pytest.raises(ValueError):
253-
mlab.apply_window(x, window, axis=1, return_window=False)
263+
_apply_window(x, window, axis=1, return_window=False)
254264

255265
def test_apply_window_3D_ValueError(self):
256266
x = self.sig_rand[np.newaxis][np.newaxis]
257267
window = mlab.window_hanning
258268
with pytest.raises(ValueError):
259-
mlab.apply_window(x, window, axis=1, return_window=False)
269+
_apply_window(x, window, axis=1, return_window=False)
260270

261271
def test_apply_window_hanning_1D(self):
262272
x = self.sig_rand
263273
window = mlab.window_hanning
264274
window1 = mlab.window_hanning(np.ones(x.shape[0]))
265-
y, window2 = mlab.apply_window(x, window, return_window=True)
275+
y, window2 = _apply_window(x, window, return_window=True)
266276
yt = window(x)
267277
assert yt.shape == y.shape
268278
assert x.shape == y.shape
@@ -272,7 +282,7 @@ def test_apply_window_hanning_1D(self):
272282
def test_apply_window_hanning_1D_axis0(self):
273283
x = self.sig_rand
274284
window = mlab.window_hanning
275-
y = mlab.apply_window(x, window, axis=0, return_window=False)
285+
y = _apply_window(x, window, axis=0, return_window=False)
276286
yt = window(x)
277287
assert yt.shape == y.shape
278288
assert x.shape == y.shape
@@ -282,7 +292,7 @@ def test_apply_window_hanning_els_1D_axis0(self):
282292
x = self.sig_rand
283293
window = mlab.window_hanning(np.ones(x.shape[0]))
284294
window1 = mlab.window_hanning
285-
y = mlab.apply_window(x, window, axis=0, return_window=False)
295+
y = _apply_window(x, window, axis=0, return_window=False)
286296
yt = window1(x)
287297
assert yt.shape == y.shape
288298
assert x.shape == y.shape
@@ -291,7 +301,7 @@ def test_apply_window_hanning_els_1D_axis0(self):
291301
def test_apply_window_hanning_2D_axis0(self):
292302
x = np.random.standard_normal([1000, 10]) + 100.
293303
window = mlab.window_hanning
294-
y = mlab.apply_window(x, window, axis=0, return_window=False)
304+
y = _apply_window(x, window, axis=0, return_window=False)
295305
yt = np.zeros_like(x)
296306
for i in range(x.shape[1]):
297307
yt[:, i] = window(x[:, i])
@@ -303,7 +313,7 @@ def test_apply_window_hanning_els1_2D_axis0(self):
303313
x = np.random.standard_normal([1000, 10]) + 100.
304314
window = mlab.window_hanning(np.ones(x.shape[0]))
305315
window1 = mlab.window_hanning
306-
y = mlab.apply_window(x, window, axis=0, return_window=False)
316+
y = _apply_window(x, window, axis=0, return_window=False)
307317
yt = np.zeros_like(x)
308318
for i in range(x.shape[1]):
309319
yt[:, i] = window1(x[:, i])
@@ -315,7 +325,7 @@ def test_apply_window_hanning_els2_2D_axis0(self):
315325
x = np.random.standard_normal([1000, 10]) + 100.
316326
window = mlab.window_hanning
317327
window1 = mlab.window_hanning(np.ones(x.shape[0]))
318-
y, window2 = mlab.apply_window(x, window, axis=0, return_window=True)
328+
y, window2 = _apply_window(x, window, axis=0, return_window=True)
319329
yt = np.zeros_like(x)
320330
for i in range(x.shape[1]):
321331
yt[:, i] = window1*x[:, i]
@@ -328,8 +338,8 @@ def test_apply_window_hanning_els3_2D_axis0(self):
328338
x = np.random.standard_normal([1000, 10]) + 100.
329339
window = mlab.window_hanning
330340
window1 = mlab.window_hanning(np.ones(x.shape[0]))
331-
y, window2 = mlab.apply_window(x, window, axis=0, return_window=True)
332-
yt = mlab.apply_window(x, window1, axis=0, return_window=False)
341+
y, window2 = _apply_window(x, window, axis=0, return_window=True)
342+
yt = _apply_window(x, window1, axis=0, return_window=False)
333343
assert yt.shape == y.shape
334344
assert x.shape == y.shape
335345
assert_allclose(yt, y, atol=1e-06)
@@ -338,7 +348,7 @@ def test_apply_window_hanning_els3_2D_axis0(self):
338348
def test_apply_window_hanning_2D_axis1(self):
339349
x = np.random.standard_normal([10, 1000]) + 100.
340350
window = mlab.window_hanning
341-
y = mlab.apply_window(x, window, axis=1, return_window=False)
351+
y = _apply_window(x, window, axis=1, return_window=False)
342352
yt = np.zeros_like(x)
343353
for i in range(x.shape[0]):
344354
yt[i, :] = window(x[i, :])
@@ -350,7 +360,7 @@ def test_apply_window_hanning_2D__els1_axis1(self):
350360
x = np.random.standard_normal([10, 1000]) + 100.
351361
window = mlab.window_hanning(np.ones(x.shape[1]))
352362
window1 = mlab.window_hanning
353-
y = mlab.apply_window(x, window, axis=1, return_window=False)
363+
y = _apply_window(x, window, axis=1, return_window=False)
354364
yt = np.zeros_like(x)
355365
for i in range(x.shape[0]):
356366
yt[i, :] = window1(x[i, :])
@@ -362,7 +372,7 @@ def test_apply_window_hanning_2D_els2_axis1(self):
362372
x = np.random.standard_normal([10, 1000]) + 100.
363373
window = mlab.window_hanning
364374
window1 = mlab.window_hanning(np.ones(x.shape[1]))
365-
y, window2 = mlab.apply_window(x, window, axis=1, return_window=True)
375+
y, window2 = _apply_window(x, window, axis=1, return_window=True)
366376
yt = np.zeros_like(x)
367377
for i in range(x.shape[0]):
368378
yt[i, :] = window1 * x[i, :]
@@ -375,8 +385,8 @@ def test_apply_window_hanning_2D_els3_axis1(self):
375385
x = np.random.standard_normal([10, 1000]) + 100.
376386
window = mlab.window_hanning
377387
window1 = mlab.window_hanning(np.ones(x.shape[1]))
378-
y = mlab.apply_window(x, window, axis=1, return_window=False)
379-
yt = mlab.apply_window(x, window1, axis=1, return_window=False)
388+
y = _apply_window(x, window, axis=1, return_window=False)
389+
yt = _apply_window(x, window1, axis=1, return_window=False)
380390
assert yt.shape == y.shape
381391
assert x.shape == y.shape
382392
assert_allclose(yt, y, atol=1e-06)
@@ -385,7 +395,7 @@ def test_apply_window_stride_windows_hanning_2D_n13_noverlapn3_axis0(self):
385395
x = self.sig_rand
386396
window = mlab.window_hanning
387397
yi = mlab.stride_windows(x, n=13, noverlap=2, axis=0)
388-
y = mlab.apply_window(yi, window, axis=0, return_window=False)
398+
y = _apply_window(yi, window, axis=0, return_window=False)
389399
yt = self.check_window_apply_repeat(x, window, 13, 2)
390400
assert yt.shape == y.shape
391401
assert x.shape != y.shape
@@ -395,45 +405,45 @@ def test_apply_window_hanning_2D_stack_axis1(self):
395405
ydata = np.arange(32)
396406
ydata1 = ydata+5
397407
ydata2 = ydata+3.3
398-
ycontrol1 = mlab.apply_window(ydata1, mlab.window_hanning)
408+
ycontrol1 = _apply_window(ydata1, mlab.window_hanning)
399409
ycontrol2 = mlab.window_hanning(ydata2)
400410
ydata = np.vstack([ydata1, ydata2])
401411
ycontrol = np.vstack([ycontrol1, ycontrol2])
402412
ydata = np.tile(ydata, (20, 1))
403413
ycontrol = np.tile(ycontrol, (20, 1))
404-
result = mlab.apply_window(ydata, mlab.window_hanning, axis=1,
405-
return_window=False)
414+
result = _apply_window(ydata, mlab.window_hanning, axis=1,
415+
return_window=False)
406416
assert_allclose(ycontrol, result, atol=1e-08)
407417

408418
def test_apply_window_hanning_2D_stack_windows_axis1(self):
409419
ydata = np.arange(32)
410420
ydata1 = ydata+5
411421
ydata2 = ydata+3.3
412-
ycontrol1 = mlab.apply_window(ydata1, mlab.window_hanning)
422+
ycontrol1 = _apply_window(ydata1, mlab.window_hanning)
413423
ycontrol2 = mlab.window_hanning(ydata2)
414424
ydata = np.vstack([ydata1, ydata2])
415425
ycontrol = np.vstack([ycontrol1, ycontrol2])
416426
ydata = np.tile(ydata, (20, 1))
417427
ycontrol = np.tile(ycontrol, (20, 1))
418-
result = mlab.apply_window(ydata, mlab.window_hanning, axis=1,
419-
return_window=False)
428+
result = _apply_window(ydata, mlab.window_hanning, axis=1,
429+
return_window=False)
420430
assert_allclose(ycontrol, result, atol=1e-08)
421431

422432
def test_apply_window_hanning_2D_stack_windows_axis1_unflatten(self):
423433
n = 32
424434
ydata = np.arange(n)
425435
ydata1 = ydata+5
426436
ydata2 = ydata+3.3
427-
ycontrol1 = mlab.apply_window(ydata1, mlab.window_hanning)
437+
ycontrol1 = _apply_window(ydata1, mlab.window_hanning)
428438
ycontrol2 = mlab.window_hanning(ydata2)
429439
ydata = np.vstack([ydata1, ydata2])
430440
ycontrol = np.vstack([ycontrol1, ycontrol2])
431441
ydata = np.tile(ydata, (20, 1))
432442
ycontrol = np.tile(ycontrol, (20, 1))
433443
ydata = ydata.flatten()
434444
ydata1 = mlab.stride_windows(ydata, 32, noverlap=0, axis=0)
435-
result = mlab.apply_window(ydata1, mlab.window_hanning, axis=0,
436-
return_window=False)
445+
result = _apply_window(ydata1, mlab.window_hanning, axis=0,
446+
return_window=False)
437447
assert_allclose(ycontrol.T, result, atol=1e-08)
438448

439449

@@ -1546,9 +1556,9 @@ def test_psd_window_hanning(self):
15461556
ydata = np.arange(self.NFFT_density)
15471557
ydata1 = ydata+5
15481558
ydata2 = ydata+3.3
1549-
ycontrol1, windowVals = mlab.apply_window(ydata1,
1550-
mlab.window_hanning,
1551-
return_window=True)
1559+
ycontrol1, windowVals = _apply_window(ydata1,
1560+
mlab.window_hanning,
1561+
return_window=True)
15521562
ycontrol2 = mlab.window_hanning(ydata2)
15531563
ydata = np.vstack([ydata1, ydata2])
15541564
ycontrol = np.vstack([ycontrol1, ycontrol2])
@@ -1593,9 +1603,9 @@ def test_psd_window_hanning_detrend_linear(self):
15931603
ydata2 = ydata+3.3
15941604
ycontrol1 = ycontrol
15951605
ycontrol2 = ycontrol
1596-
ycontrol1, windowVals = mlab.apply_window(ycontrol1,
1597-
mlab.window_hanning,
1598-
return_window=True)
1606+
ycontrol1, windowVals = _apply_window(ycontrol1,
1607+
mlab.window_hanning,
1608+
return_window=True)
15991609
ycontrol2 = mlab.window_hanning(ycontrol2)
16001610
ydata = np.vstack([ydata1, ydata2])
16011611
ycontrol = np.vstack([ycontrol1, ycontrol2])

0 commit comments

Comments
 (0)