From 41f946e43c7ceab73a269125c91cd5e6a7ee45ce Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Wed, 27 Apr 2022 12:22:58 -0700 Subject: [PATCH 1/3] MNT: deprecate mlab .... except for GaussianKDE, which is needed for violinplots --- lib/matplotlib/axes/_axes.py | 27 ++++++++++++++++++++++----- lib/matplotlib/mlab.py | 17 ++++++++++++----- lib/matplotlib/pyplot.py | 4 ++-- lib/matplotlib/tests/test_axes.py | 18 ++++++++++++++++++ lib/matplotlib/tests/test_mlab.py | 4 ++++ 5 files changed, 58 insertions(+), 12 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 0c80d87d7c22..7698a9b9982b 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -1903,7 +1903,7 @@ def acorr(self, x, **kwargs): ---------- x : array-like - detrend : callable, default: `.mlab.detrend_none` (no detrending) + detrend : callable, default: no detrending A detrending function applied to *x*. It must have the signature :: @@ -1967,7 +1967,7 @@ def acorr(self, x, **kwargs): return self.xcorr(x, x, **kwargs) @_preprocess_data(replace_names=["x", "y"], label_namer="y") - def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none, + def xcorr(self, x, y, normed=True, detrend=None, usevlines=True, maxlags=10, **kwargs): r""" Plot the cross correlation between *x* and *y*. @@ -1980,7 +1980,7 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none, ---------- x, y : array-like of length n - detrend : callable, default: `.mlab.detrend_none` (no detrending) + detrend : callable, default: no detrending A detrending function applied to *x* and *y*. It must have the signature :: @@ -2045,8 +2045,9 @@ def xcorr(self, x, y, normed=True, detrend=mlab.detrend_none, if Nx != len(y): raise ValueError('x and y must be equal length') - x = detrend(np.asarray(x)) - y = detrend(np.asarray(y)) + if detrend is not None: + x = detrend(np.asarray(x)) + y = detrend(np.asarray(y)) correls = np.correlate(x, y, mode="full") @@ -7016,6 +7017,7 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None, return h, xedges, yedges, pc + @_api.deprecated("3.6", alternative="scipy.signal.psd and ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, @@ -7127,6 +7129,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, else: return pxx, freqs, line + @_api.deprecated("3.6", alternative="scipy.signal.csd and ax.plot") @_preprocess_data(replace_names=["x", "y"], label_namer="y") @_docstring.dedent_interpd def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, @@ -7229,6 +7232,9 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, else: return pxy, freqs, line + @_api.deprecated( + "3.6", + alternative="scipy.signal.spectrogram(mode='magnitude') and ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None, @@ -7315,6 +7321,9 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, line + @_api.deprecated("3.6", + alternative="scipy.signal.spectrogram(mode='angle') and " + "ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd def angle_spectrum(self, x, Fs=None, Fc=None, window=None, @@ -7384,6 +7393,9 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] + @_api.deprecated("3.6", + alternative="scipy.signal.spectrogram(mode='phase') and " + "ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd def phase_spectrum(self, x, Fs=None, Fc=None, window=None, @@ -7453,6 +7465,8 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] + @_api.deprecated("3.6", alternative="scipy.signal.coherence and " + "ax.semilogx") @_preprocess_data(replace_names=["x", "y"]) @_docstring.dedent_interpd def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, @@ -7517,6 +7531,9 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, return cxy, freqs + @_api.deprecated("3.6", + alternative="scipy.signal.spectrogram(mode='psd') and " + "ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 5e85a9c1194f..b45331892967 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -1,8 +1,6 @@ """ -Numerical Python functions written for compatibility with MATLAB -commands with the same names. Most numerical Python functions can be found in -the `NumPy`_ and `SciPy`_ libraries. What remains here is code for performing -spectral computations and kernel density estimations. +This module is deprecated in favour of modules that can be found in +the `NumPy`_ and `SciPy`_ libraries. .. _NumPy: https://numpy.org .. _SciPy: https://www.scipy.org @@ -58,6 +56,7 @@ from matplotlib import _api, _docstring, cbook +@_api.deprecated("3.6", alternative="np.hanning") def window_hanning(x): """ Return *x* times the Hanning (or Hann) window of len(*x*). @@ -69,6 +68,7 @@ def window_hanning(x): return np.hanning(len(x))*x +@_api.deprecated("3.6", alternative="") def window_none(x): """ No window function; simply return *x*. @@ -80,6 +80,7 @@ def window_none(x): return x +@_api.deprecated("3.6", alternative="scipy.signal.detrend") def detrend(x, key=None, axis=None): """ Return *x* with its trend removed. @@ -129,6 +130,7 @@ def detrend(x, key=None, axis=None): f"'constant', 'mean', 'linear', or a function") +@_api.deprecated("3.6", alternative="scipy.signal.detrend") def detrend_mean(x, axis=None): """ Return *x* minus the mean(*x*). @@ -157,6 +159,7 @@ def detrend_mean(x, axis=None): return x - x.mean(axis, keepdims=True) +@_api.deprecated("3.6", alternative="scipy.signal.detrend") def detrend_none(x, axis=None): """ Return *x*: no detrending. @@ -179,6 +182,7 @@ def detrend_none(x, axis=None): return x +@_api.deprecated("3.6", alternative="scipy.signal.detrend") def detrend_linear(y): """ Return *x* minus best fit line; 'linear' detrending. @@ -531,6 +535,7 @@ def _single_spectrum_helper( MATLAB compatibility.""") +@_api.deprecated("3.6", alternative="scipy.signal.psd") @_docstring.dedent_interpd def psd(x, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None): @@ -587,6 +592,7 @@ def psd(x, NFFT=None, Fs=None, detrend=None, window=None, return Pxx.real, freqs +@_api.deprecated("3.6", alternative="scipy.signal.csd") @_docstring.dedent_interpd def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None): @@ -688,7 +694,6 @@ def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, Can return the complex spectrum of segments within the signal. """ - complex_spectrum = functools.partial(_single_spectrum_helper, "complex") complex_spectrum.__doc__ = _single_spectrum_docs.format( quantity="complex-valued frequency spectrum", @@ -707,6 +712,7 @@ def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, **_docstring.interpd.params) +@_api.deprecated("3.6", alternative="scipy.signal.welch") @_docstring.dedent_interpd def specgram(x, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None, @@ -790,6 +796,7 @@ def specgram(x, NFFT=None, Fs=None, detrend=None, window=None, return spec, freqs, t +@_api.deprecated("3.6", alternative="scipy.signal.coherence") @_docstring.dedent_interpd def cohere(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None): diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index faa023f8c082..8be7729a71e7 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -3005,8 +3005,8 @@ def vlines( # Autogenerated by boilerplate.py. Do not edit as changes will be lost. @_copy_docstring_and_deprecators(Axes.xcorr) def xcorr( - x, y, normed=True, detrend=mlab.detrend_none, usevlines=True, - maxlags=10, *, data=None, **kwargs): + x, y, normed=True, detrend=None, usevlines=True, maxlags=10, + *, data=None, **kwargs): return gca().xcorr( x, y, normed=normed, detrend=detrend, usevlines=usevlines, maxlags=maxlags, diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 17477ee97109..f001a3483991 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4725,6 +4725,8 @@ def test_subplot_key_hash(): assert ax.get_subplotspec().get_geometry() == (5, 1, 0, 0) +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') @image_comparison( ["specgram_freqs.png", "specgram_freqs_linear.png", "specgram_noise.png", "specgram_noise_linear.png"], @@ -4761,6 +4763,8 @@ def test_specgram(): scale="linear", norm=matplotlib.colors.LogNorm()) +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') @image_comparison( ["specgram_magnitude_freqs.png", "specgram_magnitude_freqs_linear.png", "specgram_magnitude_noise.png", "specgram_magnitude_noise_linear.png"], @@ -4798,6 +4802,8 @@ def test_specgram_magnitude(): scale="linear", norm=matplotlib.colors.LogNorm()) +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') @image_comparison( ["specgram_angle_freqs.png", "specgram_phase_freqs.png", "specgram_angle_noise.png", "specgram_phase_noise.png"], @@ -4836,6 +4842,8 @@ def test_specgram_angle(): scale="dB") +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') def test_specgram_fs_none(): """Test axes.specgram when Fs is None, should not throw error.""" spec, freqs, t, im = plt.specgram(np.ones(300), Fs=None, scale='linear') @@ -4843,6 +4851,8 @@ def test_specgram_fs_none(): assert xmin == 32 and xmax == 96 +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') @check_figures_equal(extensions=["png"]) def test_specgram_origin_rcparam(fig_test, fig_ref): """Test specgram ignores image.origin rcParam and uses origin 'upper'.""" @@ -4861,6 +4871,8 @@ def test_specgram_origin_rcparam(fig_test, fig_ref): fig_test.subplots().specgram(signal) +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') def test_specgram_origin_kwarg(): """Ensure passing origin as a kwarg raises a TypeError.""" t = np.arange(500) @@ -4870,6 +4882,8 @@ def test_specgram_origin_kwarg(): plt.specgram(signal, origin='lower') +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') @image_comparison( ["psd_freqs.png", "csd_freqs.png", "psd_noise.png", "csd_noise.png"], remove_text=True, tol=0.002) @@ -4904,6 +4918,8 @@ def test_psd_csd(): ax.set(xlabel="", ylabel="") +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') @image_comparison( ["magnitude_spectrum_freqs_linear.png", "magnitude_spectrum_freqs_dB.png", @@ -4946,6 +4962,8 @@ def test_spectrum(): ax.set(xlabel="", ylabel="") +@pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') def test_psd_csd_edge_cases(): # Inverted yaxis or fully zero inputs used to throw exceptions. axs = plt.figure().subplots(2) diff --git a/lib/matplotlib/tests/test_mlab.py b/lib/matplotlib/tests/test_mlab.py index 75ca0648a4e1..f73cec564bd4 100644 --- a/lib/matplotlib/tests/test_mlab.py +++ b/lib/matplotlib/tests/test_mlab.py @@ -5,6 +5,10 @@ from matplotlib import mlab, _api +# this module is being deprecated as of 3.6, so... +pytestmark = pytest.mark.filterwarnings( + 'ignore::matplotlib._api.deprecation.MatplotlibDeprecationWarning') + class TestStride: def get_base(self, x): From 59532aefd5688ac336a3933142584ccf60485bfc Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 22 Jul 2022 08:45:37 -0700 Subject: [PATCH 2/3] FIX: detrends --- lib/matplotlib/mlab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index b45331892967..503f94d77c0e 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -68,7 +68,7 @@ def window_hanning(x): return np.hanning(len(x))*x -@_api.deprecated("3.6", alternative="") +@_api.deprecated("3.6") def window_none(x): """ No window function; simply return *x*. @@ -159,7 +159,7 @@ def detrend_mean(x, axis=None): return x - x.mean(axis, keepdims=True) -@_api.deprecated("3.6", alternative="scipy.signal.detrend") +@_api.deprecated("3.6") def detrend_none(x, axis=None): """ Return *x*: no detrending. From c6b33a6a2c61dec27057479909dc8448ecce53d1 Mon Sep 17 00:00:00 2001 From: Jody Klymak Date: Fri, 11 Nov 2022 12:00:16 -0800 Subject: [PATCH 3/3] Fix for 3.7 --- lib/matplotlib/axes/_axes.py | 14 +++++++------- lib/matplotlib/mlab.py | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 7698a9b9982b..12ecaf70781c 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -7017,7 +7017,7 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None, return h, xedges, yedges, pc - @_api.deprecated("3.6", alternative="scipy.signal.psd and ax.loglog") + @_api.deprecated("3.7", alternative="scipy.signal.psd and ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, @@ -7129,7 +7129,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, else: return pxx, freqs, line - @_api.deprecated("3.6", alternative="scipy.signal.csd and ax.plot") + @_api.deprecated("3.7", alternative="scipy.signal.csd and ax.plot") @_preprocess_data(replace_names=["x", "y"], label_namer="y") @_docstring.dedent_interpd def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, @@ -7233,7 +7233,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, return pxy, freqs, line @_api.deprecated( - "3.6", + "3.7", alternative="scipy.signal.spectrogram(mode='magnitude') and ax.loglog") @_preprocess_data(replace_names=["x"]) @_docstring.dedent_interpd @@ -7321,7 +7321,7 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, line - @_api.deprecated("3.6", + @_api.deprecated("3.7", alternative="scipy.signal.spectrogram(mode='angle') and " "ax.loglog") @_preprocess_data(replace_names=["x"]) @@ -7393,7 +7393,7 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] - @_api.deprecated("3.6", + @_api.deprecated("3.7", alternative="scipy.signal.spectrogram(mode='phase') and " "ax.loglog") @_preprocess_data(replace_names=["x"]) @@ -7465,7 +7465,7 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None, return spec, freqs, lines[0] - @_api.deprecated("3.6", alternative="scipy.signal.coherence and " + @_api.deprecated("3.7", alternative="scipy.signal.coherence and " "ax.semilogx") @_preprocess_data(replace_names=["x", "y"]) @_docstring.dedent_interpd @@ -7531,7 +7531,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, return cxy, freqs - @_api.deprecated("3.6", + @_api.deprecated("3.7", alternative="scipy.signal.spectrogram(mode='psd') and " "ax.loglog") @_preprocess_data(replace_names=["x"]) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 503f94d77c0e..9079eb09a370 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -56,7 +56,7 @@ from matplotlib import _api, _docstring, cbook -@_api.deprecated("3.6", alternative="np.hanning") +@_api.deprecated("3.7", alternative="np.hanning") def window_hanning(x): """ Return *x* times the Hanning (or Hann) window of len(*x*). @@ -68,7 +68,7 @@ def window_hanning(x): return np.hanning(len(x))*x -@_api.deprecated("3.6") +@_api.deprecated("3.7") def window_none(x): """ No window function; simply return *x*. @@ -80,7 +80,7 @@ def window_none(x): return x -@_api.deprecated("3.6", alternative="scipy.signal.detrend") +@_api.deprecated("3.7", alternative="scipy.signal.detrend") def detrend(x, key=None, axis=None): """ Return *x* with its trend removed. @@ -130,7 +130,7 @@ def detrend(x, key=None, axis=None): f"'constant', 'mean', 'linear', or a function") -@_api.deprecated("3.6", alternative="scipy.signal.detrend") +@_api.deprecated("3.7", alternative="scipy.signal.detrend") def detrend_mean(x, axis=None): """ Return *x* minus the mean(*x*). @@ -159,7 +159,7 @@ def detrend_mean(x, axis=None): return x - x.mean(axis, keepdims=True) -@_api.deprecated("3.6") +@_api.deprecated("3.7") def detrend_none(x, axis=None): """ Return *x*: no detrending. @@ -182,7 +182,7 @@ def detrend_none(x, axis=None): return x -@_api.deprecated("3.6", alternative="scipy.signal.detrend") +@_api.deprecated("3.7", alternative="scipy.signal.detrend") def detrend_linear(y): """ Return *x* minus best fit line; 'linear' detrending. @@ -217,7 +217,7 @@ def detrend_linear(y): return y - (b*x + a) -@_api.deprecated("3.6") +@_api.deprecated("3.7") def stride_windows(x, n, noverlap=None, axis=0): """ Get all windows of *x* with length *n* as a single array, @@ -535,7 +535,7 @@ def _single_spectrum_helper( MATLAB compatibility.""") -@_api.deprecated("3.6", alternative="scipy.signal.psd") +@_api.deprecated("3.7", alternative="scipy.signal.psd") @_docstring.dedent_interpd def psd(x, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None): @@ -592,7 +592,7 @@ def psd(x, NFFT=None, Fs=None, detrend=None, window=None, return Pxx.real, freqs -@_api.deprecated("3.6", alternative="scipy.signal.csd") +@_api.deprecated("3.7", alternative="scipy.signal.csd") @_docstring.dedent_interpd def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None): @@ -712,7 +712,7 @@ def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, **_docstring.interpd.params) -@_api.deprecated("3.6", alternative="scipy.signal.welch") +@_api.deprecated("3.7", alternative="scipy.signal.welch") @_docstring.dedent_interpd def specgram(x, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, sides=None, scale_by_freq=None, @@ -796,7 +796,7 @@ def specgram(x, NFFT=None, Fs=None, detrend=None, window=None, return spec, freqs, t -@_api.deprecated("3.6", alternative="scipy.signal.coherence") +@_api.deprecated("3.7", alternative="scipy.signal.coherence") @_docstring.dedent_interpd def cohere(x, y, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning, noverlap=0, pad_to=None, sides='default', scale_by_freq=None):