Пример #1
0
def test_epochs_plot():
    """Test plotting of epochs"""
    tmin, tmax, event_id = -0.5, 1.5, 999
    # create some evil events
    events = np.array([[1000, 999], [2000, 999], [3000, 999]])
    for fname in fnames:
        raw = Raw(fname)
        epochs = Epochs(raw, events, event_id, tmin, tmax)
        assert_raises(ValueError, epochs.plot, picks=['whatever'])
        epochs.plot(picks=['ps'])
        fig = epochs.plot(n_chunks=2)

    # test clicking: find our callbacks
    for func in fig.canvas.callbacks.callbacks['button_press_event'].items():
        func = func[1].func
        if isinstance(func, partial):
            break
    params = func.keywords['params']
    for ax in fig.axes:
        _epochs_axes_onclick(DummyEvent(ax), params)

    # nav clicking
    _epochs_navigation_onclick(DummyEvent(params['next'].ax), params)
    _epochs_navigation_onclick(DummyEvent(params['back'].ax), params)
    _epochs_navigation_onclick(DummyEvent(params['reject-quit'].ax), params)
Пример #2
0
def test_epochs_deconv():
    """Test epochs deconvolution"""
    tmin, tmax = -0.5, 1.5
    event_dict = dict(foo=999)
    events = np.array([np.arange(0, 21000, 1000, int),
                       999 * np.ones(21, int)]).T
    for fi, fname in enumerate(fnames):
        if fi == 0:
            n_jobs = 1
        else:
            n_jobs = 0
        raw = read_raw(fname)
        epochs = Epochs(raw, events, event_dict,
                        tmin, tmax)
        a = raw.info['sample_fields']
        b = epochs.info['data_cols']
        assert_equal(len(a), len(b))
        assert_true(all(aa == bb for aa, bb in zip(a, b)))
        data = epochs.get_data('ps')
        assert_raises(RuntimeError, Epochs, raw, events, 'test', tmin, tmax)
        fit, times = epochs.deconvolve()
        assert_array_equal(data, epochs.get_data('ps'))
        assert_equal(fit.shape, (len(epochs), len(times)))
        fit, times = epochs.deconvolve(spacing=[-0.1, 0.4, 1.0],
                                       bounds=(0, np.inf), n_jobs=n_jobs)
        assert_equal(fit.shape, (len(epochs), len(times)))
        assert_equal(len(times), 3)
        if fi == 0:
            if _has_joblib():
                assert_raises(ValueError, epochs.deconvolve, n_jobs=-1000)
Пример #3
0
def test_epochs_deconv():
    """Test epochs deconvolution"""
    tmin, tmax = -0.5, 1.5
    event_dict = dict(foo=999, bar=77)
    events_a = np.array([[12000, 77], [1000, 999]])
    for fname in fnames:
        raw = Raw(fname)
        epochs = Epochs([raw] * 2, [events_a] * 2, event_dict,
                        tmin, tmax)
        assert_raises(RuntimeError, Epochs, raw, events_a, 'test', tmin, tmax)
        fit, times = epochs.deconvolve()
        assert_equal(fit.shape, (len(epochs), len(times)))
        fit, times = epochs.deconvolve(spacing=[-0.1, 0.4, 1.0],
                                       bounds=(0, np.inf))
        assert_equal(fit.shape, (len(epochs), len(times)))
        assert_equal(len(times), 3)
Пример #4
0
def test_epochs_plot():
    """Test plotting of epochs"""
    tmin, tmax, event_id = -0.5, 1.5, 999
    # create some evil events
    events = np.array([[1000, 999], [2000, 999], [3000, 999]])
    for fname in fnames:
        raw = read_raw(fname)
        epochs = Epochs(raw, events, event_id, tmin, tmax)
        assert_raises(ValueError, epochs.plot, picks=['whatever'])
        epochs.plot(picks=['ps'])
        fig = epochs.plot(n_chunks=2)
        epochs.plot(draw_discrete='saccades')

    # test clicking: find our callbacks
    for func in fig.canvas.callbacks.callbacks['button_press_event'].items():
        func = func[1].func
        if isinstance(func, partial):
            break
    params = func.keywords['params']
    for ax in fig.axes:
        _epochs_axes_onclick(DummyEvent(ax), params)

    # nav clicking
    _epochs_navigation_onclick(DummyEvent(params['next'].ax), params)
    _epochs_navigation_onclick(DummyEvent(params['back'].ax), params)
    _epochs_navigation_onclick(DummyEvent(params['reject-quit'].ax), params)
Пример #5
0
def test_epochs_concat():
    """Test epochs concatenation"""
    tmin, tmax = -0.5, 1.5
    event_dict = dict(foo=999, bar=77)
    events_a = np.array([[12000, 77], [1000, 999], [-1, 999]])
    events_b = np.array([[1000, 999], [10000, 999]])
    for fname in fnames:
        raw = read_raw(fname)
        events_a[-1, 0] = raw.n_samples - 1
        epochs_ab = Epochs([raw] * 2, [events_a, events_b], event_dict, tmin,
                           tmax)
        epochs_ba = Epochs([raw] * 2, [events_b, events_a], event_dict, tmin,
                           tmax)
        # make sure discretes made it through
        for epochs in [epochs_ab, epochs_ba]:
            for d in [epochs.blinks, epochs.saccades, epochs_ab.fixations]:
                assert_equal(len(d), len(epochs))
                for dd in d:
                    if len(dd) > 0:
                        for t in (dd['stime'], dd['etime']):
                            assert_true(np.all(t >= tmin) & np.all(t <= tmax))
        assert_equal(len(epochs_ab.events), 4)
        assert_equal(len(epochs_ba.events), 4)
        assert_array_equal(epochs_ab.times, epochs_ba.times)
        # make sure event numbers match
        reord = [2, 3, 0, 1]
        assert_array_equal(epochs_ab.events[:, 1], epochs_ba.events[reord, 1])
        # make sure actual data matches
        data_ab = epochs_ab.data
        data_ba = epochs_ba.data
        assert_array_equal(data_ab, data_ba[reord])
        # test discretes
        assert_equal(len(epochs_ab.blinks), len(epochs_ba.blinks))
        blink_ab = epochs_ab.blinks[3]
        blink_ba = epochs_ba.blinks[reord[3]]
        assert_equal(len(blink_ab), len(blink_ba))
        assert_true(len(blink_ab) > 0)  # make sure we've tested useful case
        for key in ('stime', 'etime'):
            blink_ab_d = blink_ab[key]
            blink_ba_d = blink_ba[key]
            assert_array_equal(blink_ab_d, blink_ba_d)
Пример #6
0
def test_epochs_deconv():
    """Test epochs deconvolution"""
    tmin, tmax = -0.5, 1.5
    event_dict = dict(foo=999)
    events = np.array([np.arange(0, 21000, 1000, int),
                       999 * np.ones(21, int)]).T
    for fi, fname in enumerate(fnames):
        if fi == 0:
            n_jobs = 1
        else:
            n_jobs = 0
        raw = read_raw(fname)
        epochs = Epochs(raw, events, event_dict, tmin, tmax)
        a = raw.info['sample_fields']
        b = epochs.info['data_cols']
        assert_equal(len(a), len(b))
        assert_true(all(aa == bb for aa, bb in zip(a, b)))
        data = epochs.get_data('ps')
        assert_raises(RuntimeError, Epochs, raw, events, 'test', tmin, tmax)
        fit, times = epochs.deconvolve()
        assert_array_equal(data, epochs.get_data('ps'))
        assert_equal(fit.shape, (len(epochs), len(times)))
        fit, times = epochs.deconvolve(spacing=[-0.1, 0.4, 1.0],
                                       bounds=(0, np.inf),
                                       n_jobs=n_jobs)
        assert_equal(fit.shape, (len(epochs), len(times)))
        assert_equal(len(times), 3)
        if fi == 0:
            if _has_joblib():
                assert_raises(ValueError, epochs.deconvolve, n_jobs=-1000)
Пример #7
0
def test_epochs_combine():
    """Test epochs combine IDs functionality"""
    tmin, tmax = -0.5, 1.5
    event_dict = dict(foo=1, bar=2, test=3)
    events_1 = np.array([[12000, 1], [1000, 2], [10000, 2], [2000, 3]])
    events_2 = np.array([[12000, 2], [1000, 1], [10000, 1], [2000, 3]])
    for fname in fnames:
        raw = read_raw(fname)
        epochs_1 = Epochs(raw, events_1, event_dict, tmin, tmax)
        epochs_2 = Epochs(raw, events_2, event_dict, tmin, tmax)
        assert_raises(ValueError, epochs_1.combine_event_ids, ['foo', 'bar'],
                      dict(foobar=1))
        epochs_1.combine_event_ids(['foo', 'bar'], 12)
        epochs_2.combine_event_ids(['foo', 'bar'], dict(foobar=12))
        d1 = epochs_1.data
        d2 = epochs_2.data
        assert_array_equal(d1, d2)

        epochs_1.equalize_event_counts(['12', 'test'])
        epochs_2.equalize_event_counts(['foobar', 'test'])
        d1 = epochs_1.data
        d2 = epochs_2.data
        assert_array_equal(d1, d2)
        # this shouldn't really do anything
        epochs_2.equalize_event_counts(['foobar', 'test'], method='truncate')
        assert_array_equal(d1, epochs_2.data)
Пример #8
0
def test_epochs_io():
    """Test epochs IO functionality"""
    tmin, tmax, event_id = -0.5, 1.5, 999
    missing_event_dict = dict(foo=999, bar=555)
    # create some evil events
    events = np.array([[12000, 77], [1000, 999], [10000, 999]])
    for fname in fnames:
        raw = read_raw(fname)
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            epochs = Epochs(raw,
                            events,
                            missing_event_dict,
                            tmin,
                            tmax,
                            ignore_missing=True)
        assert_raises(RuntimeError, Epochs, raw, events, 1.1, tmin, tmax)
        assert_raises(ValueError, Epochs, [raw] * 2, events, event_id, tmin,
                      tmax)
        assert_equal(len(_filter_warnings(w)), 0)
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            epochs = Epochs(raw, events, missing_event_dict, tmin, tmax)
        assert_equal(len(_filter_warnings(w)), 1)
        epochs = Epochs(raw, events, event_id, tmin, tmax)
        assert_raises(IndexError, epochs.drop_epochs, [1000])
        print(epochs)  # test repr works
        for disc in epochs.info['discretes']:
            assert_equal(len(vars(epochs)[disc]), len(epochs.events))
        assert_equal(len(epochs.events), 2)
        # assert_equal(epochs.data_frame.shape[0] / epochs._n_times,
        #              len(epochs.events))
        # assert_true(epochs.data_frame['time'].diff().min() >= 0)

        epochs = Epochs(raw, events, dict(a=999, b=77), tmin, tmax)
        assert_equal(len(epochs.events), 3)
        # assert_equal(epochs.data_frame.shape[0] / epochs._n_times,
        #              len(epochs.events))
        # assert_true(epochs.data_frame['time'].diff().min() >= 0)

        for disc in epochs.info['discretes']:
            this_disc = vars(epochs)[disc]
            assert_equal(len(this_disc), len(epochs.events))
            for field in ['stime', 'etime']:
                for di in this_disc:
                    if field in di:
                        for event in di[field]:
                            assert_true(epochs.tmin <= event <= epochs.tmax)

        epochs2 = epochs.copy()
        assert_true(epochs._data is not epochs2._data)
        del epochs2._data
        assert_true('_data' in vars(epochs) and '_data' not in vars(epochs2))
        assert_true(epochs is not epochs2)
        epochs2 = epochs[0]
        assert_equal(len(epochs2.events), 1)
        assert_equal(set(epochs2.events[:, -1]), set([999]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        # assert_true(epochs2.data_frame['time'].diff().min() >= 0)
        assert_equal(len(epochs2.saccades), len(epochs2.events))

        epochs2 = epochs[[1, 0]]
        assert_equal(len(epochs2.events), 2)
        assert_equal(set(epochs2.events[:, -1]), set([999]))
        # desired = len(epochs2.events) * len(epochs.times)
        assert_equal(len(epochs2.saccades), len(epochs2.events))
        # assert_equal(epochs2.data_frame.shape[0], desired)

        epochs2 = epochs['a']
        assert_equal(len(epochs2.events), 2)
        assert_equal(set(epochs2.events[:, -1]), set([999]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        assert_equal(len(epochs2.saccades), len(epochs2.events))

        epochs2 = epochs[['a', 'b']]
        assert_equal(len(epochs2.events), 3)
        assert_equal(set(epochs2.events[:, -1]), set([999, 77]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        assert_equal(len(epochs2.saccades), len(epochs2.events))
        assert_true(np.diff(epochs2.events[:, 0]).min() >= 0)

        epochs2 = epochs[slice(1, 3)]
        assert_equal(len(epochs2.events), 2)
        assert_equal(set(epochs2.events[:, -1]), set([999, 77]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        assert_equal(len(epochs2.saccades), len(epochs2.events))
        assert_true(np.diff(epochs2.events[:, 0]).min() >= 0)
        """
        data1 = epochs[0].data
        data2 = epochs.data_frame.ix[0, epochs.info['data_cols']].values
        data2 = data2.reshape(1,
                              len(epochs.times),
                              len(epochs.info['data_cols']))
        assert_array_equal(data1, np.transpose(data2, [0, 2, 1]))
        """

        for e in epochs:
            assert_true(np.argmin(e.shape) == 0)
        assert_array_equal(e, epochs.data[-1])
        # ttl_to_id function.  Those integer codes are then put into the event
        # list in place of the 1-triggers used to mark stimulus onsets.
        n_digits = [1, 2, 2, 1]
        for c, t in zip(this_cond_mat, ttls):
            assert np.array_equal(decimals_to_binary(c, n_digits), t)
        # this maps the sequence of 4 (single-digit) ints into a single int.
        # the sequence of experiment variables corresponding to each order of
        # magnitude is taken from params['cond_readme']; in this case it is
        # 1000s: attn, 100s: spatial, 10s: idents, 1s: gap
        event[:, 1] = [ttl_to_id(c) for c in this_cond_mat]
        for e in event[:, 1]:
            assert e in rev_dict
        events.append(event)

    print('\n  Epoching...')
    epochs = Epochs(raws, events, event_dict, t_min, t_max)
    if downsample:
        print('  Downsampling...')
        epochs.resample(fs_out, n_jobs=n_jobs)
    # compute gaze angles (in degrees)
    angles = get_gaze_angle(epochs, screenprops)
    # zscore pupil sizes
    zscore = epochs.pupil_zscores()
    # init some containers
    kernel_fits = list()
    kernel_zscores = list()

    print('  Deconvolving...')
    deconv_kwargs = dict(kernel=kernel, n_jobs=n_jobs, acc=1e-3)
    if deconv_time_pts is not None:
        deconv_kwargs.update(dict(spacing=deconv_time_pts))
Пример #10
0
def test_epochs_combine():
    """Test epochs combine IDs functionality"""
    tmin, tmax = -0.5, 1.5
    event_dict = dict(foo=1, bar=2, test=3)
    events_1 = np.array([[12000, 1], [1000, 2], [10000, 2], [2000, 3]])
    events_2 = np.array([[12000, 2], [1000, 1], [10000, 1], [2000, 3]])
    for fname in fnames:
        raw = read_raw(fname)
        epochs_1 = Epochs(raw, events_1, event_dict, tmin, tmax)
        epochs_2 = Epochs(raw, events_2, event_dict, tmin, tmax)
        assert_raises(ValueError, epochs_1.combine_event_ids, ['foo', 'bar'],
                      dict(foobar=1))
        epochs_1.combine_event_ids(['foo', 'bar'], 12)
        epochs_2.combine_event_ids(['foo', 'bar'], dict(foobar=12))
        d1 = epochs_1.data
        d2 = epochs_2.data
        assert_array_equal(d1, d2)

        epochs_1.equalize_event_counts(['12', 'test'])
        epochs_2.equalize_event_counts(['foobar', 'test'])
        d1 = epochs_1.data
        d2 = epochs_2.data
        assert_array_equal(d1, d2)
        # this shouldn't really do anything
        epochs_2.equalize_event_counts(['foobar', 'test'], method='truncate')
        assert_array_equal(d1, epochs_2.data)
Пример #11
0
def test_epochs_io():
    """Test epochs IO functionality"""
    tmin, tmax, event_id = -0.5, 1.5, 999
    missing_event_dict = dict(foo=999, bar=555)
    # create some evil events
    events = np.array([[12000, 77], [1000, 999], [10000, 999]])
    for fname in fnames:
        raw = read_raw(fname)
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            epochs = Epochs(raw, events, missing_event_dict, tmin, tmax,
                            ignore_missing=True)
        assert_raises(RuntimeError, Epochs, raw, events, 1.1, tmin, tmax)
        assert_raises(ValueError, Epochs, [raw] * 2, events, event_id,
                      tmin, tmax)
        assert_equal(len(_filter_warnings(w)), 0)
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter('always')
            epochs = Epochs(raw, events, missing_event_dict, tmin, tmax)
        assert_equal(len(_filter_warnings(w)), 1)
        epochs = Epochs(raw, events, event_id, tmin, tmax)
        assert_raises(IndexError, epochs.drop_epochs, [1000])
        print(epochs)  # test repr works
        for disc in epochs.info['discretes']:
            assert_equal(len(vars(epochs)[disc]), len(epochs.events))
        assert_equal(len(epochs.events), 2)
        # assert_equal(epochs.data_frame.shape[0] / epochs._n_times,
        #              len(epochs.events))
        # assert_true(epochs.data_frame['time'].diff().min() >= 0)

        epochs = Epochs(raw, events, dict(a=999, b=77), tmin, tmax)
        assert_equal(len(epochs.events), 3)
        # assert_equal(epochs.data_frame.shape[0] / epochs._n_times,
        #              len(epochs.events))
        # assert_true(epochs.data_frame['time'].diff().min() >= 0)

        for disc in epochs.info['discretes']:
            this_disc = vars(epochs)[disc]
            assert_equal(len(this_disc), len(epochs.events))
            for field in ['stime', 'etime']:
                for di in this_disc:
                    if field in di:
                        for event in di[field]:
                            assert_true(epochs.tmin <= event <= epochs.tmax)

        epochs2 = epochs.copy()
        assert_true(epochs._data is not epochs2._data)
        del epochs2._data
        assert_true('_data' in vars(epochs) and
                    '_data' not in vars(epochs2))
        assert_true(epochs is not epochs2)
        epochs2 = epochs[0]
        assert_equal(len(epochs2.events), 1)
        assert_equal(set(epochs2.events[:, -1]), set([999]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        # assert_true(epochs2.data_frame['time'].diff().min() >= 0)
        assert_equal(len(epochs2.saccades), len(epochs2.events))

        epochs2 = epochs[[1, 0]]
        assert_equal(len(epochs2.events), 2)
        assert_equal(set(epochs2.events[:, -1]), set([999]))
        # desired = len(epochs2.events) * len(epochs.times)
        assert_equal(len(epochs2.saccades), len(epochs2.events))
        # assert_equal(epochs2.data_frame.shape[0], desired)

        epochs2 = epochs['a']
        assert_equal(len(epochs2.events), 2)
        assert_equal(set(epochs2.events[:, -1]), set([999]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        assert_equal(len(epochs2.saccades), len(epochs2.events))

        epochs2 = epochs[['a', 'b']]
        assert_equal(len(epochs2.events), 3)
        assert_equal(set(epochs2.events[:, -1]), set([999, 77]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        assert_equal(len(epochs2.saccades), len(epochs2.events))
        assert_true(np.diff(epochs2.events[:, 0]).min() >= 0)

        epochs2 = epochs[slice(1, 3)]
        assert_equal(len(epochs2.events), 2)
        assert_equal(set(epochs2.events[:, -1]), set([999, 77]))
        # desired = len(epochs2.events) * len(epochs.times)
        # assert_equal(epochs2.data_frame.shape[0], desired)
        assert_equal(len(epochs2.saccades), len(epochs2.events))
        assert_true(np.diff(epochs2.events[:, 0]).min() >= 0)
        """
        data1 = epochs[0].data
        data2 = epochs.data_frame.ix[0, epochs.info['data_cols']].values
        data2 = data2.reshape(1,
                              len(epochs.times),
                              len(epochs.info['data_cols']))
        assert_array_equal(data1, np.transpose(data2, [0, 2, 1]))
        """

        for e in epochs:
            assert_true(np.argmin(e.shape) == 0)
        assert_array_equal(e, epochs.data[-1])
            old_ord = eyelink_stim_ord
            eyelink_stim_ord = np.empty_like(stim_nums)
            eyelink_stim_ord[missing] = stim_nums[missing]
            eyelink_stim_ord[not_missing] = old_ord
            print('Recovered {} trial(s)'.format(n_miss), end='\n    ')
        assert np.array_equal(eyelink_stim_ord, stim_nums)
        assert len(event) == len(stim_nums)
        # convert event numbers
        band_num = run_inds[ri][:, 1]
        gap_num = 10 * small_mat[:, 1]    # cf. line 251 of vocExperiment_v2.m
        attn_num = 100 * small_mat[:, 0]  # cf. line 250 of vocExperiment_v2.m
        event[:, 1] = band_num + gap_num + attn_num
        events.append(event)

    print('\n  Epoching...')
    epochs = Epochs(raws, events, event_dict, t_min, t_max)

    kernel_fits = list()
    kernel_zscores = list()
    kernel_fits_continuous = list()
    for kk, kernel in enumerate(kernels):
        kname = ['LABSN', 'Wierda'][kk]
        print('  Deconvolving ({} kernel)...'.format(kname))
        fit, time_pts = epochs.deconvolve(kernel=kernel, n_jobs=6)
        zscore = epochs.pupil_zscores()
        if deconv_time_pts is None:
            deconv_time_pts = time_pts
        assert np.array_equal(deconv_time_pts, time_pts)

        # reorder fits to match big_mat (stim# in serial order, voc band, time)
        order = np.array(run_inds)
Пример #13
0
            old_ord = eyelink_stim_ord
            eyelink_stim_ord = np.empty_like(stim_nums)
            eyelink_stim_ord[missing] = stim_nums[missing]
            eyelink_stim_ord[not_missing] = old_ord
            print('Recovered {} trial(s)'.format(n_miss), end='\n    ')
        assert np.array_equal(eyelink_stim_ord, stim_nums)
        assert len(event) == len(stim_nums)
        # convert event numbers
        band_num = run_inds[ri][:, 1]
        gap_num = 10 * small_mat[:, 1]  # cf. line 251 of vocExperiment_v2.m
        attn_num = 100 * small_mat[:, 0]  # cf. line 250 of vocExperiment_v2.m
        event[:, 1] = band_num + gap_num + attn_num
        events.append(event)

    print('\n  Epoching...')
    epochs = Epochs(raws, events, event_dict, t_min, t_max)

    kernel_fits = list()
    kernel_zscores = list()
    kernel_fits_continuous = list()
    for kk, kernel in enumerate(kernels):
        kname = ['LABSN', 'Wierda'][kk]
        print('  Deconvolving ({} kernel)...'.format(kname))
        fit, time_pts = epochs.deconvolve(kernel=kernel, n_jobs=6)
        zscore = epochs.pupil_zscores()
        if deconv_time_pts is None:
            deconv_time_pts = time_pts
        assert np.array_equal(deconv_time_pts, time_pts)

        # reorder fits to match big_mat (stim# in serial order, voc band, time)
        order = np.array(run_inds)