Skip to content

Commit 493418e

Browse files
cbrnragramfort
andauthored
Add metadata to combine_channels (#10504)
* Add metadata to combine_channels * Update mne/channels/channels.py Co-authored-by: Alexandre Gramfort <[email protected]> * Add changelog entry * Add test Co-authored-by: Alexandre Gramfort <[email protected]>
1 parent 321cafc commit 493418e

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

doc/changes/latest.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Bugs
3535

3636
- Fix behavior for the ``pyvista`` 3D renderer's ``quiver3D`` function so that default arguments plot a glyph in ``arrow`` mode (:gh:`10493` by `Alex Rockhill`_)
3737
38+
- Retain epochs metadata when using :func:`mne.channels.combine_channels` (:gh:`10504` by `Clemens Brunner`_)
39+
3840
API changes
3941
~~~~~~~~~~~
4042
- None yet

mne/channels/channels.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,8 @@ def combine_channels(inst, groups, method='mean', keep_stim=False,
17101710
elif isinstance(inst, BaseEpochs):
17111711
combined_inst = EpochsArray(new_data, info, events=inst.events,
17121712
tmin=inst.times[0])
1713+
if inst.metadata is not None:
1714+
combined_inst.metadata = inst.metadata.copy()
17131715
elif isinstance(inst, Evoked):
17141716
combined_inst = EvokedArray(new_data, info, tmin=inst.times[0])
17151717

mne/channels/tests/test_channels.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from mne import (pick_types, pick_channels, EpochsArray, EvokedArray,
2525
make_ad_hoc_cov, create_info, read_events, Epochs)
2626
from mne.datasets import testing
27-
from mne.utils import requires_version
27+
from mne.utils import requires_pandas, requires_version
2828

2929
io_dir = op.join(op.dirname(__file__), '..', '..', 'io')
3030
base_dir = op.join(io_dir, 'tests', 'data')
@@ -520,3 +520,20 @@ def test_combine_channels():
520520
combine_channels(raw, warn2)
521521
combine_channels(raw_ch_bad, warn3, drop_bad=True)
522522
assert len(record) == 3
523+
524+
525+
@requires_pandas
526+
def test_combine_channels_metadata():
527+
"""Test if metadata is correctly retained in combined object."""
528+
import pandas as pd
529+
530+
raw = read_raw_fif(raw_fname, preload=True)
531+
epochs = Epochs(raw, read_events(eve_fname), preload=True)
532+
533+
metadata = pd.DataFrame({"A": np.arange(len(epochs)),
534+
"B": np.ones(len(epochs))})
535+
epochs.metadata = metadata
536+
537+
good = dict(foo=[0, 1, 3, 4], bar=[5, 2]) # good grad and mag
538+
combined_epochs = combine_channels(epochs, good)
539+
pd.testing.assert_frame_equal(epochs.metadata, combined_epochs.metadata)

0 commit comments

Comments
 (0)