Skip to content
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

fix I/O to allow slash-separated conditions in EpochsSpectrum.event_id #13042

Merged
merged 4 commits into from
Dec 21, 2024
Merged
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
1 change: 1 addition & 0 deletions doc/changes/devel/13042.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix loading and saving of :class:`~mne.time_frequency.EpochsSpectrum` objects that contain slash-separators in their condition names, by `Daniel McCloy`_.
4 changes: 2 additions & 2 deletions mne/time_frequency/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ def save(self, fname, *, overwrite=False, verbose=None):
check_fname(fname, "spectrum", (".h5", ".hdf5"))
fname = _check_fname(fname, overwrite=overwrite, verbose=verbose)
out = self.__getstate__()
write_hdf5(fname, out, overwrite=overwrite, title="mnepython")
write_hdf5(fname, out, overwrite=overwrite, title="mnepython", slash="replace")

@verbose
def to_data_frame(
Expand Down Expand Up @@ -1663,7 +1663,7 @@ def read_spectrum(fname):
_validate_type(fname, "path-like", "fname")
fname = _check_fname(fname=fname, overwrite="read", must_exist=False)
# read it in
hdf5_dict = read_hdf5(fname, title="mnepython")
hdf5_dict = read_hdf5(fname, title="mnepython", slash="replace")
defaults = dict(
method=None,
fmin=None,
Expand Down
10 changes: 8 additions & 2 deletions mne/time_frequency/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from matplotlib.colors import same_color
from numpy.testing import assert_allclose, assert_array_equal

from mne import Annotations, create_info, make_fixed_length_epochs
from mne import Annotations, BaseEpochs, create_info, make_fixed_length_epochs
from mne.io import RawArray
from mne.time_frequency import read_spectrum
from mne.time_frequency.multitaper import _psd_from_mt
Expand Down Expand Up @@ -163,13 +163,19 @@ def _get_inst(inst, request, *, evoked=None, average_tfr=None):
return request.getfixturevalue(inst)


@pytest.mark.parametrize("inst", ("raw", "epochs", "evoked"))
@pytest.mark.parametrize("inst", ("raw", "epochs_full", "evoked"))
def test_spectrum_io(inst, tmp_path, request, evoked):
"""Test save/load of spectrum objects."""
pytest.importorskip("h5io")
fname = tmp_path / f"{inst}-spectrum.h5"
inst = _get_inst(inst, request, evoked=evoked)
if isinstance(inst, BaseEpochs):
# fake HED-like tags (https://mne.discourse.group/t/10634)
inst.events[-2:, -1] = 2
inst.event_id = {"foo/bar": 1, "foo/qux": 2}
orig = inst.compute_psd()
if isinstance(inst, BaseEpochs):
orig = orig["foo"]
orig.save(fname)
loaded = read_spectrum(fname)
assert orig == loaded
Expand Down
Loading