8000 Use old stride_windows implementation on 32-bit builds · matplotlib/matplotlib@f31ba35 · GitHub
[go: up one dir, main page]

Skip to content

Commit f31ba35

Browse files
committed
Use old stride_windows implementation on 32-bit builds
This was originally for i686 on Fedora, but is now applicable to WASM, which is 32-bit. The older implementation doesn't OOM.
1 parent b6777b1 commit f31ba35

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

lib/matplotlib/mlab.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
"""
4949

5050
import functools
51-
from numbers import Number
51+
from numbers import Integral, Number
52+
import sys
5253

5354
import numpy as np
5455

@@ -210,6 +211,23 @@ def detrend_linear(y):
210211
return y - (b*x + a)
211212

212213

214+
def _stride_windows(x, n, noverlap=0):
215+
x = np.asarray(x)
216+
217+
_api.check_isinstance(Integral, n=n, noverlap=noverlap)
218+
if not (1 <= n <= x.size and n < noverlap):
219+
raise ValueError(f'n ({n}) and noverlap ({noverlap}) must be positive integers '
220+
f'with n < noverlap and n <= x.size ({x.size})')
221+
222+
if n == 1 and noverlap == 0:
223+
return x[np.newaxis]
224+
225+
step = n - noverlap
226+
shape = (n, (x.shape[-1]-noverlap)//step)
227+
strides = (x.strides[0], step*x.strides[0])
228+
return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
229+
230+
213231
def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
214232
window=None, noverlap=None, pad_to=None,
215233
sides=None, scale_by_freq=None, mode=None):
@@ -304,17 +322,25 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
304322
raise ValueError(
305323
"The window length must match the data's first dimension")
306324

307-
result = np.lib.stride_tricks.sliding_window_view(
308-
x, NFFT, axis=0)[::NFFT - noverlap].T
325+
if sys.maxsize > 2**32:
326+
result = np.lib.stride_tricks.sliding_window_view(
327+
x, NFFT, axis=0)[::NFFT - noverlap].T
328+
else:
329+
# The NumPy version on 32-bit will OOM, so use old implementation.
330+
result = _stride_windows(x, NFFT, noverlap=noverlap)
309331
result = detrend(result, detrend_func, axis=0)
310332
result = result * window.reshape((-1, 1))
311333
result = np.fft.fft(result, n=pad_to, axis=0)[:numFreqs, :]
312334
freqs = np.fft.fftfreq(pad_to, 1/Fs)[:numFreqs]
313335

314336
if not same_data:
315337
# if same_data is False, mode must be 'psd'
316-
resultY = np.lib.stride_tricks.sliding_window_view(
317-
y, NFFT, axis=0)[::NFFT - noverlap].T
338+
if sys.maxsize > 2**32:
339+
resultY = np.lib.stride_tricks.sliding_window_view(
340+
y, NFFT, axis=0)[::NFFT - noverlap].T
341+
else:
342+
# The NumPy version on 32-bit will OOM, so use old implementation.
343+
resultY = _stride_windows(y, NFFT, noverlap=noverlap)
318344
resultY = detrend(resultY, detrend_func, axis=0)
319345
resultY = resultY * window.reshape((-1, 1))
320346
resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :]

0 commit comments

Comments
 (0)
0