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] Bug loading projections in CSD h5 files. #11072

Merged
merged 4 commits into from Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/latest.inc
Expand Up @@ -31,6 +31,7 @@ Enhancements

Bugs
~~~~
- Fix bug in :func:`mne.time_frequency.read_csd` that returned `mne.time_frequency.CrossSpectralDensity.projs` as a list of dict instead of `mne.proj.Projection` (:gh:`11072` by :newcontrib:`Chetan Gohil`)
cgohil8 marked this conversation as resolved.
Show resolved Hide resolved
- Fix bug in :func:`mne.decoding.TimeFrequency` that prevented cloning if constructor arguments were modified (:gh:`11004` by :newcontrib:`Daniel Carlström Schad`)
- Fix bug in :class:`mne.viz.Brain` constructor where the first argument was named ``subject_id`` instead of ``subject`` (:gh:`11049` by `Eric Larson`_)
- Document ``height`` and ``weight`` keys of ``subject_info`` entry in :class:`mne.Info` (:gh:`11019` by :newcontrib:`Sena Er`)
Expand Down
2 changes: 2 additions & 0 deletions doc/changes/names.inc
Expand Up @@ -66,6 +66,8 @@

.. _Cathy Nangini: https://github.com/KatiRG

.. _Chetan Gohil: https://github.com/cgohil8

.. _Chris Bailey: https://github.com/cjayb

.. _Chris Holdgraf: https://chrisholdgraf.com
Expand Down
6 changes: 6 additions & 0 deletions mne/time_frequency/csd.py
Expand Up @@ -607,6 +607,12 @@ def read_csd(fname):
fname += '.h5'

csd_dict = read_hdf5(fname, title='conpy')

if csd_dict["projs"] is not None:
# Avoid circular import
from ..proj import Projection
csd_dict["projs"] = [Projection(**proj) for proj in csd_dict["projs"]]

return CrossSpectralDensity(**csd_dict)


Expand Down
17 changes: 13 additions & 4 deletions mne/time_frequency/tests/test_csd.py
Expand Up @@ -16,6 +16,7 @@
CrossSpectralDensity, read_csd,
pick_channels_csd, psd_multitaper)
from mne.time_frequency.csd import _sym_mat_to_vector, _vector_to_sym_mat
from mne.proj import compute_proj_raw, Projection

base_dir = op.join(op.dirname(__file__), '..', '..', 'io', 'tests', 'data')
raw_fname = op.join(base_dir, 'test_raw.fif')
Expand Down Expand Up @@ -223,17 +224,25 @@ def test_csd_get_data():
@requires_version('h5io')
def test_csd_save(tmp_path):
"""Test saving and loading a CrossSpectralDensity."""
csd = _make_csd()
tempdir = str(tmp_path)
fname = op.join(tempdir, 'csd.h5')
raw = mne.io.read_raw_fif(raw_fname)
events = mne.find_events(raw)
picks = mne.pick_types(raw.info, meg='grad')
projs = compute_proj_raw(raw, n_grad=1, n_mag=1, n_eeg=1, reject=None)
raw.add_proj(projs)
epochs = mne.Epochs(raw, events, event_id=1, tmin=-0.2, tmax=1,
picks=picks, baseline=(None, 0),
reject=dict(grad=4000e-13), preload=True)
csd = csd_fourier(epochs, fmin=15, fmax=20)
fname = op.join(str(tmp_path), 'csd.h5')
csd.save(fname)
csd2 = read_csd(fname)
assert_array_equal(csd._data, csd2._data)
assert_array_equal(csd.frequencies, csd2.frequencies)
assert csd.tmin == csd2.tmin
assert csd.tmax == csd2.tmax
assert csd.ch_names == csd2.ch_names
assert csd.frequencies == csd2.frequencies
assert csd._is_sum == csd2._is_sum
assert isinstance(csd2.projs[0], Projection)
larsoner marked this conversation as resolved.
Show resolved Hide resolved


def test_csd_pickle(tmp_path):
Expand Down