Skip to content

epochs.plot(..., event_colors) not checking epochs.event_id #7101

@drammock

Description

@drammock

Describe the bug

epochs.plot(..., event_colors) raises value error when event_colors uses keys that are in event_id dict

Steps to reproduce

In [1]: import os                                                                                                           
In [2]: import mne                                                                                                          
In [3]: sample_data_folder = mne.datasets.sample.data_path()                                                                
In [4]: sample_data_raw_file = os.path.join(sample_data_folder, 'MEG', 'sample', 
   ...:                                     'sample_audvis_raw.fif')                                                        
In [5]: raw = mne.io.read_raw_fif(sample_data_raw_file, verbose=False).crop(tmax=120)                                       
In [6]: events = mne.find_events(raw, stim_channel='STI 014')                                                               
# 176 events found
# Event IDs: [ 1  2  3  4  5 32]
In [7]: event_dict = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3, 
   ...:               'visual/right': 4, 'face': 5, 'buttonpress': 32} 
In [8]: epochs = mne.Epochs(raw, events, tmin=-0.2, tmax=0.5, event_id=event_dict, 
   ...:                     preload=True)                                                                                   
# 176 matching events found
# Applying baseline correction (mode: mean)
# Not setting metadata
# Created an SSP operator (subspace dimension = 3)
# 3 projection items activated
# Loading data for 176 events and 421 original time points ...
# 1 bad epochs dropped
In [9]: epochs['face'].plot(events=events, event_colors={'buttonpress': 'red'})                                             
# 〈TRACEBACK OMITTED FOR BREVITY〉
# ValueError: Event ID buttonpress is in the color dict but is not present in events or event_id.
In [10]: epochs.event_id                                                                                                    
Out[10]: 
# {'auditory/left': 1,
#  'auditory/right': 2,
#  'visual/left': 3,
#  'visual/right': 4,
#  'face': 5,
#  'buttonpress': 32}

Expected results

epochs.plot(..., event_colors) should check its keys against keys in epochs.event_id and plot the event lines (in this case event 32) in the requested color.

Actual results

Here's the full traceback

Details
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-92c02e7b010c> in <module>
----> 1 epochs['face'].plot(events=events, event_colors={'buttonpress': 'red'})

/opt/mne/python/mne/epochs.py in plot(self, picks, scalings, n_epochs, n_channels, title, events, event_colors, order, show, block, decim, noise_cov, butterfly, show_scrollbars, epoch_colors)
   1051                            noise_cov=noise_cov, butterfly=butterfly,
   1052                            show_scrollbars=show_scrollbars,
-> 1053                            epoch_colors=epoch_colors)
   1054 
   1055     @copy_function_doc_to_method_doc(plot_epochs_psd)

/opt/mne/python/mne/viz/epochs.py in plot_epochs(epochs, picks, scalings, n_epochs, n_channels, title, events, event_colors, order, show, block, decim, noise_cov, butterfly, show_scrollbars, epoch_colors)
    850     _prepare_mne_browse_epochs(params, projs, n_channels, n_epochs, scalings,
    851                                title, picks, events=events, order=order,
--> 852                                event_colors=event_colors, butterfly=butterfly)
    853     _prepare_projectors(params)
    854 

/opt/mne/python/mne/viz/epochs.py in _prepare_mne_browse_epochs(params, projs, n_channels, n_epochs, scalings, title, picks, events, event_colors, order, butterfly, info)
   1110     if events is not None:
   1111         event_set = set(events[:, 2])
-> 1112         event_colors = _handle_event_colors(event_set, event_colors, event_set)
   1113         epoch_nr = False  # epoch number off by default to avoid overlap
   1114         for label in ax.xaxis.get_ticklabels():

/opt/mne/python/mne/viz/misc.py in _handle_event_colors(unique_events, color, unique_events_id)
   1035             if this_event not in unique_events_id:
   1036                 raise ValueError('Event ID %s is in the color dict but is not '
-> 1037                                  'present in events or event_id.' % this_event)
   1038 
   1039         for this_event in unique_events_id:

ValueError: Event ID buttonpress is in the color dict but is not present in events or event_id.

Additional information

mne.sys_info()

Details ``` Platform: Linux-4.15.0-70-generic-x86_64-with-debian-buster-sid Python: 3.7.3 | packaged by conda-forge | (default, Mar 27 2019, 23:01:00) [GCC 7.3.0] Executable: /opt/miniconda3/envs/mnedev/bin/python CPU: x86_64: 8 cores Memory: 7.6 GB

mne: 0.20.dev0
numpy: 1.16.4 {blas=blas, lapack=blas}
scipy: 1.3.0
matplotlib: 3.1.0 {backend=Qt5Agg}

sklearn: 0.21.2
numba: Not found
nibabel: 2.4.1
cupy: Not found
pandas: 0.24.2
dipy: 0.16.0
mayavi: 4.7.1.dev0 {qt_api=pyqt5, PyQt5=5.9.2}
pyvista: 0.20.4
vtk: 8.1.2

</details>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions