Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7e1a12e

Browse files
committedNov 30, 2024·
Use old stride_windows implementation on 32-bit x86
1 parent e90a162 commit 7e1a12e

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed
 

‎lib/matplotlib/mlab.py

+35-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import functools
5151
from numbers import Number
52+
import sys
5253

5354
import numpy as np
5455

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

212213

214+
def _stride_windows(x, n, noverlap=0):
215+
if noverlap >= n:
216+
raise ValueError('noverlap must be less than n')
217+
if n < 1:
218+
raise ValueError('n cannot be less than 1')
219+
220+
x = np.asarray(x)
221+
222+
if n == 1 and noverlap == 0:
223+
return x[np.newaxis]
224+
if n > x.size:
225+
raise ValueError('n cannot be greater than the length of x')
226+
227+
# np.lib.stride_tricks.as_strided easily leads to memory corruption for
228+
# non integer shape and strides, i.e. noverlap or n. See #3845.
229+
noverlap = int(noverlap)
230+
n = int(n)
231+
232+
step = n - noverlap
233+
shape = (n, (x.shape[-1]-noverlap)//step)
234+
strides = (x.strides[0], step*x.strides[0])
235+
return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
236+
237+
213238
def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
214239
window=None, noverlap=None, pad_to=None,
215240
sides=None, scale_by_freq=None, mode=None):
@@ -304,17 +329,23 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
304329
raise ValueError(
305330
"The window length must match the data's first dimension")
306331

307-
result = np.lib.stride_tricks.sliding_window_view(
308-
x, NFFT, axis=0)[::NFFT - noverlap].T
332+
if sys.maxsize > 2**32: # NumPy version on 32-bit OOMs.
333+
result = np.lib.stride_tricks.sliding_window_view(
334+
x, NFFT, axis=0)[::NFFT - noverlap].T
335+
else:
336+
result = _stride_windows(x, NFFT, noverlap=noverlap)
309337
result = detrend(result, detrend_func, axis=0)
310338
result = result * window.reshape((-1, 1))
311339
result = np.fft.fft(result, n=pad_to, axis=0)[:numFreqs, :]
312340
freqs = np.fft.fftfreq(pad_to, 1/Fs)[:numFreqs]
313341

314342
if not same_data:
315343
# 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
344+
if sys.maxsize > 2**32: # NumPy version on 32-bit OOMs.
345+
resultY = np.lib.stride_tricks.sliding_window_view(
346+
y, NFFT, axis=0)[::NFFT - noverlap].T
347+
else:
348+
resultY = _stride_windows(y, NFFT, noverlap=noverlap)
318349
resultY = detrend(resultY, detrend_func, axis=0)
319350
resultY = resultY * window.reshape((-1, 1))
320351
resultY = np.fft.fft(resultY, n=pad_to, axis=0)[:numFreqs, :]

0 commit comments

Comments
 (0)
Please sign in to comment.