Skip to content

Commit

Permalink
MAINT refactor logic for event_id and events
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmainak committed Dec 23, 2015
1 parent 95baca0 commit 74b02c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 5 additions & 3 deletions mne/io/eeglab/eeglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ def __init__(self, input_fname, events=None, event_id=None, tmin=0,
eeg = io.loadmat(input_fname, struct_as_record=False,
squeeze_me=True)['EEG']

if not ((events is None and event_id is None) or
(events is not None and event_id is not None)):
raise ValueError('Both `events` and `event_id` must be '
'None or not None')

if events is None and eeg.trials > 1:
# first extract the events and construct an event_id dict
event_name, event_latencies, unique_ev = [], [], []
Expand Down Expand Up @@ -366,9 +371,6 @@ def __init__(self, input_fname, events=None, event_id=None, tmin=0,
input_fname = op.abspath(input_fname)
info = _get_info(eeg, montage)

if event_id is None: # convert to int to make typing-checks happy
event_id = dict((ev, idx) for idx, ev in enumerate(unique_ev))

for key, val in event_id.items():
if val not in events[:, 2]:
raise ValueError('No matching events found for %s '
Expand Down
26 changes: 24 additions & 2 deletions mne/io/eeglab/tests/test_eeglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,43 @@

import os.path as op

import warnings
from nose.tools import assert_raises, assert_equal

from mne import write_events
from mne.io import read_raw_eeglab, read_epochs_eeglab
from mne.io.tests.test_raw import _test_raw_reader
from mne.datasets import testing
from mne.utils import run_tests_if_main
from mne.utils import _TempDir, run_tests_if_main

base_dir = op.join(testing.data_path(download=False), 'EEGLAB')
raw_fname = op.join(base_dir, 'test_raw.set')
epochs_fname = op.join(base_dir, 'test_epochs.set')
montage = op.join(base_dir, 'test_chans.locs')

warnings.simplefilter('always') # enable b/c these tests throw warnings


@testing.requires_testing_data
def test_io_set():
"""Test importing EEGLAB .set files"""
_test_raw_reader(read_raw_eeglab, input_fname=raw_fname, montage=montage)
read_epochs_eeglab(epochs_fname)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
epochs = read_epochs_eeglab(epochs_fname)
assert_equal(len(w), 3)

temp_dir = _TempDir()
out_fname = op.join(temp_dir, 'test-eve.fif')
write_events(out_fname, epochs.events)
event_id = {'S255/S8': 1, 'S8': 2, 'S255/S9': 3}

epochs = read_epochs_eeglab(epochs_fname, epochs.events, event_id)
epochs = read_epochs_eeglab(epochs_fname, out_fname, event_id)
assert_raises(ValueError, read_epochs_eeglab, epochs_fname,
None, event_id)
assert_raises(ValueError, read_epochs_eeglab, epochs_fname,
epochs.events, None)

run_tests_if_main()

0 comments on commit 74b02c4

Please sign in to comment.