def test_plot_raw(): """Test plotting of raw data """ import matplotlib.pyplot as plt raw = _get_raw() events = _get_events() plt.close("all") # ensure all are closed with warnings.catch_warnings(record=True): fig = raw.plot(events=events, show_options=True) # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.get_axes()[0] _fake_click(fig, data_ax, [x, y], xform="data") # mark a bad channel _fake_click(fig, data_ax, [x, y], xform="data") # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels _fake_click(fig, fig.get_axes()[3], [0.5, 0.5]) # open SSP window fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up # sadly these fail when no renderer is used (i.e., when using Agg): # ssp_fig = set(plt.get_fignums()) - set([fig.number]) # assert_equal(len(ssp_fig), 1) # ssp_fig = plt.figure(list(ssp_fig)[0]) # ax = ssp_fig.get_axes()[0] # only one axis is used # t = [c for c in ax.get_children() if isinstance(c, # matplotlib.text.Text)] # pos = np.array(t[0].get_position()) + 0.01 # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on # test keypresses fig.canvas.key_press_event("escape") fig.canvas.key_press_event("down") fig.canvas.key_press_event("up") fig.canvas.key_press_event("right") fig.canvas.key_press_event("left") fig.canvas.key_press_event("o") fig.canvas.key_press_event("-") fig.canvas.key_press_event("+") fig.canvas.key_press_event("=") fig.canvas.key_press_event("pageup") fig.canvas.key_press_event("pagedown") fig.canvas.key_press_event("home") fig.canvas.key_press_event("end") fig.canvas.key_press_event("?") fig.canvas.key_press_event("f11") fig.canvas.key_press_event("escape") # Color setting assert_raises(KeyError, raw.plot, event_color={0: "r"}) assert_raises(TypeError, raw.plot, event_color={"foo": "r"}) annot = Annotations( [10, 10 + raw.first_samp / raw.info["sfreq"]], [10, 10], ["test", "test"], raw.info["meas_date"] ) raw.annotations = annot fig = plot_raw(raw, events=events, event_color={-1: "r", 998: "b"}) plt.close("all")
def test_plot_raw(): """Test plotting of raw data """ import matplotlib.pyplot as plt raw = _get_raw() events = _get_events() plt.close('all') # ensure all are closed with warnings.catch_warnings(record=True): fig = raw.plot(events=events, show_options=True) # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.get_axes()[0] _fake_click(fig, data_ax, [x, y], xform='data') # mark a bad channel _fake_click(fig, data_ax, [x, y], xform='data') # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels _fake_click(fig, fig.get_axes()[3], [0.5, 0.5]) # open SSP window fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up # sadly these fail when no renderer is used (i.e., when using Agg): # ssp_fig = set(plt.get_fignums()) - set([fig.number]) # assert_equal(len(ssp_fig), 1) # ssp_fig = plt.figure(list(ssp_fig)[0]) # ax = ssp_fig.get_axes()[0] # only one axis is used # t = [c for c in ax.get_children() if isinstance(c, # matplotlib.text.Text)] # pos = np.array(t[0].get_position()) + 0.01 # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on # test keypresses fig.canvas.key_press_event('escape') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.key_press_event('right') fig.canvas.key_press_event('left') fig.canvas.key_press_event('o') fig.canvas.key_press_event('-') fig.canvas.key_press_event('+') fig.canvas.key_press_event('=') fig.canvas.key_press_event('pageup') fig.canvas.key_press_event('pagedown') fig.canvas.key_press_event('home') fig.canvas.key_press_event('end') fig.canvas.key_press_event('?') fig.canvas.key_press_event('f11') fig.canvas.key_press_event('escape') # Color setting assert_raises(KeyError, raw.plot, event_color={0: 'r'}) assert_raises(TypeError, raw.plot, event_color={'foo': 'r'}) annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) raw.annotations = annot fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all')
def test_plot_raw(): """Test plotting of raw data """ import matplotlib.pyplot as plt raw = _get_raw() events = _get_events() plt.close('all') # ensure all are closed with warnings.catch_warnings(record=True): fig = raw.plot(events=events, show_options=True) # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.get_axes()[0] _fake_click(fig, data_ax, [x, y], xform='data') # mark a bad channel _fake_click(fig, data_ax, [x, y], xform='data') # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels _fake_click(fig, fig.get_axes()[3], [0.5, 0.5]) # open SSP window fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up # sadly these fail when no renderer is used (i.e., when using Agg): # ssp_fig = set(plt.get_fignums()) - set([fig.number]) # assert_equal(len(ssp_fig), 1) # ssp_fig = plt.figure(list(ssp_fig)[0]) # ax = ssp_fig.get_axes()[0] # only one axis is used # t = [c for c in ax.get_children() if isinstance(c, # matplotlib.text.Text)] # pos = np.array(t[0].get_position()) + 0.01 # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on # test keypresses fig.canvas.key_press_event('escape') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.key_press_event('right') fig.canvas.key_press_event('left') fig.canvas.key_press_event('o') fig.canvas.key_press_event('-') fig.canvas.key_press_event('+') fig.canvas.key_press_event('=') fig.canvas.key_press_event('pageup') fig.canvas.key_press_event('pagedown') fig.canvas.key_press_event('home') fig.canvas.key_press_event('end') fig.canvas.key_press_event('?') fig.canvas.key_press_event('f11') fig.canvas.key_press_event('escape') # Color setting assert_raises(KeyError, raw.plot, event_color={0: 'r'}) assert_raises(TypeError, raw.plot, event_color={'foo': 'r'}) fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all')
def test_plot_raw_keypresses(raw): """Test keypress interactivity of plot_raw().""" raw.info['lowpass'] = 10. # allow heavy decim during plotting fig = raw.plot() # test twice → once in normal, once in butterfly view. # NB: keys a, j, and ? are tested in test_plot_raw_child_figures() keys = ('pagedown', 'down', 'up', 'down', 'right', 'left', '-', '+', '=', 'd', 'd', 'pageup', 'home', 'end', 'z', 'z', 's', 's', 'f11', 'b') # test for group_by='original' for key in 2 * keys + ('escape',): fig.canvas.key_press_event(key) # test for group_by='selection' fig = plot_raw(raw, group_by='selection') for key in 2 * keys + ('escape',): fig.canvas.key_press_event(key)
def test_plot_raw_keypresses(raw, browser_backend, monkeypatch): """Test keypress interactivity of plot_raw().""" with raw.info._unlock(): raw.info['lowpass'] = 10. # allow heavy decim during plotting fig = raw.plot() # test twice → once in normal, once in butterfly view. # NB: keys a, j, and ? are tested in test_plot_raw_child_figures() keys = ('pagedown', 'down', 'up', 'down', 'right', 'left', '-', '+', '=', 'd', 'd', 'pageup', 'home', 'end', 'z', 'z', 's', 's', 'f11', 't', 'b') # Avoid annoying fullscreen issues by monkey-patching our handlers _monkeypatch_fig(fig, browser_backend) # test for group_by='original' for key in 2 * keys + ('escape', ): fig._fake_keypress(key) # test for group_by='selection' fig = plot_raw(raw, group_by='selection') _monkeypatch_fig(fig, browser_backend) for key in 2 * keys + ('escape', ): fig._fake_keypress(key)
def test_plot_raw_traces(raw, events, browser_backend): """Test plotting of raw data.""" ismpl = browser_backend.name == 'matplotlib' with raw.info._unlock(): raw.info['lowpass'] = 10. # allow heavy decim during plotting fig = raw.plot(events=events, order=[1, 7, 5, 2, 3], n_channels=3, group_by='original') assert hasattr(fig, 'mne') # make sure fig.mne param object is present if ismpl: assert len(fig.axes) == 5 # setup x = fig.mne.traces[0].get_xdata()[5] y = fig.mne.traces[0].get_ydata()[5] hscroll = fig.mne.ax_hscroll vscroll = fig.mne.ax_vscroll # test marking bad channels label = fig._get_ticklabels('y')[0] assert label not in fig.mne.info['bads'] # click data to mark bad fig._fake_click((x, y), xform='data') assert label in fig.mne.info['bads'] # click data to unmark bad fig._fake_click((x, y), xform='data') assert label not in fig.mne.info['bads'] # click name to mark bad fig._click_ch_name(ch_index=0, button=1) assert label in fig.mne.info['bads'] # test other kinds of clicks fig._fake_click((0.5, 0.98)) # click elsewhere (add vline) assert fig.mne.vline_visible is True fig._fake_click((0.5, 0.98), button=3) # remove vline assert fig.mne.vline_visible is False fig._fake_click((0.5, 0.5), ax=hscroll) # change time t_start = fig.mne.t_start fig._fake_click((0.5, 0.5), ax=hscroll) # shouldn't change time this time assert round(t_start, 6) == round(fig.mne.t_start, 6) # test scrolling through channels labels = fig._get_ticklabels('y') assert labels == [raw.ch_names[1], raw.ch_names[7], raw.ch_names[5]] fig._fake_click((0.5, 0.05), ax=vscroll) # change channels to end labels = fig._get_ticklabels('y') assert labels == [raw.ch_names[5], raw.ch_names[2], raw.ch_names[3]] for _ in (0, 0): # first click changes channels to mid; second time shouldn't change # This needs to be changed for pyqtgraph, because there scrollbars are # drawn differently (value of slider at lower end, not at middle) yclick = 0.5 if ismpl else 0.7 fig._fake_click((0.5, yclick), ax=vscroll) labels = fig._get_ticklabels('y') assert labels == [raw.ch_names[7], raw.ch_names[5], raw.ch_names[2]] # test clicking a channel name in butterfly mode bads = fig.mne.info['bads'].copy() fig._fake_keypress('b') fig._click_ch_name(ch_index=0, button=1) # should be no-op assert fig.mne.info['bads'] == bads # unchanged fig._fake_keypress('b') # test starting up in zen mode fig = plot_raw(raw, show_scrollbars=False) # test order, title, & show_options kwargs with pytest.raises(ValueError, match='order should be array-like; got'): raw.plot(order='foo') with pytest.raises(TypeError, match='title must be None or a string, got'): raw.plot(title=1) raw.plot(show_options=True) browser_backend._close_all() # annotations outside data range annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) # Color setting with pytest.raises(KeyError, match='must be strictly positive, or -1'): raw.plot(event_color={0: 'r'}) with pytest.raises(TypeError, match='event_color key must be an int, got'): raw.plot(event_color={'foo': 'r'}) plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'})
def test_plot_raw(): """Test plotting of raw data.""" raw = _get_raw() raw.info['lowpass'] = 10. # allow heavy decim during plotting events = _get_events() plt.close('all') # ensure all are closed assert len(plt.get_fignums()) == 0 fig = raw.plot(events=events, order=[1, 7, 3], group_by='original') assert len(plt.get_fignums()) == 1 # make sure fig._mne_params is present assert isinstance(fig._mne_params, dict) # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.axes[0] _fake_click(fig, data_ax, [x, y], xform='data') # mark a bad channel _fake_click(fig, data_ax, [x, y], xform='data') # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels assert len(plt.get_fignums()) == 1 # open SSP window _fake_click(fig, fig.get_axes()[-1], [0.5, 0.5]) _fake_click(fig, fig.get_axes()[-1], [0.5, 0.5], kind='release') assert len(plt.get_fignums()) == 2 ssp_fig = plt.figure(plt.get_fignums()[-1]) fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up ax = ssp_fig.get_axes()[0] # only one axis is used assert _proj_status(ax) == [True] * 3 t = [c for c in ax.get_children() if isinstance(c, matplotlib.text.Text)] pos = np.array(t[0].get_position()) + 0.01 _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off assert _proj_status(ax) == [False, True, True] _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on assert _proj_status(ax) == [True] * 3 _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5]) # all off _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5], kind='release') assert _proj_status(ax) == [False] * 3 _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5]) # all on _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5], kind='release') assert _proj_status(ax) == [True] * 3 # test keypresses # test for group_by='original' for key in [ 'down', 'up', 'right', 'left', 'o', '-', '+', '=', 'd', 'd', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'z', 'escape' ]: fig.canvas.key_press_event(key) # test for group_by='selection' fig = plot_raw(raw, events=events, group_by='selection') for key in [ 'b', 'down', 'up', 'right', 'left', 'o', '-', '+', '=', 'd', 'd', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'b', 'z', 'escape' ]: fig.canvas.key_press_event(key) # test zen mode fig = plot_raw(raw, show_scrollbars=False) # Color setting pytest.raises(KeyError, raw.plot, event_color={0: 'r'}) pytest.raises(TypeError, raw.plot, event_color={'foo': 'r'}) annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all') for group_by, order in zip( ['position', 'selection'], [np.arange(len(raw.ch_names))[::-3], [1, 2, 4, 6]]): with pytest.warns(None): # sometimes projection fig = raw.plot(group_by=group_by, order=order) x = fig.get_axes()[0].lines[1].get_xdata()[10] y = fig.get_axes()[0].lines[1].get_ydata()[10] with pytest.warns(None): # old mpl (at least 2.0) can warn _fake_click(fig, data_ax, [x, y], xform='data') # mark bad fig.canvas.key_press_event('down') # change selection _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels sel_fig = plt.figure(1) topo_ax = sel_fig.axes[1] _fake_click(sel_fig, topo_ax, [-0.425, 0.20223853], xform='data') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.scroll_event(0.5, 0.5, -1) # scroll down fig.canvas.scroll_event(0.5, 0.5, 1) # scroll up _fake_click(sel_fig, topo_ax, [-0.5, 0.], xform='data') _fake_click(sel_fig, topo_ax, [0.5, 0.], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [0.5, 0.5], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [-0.5, 0.5], xform='data', kind='release') plt.close('all') # test if meas_date has only one element raw.info['meas_date'] = (raw.info['meas_date'][0], 0) annot = Annotations([1 + raw.first_samp / raw.info['sfreq']], [5], ['bad']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) with pytest.warns(None): # sometimes projection raw.plot(group_by='position', order=np.arange(8)) for fig_num in plt.get_fignums(): fig = plt.figure(fig_num) if hasattr(fig, 'radio'): # Get access to selection fig. break for key in ['down', 'up', 'escape']: fig.canvas.key_press_event(key) plt.close('all')
def test_plot_raw(): """Test plotting of raw data.""" import matplotlib.pyplot as plt raw = _get_raw() raw.info['lowpass'] = 10. # allow heavy decim during plotting events = _get_events() plt.close('all') # ensure all are closed with warnings.catch_warnings(record=True): fig = raw.plot(events=events, show_options=True, order=[1, 7, 3], group_by='original') # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.axes[0] _fake_click(fig, data_ax, [x, y], xform='data') # mark a bad channel _fake_click(fig, data_ax, [x, y], xform='data') # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels _fake_click(fig, fig.get_axes()[3], [0.5, 0.5]) # open SSP window fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up # sadly these fail when no renderer is used (i.e., when using Agg): # ssp_fig = set(plt.get_fignums()) - set([fig.number]) # assert_equal(len(ssp_fig), 1) # ssp_fig = plt.figure(list(ssp_fig)[0]) # ax = ssp_fig.get_axes()[0] # only one axis is used # t = [c for c in ax.get_children() if isinstance(c, # matplotlib.text.Text)] # pos = np.array(t[0].get_position()) + 0.01 # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on # test keypresses for key in [ 'down', 'up', 'right', 'left', 'o', '-', '+', '=', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'escape' ]: fig.canvas.key_press_event(key) fig = plot_raw(raw, events=events, group_by='selection') for key in [ 'b', 'down', 'up', 'right', 'left', 'o', '-', '+', '=', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'b', 'escape' ]: fig.canvas.key_press_event(key) # Color setting assert_raises(KeyError, raw.plot, event_color={0: 'r'}) assert_raises(TypeError, raw.plot, event_color={'foo': 'r'}) annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) raw.annotations = annot fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all') for group_by, order in zip( ['position', 'selection'], [np.arange(len(raw.ch_names))[::-3], [1, 2, 4, 6]]): fig = raw.plot(group_by=group_by, order=order) x = fig.get_axes()[0].lines[1].get_xdata()[10] y = fig.get_axes()[0].lines[1].get_ydata()[10] _fake_click(fig, data_ax, [x, y], xform='data') # mark bad fig.canvas.key_press_event('down') # change selection _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels sel_fig = plt.figure(1) topo_ax = sel_fig.axes[1] _fake_click(sel_fig, topo_ax, [-0.425, 0.20223853], xform='data') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.scroll_event(0.5, 0.5, -1) # scroll down fig.canvas.scroll_event(0.5, 0.5, 1) # scroll up _fake_click(sel_fig, topo_ax, [-0.5, 0.], xform='data') _fake_click(sel_fig, topo_ax, [0.5, 0.], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [0.5, 0.5], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [-0.5, 0.5], xform='data', kind='release') plt.close('all') # test if meas_date has only one element raw.info['meas_date'] = np.array([raw.info['meas_date'][0]], dtype=np.int32) raw.annotations = Annotations([1 + raw.first_samp / raw.info['sfreq']], [5], ['bad']) raw.plot(group_by='position', order=np.arange(8)) for fig_num in plt.get_fignums(): fig = plt.figure(fig_num) if hasattr(fig, 'radio'): # Get access to selection fig. break for key in ['down', 'up', 'escape']: fig.canvas.key_press_event(key) plt.close('all')
def test_plot_raw_traces(raw): """Test plotting of raw data.""" raw.info['lowpass'] = 10. # allow heavy decim during plotting events = _get_events() plt.close('all') # ensure all are closed fig = raw.plot(events=events, order=[1, 7, 5, 2, 3], n_channels=3, group_by='original') assert hasattr(fig, 'mne') # make sure fig.mne param object is present assert len(fig.axes) == 5 # setup x = fig.mne.traces[0].get_xdata()[5] y = fig.mne.traces[0].get_ydata()[5] data_ax = fig.mne.ax_main hscroll = fig.mne.ax_hscroll vscroll = fig.mne.ax_vscroll # test marking bad channels label = fig.mne.ax_main.get_yticklabels()[0].get_text() assert label not in fig.mne.info['bads'] _fake_click(fig, data_ax, [x, y], xform='data') # click data to mark bad assert label in fig.mne.info['bads'] _fake_click(fig, data_ax, [x, y], xform='data') # click data to unmark bad assert label not in fig.mne.info['bads'] _click_ch_name(fig, ch_index=0, button=1) # click name to mark bad assert label in fig.mne.info['bads'] # test other kinds of clicks _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere (add vline) _fake_click(fig, data_ax, [0.5, 0.999], button=3) # remove vline _fake_click(fig, hscroll, [0.5, 0.5]) # change time _fake_click(fig, hscroll, [0.5, 0.5]) # shouldn't change time this time # test scrolling through channels labels = [label.get_text() for label in data_ax.get_yticklabels()] assert labels == [raw.ch_names[1], raw.ch_names[7], raw.ch_names[5]] _fake_click(fig, vscroll, [0.5, 0.01]) # change channels to end labels = [label.get_text() for label in data_ax.get_yticklabels()] assert labels == [raw.ch_names[5], raw.ch_names[2], raw.ch_names[3]] for _ in (0, 0): # first click changes channels to mid; second time shouldn't change _fake_click(fig, vscroll, [0.5, 0.5]) labels = [label.get_text() for label in data_ax.get_yticklabels()] assert labels == [raw.ch_names[7], raw.ch_names[5], raw.ch_names[2]] assert len(plt.get_fignums()) == 1 # test clicking a channel name in butterfly mode bads = fig.mne.info['bads'].copy() fig.canvas.key_press_event('b') _click_ch_name(fig, ch_index=0, button=1) # should be no-op assert fig.mne.info['bads'] == bads # unchanged fig.canvas.key_press_event('b') # test starting up in zen mode fig = plot_raw(raw, show_scrollbars=False) # test order, title, & show_options kwargs with pytest.raises(ValueError, match='order should be array-like; got'): raw.plot(order='foo') with pytest.raises(TypeError, match='title must be None or a string, got'): raw.plot(title=1) raw.plot(show_options=True) plt.close('all') # Color setting with pytest.raises(KeyError, match='must be strictly positive, or -1'): raw.plot(event_color={0: 'r'}) with pytest.raises(TypeError, match='event_color key must be an int, got'): raw.plot(event_color={'foo': 'r'}) annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all') for group_by, order in zip(['position', 'selection'], [np.arange(len(raw.ch_names))[::-3], [1, 2, 4, 6]]): fig = raw.plot(group_by=group_by, order=order) x = fig.get_axes()[0].lines[1].get_xdata()[10] y = fig.get_axes()[0].lines[1].get_ydata()[10] _fake_click(fig, data_ax, [x, y], xform='data') # mark bad fig.canvas.key_press_event('down') # change selection _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels sel_fig = plt.figure(1) topo_ax = sel_fig.axes[1] _fake_click(sel_fig, topo_ax, [-0.425, 0.20223853], xform='data') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.scroll_event(0.5, 0.5, -1) # scroll down fig.canvas.scroll_event(0.5, 0.5, 1) # scroll up _fake_click(sel_fig, topo_ax, [-0.5, 0.], xform='data') _fake_click(sel_fig, topo_ax, [0.5, 0.], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [0.5, 0.5], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [-0.5, 0.5], xform='data', kind='release') plt.close('all') # test if meas_date is off raw.set_meas_date(_dt_to_stamp(raw.info['meas_date'])[0]) annot = Annotations([1 + raw.first_samp / raw.info['sfreq']], [5], ['bad']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) with pytest.warns(None): # sometimes projection raw.plot(group_by='position', order=np.arange(8)) for fig_num in plt.get_fignums(): fig = plt.figure(fig_num) if hasattr(fig, 'radio'): # Get access to selection fig. break for key in ['down', 'up', 'escape']: fig.canvas.key_press_event(key) raw._data[:] = np.nan # this should (at least) not die, the output should pretty clearly show # that there is a problem so probably okay to just plot something blank with pytest.warns(None): raw.plot(scalings='auto') plt.close('all')
def test_plot_raw(): """Test plotting of raw data.""" import matplotlib.pyplot as plt raw = _get_raw() raw.info['lowpass'] = 10. # allow heavy decim during plotting events = _get_events() plt.close('all') # ensure all are closed with warnings.catch_warnings(record=True): fig = raw.plot(events=events, show_options=True, order=[1, 7, 3], group_by='original') # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.axes[0] _fake_click(fig, data_ax, [x, y], xform='data') # mark a bad channel _fake_click(fig, data_ax, [x, y], xform='data') # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels _fake_click(fig, fig.get_axes()[3], [0.5, 0.5]) # open SSP window fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up # sadly these fail when no renderer is used (i.e., when using Agg): # ssp_fig = set(plt.get_fignums()) - set([fig.number]) # assert_equal(len(ssp_fig), 1) # ssp_fig = plt.figure(list(ssp_fig)[0]) # ax = ssp_fig.get_axes()[0] # only one axis is used # t = [c for c in ax.get_children() if isinstance(c, # matplotlib.text.Text)] # pos = np.array(t[0].get_position()) + 0.01 # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off # _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on # test keypresses for key in ['down', 'up', 'right', 'left', 'o', '-', '+', '=', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'escape']: fig.canvas.key_press_event(key) fig = plot_raw(raw, events=events, group_by='selection') for key in ['b', 'down', 'up', 'right', 'left', 'o', '-', '+', '=', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'b', 'escape']: fig.canvas.key_press_event(key) # Color setting assert_raises(KeyError, raw.plot, event_color={0: 'r'}) assert_raises(TypeError, raw.plot, event_color={'foo': 'r'}) annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) raw.annotations = annot fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all') for group_by, order in zip(['position', 'selection'], [np.arange(len(raw.ch_names))[::-3], [1, 2, 4, 6]]): fig = raw.plot(group_by=group_by, order=order) x = fig.get_axes()[0].lines[1].get_xdata()[10] y = fig.get_axes()[0].lines[1].get_ydata()[10] _fake_click(fig, data_ax, [x, y], xform='data') # mark bad fig.canvas.key_press_event('down') # change selection _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels sel_fig = plt.figure(1) topo_ax = sel_fig.axes[1] _fake_click(sel_fig, topo_ax, [-0.425, 0.20223853], xform='data') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.scroll_event(0.5, 0.5, -1) # scroll down fig.canvas.scroll_event(0.5, 0.5, 1) # scroll up _fake_click(sel_fig, topo_ax, [-0.5, 0.], xform='data') _fake_click(sel_fig, topo_ax, [0.5, 0.], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [0.5, 0.5], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [-0.5, 0.5], xform='data', kind='release') plt.close('all') # test if meas_date has only one element raw.info['meas_date'] = np.array([raw.info['meas_date'][0]], dtype=np.int32) raw.annotations = Annotations([1 + raw.first_samp / raw.info['sfreq']], [5], ['bad']) raw.plot(group_by='position', order=np.arange(8)) for fig_num in plt.get_fignums(): fig = plt.figure(fig_num) if hasattr(fig, 'radio'): # Get access to selection fig. break for key in ['down', 'up', 'escape']: fig.canvas.key_press_event(key) plt.close('all')
def test_plot_raw_traces(raw, events, browse_backend): """Test plotting of raw data.""" raw.info['lowpass'] = 10. # allow heavy decim during plotting fig = raw.plot(events=events, order=[1, 7, 5, 2, 3], n_channels=3, group_by='original') assert hasattr(fig, 'mne') # make sure fig.mne param object is present assert len(fig.axes) == 5 # setup x = fig.mne.traces[0].get_xdata()[5] y = fig.mne.traces[0].get_ydata()[5] data_ax = fig.mne.ax_main # ToDo: The interaction with scrollbars will be different in pyqtgraph. hscroll = fig.mne.ax_hscroll vscroll = fig.mne.ax_vscroll # test marking bad channels label = fig.mne.ax_main.get_yticklabels()[0].get_text() assert label not in fig.mne.info['bads'] # click data to mark bad fig._fake_click((x, y), xform='data') assert label in fig.mne.info['bads'] # click data to unmark bad fig._fake_click((x, y), xform='data') assert label not in fig.mne.info['bads'] # click name to mark bad fig._click_ch_name(ch_index=0, button=1) assert label in fig.mne.info['bads'] # test other kinds of clicks fig._fake_click((0.5, 0.999)) # click elsewhere (add vline) fig._fake_click((0.5, 0.999), button=3) # remove vline fig._fake_click((0.5, 0.5), ax=hscroll) # change time fig._fake_click((0.5, 0.5), ax=hscroll) # shouldn't change time this time # test scrolling through channels labels = [label.get_text() for label in data_ax.get_yticklabels()] assert labels == [raw.ch_names[1], raw.ch_names[7], raw.ch_names[5]] fig._fake_click((0.5, 0.01), ax=vscroll) # change channels to end labels = [label.get_text() for label in data_ax.get_yticklabels()] assert labels == [raw.ch_names[5], raw.ch_names[2], raw.ch_names[3]] for _ in (0, 0): # first click changes channels to mid; second time shouldn't change fig._fake_click((0.5, 0.5), ax=vscroll) labels = [label.get_text() for label in data_ax.get_yticklabels()] assert labels == [raw.ch_names[7], raw.ch_names[5], raw.ch_names[2]] assert browse_backend._get_n_figs() == 1 # test clicking a channel name in butterfly mode bads = fig.mne.info['bads'].copy() fig._fake_keypress('b') fig._click_ch_name(ch_index=0, button=1) # should be no-op assert fig.mne.info['bads'] == bads # unchanged fig._fake_keypress('b') # test starting up in zen mode fig = plot_raw(raw, show_scrollbars=False) # test order, title, & show_options kwargs with pytest.raises(ValueError, match='order should be array-like; got'): raw.plot(order='foo') with pytest.raises(TypeError, match='title must be None or a string, got'): raw.plot(title=1) raw.plot(show_options=True) browse_backend._close_all() # annotations outside data range annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) # Color setting with pytest.raises(KeyError, match='must be strictly positive, or -1'): raw.plot(event_color={0: 'r'}) with pytest.raises(TypeError, match='event_color key must be an int, got'): raw.plot(event_color={'foo': 'r'}) fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) browse_backend._close_all()
def test_plot_raw(): """Test plotting of raw data.""" raw = _get_raw() raw.info['lowpass'] = 10. # allow heavy decim during plotting events = _get_events() plt.close('all') # ensure all are closed assert len(plt.get_fignums()) == 0 fig = raw.plot(events=events, order=[1, 7, 3], group_by='original') assert len(plt.get_fignums()) == 1 # test mouse clicks x = fig.get_axes()[0].lines[1].get_xdata().mean() y = fig.get_axes()[0].lines[1].get_ydata().mean() data_ax = fig.axes[0] _fake_click(fig, data_ax, [x, y], xform='data') # mark a bad channel _fake_click(fig, data_ax, [x, y], xform='data') # unmark a bad channel _fake_click(fig, data_ax, [0.5, 0.999]) # click elsewhere in 1st axes _fake_click(fig, data_ax, [-0.1, 0.9]) # click on y-label _fake_click(fig, fig.get_axes()[1], [0.5, 0.5]) # change time _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels assert len(plt.get_fignums()) == 1 # open SSP window _fake_click(fig, fig.get_axes()[-1], [0.5, 0.5]) _fake_click(fig, fig.get_axes()[-1], [0.5, 0.5], kind='release') assert len(plt.get_fignums()) == 2 ssp_fig = plt.figure(plt.get_fignums()[-1]) fig.canvas.button_press_event(1, 1, 1) # outside any axes fig.canvas.scroll_event(0.5, 0.5, -0.5) # scroll down fig.canvas.scroll_event(0.5, 0.5, 0.5) # scroll up ax = ssp_fig.get_axes()[0] # only one axis is used assert _proj_status(ax) == [True] * 3 t = [c for c in ax.get_children() if isinstance(c, matplotlib.text.Text)] pos = np.array(t[0].get_position()) + 0.01 _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # off assert _proj_status(ax) == [False, True, True] _fake_click(ssp_fig, ssp_fig.get_axes()[0], pos, xform='data') # on assert _proj_status(ax) == [True] * 3 _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5]) # all off _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5], kind='release') assert _proj_status(ax) == [False] * 3 _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5]) # all on _fake_click(ssp_fig, ssp_fig.get_axes()[1], [0.5, 0.5], kind='release') assert _proj_status(ax) == [True] * 3 # test keypresses # test for group_by='original' for key in ['down', 'up', 'right', 'left', 'o', '-', '+', '=', 'd', 'd', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'escape']: fig.canvas.key_press_event(key) # test for group_by='selection' fig = plot_raw(raw, events=events, group_by='selection') for key in ['b', 'down', 'up', 'right', 'left', 'o', '-', '+', '=', 'd', 'd', 'pageup', 'pagedown', 'home', 'end', '?', 'f11', 'b', 'escape']: fig.canvas.key_press_event(key) # Color setting pytest.raises(KeyError, raw.plot, event_color={0: 'r'}) pytest.raises(TypeError, raw.plot, event_color={'foo': 'r'}) annot = Annotations([10, 10 + raw.first_samp / raw.info['sfreq']], [10, 10], ['test', 'test'], raw.info['meas_date']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) fig = plot_raw(raw, events=events, event_color={-1: 'r', 998: 'b'}) plt.close('all') for group_by, order in zip(['position', 'selection'], [np.arange(len(raw.ch_names))[::-3], [1, 2, 4, 6]]): with pytest.warns(None): # sometimes projection fig = raw.plot(group_by=group_by, order=order) x = fig.get_axes()[0].lines[1].get_xdata()[10] y = fig.get_axes()[0].lines[1].get_ydata()[10] _fake_click(fig, data_ax, [x, y], xform='data') # mark bad fig.canvas.key_press_event('down') # change selection _fake_click(fig, fig.get_axes()[2], [0.5, 0.5]) # change channels sel_fig = plt.figure(1) topo_ax = sel_fig.axes[1] _fake_click(sel_fig, topo_ax, [-0.425, 0.20223853], xform='data') fig.canvas.key_press_event('down') fig.canvas.key_press_event('up') fig.canvas.scroll_event(0.5, 0.5, -1) # scroll down fig.canvas.scroll_event(0.5, 0.5, 1) # scroll up _fake_click(sel_fig, topo_ax, [-0.5, 0.], xform='data') _fake_click(sel_fig, topo_ax, [0.5, 0.], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [0.5, 0.5], xform='data', kind='motion') _fake_click(sel_fig, topo_ax, [-0.5, 0.5], xform='data', kind='release') plt.close('all') # test if meas_date has only one element raw.info['meas_date'] = (raw.info['meas_date'][0], 0) annot = Annotations([1 + raw.first_samp / raw.info['sfreq']], [5], ['bad']) with pytest.warns(RuntimeWarning, match='outside data range'): raw.set_annotations(annot) with pytest.warns(None): # sometimes projection raw.plot(group_by='position', order=np.arange(8)) for fig_num in plt.get_fignums(): fig = plt.figure(fig_num) if hasattr(fig, 'radio'): # Get access to selection fig. break for key in ['down', 'up', 'escape']: fig.canvas.key_press_event(key) plt.close('all')
def preprocess(self): # 预处理步骤:静息态数据截取->49-51Hz陷波滤波->ICA滤波 plot_raw(self.raw_data) QMessageBox.information(self, '消息', '数据预处理完成', QMessageBox.Ok)