From a5c273b130f36e203a4ccc71fb1a81b071319bbc Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Tue, 8 Jul 2025 00:30:51 -0400 Subject: [PATCH] Fix mlab fallback for 32-bit systems Unfortunately, I applied the change from https://github.com/matplotlib/matplotlib/pull/29115#discussion_r2189746839 directly without noticing the typo, or running full tests. So fix the swapped condition, and add a test (for `csd` only, which should be enough since everything goes though `_spectral_helper`.) --- lib/matplotlib/mlab.py | 12 +++++------- lib/matplotlib/tests/test_mlab.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index b4b4c3f96828..5e5987706014 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -215,12 +215,10 @@ def _stride_windows(x, n, noverlap=0): x = np.asarray(x) _api.check_isinstance(Integral, n=n, noverlap=noverlap) - if not (1 <= n <= x.size and n < noverlap): - raise ValueError(f'n ({n}) and noverlap ({noverlap}) must be positive integers ' - f'with n < noverlap and n <= x.size ({x.size})') - - if n == 1 and noverlap == 0: - return x[np.newaxis] + if not (0 <= noverlap < n <= x.size): + raise ValueError( + f'n ({n}), noverlap ({noverlap}), and x.size ({x.size}) must satisfy ' + f'0 <= noverlap < n <= x.size') step = n - noverlap shape = (n, (x.shape[-1]-noverlap)//step) @@ -257,7 +255,7 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None, if NFFT is None: NFFT = 256 - if noverlap >= NFFT: + if not (0 <= noverlap < NFFT): raise ValueError('noverlap must be less than NFFT') if mode is None or mode == 'default': diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 3b0d2529b5f1..109a6d542450 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -1,3 +1,5 @@ +import sys + from numpy.testing import (assert_allclose, assert_almost_equal, assert_array_equal, assert_array_almost_equal_nulp) import numpy as np @@ -429,7 +431,16 @@ def test_spectral_helper_psd(self, mode, case): assert spec.shape[0] == freqs.shape[0] assert spec.shape[1] == getattr(self, f"t_{case}").shape[0] - def test_csd(self): + @pytest.mark.parametrize('bitsize', [ + pytest.param(None, id='default'), + pytest.param(32, + marks=pytest.mark.skipif(sys.maxsize <= 2**32, + reason='System is already 32-bit'), + id='32-bit') + ]) + def test_csd(self, bitsize, monkeypatch): + if bitsize is not None: + monkeypatch.setattr(sys, 'maxsize', 2**bitsize) freqs = self.freqs_density spec, fsp = mlab.csd(x=self.y, y=self.y+1, NFFT=self.NFFT_density,