Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Handle events with non-unique event time samples #6688

Merged
merged 45 commits into from Aug 29, 2019

Conversation

sappelhoff
Copy link
Member

@sappelhoff sappelhoff commented Aug 21, 2019

fixes #6665

To Do

  • update event_id by removing a key-value-pair if the value is no longer in events after handle_duplicate_events
  • if event_repeated is "merge" but a duplicate actually has the same event code, switch to "drop" behavior
  • fix for 31d1eda: instead of updating events on every loop iteration, collect indices to delete and deleta all at once. --> see 323b61a
  • 2nd fix for 31d1eda: "aud/vis" and "vis/aud" should map to the same thing --> see e72fcaa
  • updated whatsnew

@sappelhoff
Copy link
Member Author

I am realizing that the '%s/%s' % (event_1_key, event_2_key) does not work, because in the mne events, we expect integers ... not strings.

Any advice @larsoner ?

@larsoner
Copy link
Member

You need to

  1. Come up with new event numbers for the events array, and
  2. associate these properly with event_id dict

For example if in neither events nor event_id they use the integer 5 for anything, then you would make your events_id['x/y'] = 5 and set events[idx, 2] = 5

@lgtm-com
Copy link

lgtm-com bot commented Aug 21, 2019

This pull request introduces 2 alerts when merging c03b591 into bf5df34 - view on LGTM.com

new alerts:

  • 2 for Unused local variable

mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
@jasmainak
Copy link
Member

@sappelhoff you also need to update some tests

@sappelhoff sappelhoff marked this pull request as ready for review August 22, 2019 13:48
@sappelhoff
Copy link
Member Author

Okay, I think this is a reasonable proposal now @jasmainak @larsoner @agramfort @massich

Before I start to implement testing, please let me know what you think.

Also: I need advise on what I need to edit if I want this parameter to be available in Epochs, EpochsArray, BaseEpochs, and so on.

mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
@jasmainak
Copy link
Member

Please go ahead and write the tests. It will help us check the logic better.

@codecov
Copy link

codecov bot commented Aug 22, 2019

Codecov Report

Merging #6688 into master will increase coverage by <.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #6688      +/-   ##
==========================================
+ Coverage   89.48%   89.48%   +<.01%     
==========================================
  Files         420      420              
  Lines       75474    75596     +122     
  Branches    12369    12388      +19     
==========================================
+ Hits        67535    67647     +112     
- Misses       5134     5137       +3     
- Partials     2805     2812       +7

@sappelhoff sappelhoff changed the title Handle events with non-unique event time samples [MRG] Handle events with non-unique event time samples Aug 22, 2019
"""Test handling of duplicate events."""
# A general test case
EVENT_ID = {'aud': 1, 'vis': 2}
EVENTS = np.array([[0, 0, 1], [0, 0, 2]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why capital letters?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because these are my "constants" for the test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename them EXPECTED_BLA and I would not use them on the input. ;)

mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
@sappelhoff
Copy link
Member Author

Okay, tests are fully implemented now. Ready for another review :-)

mne/epochs.py Outdated
@@ -183,6 +184,93 @@ def _save_split(epochs, fname, part_idx, n_parts, fmt):
end_file(fid)


def _handle_duplicate_events(events, event_id, event_repeated):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like event_repeated_policy better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_handle is a really ugly name.

what about def _apply_duplicate_events_policy(evetns, event_id, policy) or simply _duplicate_events_policy?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather not make the name longer. There is a tradeoff between explicit name clarity and usability. There is a good reason we don't go farther down the road of making it even more explicit like how_to_behave_when_events_occur_on_the_same_sample. I think event_repeated is clear enough as it is and more in line with how we tend to balance clarity with brevity.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll stay with event_repeated then, but will rename _handle_duplicate_events -> _handle_event_repeated

why don't you like "handle" @massich ? We are "handling" a situation after all?

mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/epochs.py Show resolved Hide resolved
mne/epochs.py Show resolved Hide resolved
mne/epochs.py Outdated
@@ -183,6 +184,93 @@ def _save_split(epochs, fname, part_idx, n_parts, fmt):
end_file(fid)


def _handle_duplicate_events(events, event_id, event_repeated):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_handle is a really ugly name.

what about def _apply_duplicate_events_policy(evetns, event_id, policy) or simply _duplicate_events_policy?

"""Test handling of duplicate events."""
# A general test case
EVENT_ID = {'aud': 1, 'vis': 2}
EVENTS = np.array([[0, 0, 1], [0, 0, 2]])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename them EXPECTED_BLA and I would not use them on the input. ;)

mne/tests/test_epochs.py Outdated Show resolved Hide resolved
@@ -57,6 +59,66 @@
rng = np.random.RandomState(42)


def test_handle_duplicate_events():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we parametrize some of it? (I've not checked)

mne/epochs.py Outdated Show resolved Hide resolved
@massich
Copy link
Contributor

massich commented Aug 23, 2019

all the code doing the job (the part inside merge) is really weird.

@agramfort
Copy link
Member

@sappelhoff while trying the code I tried to the make the test more ambitious. It breaks the tests. Please have a look.

@sappelhoff
Copy link
Member Author

all the code doing the job (the part inside merge) is really weird.

I am following what @agramfort and @jasmainak told me:

  1. make it work
  2. make it nice
  3. make it fast

I feel like I can use your feedback well for step 2. @massich :-)

@sappelhoff
Copy link
Member Author

Thanks for the reviews everybody. I addressed almost everything and the code is much better now (in the second iteration).

Unfortunately I won't have time to do a third iteration ... so it'd be great if somebody could take it from here!

@lgtm-com
Copy link

lgtm-com bot commented Aug 25, 2019

This pull request introduces 1 alert when merging 66fd60a into bf03e17 - view on LGTM.com

new alerts:

  • 1 for Suspicious unused loop iteration variable

@agramfort
Copy link
Member

agramfort commented Aug 25, 2019 via email

@sappelhoff
Copy link
Member Author

Let us know when it's ready for reviews.

it's ready now - I just fixed a LGTM issue and a pydocstyle thing .... CIs should come back all green now.

mne/epochs.py Show resolved Hide resolved
mne/epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
mne/tests/test_epochs.py Outdated Show resolved Hide resolved
Co-Authored-By: Alexandre Gramfort <alexandre.gramfort@m4x.org>
Copy link
Member

@agramfort agramfort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasmainak or @massich feel free to merge if happy

@jasmainak
Copy link
Member

glad I didn't volunteer for this issue ;-) @massich I let you merge since I didn't follow the developments very closely

mne/epochs.py Show resolved Hide resolved
@massich
Copy link
Contributor

massich commented Aug 29, 2019

LGTM

@agramfort agramfort merged commit 490e39b into mne-tools:master Aug 29, 2019
@sappelhoff sappelhoff deleted the uni branch August 29, 2019 10:30
alexrockhill pushed a commit to alexrockhill/mne-python that referenced this pull request Oct 1, 2019
* add skeleton

* show blocker

* clarify docstr

* add option 'drop'

* find new event code and update event_id

* dont reuse event codes that are in events

* typo

* continue work in private func

* use event_id combination to make up new event_id

* improve docstr

* flat is better than nested

* add first test

* fix lists and concat->concatenate

* add more tests

* one more test

* add event_repeated param to epochs class

* do not put np.nan into int array

* better copy handling

* also consider 'prior-to-event' codes in selecting new code

* test fallback for prior event codes and mixing up

* update event_id after removing vals from events

* test keeping homogeneous 'prior-to-event' code

* revert to 'drop' behavior if duplicates have equal event code

* add short example to notes section

* make the test more ambitious + reveal issues

* only delete indices after loop

* fix aud/vis to be same code as vis/aud

* fix alex's tests

* _handle_duplicate_events -> _handle_event_repeated

* use _check_option instead of custom code

* remove duplicated tests

* simplify pruning of event_id

* improve logging msg and readability

* cleanup section for setting new events[:, 1] val

* event_code -> event_val to disambiguate between key/value/code

* refactor: pull _merge_events into its own func

* incorporate several points by @jona-sassenhagen

* more feedback by jona

* improve doc

* minor docs

* add whatsnew

* get rid of superfluous loop

* fix pydocstyle

* np.testing can go away

Co-Authored-By: Alexandre Gramfort <alexandre.gramfort@m4x.org>

* using for...else construct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Making Epochs when two different events happend at the same time (=duplicate timepoints)
6 participants