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

Skip to content

Use old stride_windows implementation on 32-bit builds #29115

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
41 changes: 37 additions & 4 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import functools
from numbers import Number
import sys

import numpy as np

Expand Down Expand Up @@ -210,6 +211,30 @@
return y - (b*x + a)


def _stride_windows(x, n, noverlap=0):
if noverlap >= n:
raise ValueError('noverlap must be less than n')

Check warning on line 216 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L216

Added line #L216 was not covered by tests
if n < 1:
raise ValueError('n cannot be less than 1')

Check warning on line 218 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L218

Added line #L218 was not covered by tests

x = np.asarray(x)

Check warning on line 220 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L220

Added line #L220 was not covered by tests

if n == 1 and noverlap == 0:
return x[np.newaxis]

Check warning on line 223 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L223

Added line #L223 was not covered by tests
if n > x.size:
raise ValueError('n cannot be greater than the length of x')

Check warning on line 225 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L225

Added line #L225 was not covered by tests

# np.lib.stride_tricks.as_strided easily leads to memory corruption for
# non integer shape and strides, i.e. noverlap or n. See #3845.
noverlap = int(noverlap)
n = int(n)

Check warning on line 230 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L229-L230

Added lines #L229 - L230 were not covered by tests

step = n - noverlap
shape = (n, (x.shape[-1]-noverlap)//step)
strides = (x.strides[0], step*x.strides[0])
return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)

Check warning on line 235 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L232-L235

Added lines #L232 - L235 were not covered by tests


def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
window=None, noverlap=None, pad_to=None,
sides=None, scale_by_freq=None, mode=None):
Expand Down Expand Up @@ -304,17 +329,25 @@
raise ValueError(
"The window length must match the data's first dimension")

result = np.lib.stride_tricks.sliding_window_view(
x, NFFT, axis=0)[::NFFT - noverlap].T
if sys.maxsize > 2**32:
result = np.lib.stride_tricks.sliding_window_view(
x, NFFT, axis=0)[::NFFT - noverlap].T
else:
# The NumPy version on 32-bit will OOM, so use old implementation.
result = _stride_windows(x, NFFT, noverlap=noverlap)

Check warning on line 337 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L337

Added line #L337 was not covered by tests
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 = np.lib.stride_tricks.sliding_window_view(
y, NFFT, axis=0)[::NFFT - noverlap].T
if sys.maxsize > 2**32:
resultY = np.lib.stride_tricks.sliding_window_view(
y, NFFT, axis=0)[::NFFT - noverlap].T
else:
# The NumPy version on 32-bit will OOM, so use old implementation.
resultY = _stride_windows(y, NFFT, noverlap=noverlap)

Check warning on line 350 in lib/matplotlib/mlab.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/mlab.py#L350

Added line #L350 was not covered by tests
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
Loading