Example #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 = 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)
Example #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)
Example #3
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)
Example #4
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)
Example #5
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)
Example #6
0
def test_find_custom_events():
    """Test finding user-defined events"""
    raw = read_raw(fname)
    events = raw.find_events('TRIALID', 1)
    assert_true(len(events) > 0)
    assert_raises(ValueError, raw.find_events, list(), 1)
    events_2 = raw.find_events(lambda x: 'TRIALID' in x, 1)
    assert_array_equal(events, events_2)
Example #7
0
def test_raw_plot():
    """Test plotting of raw"""
    for fi, fname in enumerate(fnames):
        raw = read_raw(fname)
        raw.plot_calibration()
        raw.plot_heatmap(0., 10., vmax=1)
        raw.plot_heatmap(0., 10., kernel=None)
        raw.plot()
Example #8
0
def test_read_write_hd5():
    """Test reading and writing of HD5."""
    for fname in fnames:
        r = read_raw(fname)
        out_fname = op.join(temp_dir, 'temp.hd5')
        r.save(out_fname, overwrite=True)
        pytest.raises(IOError, r.save, out_fname)  # overwrite=False
        r2 = read_raw(out_fname)
        r2.save(out_fname, overwrite=True)  # double write (make sure works)
        r2 = read_raw(out_fname)
        # samples
        assert_array_equal(r._samples, r2._samples)
        # times
        assert_array_equal(r._times, r2._times)
        # discrete
        for key in r.discrete.keys():
            assert_array_equal(r.discrete[key], r2.discrete[key])
        # info
        assert set(r.info.keys()) == set(r2.info.keys())
        assert_array_equal(r.info['calibrations'], r2.info['calibrations'])
Example #9
0
def test_read_write_hd5():
    """Test reading and writing of HD5"""
    for fname in fnames:
        r = read_raw(fname)
        out_fname = op.join(temp_dir, 'temp.hd5')
        r.save(out_fname, overwrite=True)
        assert_raises(IOError, r.save, out_fname)  # overwrite=False
        r2 = read_raw(out_fname)
        r2.save(out_fname, overwrite=True)  # double write (make sure works)
        r2 = read_raw(out_fname)
        # samples
        assert_array_equal(r._samples, r2._samples)
        # times
        assert_array_equal(r._times, r2._times)
        # discrete
        for key in r.discrete.keys():
            assert_array_equal(r.discrete[key], r2.discrete[key])
        # info
        assert_true(set(r.info.keys()) == set(r2.info.keys()))
        assert_array_equal(r.info['calibrations'], r2.info['calibrations'])
Example #10
0
def test_access_data():
    """Test raw slicing and indexing."""
    for fname in fnames:
        raw = read_raw(fname)
        for idx in [[1, 3], slice(50)]:
            data, times = raw[:, idx]
            assert data.shape[1] == len(times)
        pytest.raises(KeyError, raw.__getitem__, 'foo')
        data, times = raw[:, 1]
        assert data.ndim == 1
        assert np.atleast_1d(times).size == 1

        # test for monotonous continuity
        deltas = np.diff(raw.times)
        assert_allclose(deltas, deltas[0] * np.ones_like(deltas))
        assert_allclose(deltas[0], 0.001)
Example #11
0
def test_access_data():
    """Test raw slicing and indexing"""
    for fname in fnames:
        raw = read_raw(fname)
        for idx in [[1, 3], slice(50)]:
            data, times = raw[:, idx]
            assert_equal(data.shape[1], len(times))
        assert_raises(KeyError, raw.__getitem__, 'foo')
        data, times = raw[:, 1]
        assert_equal(data.ndim, 1)
        assert_equal(np.atleast_1d(times).size, 1)

        # test for monotonous continuity
        deltas = np.diff(raw.times)
        assert_allclose(deltas, deltas[0] * np.ones_like(deltas))
        assert_allclose(deltas[0], 0.001)
Example #12
0
def test_raw_io():
    """Test raw EDF IO functionality."""
    for fi, fname in enumerate(fnames):
        raw = read_raw(fname)
        print(raw)  # test repr works

        # tests dtypes are parsed correctly that is double only
        assert raw._samples.dtype == np.float64

        if fi == 0:  # First test file has this property
            for kind in ['saccades', 'fixations', 'blinks']:
                # relax, depends on data
                assert raw.discrete[kind]['stime'][0] < 12.0
        assert raw.times[0] < 1.0
        raw.remove_blink_artifacts(use_only_blink=True)
        for interp in [None, 'zoh', 'linear']:
            raw.remove_blink_artifacts(interp)
Example #13
0
def test_raw_io():
    """Test raw EDF IO functionality"""
    for fi, fname in enumerate(fnames):
        raw = read_raw(fname)
        print(raw)  # test repr works

        # tests dtypes are parsed correctly that is double only
        assert_equal(raw._samples.dtype, np.float64)

        if fi == 0:  # First test file has this property
            for kind in ['saccades', 'fixations', 'blinks']:
                # relax, depends on data
                assert_true(raw.discrete[kind]['stime'][0] < 12.0)
        assert_true(raw.times[0] < 1.0)
        raw.remove_blink_artifacts(use_only_blink=True)
        for interp in [None, 'zoh', 'linear']:
            raw.remove_blink_artifacts(interp)
Example #14
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)
Example #15
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)
Example #16
0
# Authors: Denis Engemann <*****@*****.**>
#
# License: BSD (3-clause)

import numpy as np
import matplotlib.pyplot as plt

import pyeparse as pp

fname = '../pyeparse/tests/data/test_raw.edf'

raw = pp.read_raw(fname)

# visualize initial calibration
raw.plot_calibration(title='5-Point Calibration')

# create heatmap
raw.plot_heatmap(start=3., stop=60.)

# find events and epoch data
events = raw.find_events('SYNCTIME', event_id=1)
tmin, tmax, event_id = -0.5, 1.5, 1
epochs = pp.Epochs(raw, events=events, event_id=event_id, tmin=tmin, tmax=tmax)

# access pandas data frame and plot single epoch
fig, ax = plt.subplots()
ax.plot(epochs[3].get_data('xpos')[0], epochs[3].get_data('ypos')[0])

# iterate over and access numpy arrays.
# find epochs withouth loss of tracking / blinks
print(len([e for e in epochs if not np.isnan(e).any()]))
 assert len(subj_data_dir) == 1
 subj_data_dir = subj_data_dir[0]
 fnames = sorted(glob(op.join(subj_data_dir, 'subj{}_*.edf'.format(subj))))
 assert len(fnames) in [13, 14, 15]
 fnames = fnames[-10:]
 subj_mat = glob(op.join(data_dir, 'subj{}_*.mat'.format(subj)))
 assert len(subj_mat) == 1
 subj_mat = loadmat(subj_mat[0])
 time_vecs = subj_mat['timeVecs'][-10:]
 time_vecs = [t[0][:, 1] for t in time_vecs]  # column 1 is "sound onset"
 raws = list()
 events = list()
 print('  Loading block', end=' ')
 for ri, fname in enumerate(fnames):
     print(str(ri + 1), end=' ')
     raw = read_raw(fname)
     assert raw.info['sfreq'] == fs
     raw.remove_blink_artifacts()
     raws.append(raw)
     # convert 1-based trial numbers (matlab) to 0-based
     stim_nums = run_inds[ri][:, 0] - 1
     small_mat = big_mat[stim_nums]
     # TRIALID 3 = a real trial (0, 1, and 2 are types of training trials)
     event = raw.find_events('TRIALID 3', 1)
     eyelink_stim_ord = [int(m[1].split(',')[3]) - 1
                         for m in raw.discrete['messages']
                         if 'TRIALID 3' in m[1]]
     # check for missing trials
     n_miss = len(stim_nums) - len(event)
     if n_miss > 0:
         missing = list()
Example #18
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])
Example #19
0
                                 dtype=bool)
            target_exp = np.array([(x & 2**5) >> 5 for x in target_triggers],
                                  dtype=bool)
        if exp.startswith('SENT'):
            prime_triggers = dat[:, 4].astype(int)
            target_triggers = dat[:, 8].astype(int)
            prime_exp = np.array([(x & 2**4) >> 4 for x in prime_triggers],
                                 dtype=bool)
            target_exp = np.array([(x & 2**4) >> 4 for x in target_triggers],
                                  dtype=bool)

        prime_triggers = prime_triggers[prime_exp]
        target_triggers = target_triggers[target_exp]

        # extracting fixation times from the edf file.
        raw = pp.read_raw(file_raw)
        times = list()
        triggers = list()
        trialids = list()
        ias = list()
        # first for the primes
        pat = '!V TRIAL_VAR TIME_PRIME'
        msgs = raw.find_events(pat, 1)[:, -1]
        prime_times = np.array([int(x[(len(pat) + 1):]) for x in msgs], int)
        prime_times = prime_times[prime_exp]
        assert prime_triggers.shape[0] == prime_times.shape[0]
        times.append(prime_times)
        triggers.append(prime_triggers)
        trialids.append(np.arange(len(prime_triggers)) + ii * 240)
        ias.append(['prime'] * len(prime_triggers))
        # then for the targets
    fnames = fnames[ix:]  # first blocks are training & pupil response function

    # subject's expyfun log
    subj_tab = glob(op.join(indir, '{}_*.tab'.format(subj)))
    assert len(subj_tab) == 1
    subj_tab = subj_tab[0]
    with open(subj_tab, 'r') as fid:
        session = int(eval(fid.readline().strip()[2:])['session']) - 1
    subj_tab = read_tab(subj_tab)
    subj_tab = subj_tab[-n_trials:]
    stim_onset_times = [s['play'][0][1] for s in subj_tab]

    print('  Loading block', end=' ')
    for run_ix, fname in enumerate(fnames):
        print(str(run_ix + 1), end=' ')
        raw = read_raw(fname)
        assert raw.info['sfreq'] == fs_in
        raw.remove_blink_artifacts()
        raws.append(raw)
        # get the stimulus numbers presented in this block
        this_stim_nums = \
            params['block_trials'][params['blocks'][session][run_ix]]
        stim_nums.extend(this_stim_nums)
        this_cond_mat = cond_mat[this_stim_nums]
        # extract event codes from eyelink data
        event = raw.find_events('SYNCTIME', 1)
        ttls = [
            np.array([int(mm) for mm in m[1].decode().split(' ')[1:]])
            for m in raw.discrete['messages']
            if m[1].decode().startswith('TRIALID')
        ]
def process_fixations(sub, n, epoch_epoch_start, epoch_epoch_end,
                      eyetracking_dir, behavioural_dir, simulated_dir,
                      left_aoi, right_aoi, phase):
    """
    Produces a dataframe of fixation bias indices for each trial and each subject

    Args:
        epoch_epoch_start: Start of the epoch (post trial phase onset)
        epoch_epoch_end: End of the epoch
        eyetracking_dir: Directory containing eyetracking data
        behavioural_data: Directory containing behavioural data for all subjects
        simulated_data: Directory containing simulated data for all subjects
        left_aoi: matplotlib path containing left aoi
        right_aoi: matplotlib path containing right aoi

    Returns:
        A dataframe

    """

    # Load eyetracking data
    et_path = os.path.join(eyetracking_dir, [
        i for i in os.listdir(eyetracking_dir)
        if str(sub) in i and 'eyetracker' in i
    ][0])

    raw = pp.read_raw(et_path)

    # Get trials
    preoutcome_events = raw.find_events('_preoutcome', 1)
    outcome_events = raw.find_events('_outcome', 1)

    for n, i in enumerate(raw.discrete['messages']):
        m = re.search('Trial_[0-9]+$', i[1])
        if m:
            raw.discrete['messages'][n] = (i[0], i[1] + '_start')
    trial_start_events = raw.find_events('_start', 1)

    # Trial numbers
    trial_numbers = [
        int(re.search('\d+', i[1]).group()) for i in raw.discrete['messages']
        if phase in i[1]
    ]
    trial_start_events = trial_start_events[
        trial_numbers]  # Remove non-existent trials and shift in time

    tmin, tmax, event_id = epoch_epoch_start, epoch_epoch_end, 1
    preoutcome_epochs = pp.Epochs(raw,
                                  events=preoutcome_events,
                                  event_id=event_id,
                                  tmin=tmin,
                                  tmax=tmax)

    if len(trial_numbers) > len(preoutcome_epochs):
        trial_numbers = trial_numbers[:len(preoutcome_epochs)]

    preoutcome_fixations = {}

    preoutcome_duration_total = 0
    blink_duration_total = 0

    # The preoutcome phase doesn't have a standard duration so we need to customise this for each trial
    for n in range(len(preoutcome_epochs)):
        if 'preoutcome' in phase:
            preoutcome_duration = raw.times[outcome_events[n][0]] - raw.times[
                preoutcome_events[n][0]]
        else:
            preoutcome_duration = raw.times[
                trial_start_events[n][0]] - raw.times[outcome_events[n][0]]
            if preoutcome_duration > 7:
                preoutcome_duration = 4  # durations before breaks can be a longer than they should be so set to minimum expected
        preoutcome_fixations[trial_numbers[n]] = preoutcome_epochs[
            n].fixations[0][preoutcome_epochs[n].fixations[0]['stime'] <
                            preoutcome_duration]
        preoutcome_fixations[trial_numbers[n]]['etime'][
            preoutcome_fixations[trial_numbers[n]]['etime'] >
            preoutcome_duration] = preoutcome_duration

        preoutcome_blinks = preoutcome_epochs[n].blinks[0][
            preoutcome_epochs[n].blinks[0]['stime'] < preoutcome_duration]
        preoutcome_blinks['etime'][preoutcome_blinks['etime'] >
                                   preoutcome_duration] = preoutcome_duration
        blink_duration_total += np.sum(
            np.array([blink[1] - blink[0] for blink in preoutcome_blinks]))
        preoutcome_duration_total += preoutcome_duration

        if np.any(preoutcome_fixations[trial_numbers[n]]['etime'] -
                  preoutcome_fixations[trial_numbers[n]]['stime'] < 0):
            print preoutcome_fixations[trial_numbers[n]]
            raise ValueError(
                "Fixations of less than 0 seconds on trial {0}".format(n))

    # Load behavioural
    behavioural_path = [
        i for i in os.listdir(behavioural_dir)
        if str(sub) in i and 'behaviour' in i
    ][0]
    behavioural = pd.read_csv(os.path.join(behavioural_dir, behavioural_path))
    behavioural = behavioural[(~behavioural.A_shock_prob.isnull()) &
                              (behavioural.trial_number != 999)].reset_index()

    # Load behavioural & simulated data
    simulated = pd.read_csv(
        os.path.join(simulated_dir, '{0}_simulated_data.txt'.format(sub)))
    simulated.loc[:,
                  'A_pe'] = simulated.loc[:,
                                          'A_Outcome'] - simulated.loc[:,
                                                                       'A_True_response']
    simulated.loc[:,
                  'B_pe'] = simulated.loc[:,
                                          'B_Outcome'] - simulated.loc[:,
                                                                       'B_True_response']
    simulated.loc[:, 'abs_pe_RL_diff'] = np.abs(simulated.A_pe) - np.abs(
        simulated.B_pe)
    simulated.loc[:, 'abs_pe_RL_diff'] = np.roll(
        simulated.loc[:, 'abs_pe_RL_diff'], 1)
    simulated.loc[:,
                  'prob_estimate_RL_diff'] = simulated.A_True_response - simulated.B_True_response
    simulated.loc[:,
                  'model_prob_estimate_RL_diff'] = simulated.A_Response - simulated.B_Response
    simulated.loc[:, 'var_RL_diff'] = simulated.A_var - simulated.B_var
    simulated.loc[:,
                  'objective_prob_RL_diff'] = behavioural.A_shock_prob - behavioural.B_shock_prob

    # trial number correction
    simulated.trial_number += 1

    # Get bias
    bias = []
    # Fixation durations (total for each trial)
    l_durations = []
    r_durations = []
    # First fixation durations for each stimulus
    l_first_durations = []
    r_first_durations = []
    outside_durations = []
    # First fixation location
    first_fix_locations = []

    l_duration_total = 0
    r_duration_total = 0
    outside_duration_total = 0

    for e in range(1, 161):
        if e not in trial_numbers:
            bias.append(np.nan)
            l_durations.append(np.nan)
            r_durations.append(np.nan)
            l_first_durations.append(np.nan)
            r_first_durations.append(np.nan)
            outside_durations.append(np.nan)
            first_fix_locations.append(np.nan)
        else:
            fix_locs = np.array([
                get_aoi(fix[2], fix[3], left_aoi, right_aoi)
                for fix in preoutcome_fixations[e]
            ])
            fix_duration = np.array(
                [fix[1] - fix[0] for fix in preoutcome_fixations[e]])

            l_duration = (fix_duration[fix_locs < 0]).sum()
            l_duration_total += l_duration
            r_duration = (fix_duration[fix_locs > 0]).sum()
            r_duration_total += r_duration

            stim_fix_locs = [i for i in fix_locs if not np.isnan(i)]
            if not len(stim_fix_locs):
                first_fix_locations.append(np.nan)
            else:
                first_fix_locations.append(stim_fix_locs[0])

            # First fixations
            if len(fix_duration[fix_locs < 0]):
                l_first_duration = fix_duration[fix_locs < 0][0]
            else:
                l_first_duration = 0

            if len(fix_duration[fix_locs > 0]):
                r_first_duration = fix_duration[fix_locs > 0][0]
            else:
                r_first_duration = 0

            outside_duration = (fix_duration[np.isnan(fix_locs)]).sum()
            outside_duration_total += outside_duration
            if l_duration > 0 or r_duration > 0:
                b = l_duration / (l_duration + r_duration)
            else:
                b = np.nan
            bias.append(b)
            l_durations.append(l_duration)
            r_durations.append(r_duration)
            l_first_durations.append(l_first_duration)
            r_first_durations.append(r_first_duration)
            outside_durations.append(outside_duration)

    bias = np.array(bias)
    bias[np.isnan(bias)] = np.nanmean(bias)  # Impute nans

    bias_df = behavioural[['Subject', 'trial_number',
                           'Outcome_image_L']].copy()
    bias_df.loc[:, 'bias'] = bias
    bias_df.loc[:, 'l_duration'] = l_durations
    bias_df.loc[:, 'r_duration'] = r_durations
    bias_df.loc[:, 'l_first_duration'] = l_first_durations
    bias_df.loc[:, 'r_first_duration'] = r_first_durations
    bias_df.loc[:, 'first_fixation_location'] = first_fix_locations
    bias_df.loc[:, 'outside_duration'] = outside_durations
    bias_df.loc[:, 'l_prop'] = bias_df.loc[:, 'l_duration'] / (
        bias_df.loc[:, 'l_duration'] + bias_df.loc[:, 'r_duration'] +
        bias_df.loc[:, 'outside_duration'])
    bias_df.loc[:, 'r_prop'] = bias_df.loc[:, 'r_duration'] / (
        bias_df.loc[:, 'l_duration'] + bias_df.loc[:, 'r_duration'] +
        bias_df.loc[:, 'outside_duration'])
    bias_df.loc[:, 'l_prop'] = np.nanmean(bias_df.loc[:, 'l_prop'])
    bias_df.loc[:, 'r_prop'] = np.nanmean(bias_df.loc[:, 'r_prop'])

    bias_df.loc[:, 'preoutcome_duration_total'] = preoutcome_duration_total
    bias_df.loc[:, 'blink_duration_total'] = blink_duration_total
    bias_df.loc[:,
                'blink_proportion'] = blink_duration_total / preoutcome_duration_total
    bias_df.loc[:, 'l_duration_total'] = l_duration_total
    bias_df.loc[:, 'r_duration_total'] = r_duration_total
    bias_df.loc[:, 'outside_duration_total'] = outside_duration_total
    bias_df.loc[:, 'left_proportion'] = l_duration_total / (r_duration_total +
                                                            l_duration_total)
    bias_df.loc[:, 'right_proportion'] = r_duration_total / (r_duration_total +
                                                             l_duration_total)
    bias_df.loc[:, 'outside_proportion'] = outside_duration_total / (
        r_duration_total + l_duration_total + outside_duration_total)

    bias_df = pd.merge(bias_df,
                       simulated[[
                           'trial_number', 'prob_estimate_RL_diff',
                           'var_RL_diff', 'objective_prob_RL_diff', 'A_var',
                           'A_True_response', 'B_var', 'B_True_response',
                           'A_Outcome', 'B_Outcome', 'abs_pe_RL_diff', 'A_pe',
                           'B_pe', 'model_prob_estimate_RL_diff'
                       ]],
                       on='trial_number')

    bias_df2 = bias_df[[c for c in bias_df.columns if 'B_' not in c]].copy()
    bias_df3 = bias_df[[c for c in bias_df.columns if 'A_' not in c]].copy()
    bias_df2.loc[bias_df2.Outcome_image_L == 'B',
                 'l_prop'] = bias_df2['r_prop']
    bias_df3.loc[bias_df2.Outcome_image_L == 'A',
                 'l_prop'] = bias_df3['r_prop']

    bias_df2['stimulus'] = 0
    bias_df3['stimulus'] = 1

    bias_df2.columns = [c.replace('A_', '') for c in bias_df2.columns]
    bias_df3.columns = [c.replace('B_', '') for c in bias_df3.columns]

    duration_df = pd.concat([bias_df2, bias_df3])

    return bias_df, duration_df
Example #22
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])
# Authors: Denis Engemann <*****@*****.**>
#
# License: BSD (3-clause)

import numpy as np
import matplotlib.pyplot as plt

import pyeparse as pp

fname = '../pyeparse/tests/data/test_raw.edf'

raw = pp.read_raw(fname)

# visualize initial calibration
raw.plot_calibration(title='5-Point Calibration')

# create heatmap
raw.plot_heatmap(start=3., stop=60.)

# find events and epoch data
events = raw.find_events('SYNCTIME', event_id=1)
tmin, tmax, event_id = -0.5, 1.5, 1
epochs = pp.Epochs(raw, events=events, event_id=event_id, tmin=tmin,
                   tmax=tmax)

# access pandas data frame and plot single epoch
fig, ax = plt.subplots()
ax.plot(epochs[3].get_data('xpos')[0], epochs[3].get_data('ypos')[0])

# iterate over and access numpy arrays.
# find epochs withouth loss of tracking / blinks