Skip to content

Commit 2bd0c3e

Browse files
authored
Remove legacy plot_psd* funcs/methods in our docs and codebase (#11563)
1 parent 0e305ee commit 2bd0c3e

30 files changed

+222
-184
lines changed

doc/changes/latest.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Bugs
5555
- Fix visualization dialog compatibility with matplotlib 3.7 (:gh:`11409` by `Daniel McCloy`_ and `Eric Larson`_)
5656
- Expand tilde (user directory) in config keys (:gh:`11537` by `Clemens Brunner`_)
5757
- Fix bug in :func:`mne.preprocessing.compute_maxwell_basis` where using ``int_order=0`` would raise an error (:gh:`11562` by `Eric Larson`_)
58+
- In the legacy function :func:`mne.viz.plot_epochs_psd_topomap`, the parameter ``names`` now works again (:gh:`11563` by `Daniel McCloy`_)
5859
- Fix :func:`mne.io.read_raw` for file names containing multiple dots (:gh:`11521` by `Clemens Brunner`_)
5960
- Fix bug in :func:`mne.export.export_raw` when exporting to EDF with a physical range set smaller than the data range (:gh:`11569` by `Mathieu Scheltienne`_)
6061
- Fix bug in :func:`mne.concatenate_raws` where two raws could not be merged if the order of the bad channel lists did not match (:gh:`11502` by `Moritz Gerster`_)

examples/datasets/brainstorm_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
raw.set_eeg_reference('average', projection=True)
4343

4444
# show power line interference and remove it
45-
raw.plot_psd(tmax=60., average=False)
45+
raw.compute_psd(tmax=60).plot(average=False)
4646
raw.notch_filter(np.arange(60, 181, 60), fir_design='firwin')
4747

4848
events = mne.find_events(raw, stim_channel='UPPT001')

examples/preprocessing/eeg_csd.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
# %%
5151
# Also look at the power spectral densities:
5252

53-
raw.plot_psd()
54-
raw_csd.plot_psd()
53+
raw.compute_psd().plot()
54+
raw_csd.compute_psd().plot()
5555

5656
# %%
5757
# CSD can also be computed on Evoked (averaged) data.

examples/preprocessing/find_ref_artifacts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
# %%
6565
# The PSD of these data show the noise as clear peaks.
66-
raw.plot_psd(fmax=30)
66+
raw.compute_psd(fmax=30).plot()
6767

6868
# %%
6969
# Run the "together" algorithm.
@@ -93,7 +93,7 @@
9393

9494
# %%
9595
# Cleaned data:
96-
raw_tog.plot_psd(fmax=30)
96+
raw_tog.compute_psd(fmax=30).plot()
9797

9898
# %%
9999
# Now try the "separate" algorithm.
@@ -138,7 +138,7 @@
138138
# %%
139139
# Cleaned raw data PSD:
140140

141-
raw_sep.plot_psd(fmax=30)
141+
raw_sep.compute_psd(fmax=30).plot()
142142

143143
##############################################################################
144144
# References

examples/time_frequency/source_power_spectrum_opm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
n_fft = next_fast_len(int(round(4 * new_sfreq)))
8080
print('Using n_fft=%d (%0.1f s)' % (n_fft, n_fft / raws['vv'].info['sfreq']))
8181
for kind in kinds:
82-
fig = raws[kind].plot_psd(n_fft=n_fft, proj=True)
82+
fig = raws[kind].compute_psd(n_fft=n_fft, proj=True).plot()
8383
fig.suptitle(titles[kind])
8484
fig.subplots_adjust(0.1, 0.1, 0.95, 0.85)
8585

examples/visualization/sensor_noise_level.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
# %%
2727
# We can plot the absolute noise levels:
28-
raw_erm.plot_psd(tmax=10., average=True, spatial_colors=False,
29-
dB=False, xscale='log')
28+
raw_erm.compute_psd(tmax=10).plot(average=True, spatial_colors=False,
29+
dB=False, xscale='log')
3030
# %%
3131
# References
3232
# ----------

mne/io/array/tests/test_array.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ def test_array_raw():
143143

144144
# plotting
145145
raw2.plot()
146-
raw2.plot_psd(tmax=2., average=True, n_fft=1024,
147-
spatial_colors=False)
146+
(raw2.compute_psd(tmax=2., n_fft=1024)
147+
.plot(average=True, spatial_colors=False))
148148
plt.close('all')
149149

150150
# epoching
@@ -177,5 +177,6 @@ def test_array_raw():
177177

178178
raw = RawArray(data, info)
179179
raw.set_montage(montage)
180-
raw.plot_psd(average=False) # looking for nonexistent layout
181-
raw.plot_psd_topo()
180+
spectrum = raw.compute_psd()
181+
spectrum.plot(average=False) # looking for nonexistent layout
182+
spectrum.plot_topo()

mne/report/report.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
_path_like, use_log_level, _check_fname, _pl,
4242
_check_ch_locs, _import_h5io_funcs, _verbose_safe_false,
4343
check_version)
44+
from ..utils.spectrum import _split_psd_kwargs
4445
from ..viz import (plot_events, plot_alignment, plot_cov, plot_projs_topomap,
4546
plot_compare_evokeds, set_3d_view, get_3d_backend,
4647
Figure3D, use_browser_backend)
@@ -687,9 +688,11 @@ class Report:
687688
raw_psd : bool | dict
688689
If True, include PSD plots for raw files. Can be False (default) to
689690
omit, True to plot, or a dict to pass as ``kwargs`` to
690-
:meth:`mne.io.Raw.plot_psd`.
691+
:meth:`mne.time_frequency.Spectrum.plot`.
691692
692693
.. versionadded:: 0.17
694+
.. versionchanged:: 1.4
695+
kwargs are sent to ``spectrum.plot`` instead of ``raw.plot_psd``.
693696
projs : bool
694697
Whether to include topographic plots of SSP projectors, if present in
695698
the data. Defaults to ``False``.
@@ -717,9 +720,11 @@ class Report:
717720
raw_psd : bool | dict
718721
If True, include PSD plots for raw files. Can be False (default) to
719722
omit, True to plot, or a dict to pass as ``kwargs`` to
720-
:meth:`mne.io.Raw.plot_psd`.
723+
:meth:`mne.time_frequency.Spectrum.plot`.
721724
722725
.. versionadded:: 0.17
726+
.. versionchanged:: 1.4
727+
kwargs are sent to ``spectrum.plot`` instead of ``raw.plot_psd``.
723728
projs : bool
724729
Whether to include topographic plots of SSP projectors, if present in
725730
the data. Defaults to ``False``.
@@ -2837,7 +2842,11 @@ def _add_raw(
28372842
else:
28382843
fmax = np.inf
28392844

2840-
fig = raw.plot_psd(fmax=fmax, show=False, **add_psd)
2845+
# shim: convert legacy .plot_psd(...) → .compute_psd(...).plot(...)
2846+
init_kwargs, plot_kwargs = _split_psd_kwargs(kwargs=add_psd)
2847+
init_kwargs.setdefault('fmax', fmax)
2848+
plot_kwargs.setdefault('show', False)
2849+
fig = raw.compute_psd(**init_kwargs).plot(**plot_kwargs)
28412850
tight_layout(fig=fig)
28422851
_constrain_fig_resolution(
28432852
fig, max_width=MAX_IMG_WIDTH, max_res=MAX_IMG_RES
@@ -3358,7 +3367,7 @@ def _add_epochs_psd(
33583367
if fmax > 0.5 * epochs.info['sfreq']:
33593368
fmax = np.inf
33603369

3361-
fig = epochs_for_psd.plot_psd(fmax=fmax, show=False)
3370+
fig = epochs_for_psd.compute_psd(fmax=fmax).plot(show=False)
33623371
_constrain_fig_resolution(
33633372
fig, max_width=MAX_IMG_WIDTH, max_res=MAX_IMG_RES
33643373
)

mne/report/tests/test_report.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ def test_report_raw_psd_and_date(tmp_path):
359359
assert 'Unknown' not in ''.join(report.html)
360360
assert 'GMT' in ''.join(report.html)
361361

362+
# test kwargs passed through to underlying array func
363+
Report(raw_psd=dict(window='boxcar'))
364+
362365
# test new anonymize functionality
363366
report = Report()
364367
raw.anonymize()

mne/time_frequency/spectrum.py

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from ..utils.check import (_check_fname, _check_option, _import_h5io_funcs,
2525
_is_numeric, check_fname)
2626
from ..utils.misc import _pl
27+
from ..utils.spectrum import _split_psd_kwargs
2728
from ..viz.topo import _plot_timeseries, _plot_timeseries_unified, _plot_topo
2829
from ..viz.topomap import (_make_head_outlines, _prepare_topomap_plot,
2930
plot_psds_topomap)
@@ -92,29 +93,8 @@ def plot_psd(self, fmin=0, fmax=np.inf, tmin=None, tmax=None, picks=None,
9293
-----
9394
%(notes_plot_psd_meth)s
9495
"""
95-
from ..io import BaseRaw
96-
97-
# triage reject_by_annotation
98-
rba = dict()
99-
if isinstance(self, BaseRaw):
100-
rba = dict(reject_by_annotation=reject_by_annotation)
101-
102-
spectrum = self.compute_psd(
103-
method=method, fmin=fmin, fmax=fmax, tmin=tmin, tmax=tmax,
104-
picks=picks, proj=proj, n_jobs=n_jobs, verbose=verbose, **rba,
105-
**method_kw)
106-
107-
# translate kwargs
108-
amplitude = 'auto' if estimate == 'auto' else (estimate == 'amplitude')
109-
ci = 'sd' if area_mode == 'std' else area_mode
110-
# ↓ here picks="all" because we've already restricted the `info` to
111-
# ↓ have only `picks` channels
112-
fig = spectrum.plot(
113-
picks='all', average=average, dB=dB, amplitude=amplitude,
114-
xscale=xscale, ci=ci, ci_alpha=area_alpha, color=color,
115-
alpha=line_alpha, spatial_colors=spatial_colors, sphere=sphere,
116-
exclude=exclude, axes=ax, show=show)
117-
return fig
96+
init_kw, plot_kw = _split_psd_kwargs(plot_fun=Spectrum.plot)
97+
return self.compute_psd(**init_kw).plot(**plot_kw)
11898

11999
@legacy(alt='.compute_psd().plot_topo()')
120100
@verbose
@@ -148,13 +128,8 @@ def plot_psd_topo(self, tmin=None, tmax=None, fmin=0, fmax=100, proj=False,
148128
fig : instance of matplotlib.figure.Figure
149129
Figure distributing one image per channel across sensor topography.
150130
"""
151-
spectrum = self.compute_psd(
152-
method=method, fmin=fmin, fmax=fmax, tmin=tmin, tmax=tmax,
153-
proj=proj, n_jobs=n_jobs, verbose=verbose, **method_kw)
154-
155-
return spectrum.plot_topo(
156-
dB=dB, layout=layout, color=color, fig_facecolor=fig_facecolor,
157-
axis_facecolor=axis_facecolor, axes=axes, block=block, show=show)
131+
init_kw, plot_kw = _split_psd_kwargs(plot_fun=Spectrum.plot_topo)
132+
return self.compute_psd(**init_kw).plot_topo(**plot_kw)
158133

159134
@legacy(alt='.compute_psd().plot_topomap()')
160135
@verbose
@@ -212,19 +187,8 @@ def plot_psd_topomap(self, bands=None, tmin=None, tmax=None, ch_type=None,
212187
fig : instance of Figure
213188
Figure showing one scalp topography per frequency band.
214189
"""
215-
spectrum = self.compute_psd(
216-
method=method, tmin=tmin, tmax=tmax, proj=proj,
217-
n_jobs=n_jobs, verbose=verbose, **method_kw)
218-
219-
fig = spectrum.plot_topomap(
220-
bands=bands, ch_type=ch_type, normalize=normalize, agg_fun=agg_fun,
221-
dB=dB, sensors=sensors, show_names=show_names, mask=mask,
222-
mask_params=mask_params, contours=contours, outlines=outlines,
223-
sphere=sphere, image_interp=image_interp, extrapolate=extrapolate,
224-
border=border, res=res, size=size, cmap=cmap, vlim=vlim,
225-
cnorm=cnorm, colorbar=colorbar, cbar_fmt=cbar_fmt, units=units,
226-
axes=axes, show=show)
227-
return fig
190+
init_kw, plot_kw = _split_psd_kwargs(plot_fun=Spectrum.plot_topomap)
191+
return self.compute_psd(**init_kw).plot_topomap(**plot_kw)
228192

229193
def _set_legacy_nfft_default(self, tmin, tmax, method, method_kw):
230194
"""Update method_kw with legacy n_fft default for plot_psd[_topo]().
@@ -912,9 +876,8 @@ class Spectrum(BaseSpectrum):
912876
.. footbibliography::
913877
"""
914878

915-
def __init__(self, inst, method, fmin, fmax, tmin, tmax, picks,
916-
proj, reject_by_annotation, *, n_jobs, verbose=None,
917-
**method_kw):
879+
def __init__(self, inst, method, fmin, fmax, tmin, tmax, picks, proj,
880+
reject_by_annotation, *, n_jobs, verbose=None, **method_kw):
918881
from ..io import BaseRaw
919882

920883
# triage reading from file

0 commit comments

Comments
 (0)