-
Notifications
You must be signed in to change notification settings - Fork 23
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
ENH: Single channel annotation interaction #255
Changes from 1 commit
6fa53e5
d1b556f
4c97dcc
52b5a9d
c425376
f2ba689
30567a4
64ec5b1
febfc98
fb1470b
94e9663
b13a246
4473d73
4531cb5
ce625dc
b03eb30
5713964
7688ca4
0c72430
fc1ab80
735b56b
ed93017
2ed88ad
1763088
cb89927
ebc495d
0bb8196
76921e1
c32fd84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,10 +3,12 @@ | |
# | ||
# License: BSD-3-Clause | ||
|
||
import mne.viz._figure | ||
import warnings | ||
|
||
import numpy as np | ||
import pytest | ||
from mne import Annotations | ||
from mne.utils import check_version | ||
from numpy.testing import assert_allclose | ||
from qtpy.QtCore import Qt | ||
from qtpy.QtTest import QTest | ||
|
@@ -101,7 +103,7 @@ def test_annotations_interactions(raw_orig, pg_backend): | |
fig.msg_box.close() | ||
|
||
|
||
def test_ch_specific_annot_display(raw_orig, pg_backend): | ||
def test_ch_specific_annot(raw_orig, pg_backend): | ||
"""Test plotting channel specific annotations.""" | ||
ch_names = ["MEG 0133", "MEG 0142", "MEG 0143", "MEG 0423"] | ||
annot_onset, annot_dur = 1, 2 | ||
|
@@ -145,30 +147,20 @@ def test_ch_specific_annot_display(raw_orig, pg_backend): | |
assert annot_dock.start_bx.value() == 4 | ||
assert single_channel_annot.lower.xData[0] == 4 | ||
|
||
fig.close() | ||
|
||
|
||
@pytest.mark.skipif( | ||
not hasattr(mne.viz._figure.BrowserBase, "_toggle_single_channel_annotation"), | ||
reason="needs MNE 1.8", | ||
) | ||
def test_ch_specific_annot_interactions(raw_orig, pg_backend): | ||
"""Test interactiveness and responsiveness of channel specific annotations.""" | ||
ch_names = ["MEG 0133", "MEG 0142", "MEG 0143", "MEG 0423"] | ||
annot_onset, annot_dur = 1, 2 | ||
annots = Annotations([annot_onset], [annot_dur], "some_chs", ch_names=[ch_names]) | ||
raw_orig.set_annotations(annots) | ||
|
||
fig = raw_orig.plot() | ||
fig.test_mode = True | ||
annot = fig.mne.regions[0] | ||
|
||
fig._fake_keypress("a") # activate annotation mode | ||
# MNE >= 1.8 | ||
if not check_version("mne", "1.8"): | ||
warning_message = ( | ||
"must update MNE to >= 1.8 to test single channel annots interactions" | ||
) | ||
# emit a warning if the user tries to test single channel annots | ||
with pytest.warns(UserWarning, match=warning_message): | ||
warnings.warn(warning_message, UserWarning) | ||
return | ||
|
||
# test if shift click an existing annotation removes object | ||
ch_index = np.mean(annot.single_channel_annots["MEG 0133"].ypos).astype(int) | ||
fig._fake_click( | ||
((annot_onset + annot_dur) / 2, ch_index), | ||
(4 + 2 / 2, ch_index), | ||
xform="data", | ||
button=1, | ||
modifier=Qt.ShiftModifier, | ||
|
@@ -177,7 +169,7 @@ def test_ch_specific_annot_interactions(raw_orig, pg_backend): | |
|
||
# test if shift click on channel adds annotation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Indeed mne-tools/mne-python#12669 hasn't landed and even once it does the
(and maybe rename the existing one
or similar. Then in this test suite should pass, and once mne-tools/mne-python#12669 lands tests will run on the Make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think so. I'll keep trying and see how far I can get |
||
fig._fake_click( | ||
((annot_onset + annot_dur) / 2, ch_index), | ||
(4 + 2 / 2, ch_index), | ||
xform="data", | ||
button=1, | ||
modifier=Qt.ShiftModifier, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you are testing that the warning you are emitting in the test function is emitted, which is not very interesting 😄 The idea is that if you run this test with MNE < 1.8 and run a
_fake_click
function that would add a channel specific annotation, it will issue the warning you added in_pg_figure
and it won't raise an exception because it exits early (and thus skips the channel specific annotation part):i.e. add the same
_fake_click
command which adds a channel specific annotation within thepytest.warns
context manager.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I'm not clearly understanding. Are you thinking something like this?
Should there be a context manager with pytest anywhere or does this suffice? I thought @larsoner suggested something of this form:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, in
_pg_figure
, i.e. in the code of the browser, you should emit a warning if the version of MNE is not compatible with a feature available in the version of browser. Imagine if a users runs a browser version compatible with channel-specific annotation but with MNE 1.6. The code within_pg_figure
will attempt to run the channel-specific annotation part, callself.weakmain()._toggle_single_channel_annotation
and crash. This is why in the code of_pg_figure
you need to check for the existence of the attribute_toggle_single_channel_annotation
inself.weakmain()
and issue a warning if it does not exist. This is what you did in 0bb8196 in the modification to_pg_figure
and is correct.Now, in the tests, consider the following test function which checks that a
_fake_click
creates a channel specific annotations:We have 2 scenarios: either we run this test function witth MNE 1.8, in which case it will work and pass; or we run it with an old MNE version and it will raise an error and fail. Now, since you modified
_pg_figure
with a warning emited for old MNE version, it will not raise an error anymore but issue the warning (and still fail as we automatically turn warnings into errors in our tests). Thus, you need to differentiate those 2 scenarios in the tests and to verify that the warning emited by_pg_figure
is indeed present.Let us know if it's still unclear, you are getting there!
FYI, I might not be able to join today, I'm at a workshop abroad all day; and I will be on vacation for 2 weeks as of next Thursday.