Skip to content

Commit

Permalink
MRG: warn when read_events drops/masks events; allow mask=None (#3197)
Browse files Browse the repository at this point in the history
* warn when read_events drops/masks events; allow mask=None

* add tests, don't skip first event when mask=None
  • Loading branch information
drammock authored and agramfort committed May 7, 2016
1 parent 76245d0 commit 5a71c71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 12 additions & 5 deletions mne/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ def read_events(filename, include=None, exclude=None, mask=0):
A event id to exclude or a list of them.
If None no event is excluded. If include is not None
the exclude parameter is ignored.
mask : int
mask : int | None
The value of the digital mask to apply to the stim channel values.
The default value is 0.
The default value is 0. ``None`` skips masking.
Returns
-------
Expand Down Expand Up @@ -257,12 +257,19 @@ def read_events(filename, include=None, exclude=None, mask=0):
raise ValueError('Unknown number of columns in event text file')

event_list = lines[:, goods]
if event_list.shape[0] > 0 and event_list[0, 2] == 0:
if (mask is not None and event_list.shape[0] > 0 and
event_list[0, 2] == 0):
event_list = event_list[1:]
warn('first row of event file discarded (zero-valued)')

event_list = pick_events(event_list, include, exclude)
event_list = _mask_trigs(event_list, mask)

unmasked_len = event_list.shape[0]
if mask is not None:
event_list = _mask_trigs(event_list, mask)
masked_len = event_list.shape[0]
if masked_len < unmasked_len:
warn('{0} of {1} events masked'.format(unmasked_len - masked_len,
unmasked_len))
return event_list


Expand Down
17 changes: 15 additions & 2 deletions mne/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ def test_io_events():
write_events(op.join(tempdir, 'events.eve'), events)
events2 = read_events(op.join(tempdir, 'events.eve'))
assert_array_almost_equal(events, events2)
events2 = read_events(fname_txt_mpr)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
events2 = read_events(fname_txt_mpr)
assert_true(sum('first row of' in str(ww.message) for ww in w) == 1)
assert_array_almost_equal(events, events2)

# Test old format text file IO
Expand All @@ -138,6 +141,13 @@ def test_io_events():
assert_array_equal(a, c)
assert_array_equal(a, d)

# test reading file with mask=None
events2 = events.copy()
events2[:, -1] = range(events2.shape[0])
write_events(op.join(tempdir, 'events-eve.fif'), events2)
events3 = read_events(op.join(tempdir, 'events-eve.fif'), mask=None)
assert_array_almost_equal(events2, events3)

# Test binary file IO for 1 event
events = read_events(fname_1) # Use as the new gold standard
write_events(op.join(tempdir, 'events-eve.fif'), events)
Expand Down Expand Up @@ -172,7 +182,10 @@ def test_find_events():
assert_array_almost_equal(events, events2)
# now test with mask
events11 = find_events(raw, mask=3)
events22 = read_events(fname, mask=3)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
events22 = read_events(fname, mask=3)
assert_true(sum('events masked' in str(ww.message) for ww in w) == 1)
assert_array_equal(events11, events22)

# Reset some data for ease of comparison
Expand Down

0 comments on commit 5a71c71

Please sign in to comment.