Skip to content

Mnt deprecate mlab #22920

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
27 changes: 22 additions & 5 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ::

Expand Down Expand Up @@ -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*.
Expand All @@ -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 ::

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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.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,
Expand Down Expand Up @@ -7127,6 +7129,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
else:
return pxx, freqs, line

@_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,
Expand Down Expand Up @@ -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.7",
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,
Expand Down Expand Up @@ -7315,6 +7321,9 @@ def magnitude_spectrum(self, x, Fs=None, Fc=None, window=None,

return spec, freqs, line

@_api.deprecated("3.7",
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,
Expand Down Expand Up @@ -7384,6 +7393,9 @@ def angle_spectrum(self, x, Fs=None, Fc=None, window=None,

return spec, freqs, lines[0]

@_api.deprecated("3.7",
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,
Expand Down Expand Up @@ -7453,6 +7465,8 @@ def phase_spectrum(self, x, Fs=None, Fc=None, window=None,

return spec, freqs, lines[0]

@_api.deprecated("3.7", 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,
Expand Down Expand Up @@ -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.7",
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,
Expand Down
19 changes: 13 additions & 6 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the long-term, will GaussianKDE live in another module? Or will mlab stay around to host it?

the `NumPy`_ and `SciPy`_ libraries.

.. _NumPy: https://numpy.org
.. _SciPy: https://www.scipy.org
Expand Down Expand Up @@ -58,6 +56,7 @@
from matplotlib import _api, _docstring, cbook


@_api.deprecated("3.7", alternative="np.hanning")
def window_hanning(x):
"""
Return *x* times the Hanning (or Hann) window of len(*x*).
Expand All @@ -69,6 +68,7 @@ def window_hanning(x):
return np.hanning(len(x))*x


@_api.deprecated("3.7")
def window_none(x):
"""
No window function; simply return *x*.
Expand All @@ -80,6 +80,7 @@ def window_none(x):
return x


@_api.deprecated("3.7", alternative="scipy.signal.detrend")
def detrend(x, key=None, axis=None):
"""
Return *x* with its trend removed.
Expand Down Expand Up @@ -129,6 +130,7 @@ def detrend(x, key=None, axis=None):
f"'constant', 'mean', 'linear', or a function")


@_api.deprecated("3.7", alternative="scipy.signal.detrend")
def detrend_mean(x, axis=None):
"""
Return *x* minus the mean(*x*).
Expand Down Expand Up @@ -157,6 +159,7 @@ def detrend_mean(x, axis=None):
return x - x.mean(axis, keepdims=True)


@_api.deprecated("3.7")
def detrend_none(x, axis=None):
"""
Return *x*: no detrending.
Expand All @@ -179,6 +182,7 @@ def detrend_none(x, axis=None):
return x


@_api.deprecated("3.7", alternative="scipy.signal.detrend")
def detrend_linear(y):
"""
Return *x* minus best fit line; 'linear' detrending.
Expand Down Expand Up @@ -213,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,
Expand Down Expand Up @@ -531,6 +535,7 @@ def _single_spectrum_helper(
MATLAB compatibility.""")


@_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):
Expand Down Expand Up @@ -587,6 +592,7 @@ def psd(x, NFFT=None, Fs=None, detrend=None, window=None,
return Pxx.real, freqs


@_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):
Expand Down Expand Up @@ -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",
Expand All @@ -707,6 +712,7 @@ def csd(x, y, NFFT=None, Fs=None, detrend=None, window=None,
**_docstring.interpd.params)


@_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,
Expand Down Expand Up @@ -790,6 +796,7 @@ def specgram(x, NFFT=None, Fs=None, detrend=None, window=None,
return spec, freqs, t


@_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):
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -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"],
Expand Down Expand Up @@ -4836,13 +4842,17 @@ 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')
xmin, xmax, freq0, freq1 = im.get_extent()
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'."""
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/tests/test_mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down