mne-tools / mne-python

MNE: Magnetoencephalography (MEG) and Electroencephalography (EEG) in Python
https://mne.tools
BSD 3-Clause "New" or "Revised" License
2.66k stars 1.31k forks source link

No `on_missing` in `mne.channels.combine_channels` #12591

Open toddrjen opened 4 months ago

toddrjen commented 4 months ago

Description of the problem

When using mne.channels.combine_channels on mne.Epochs or mne.EpochsArray objects with missing channels where the user had previously used on_missing='ignore' or on_missing='warn' it will raise an exception. There does not appear to be a way within mne.channels.combine_channels to avoid the exception.

Steps to reproduce

import mne

sample_data_folder = mne.datasets.sample.data_path()
sample_data_raw_file = sample_data_folder / "MEG" / "sample" / "sample_audvis_raw.fif"
raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False).crop(tmax=60)

events = mne.find_events(raw, stim_channel="STI 014")

# event ID 1000 does not exist
event_id = [5, 1000]

epochs = mne.Epochs(raw, events=events, event_id=event_id, on_missing='warn')

mne.channels.combine_channels(inst=epochs, groups={'test': [0, 1]})

Link to data

No response

Expected results

Ideally the previous use of on_missing='ignore' or on_missing='warn' would be stored and used again for constructing the new mne.EpochsArray object, but at least having an on_missing argument in mne.channels.combine_channels would allow this problem to be avoided.

Actual results

The following exception is raised, with apparently no way to avoid it other than manually removing the unused events:

ValueError: No matching events found for 1000 (event id 1000)

Additional information

Platform Linux-5.15.0-105-generic-x86_64-with-glibc2.31 Python 3.10.11 (main, May 16 2023, 00:28:57) [GCC 11.2.0] Executable /home/toddrjen/bin/anaconda3/envs/mne/bin/python3 CPU x86_64 (16 cores) Memory 31.2 GB

Core ├☑ mne 1.7.0 (latest release) ├☑ numpy 1.26.4 (OpenBLAS 0.3.23.dev with 16 threads) ├☑ scipy 1.13.0 └☑ matplotlib 3.8.4 (backend=TkAgg)

Numerical (optional) ├☑ sklearn 1.4.2 ├☑ numba 0.59.1 ├☑ nibabel 5.2.1 ├☑ pandas 2.2.1 └☐ unavailable nilearn, dipy, openmeeg, cupy, h5io, h5py

Visualization (optional) ├☑ ipympl 0.9.4 ├☑ ipywidgets 8.1.2 └☐ unavailable pyvista, pyvistaqt, vtk, qtpy, pyqtgraph, mne-qt-browser, trame_client, trame_server, trame_vtk, trame_vuetify

Ecosystem (optional) └☐ unavailable mne-bids, mne-nirs, mne-features, mne-connectivity, mne-icalabel, mne-bids-pipeline, neo, eeglabio, edfio, mffpy, pybv

welcome[bot] commented 4 months ago

Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴

sappelhoff commented 1 month ago

this is where on_missing would need to be added: https://github.com/mne-tools/mne-python/blob/43fb9d8c28193ad2c0e11c9282e65338e02f98b7/mne/channels/channels.py#L1970-L1978