Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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/dev/13375.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug with :func:`mne.grand_average` not working with :class:`mne.time_frequency.Spectrum` objects, by `Thomas Binns`_.
10 changes: 8 additions & 2 deletions mne/channels/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ def equalize_channels(instances, copy=True, verbose=None):
----------
instances : list
A list of MNE-Python objects to equalize the channels for. Objects can
be of type Raw, Epochs, Evoked, AverageTFR, Forward, Covariance,
be of type Raw, Epochs, Evoked, Spectrum, AverageTFR, Forward, Covariance,
CrossSpectralDensity or Info.

.. versionchanged:: 1.11
Added support for :class:`mne.time_frequency.Spectrum` objects.
copy : bool
When dropping and/or re-ordering channels, an object will be copied
when this parameter is set to ``True``. When set to ``False`` (the
Expand All @@ -148,21 +151,24 @@ def equalize_channels(instances, copy=True, verbose=None):
from ..forward import Forward
from ..io import BaseRaw
from ..time_frequency import BaseTFR, CrossSpectralDensity
from ..time_frequency.spectrum import BaseSpectrum
Comment on lines 153 to +154
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Small note: can't import BaseSpectrum directly from the time_frequency module, and it's not included in the API reference, but BaseTFR is.


# Instances need to have a `ch_names` attribute and a `pick_channels`
# method that supports `ordered=True`.
allowed_types = (
BaseRaw,
BaseEpochs,
Evoked,
BaseSpectrum,
BaseTFR,
Forward,
Covariance,
CrossSpectralDensity,
Info,
)
allowed_types_str = (
"Raw, Epochs, Evoked, TFR, Forward, Covariance, CrossSpectralDensity or Info"
"Raw, Epochs, Evoked, Spectrum, TFR, Forward, Covariance, CrossSpectralDensity "
"or Info"
)
for inst in instances:
_validate_type(
Expand Down
4 changes: 4 additions & 0 deletions mne/time_frequency/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ def method(self):
def nave(self):
return self._nave

@nave.setter
def nave(self, nave):
self._nave = nave

@property
def weights(self):
return self._weights
Expand Down
15 changes: 13 additions & 2 deletions mne/time_frequency/tests/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from numpy.testing import assert_allclose, assert_array_equal

from mne import Annotations, BaseEpochs, create_info, make_fixed_length_epochs
from mne.channels import equalize_channels
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 @@ -200,8 +201,8 @@ def test_combine_spectrum(raw_spectrum, weights):
spectrum1 = raw_spectrum.copy()
spectrum2 = raw_spectrum.copy()
if weights == "nave":
spectrum1._nave = 1
spectrum2._nave = 2
spectrum1.nave = 1
spectrum2.nave = 2
spectrum2._data *= 2
new_spectrum = combine_spectrum([spectrum1, spectrum2], weights=weights)
assert_allclose(new_spectrum.data, spectrum1.data * (5 / 3))
Expand Down Expand Up @@ -243,6 +244,16 @@ def test_combine_spectrum_error_catch(raw_spectrum):
combine_spectrum([raw_spectrum, raw_spectrum2], weights="equal")


def test_equalize_channels(raw_spectrum):
"""Test equalization of channels for instances of `BaseSpectrum`."""
spect1 = raw_spectrum.copy()
spect2 = spect1.copy().pick(["MEG 0122", "MEG 0111"])
spect1, spect2 = equalize_channels([spect1, spect2])

assert spect1.ch_names == ["MEG 0111", "MEG 0122"]
assert spect2.ch_names == ["MEG 0111", "MEG 0122"]
Comment on lines +262 to +269
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This follows the existing tests for checking equalize_channels with Raw, Evoked, CSD, etc...



def test_spectrum_reject_by_annot(raw):
"""Test rejecting by annotation.

Expand Down
Loading