Skip to content

Commit 0515b02

Browse files
GonReinapre-commit-ci[bot]drammock
authored andcommitted
Allow not dropping bads when creating or plotting Spectrum objs (mne-tools#12006)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel McCloy <dan@mccloy.info>
1 parent ef0e844 commit 0515b02

File tree

7 files changed

+41
-6
lines changed

7 files changed

+41
-6
lines changed

doc/changes/names.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@
184184
185185
.. _George O'Neill: https://georgeoneill.github.io
186186

187+
.. _Gonzalo Reina: https://greina.me/
188+
187189
.. _Guillaume Dumas: https://mila.quebec/en/person/guillaume-dumas
188190

189191
.. _Guillaume Favelier: https://github.com/GuillaumeFavelier

mne/epochs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,7 @@ def compute_psd(
23662366
picks=None,
23672367
proj=False,
23682368
remove_dc=True,
2369+
exclude=(),
23692370
*,
23702371
n_jobs=1,
23712372
verbose=None,
@@ -2382,6 +2383,7 @@ def compute_psd(
23822383
%(picks_good_data_noref)s
23832384
%(proj_psd)s
23842385
%(remove_dc)s
2386+
%(exclude_psd)s
23852387
%(n_jobs)s
23862388
%(verbose)s
23872389
%(method_kw_psd)s
@@ -2410,6 +2412,7 @@ def compute_psd(
24102412
tmin=tmin,
24112413
tmax=tmax,
24122414
picks=picks,
2415+
exclude=exclude,
24132416
proj=proj,
24142417
remove_dc=remove_dc,
24152418
n_jobs=n_jobs,

mne/evoked.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ def compute_psd(
10511051
picks=None,
10521052
proj=False,
10531053
remove_dc=True,
1054+
exclude=(),
10541055
*,
10551056
n_jobs=1,
10561057
verbose=None,
@@ -1067,6 +1068,7 @@ def compute_psd(
10671068
%(picks_good_data_noref)s
10681069
%(proj_psd)s
10691070
%(remove_dc)s
1071+
%(exclude_psd)s
10701072
%(n_jobs)s
10711073
%(verbose)s
10721074
%(method_kw_psd)s
@@ -1095,6 +1097,7 @@ def compute_psd(
10951097
tmin=tmin,
10961098
tmax=tmax,
10971099
picks=picks,
1100+
exclude=exclude,
10981101
proj=proj,
10991102
remove_dc=remove_dc,
11001103
reject_by_annotation=False,

mne/io/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,7 @@ def compute_psd(
21362136
tmin=None,
21372137
tmax=None,
21382138
picks=None,
2139+
exclude=(),
21392140
proj=False,
21402141
remove_dc=True,
21412142
reject_by_annotation=True,
@@ -2153,6 +2154,7 @@ def compute_psd(
21532154
%(fmin_fmax_psd)s
21542155
%(tmin_tmax_psd)s
21552156
%(picks_good_data_noref)s
2157+
%(exclude_psd)s
21562158
%(proj_psd)s
21572159
%(remove_dc)s
21582160
%(reject_by_annotation_psd)s
@@ -2184,6 +2186,7 @@ def compute_psd(
21842186
tmin=tmin,
21852187
tmax=tmax,
21862188
picks=picks,
2189+
exclude=exclude,
21872190
proj=proj,
21882191
remove_dc=remove_dc,
21892192
reject_by_annotation=reject_by_annotation,

mne/time_frequency/spectrum.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def __init__(
300300
tmin,
301301
tmax,
302302
picks,
303+
exclude,
303304
proj,
304305
remove_dc,
305306
*,
@@ -348,7 +349,9 @@ def __init__(
348349

349350
# prep times and picks
350351
self._time_mask = _time_mask(inst.times, tmin, tmax, sfreq=self.sfreq)
351-
self._picks = _picks_to_idx(inst.info, picks, "data", with_ref_meg=False)
352+
self._picks = _picks_to_idx(
353+
inst.info, picks, "data", exclude, with_ref_meg=False
354+
)
352355

353356
# add the info object. bads and non-data channels were dropped by
354357
# _picks_to_idx() so we update the info accordingly:
@@ -1081,6 +1084,7 @@ class Spectrum(BaseSpectrum):
10811084
%(fmin_fmax_psd)s
10821085
%(tmin_tmax_psd)s
10831086
%(picks_good_data_noref)s
1087+
%(exclude_psd)s
10841088
%(proj_psd)s
10851089
%(remove_dc)s
10861090
%(reject_by_annotation_psd)s
@@ -1122,6 +1126,7 @@ def __init__(
11221126
tmin,
11231127
tmax,
11241128
picks,
1129+
exclude,
11251130
proj,
11261131
remove_dc,
11271132
reject_by_annotation,
@@ -1145,6 +1150,7 @@ def __init__(
11451150
tmin,
11461151
tmax,
11471152
picks,
1153+
exclude,
11481154
proj,
11491155
remove_dc,
11501156
n_jobs=n_jobs,
@@ -1290,6 +1296,7 @@ class EpochsSpectrum(BaseSpectrum, GetEpochsMixin):
12901296
%(fmin_fmax_psd)s
12911297
%(tmin_tmax_psd)s
12921298
%(picks_good_data_noref)s
1299+
%(exclude_psd)s
12931300
%(proj_psd)s
12941301
%(remove_dc)s
12951302
%(n_jobs)s
@@ -1327,6 +1334,7 @@ def __init__(
13271334
tmin,
13281335
tmax,
13291336
picks,
1337+
exclude,
13301338
proj,
13311339
remove_dc,
13321340
*,
@@ -1347,6 +1355,7 @@ def __init__(
13471355
tmin,
13481356
tmax,
13491357
picks,
1358+
exclude,
13501359
proj,
13511360
remove_dc,
13521361
n_jobs=n_jobs,
@@ -1459,6 +1468,7 @@ def average(self, method="mean"):
14591468
tmin=None,
14601469
tmax=None,
14611470
picks=None,
1471+
exclude=(),
14621472
proj=None,
14631473
remove_dc=None,
14641474
reject_by_annotation=None,
@@ -1561,6 +1571,7 @@ def read_spectrum(fname):
15611571
tmin=None,
15621572
tmax=None,
15631573
picks=None,
1574+
exclude=(),
15641575
proj=None,
15651576
remove_dc=None,
15661577
reject_by_annotation=None,

mne/time_frequency/tests/test_spectrum.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ def test_spectrum_reject_by_annot(raw):
171171
assert spect_no_annot != spect_reject_annot
172172

173173

174+
def test_spectrum_bads_exclude(raw):
175+
"""Test bads are not removed unless exclude="bads"."""
176+
raw.pick("mag") # get rid of IAS channel
177+
spect_no_excld = raw.compute_psd()
178+
spect_with_excld = raw.compute_psd(exclude="bads")
179+
assert raw.info["bads"] == spect_no_excld.info["bads"]
180+
assert spect_with_excld.info["bads"] == []
181+
assert set(raw.ch_names) - set(spect_with_excld.ch_names) == set(raw.info["bads"])
182+
183+
174184
def test_spectrum_getitem_raw(raw_spectrum):
175185
"""Test Spectrum.__getitem__ for Raw-derived spectra."""
176186
want = raw_spectrum.get_data(slice(1, 3), fmax=7)
@@ -280,7 +290,7 @@ def test_spectrum_to_data_frame(inst, request, evoked):
280290
extra_dim = () if is_epochs else (1,)
281291
extra_cols = ["freq", "condition", "epoch"] if is_epochs else ["freq"]
282292
# compute PSD
283-
spectrum = inst if is_already_psd else inst.compute_psd()
293+
spectrum = inst if is_already_psd else inst.compute_psd(exclude="bads")
284294
n_epo, n_chan, n_freq = extra_dim + spectrum.get_data().shape
285295
# test wide format
286296
df_wide = spectrum.to_data_frame()

mne/utils/docs.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,12 +1356,15 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):
13561356
_exclude_spectrum = """\
13571357
exclude : list of str | 'bads'
13581358
Channel names to exclude{}. If ``'bads'``, channels
1359-
in ``spectrum.info['bads']`` are excluded; pass an empty list or tuple to
1360-
plot all channels (including "bad" channels, if any).
1359+
in ``{}info['bads']`` are excluded; pass an empty list to
1360+
include all channels (including "bad" channels, if any).
13611361
"""
13621362

1363-
docdict["exclude_spectrum_get_data"] = _exclude_spectrum.format("")
1364-
docdict["exclude_spectrum_plot"] = _exclude_spectrum.format(" from being drawn")
1363+
docdict["exclude_psd"] = _exclude_spectrum.format("", "")
1364+
docdict["exclude_spectrum_get_data"] = _exclude_spectrum.format("", "spectrum.")
1365+
docdict["exclude_spectrum_plot"] = _exclude_spectrum.format(
1366+
" from being drawn", "spectrum."
1367+
)
13651368

13661369
docdict[
13671370
"export_edf_note"

0 commit comments

Comments
 (0)