def load(subject, event_type):
    # Behavior
    fname = op.join(path_data, subject, 'behavior_%s.hdf5' % event_type)
    events = read_hdf5(fname)
    # add explicit conditions
    events = complete_behavior(events)

    # MEG
    fname = op.join(path_data, subject, 'epochs_%s.fif' % event_type)
    epochs = mne.read_epochs(fname)
    return epochs, events
Пример #2
0
def load(subject, event_type):
    # Behavior
    fname = op.join(path_data, subject, 'behavior_%s.hdf5' % event_type)
    events = read_hdf5(fname)
    # add explicit conditions
    events = complete_behavior(events)

    # MEG
    if target_baseline:
        fname = op.join(path_data, subject,
                        'epochs_tf_%s.fif' % event_type)  # noqa
    else:
        fname = op.join(path_data, subject,
                        'epochs_tf_%s_bsl.fif' % event_type)  # noqa
    epochs = mne.read_epochs(fname)
    # epochs.decimate(10)
    return epochs, events
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['xtick.labelsize'] = 16
plt.rcParams['ytick.labelsize'] = 16

all_correct = list()
cueangle_correct = list()
cuesfreq_correct = list()
cueleft_correct = list()
cueright_correct = list()

for subject in subjects:
    # Read behav file (hdf5)
    print '**********' + subject + '************'
    fname = op.join(path_data, subject, 'behavior_target.hdf5')
    events = read_hdf5(fname)
    events = complete_behavior(events)
    # Select behav perf on all trials
    isfixed = np.where(events['is_eye_fixed'] == 1)
    iscorrect = np.array(events['is_correct'])
    iscorrect_fixed = iscorrect[isfixed]
    if len(iscorrect_fixed) != 800:
        warnings.warn("Total isfixed trial is not 800")
        print 'total is:' + str(len(iscorrect_fixed))
    perc = sum(iscorrect_fixed) / len(iscorrect_fixed)
    all_correct.append(perc)

    # behav perf only cue angle
    cue_angle = np.where(events['cue_type'] == 'angle')
    if len(cue_angle[0]) != 400:
        warnings.warn("Total trial with cue angle is not 400")
        print 'total is:' + str(len(cue_angle[0]))
def make_cue_epoch_tf(subject):
    """Create cue epochs during WM task """
    fname_raw = op.join(path_data, subject)
    # Read behavioral file
    fname_bhv = list()
    files = os.listdir(op.join(path_data, subject, 'behavdata'))
    fname_bhv.extend(([
        op.join(fname_raw + '/behavdata/') + f for f in files if 'WorkMem' in f
    ]))
    for fname_behavior in fname_bhv:
        events_behavior = get_events_from_mat(fname_behavior)
    # Read raw MEG data and extract event triggers
    runs = list()
    files = os.listdir(fname_raw)
    runs.extend(([op.join(fname_raw + '/') + f for f in files if '.ds' in f]))
    events_meg = list()
    for run_number, this_run in enumerate(runs):
        fname_raw = op.join(path_data, subject, this_run)
        print(fname_raw)
        raw = read_raw_ctf(fname_raw, preload=True, system_clock='ignore')
        channel_trigger = np.where(np.array(raw.ch_names) == 'USPT001')[0][0]
        # replace 255 values with 0
        trigger_baseline = np.where(raw._data[channel_trigger, :] == 255)[0]
        raw._data[channel_trigger, trigger_baseline] = 0.
        # find triggers
        events_meg_ = mne.find_events(raw)
        # Add 48ms to the trigger events (according to delay with photodiod)
        events_meg_ = np.array(events_meg_, float)
        events_meg_[:, 0] += round(.048 * raw.info['sfreq'])
        events_meg_ = np.array(events_meg_, int)
        # to keep the run from which the event was found
        events_meg_[:, 1] = run_number
        events_meg.append(events_meg_)
    # concatenate all meg events
    events_meg = np.vstack(events_meg)
    # add trigger index to meg_events
    events_target = range(1, 126)
    events_cue = range(126, 130)
    events_probe = range(130, 141)
    triggidx_array = []
    for trigg in events_meg[:, 2]:
        if trigg in events_target:
            triggidx = 1
        elif trigg in events_cue:
            triggidx = 2
        elif trigg in events_probe:
            triggidx = 3
        triggidx_array.append(triggidx)
    events_meg = np.insert(events_meg, 3, triggidx_array, axis=1)
    # Compare MEG and bhv triggers and save events_behavior for each event
    event_type = 'Cue'
    events_behavior_type = []
    events_behavior_type = fix_triggers(events_meg,
                                        events_behavior,
                                        event_type='trigg' + event_type)
    epochs_list = list()
    # Read raw MEG, filter and epochs
    for run_number, this_run in enumerate(runs):
        fname_raw = op.join(path_data, subject, this_run)
        print(fname_raw)
        raw = read_raw_ctf(fname_raw, preload=True, system_clock='ignore')
        events_meg_run = make_events_run(events_behavior_type, run_number)
        event_id = {
            'ttl_%i' % ii: ii
            for ii in np.unique(events_meg_run[:, 2])
        }
        tmin = -.200
        tmax = 1.500
        epochs = Epochs(raw,
                        events_meg_run,
                        event_id=event_id,
                        tmin=tmin,
                        tmax=tmax,
                        preload=True,
                        baseline=None,
                        decim=10)
        # Copy dev_head_t of the first run to others run
        if run_number == 0:
            dev_head_t = epochs.info['dev_head_t']
        else:
            epochs.info['dev_head_t'] = dev_head_t
        # Apply baseline from beginning of epoch to t0
        epochs.apply_baseline((-0.2, 0.))
        epochs_list.append(epochs)
    epochs = concatenate_epochs(epochs_list)
    epochs.pick_types(meg=True, ref_meg=False)
    events = events_behavior_type
    events = complete_behavior(events)
    return epochs, events