def test_plot_annotations(): """Test annotation mode of the plotter.""" import matplotlib.pyplot as plt raw = _get_raw() fig = raw.plot() data_ax = fig.axes[0] fig.canvas.key_press_event('a') # annotation mode # modify description ann_fig = plt.gcf() for key in ' test': ann_fig.canvas.key_press_event(key) ann_fig.canvas.key_press_event('enter') ann_fig = plt.gcf() # XXX: _fake_click raises an error on Agg backend _annotation_radio_clicked('', ann_fig.radio, data_ax.selector) # draw annotation _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='release') # hover event _fake_click(fig, data_ax, [4.5, 1.], xform='data', button=None, kind='motion') _fake_click(fig, data_ax, [4.7, 1.], xform='data', button=None, kind='motion') # modify annotation from end _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='release') # modify annotation from beginning _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [1.1, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [1.1, 1.], xform='data', button=1, kind='release') assert_equal(len(raw.annotations.onset), 1) assert_equal(len(raw.annotations.duration), 1) assert_equal(len(raw.annotations.description), 1) assert_equal(raw.annotations.description[0], 'BAD test') # draw another annotation merging the two _fake_click(fig, data_ax, [5.5, 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='release') # delete the annotation _fake_click(fig, data_ax, [1.5, 1.], xform='data', button=3, kind='press') fig.canvas.key_press_event('a') # exit annotation mode plt.close('all') assert_equal(len(raw.annotations.onset), 0) assert_equal(len(raw.annotations.duration), 0) assert_equal(len(raw.annotations.description), 0) plt.close('all') raw.annotations = Annotations([1], [1], 'test', raw.info['meas_date']) fig = raw.plot() assert_raises(NotImplementedError, fig.canvas.key_press_event, 'a') plt.close('all')
def _annotation_helper(raw): """Helper for testing interactive annotations.""" import matplotlib.pyplot as plt n_anns = 0 if raw.annotations is None else len(raw.annotations.onset) fig = raw.plot() data_ax = fig.axes[0] fig.canvas.key_press_event('a') # annotation mode # modify description ann_fig = plt.gcf() for key in ' test': ann_fig.canvas.key_press_event(key) ann_fig.canvas.key_press_event('enter') ann_fig = plt.gcf() # XXX: _fake_click raises an error on Agg backend _annotation_radio_clicked('', ann_fig.radio, data_ax.selector) # draw annotation _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='release') # hover event _fake_click(fig, data_ax, [4.5, 1.], xform='data', button=None, kind='motion') _fake_click(fig, data_ax, [4.7, 1.], xform='data', button=None, kind='motion') # modify annotation from end _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='release') # modify annotation from beginning _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [1.1, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [1.1, 1.], xform='data', button=1, kind='release') assert_equal(len(raw.annotations.onset), n_anns + 1) assert_equal(len(raw.annotations.duration), n_anns + 1) assert_equal(len(raw.annotations.description), n_anns + 1) assert_equal(raw.annotations.description[n_anns], 'BAD test') # draw another annotation merging the two _fake_click(fig, data_ax, [5.5, 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='release') # delete the annotation _fake_click(fig, data_ax, [1.5, 1.], xform='data', button=3, kind='press') fig.canvas.key_press_event('a') # exit annotation mode assert_equal(len(raw.annotations.onset), n_anns) assert_equal(len(raw.annotations.duration), n_anns) assert_equal(len(raw.annotations.description), n_anns) plt.close('all')
def _annotation_helper(raw, events=False): """Test interactive annotations.""" # Some of our checks here require modern mpl to work properly mpl_good_enough = LooseVersion(matplotlib.__version__) >= '2.0' n_anns = len(raw.annotations) plt.close('all') if events: events = np.array([[raw.first_samp + 100, 0, 1], [raw.first_samp + 300, 0, 3]]) n_events = len(events) else: events = None n_events = 0 fig = raw.plot(events=events) assert len(plt.get_fignums()) == 1 data_ax = fig.axes[0] fig.canvas.key_press_event('a') # annotation mode assert len(plt.get_fignums()) == 2 # +2 from the scale bars n_scale = 2 assert len(fig.axes[0].texts) == n_anns + n_events + n_scale # modify description ann_fig = plt.gcf() for key in ' test': ann_fig.canvas.key_press_event(key) ann_fig.canvas.key_press_event('enter') ann_fig = plt.gcf() # XXX: _fake_click raises an error on Agg backend _annotation_radio_clicked('', ann_fig.radio, data_ax.selector) # draw annotation fig.canvas.key_press_event('p') # use snap mode _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='release') assert len(raw.annotations.onset) == n_anns + 1 assert len(raw.annotations.duration) == n_anns + 1 assert len(raw.annotations.description) == n_anns + 1 assert raw.annotations.description[n_anns] == 'BAD_ test' assert len(fig.axes[0].texts) == n_anns + 1 + n_events + n_scale onset = raw.annotations.onset[n_anns] want_onset = _sync_onset(raw, 1., inverse=True) assert_allclose(onset, want_onset) assert_allclose(raw.annotations.duration[n_anns], 4.) # hover event _fake_click(fig, data_ax, [4.5, 1.], xform='data', button=None, kind='motion') _fake_click(fig, data_ax, [4.7, 1.], xform='data', button=None, kind='motion') # modify annotation from end _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='release') if mpl_good_enough: assert raw.annotations.onset[n_anns] == onset assert_allclose(raw.annotations.duration[n_anns], 1.5) # modify annotation from beginning _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [0.5, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [0.5, 1.], xform='data', button=1, kind='release') if mpl_good_enough: assert_allclose(raw.annotations.onset[n_anns], onset - 0.5, atol=1e-10) assert_allclose(raw.annotations.duration[n_anns], 2.0) assert len(raw.annotations.onset) == n_anns + 1 assert len(raw.annotations.duration) == n_anns + 1 assert len(raw.annotations.description) == n_anns + 1 assert raw.annotations.description[n_anns] == 'BAD_ test' assert len(fig.axes[0].texts) == n_anns + 1 + n_events + n_scale fig.canvas.key_press_event('shift+right') assert len(fig.axes[0].texts) == n_scale fig.canvas.key_press_event('shift+left') assert len(fig.axes[0].texts) == n_anns + 1 + n_events + n_scale # draw another annotation merging the two _fake_click(fig, data_ax, [5.5, 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='release') # delete the annotation assert len(raw.annotations.onset) == n_anns + 1 assert len(raw.annotations.duration) == n_anns + 1 assert len(raw.annotations.description) == n_anns + 1 if mpl_good_enough: assert_allclose(raw.annotations.onset[n_anns], onset - 0.5, atol=1e-10) assert_allclose(raw.annotations.duration[n_anns], 5.0) assert len(fig.axes[0].texts) == n_anns + 1 + n_events + n_scale # Delete _fake_click(fig, data_ax, [1.5, 1.], xform='data', button=3, kind='press') fig.canvas.key_press_event('a') # exit annotation mode assert len(raw.annotations.onset) == n_anns assert len(fig.axes[0].texts) == n_anns + n_events + n_scale fig.canvas.key_press_event('shift+right') assert len(fig.axes[0].texts) == n_scale fig.canvas.key_press_event('shift+left') assert len(fig.axes[0].texts) == n_anns + n_events + n_scale plt.close('all')
def _annotation_helper(raw, events=False): """Helper for testing interactive annotations.""" import matplotlib.pyplot as plt # Some of our checks here require modern mpl to work properly mpl_good_enough = LooseVersion(matplotlib.__version__) >= '2.0' n_anns = 0 if raw.annotations is None else len(raw.annotations.onset) plt.close('all') if events: events = np.array([[raw.first_samp + 100, 0, 1], [raw.first_samp + 300, 0, 3]]) n_events = len(events) else: events = None n_events = 0 fig = raw.plot(events=events) assert len(plt.get_fignums()) == 1 data_ax = fig.axes[0] fig.canvas.key_press_event('a') # annotation mode assert len(plt.get_fignums()) == 2 assert len(fig.axes[0].texts) == n_anns + n_events # modify description ann_fig = plt.gcf() for key in ' test': ann_fig.canvas.key_press_event(key) ann_fig.canvas.key_press_event('enter') ann_fig = plt.gcf() # XXX: _fake_click raises an error on Agg backend _annotation_radio_clicked('', ann_fig.radio, data_ax.selector) # draw annotation _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='release') assert len(raw.annotations.onset) == n_anns + 1 assert len(raw.annotations.duration) == n_anns + 1 assert len(raw.annotations.description) == n_anns + 1 assert raw.annotations.description[n_anns] == 'BAD_ test' assert len(fig.axes[0].texts) == n_anns + 1 + n_events onset = raw.annotations.onset[n_anns] want_onset = _sync_onset(raw, 1., inverse=True) assert_allclose(onset, want_onset) assert_allclose(raw.annotations.duration[n_anns], 4.) # hover event _fake_click(fig, data_ax, [4.5, 1.], xform='data', button=None, kind='motion') _fake_click(fig, data_ax, [4.7, 1.], xform='data', button=None, kind='motion') # modify annotation from end _fake_click(fig, data_ax, [5., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2.5, 1.], xform='data', button=1, kind='release') if mpl_good_enough: assert raw.annotations.onset[n_anns] == onset assert_allclose(raw.annotations.duration[n_anns], 1.5) # modify annotation from beginning _fake_click(fig, data_ax, [1., 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [0.5, 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [0.5, 1.], xform='data', button=1, kind='release') if mpl_good_enough: assert_allclose(raw.annotations.onset[n_anns], onset - 0.5, atol=1e-10) assert_allclose(raw.annotations.duration[n_anns], 2.0) assert len(raw.annotations.onset) == n_anns + 1 assert len(raw.annotations.duration) == n_anns + 1 assert len(raw.annotations.description) == n_anns + 1 assert raw.annotations.description[n_anns] == 'BAD_ test' assert len(fig.axes[0].texts) == n_anns + 1 + n_events fig.canvas.key_press_event('shift+right') assert len(fig.axes[0].texts) == 0 fig.canvas.key_press_event('shift+left') assert len(fig.axes[0].texts) == n_anns + 1 + n_events # draw another annotation merging the two _fake_click(fig, data_ax, [5.5, 1.], xform='data', button=1, kind='press') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='motion') _fake_click(fig, data_ax, [2., 1.], xform='data', button=1, kind='release') # delete the annotation assert len(raw.annotations.onset) == n_anns + 1 assert len(raw.annotations.duration) == n_anns + 1 assert len(raw.annotations.description) == n_anns + 1 if mpl_good_enough: assert_allclose(raw.annotations.onset[n_anns], onset - 0.5, atol=1e-10) assert_allclose(raw.annotations.duration[n_anns], 5.0) assert len(fig.axes[0].texts) == n_anns + 1 + n_events # Delete _fake_click(fig, data_ax, [1.5, 1.], xform='data', button=3, kind='press') fig.canvas.key_press_event('a') # exit annotation mode assert len(raw.annotations.onset) == n_anns assert len(fig.axes[0].texts) == n_anns + n_events fig.canvas.key_press_event('shift+right') assert len(fig.axes[0].texts) == 0 fig.canvas.key_press_event('shift+left') assert len(fig.axes[0].texts) == n_anns + n_events plt.close('all')