コード例 #1
0
def test_define_events():
    """Test defining response events."""
    events = read_events(fname)
    raw = read_raw_fif(raw_fname)
    events_, _ = define_target_events(events, 5, 32, raw.info['sfreq'],
                                      .2, 0.7, 42, 99)
    n_target = events[events[:, 2] == 5].shape[0]
    n_miss = events_[events_[:, 2] == 99].shape[0]
    n_target_ = events_[events_[:, 2] == 42].shape[0]

    assert (n_target_ == (n_target - n_miss))

    events = np.array([[0, 0, 1],
                       [375, 0, 2],
                       [500, 0, 1],
                       [875, 0, 3],
                       [1000, 0, 1],
                       [1375, 0, 3],
                       [1100, 0, 1],
                       [1475, 0, 2],
                       [1500, 0, 1],
                       [1875, 0, 2]])
    true_lag_nofill = [1500., 1500., 1500.]
    true_lag_fill = [1500., np.nan, np.nan, 1500., 1500.]
    n, lag_nofill = define_target_events(events, 1, 2, 250., 1.4, 1.6, 5)
    n, lag_fill = define_target_events(events, 1, 2, 250., 1.4, 1.6, 5, 99)

    assert_array_equal(true_lag_fill, lag_fill)
    assert_array_equal(true_lag_nofill, lag_nofill)
コード例 #2
0
def test_define_events():
    """Test defining response events."""
    events = read_events(fname)
    raw = read_raw_fif(raw_fname)
    events_, _ = define_target_events(events, 5, 32, raw.info['sfreq'],
                                      .2, 0.7, 42, 99)
    n_target = events[events[:, 2] == 5].shape[0]
    n_miss = events_[events_[:, 2] == 99].shape[0]
    n_target_ = events_[events_[:, 2] == 42].shape[0]

    assert_true(n_target_ == (n_target - n_miss))

    events = np.array([[0, 0, 1],
                       [375, 0, 2],
                       [500, 0, 1],
                       [875, 0, 3],
                       [1000, 0, 1],
                       [1375, 0, 3],
                       [1100, 0, 1],
                       [1475, 0, 2],
                       [1500, 0, 1],
                       [1875, 0, 2]])
    true_lag_nofill = [1500., 1500., 1500.]
    true_lag_fill = [1500., np.nan, np.nan, 1500., 1500.]
    n, lag_nofill = define_target_events(events, 1, 2, 250., 1.4, 1.6, 5)
    n, lag_fill = define_target_events(events, 1, 2, 250., 1.4, 1.6, 5, 99)

    assert_array_equal(true_lag_fill, lag_fill)
    assert_array_equal(true_lag_nofill, lag_nofill)
コード例 #3
0
ファイル: test_event.py プロジェクト: kingjr/mne-python
def test_define_events():
    """Test defining response events
    """
    events = read_events(fname)
    raw = io.Raw(raw_fname)
    events_, _ = define_target_events(events, 5, 32, raw.info["sfreq"], 0.2, 0.7, 42, 99)
    n_target = events[events[:, 2] == 5].shape[0]
    n_miss = events_[events_[:, 2] == 99].shape[0]
    n_target_ = events_[events_[:, 2] == 42].shape[0]

    assert_true(n_target_ == (n_target - n_miss))
コード例 #4
0
def test_define_events():
    """Test defining response events
    """
    events = read_events(fname)
    raw = io.Raw(raw_fname)
    events_, _ = define_target_events(events, 5, 32, raw.info['sfreq'],
                                      .2, 0.7, 42, 99)
    n_target = events[events[:, 2] == 5].shape[0]
    n_miss = events_[events_[:, 2] == 99].shape[0]
    n_target_ = events_[events_[:, 2] == 42].shape[0]

    assert_true(n_target_ == (n_target - n_miss))
コード例 #5
0
# pick MEG channels
picks = fiff.pick_types(raw.info, meg='mag', eeg=False, stim=False, eog=True,
                        include=include, exclude='bads')

###############################################################################
# Find stimulus event followed by quick button presses

reference_id = 5  # presentation of a smiley face
target_id = 32  # button press
sfreq = raw.info['sfreq']  # sampling rate
tmin = 0.1  # trials leading to very early responses will be rejected
tmax = 0.59  # ignore face stimuli followed by button press later than 590 ms
new_id = 42  # the new event id for a hit. If None, reference_id is used.
fill_na = 99  # the fill value for misses

events_, lag = define_target_events(events, reference_id, target_id,
                    sfreq, tmin, tmax, new_id, fill_na)

print events_  # The 99 indicates missing or too late button presses

# besides the events also the lag between target and reference is returned
# this could e.g. be used as parametric regressor in subsequent analyses.

print lag[lag != fill_na]  # lag in milliseconds

# #############################################################################
# Construct epochs

tmin_ = -0.2
tmax_ = 0.4
event_id = dict(early=new_id, late=fill_na)
コード例 #6
0
picks = pick_types(raw.info, eeg=True)
events = mne.find_events(raw)

###############################################################################
# Create Epochs

# define target events:
# 1. find response times: distance between "square" and "rt" events
# 2. extract A. "square" events B. followed by a button press within 700 msec
tmax = .7
sfreq = raw.info["sfreq"]
reference_id, target_id = 2, 1
new_events, rts = define_target_events(events,
                                       reference_id,
                                       target_id,
                                       sfreq,
                                       tmin=0.,
                                       tmax=tmax,
                                       new_id=2)

epochs = Epochs(raw,
                events=new_events,
                tmax=tmax + .1,
                event_id={"square": 2},
                picks=picks)

###############################################################################
# Plot

# Parameters for plotting
order = rts.argsort()  # sorting from fast to slow trials
コード例 #7
0
eog = {"FPz", "EOG1", "EOG2"}
raw = io.eeglab.read_raw_eeglab(fname, eog=eog, montage=montage,
                                event_id=event_id)
picks = pick_types(raw.info, eeg=True)
events = mne.find_events(raw)

###############################################################################
# Create Epochs

# define target events:
# 1. find response times: distance between "square" and "rt" events
# 2. extract A. "square" events B. followed by a button press within 700 msec
tmax = .7
sfreq = raw.info["sfreq"]
reference_id, target_id = 2, 1
new_events, rts = define_target_events(events, reference_id, target_id, sfreq,
                                       tmin=0., tmax=tmax, new_id=2)

epochs = Epochs(raw, events=new_events, tmax=tmax + .1,
                event_id={"square": 2}, picks=picks)

###############################################################################
# Plot

# Parameters for plotting
order = rts.argsort()  # sorting from fast to slow trials

rois = dict()
for pick, channel in enumerate(epochs.ch_names):
    last_char = channel[-1]  # for 10/20, last letter codes the hemisphere
    roi = ("Midline" if last_char == "z" else
           ("Left" if int(last_char) % 2 else "Right"))
コード例 #8
0
def epoch(raw, mat,Tmin, Tmax):
	# ignore stimuli shorter than tmax ms
	events = mne.find_events(raw, stim_channel='Trigger')
	reference_id = 105  # speech onset
	target_id = 106  # speech offset
	sfreq = raw.info['sfreq']  # sampling rate
	tmin = 0  
	new_id = 99  # the new event id for a hit. If None, reference_id is used.
	fill_na = 105  # the fill value for misses
	events_, lag = define_target_events(events, reference_id, target_id,sfreq, tmin, Tmax, new_id, fill_na)
	events_  = np.where(events_[:,2] == 105)[0] +1


	#behaviour (remove the wrong answer trials and seperate the in three conditions)	
	condition= mat['behaviour']['condition'][0]
	response= mat['behaviour']['response'][0]
	a = np.hstack((condition[0],response[0]))

	df = pd.DataFrame({'condition':a[:,0],'response':a[:,1]})
	df.index = df.index + 1

	hyper  = df.loc[(df['condition'] == 1) & (df['response'] == 0)]
	normal = df.loc[(df['condition'] == 2) & (df['response'] == 0)]
	hypo   = df.loc[(df['condition'] == 3) & (df['response'] == 0)]

	events = mne.find_events(raw, stim_channel='trial_no')
	hyper = np.intersect1d(events_, hyper.index.values)
	normal = np.intersect1d(events_, normal.index.values)
	hypo = np.intersect1d(events_, hypo.index.values)

	a = np.concatenate((hyper,normal,hypo),axis=0)
	IDX = np.sort(a, axis=0) -1

	hyper = events[hyper-1]
	hyper[:,2] = 1
	normal = events[normal-1]
	normal[:,2] = 2
	hypo = events[hypo-1]
	hypo[:,2] = 3
	a = np.vstack((hyper,normal,hypo))
	events = np.sort(a, axis=0)

	# epoching
	event_id = {'hyper': 1,'normal': 2,'hypo': 3}
	# Set up indices of channels to include in analysis
	picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,misc=False)
	epochs_params = dict(events=events, picks=picks,event_id=event_id, tmin=Tmin, tmax=Tmax,preload=True)
	epochs = mne.Epochs(raw, **epochs_params)

	#downsampling
	epochs_resampled = epochs.copy().resample(400, npad='auto')

	#add EMA, envelop signal as extra channels
	extra_channels = ['envelop','jawaopening','lipaparature','lipProtrusion','TTCD','TMCD','TBCD']
	envelop = mat['behaviour']['envelop'][0][0]
	jawaopening = mat['behaviour']['jawaopening'][0][0]
	lipaparature = mat['behaviour']['lipaparature'][0][0]
	lipProtrusion = mat['behaviour']['lipProtrusion'][0][0]
	TTCD = mat['behaviour']['TTCD'][0][0]
	TMCD = mat['behaviour']['TMCD'][0][0]
	TBCD = mat['behaviour']['TBCD'][0][0]

	# Initialize an info structure
	ch_names = np.hstack((epochs.ch_names,extra_channels))
	ch_types = np.repeat('eeg', len(ch_names))
	info = mne.create_info(ch_names = ch_names.tolist(),ch_types = ch_types,sfreq = epochs_resampled.info['sfreq'])

	# create new epoches
	b=abs(Tmin)*epochs_resampled.info['sfreq']
	first_samp=int(b)
	last_samp=int(epochs_resampled.tmax *epochs_resampled.info['sfreq'])

	A = np.zeros((len(epochs_resampled), len(extra_channels), first_samp+last_samp+1))

	for i in range(0, len(epochs_resampled)):
		a = np.vstack((envelop[IDX[i]][0],jawaopening[IDX[i]][0].T,lipaparature[IDX[i]][0].T,lipProtrusion[IDX[i]][0].T,TTCD[IDX[i]][0].T,TMCD[IDX[i]][0].T,TBCD[IDX[i]][0].T))
		b = np.zeros((a.shape[0],first_samp))
		a = np.hstack((b,a))
		A[i,:,:] = a[:,0:first_samp+last_samp+1]

	b = epochs_resampled.get_data()
	c = np.concatenate((b,A),axis=1)

	tmin = Tmin
	epochs = mne.EpochsArray(c, info, events, tmin, event_id)
	return epochs
コード例 #9
0
    #'201195_Chrono01_110516_1413_1_c,rfDC,nr,ocarta,evtW_RLst_bc-epo.fif'
    raw = mne.io.read_raw_fif(fn_raw, preload=True)
    #raw.plot(start=0, duration=120)
    tri_events = mne.find_events(raw, stim_channel='STI 014')
    res_events = mne.find_events(raw, stim_channel='STI 013')

    #Get the events list
    sfreq = raw.info['sfreq']
    levents = np.array(list(tri_events) + list(res_events))
    cons_epochs = []
    i = 0
    # Based on the correct event couples and fixed time range, to find the appropriate epochs
    for cond_id in conds_id:
        tri_id = cond_id[0]
        res_id = cond_id[1]
        events_, lag = define_target_events(levents, tri_id, res_id, sfreq,
                                            ltmin, ltmax)
        picks = mne.pick_types(raw.info,
                               meg='mag',
                               eeg=False,
                               stim=False,
                               eog=False,
                               exclude='bads')
        epochs = mne.Epochs(raw,
                            events_,
                            tri_id,
                            tmin,
                            tmax,
                            picks=picks,
                            baseline=(None, 0),
                            reject=dict(mag=4e-12))
        fn_epo = fn_raw[:fn_raw.rfind(
コード例 #10
0
                       eog=True,
                       include=include,
                       exclude='bads')

###############################################################################
# Find stimulus event followed by quick button presses

reference_id = 5  # presentation of a smiley face
target_id = 32  # button press
sfreq = raw.info['sfreq']  # sampling rate
tmin = 0.1  # trials leading to very early responses will be rejected
tmax = 0.59  # ignore face stimuli followed by button press later than 590 ms
new_id = 42  # the new event id for a hit. If None, reference_id is used.
fill_na = 99  # the fill value for misses

events_, lag = define_target_events(events, reference_id, target_id, sfreq,
                                    tmin, tmax, new_id, fill_na)

print(events_)  # The 99 indicates missing or too late button presses

# besides the events also the lag between target and reference is returned
# this could e.g. be used as parametric regressor in subsequent analyses.

print(lag[lag != fill_na])  # lag in milliseconds

# #############################################################################
# Construct epochs

tmin_ = -0.2
tmax_ = 0.4
event_id = dict(early=new_id, late=fill_na)
コード例 #11
0
        levents = np.array(list(tri_events) + list(res_events))
        cons_epochs = []
        i = 0
        # Based on the correct event couples and fixed time range, to find the appropriate epochs
        for cond_id in conds_id:
            tri_id = cond_id[0]
            res_id = cond_id[1]
            picks = mne.pick_types(raw.info,
                                   meg='mag',
                                   eeg=False,
                                   stim=False,
                                   eog=False,
                                   exclude='bads')

            # Take the triger events as the reference events
            events_, lag = define_target_events(levents, tri_id, res_id, sfreq,
                                                tstmin, tstmax)
            epochs = mne.Epochs(raw,
                                events_,
                                tri_id,
                                tmin,
                                tmax,
                                picks=picks,
                                baseline=None,
                                reject=dict(mag=4e-12))
            #Crope the baseline time range
            epochs.load_data()
            epo_base = epochs.copy().crop(None, 0)
            line_base = epo_base.get_data().mean(axis=-1, keepdims=True)
            # Baseline corrected for trigger events
            epochs._data = epochs.get_data() - line_base
            fn_epo = fn_raw[:fn_raw.rfind(
コード例 #12
0
def listen_italian_epoch(raw, mat, Tmin, Tmax):
    # extract trials of tmax second and remove the wrong answer trials and seperate the in three conditions
    # start and end of an epoch in sec.

    # ignore stimuli shorter than tmax ms
    events = mne.find_events(raw, stim_channel='Trigger')
    reference_id = 105  # speech onset
    target_id = 106  # speech offset
    sfreq = raw.info['sfreq']  # sampling rate
    tmin = 0
    new_id = 99  # the new event id for a hit. If None, reference_id is used.
    fill_na = 105  # the fill value for misses
    events_, lag = define_target_events(events, reference_id, target_id, sfreq,
                                        tmin, Tmax, new_id, fill_na)
    events_ = np.where(events_[:, 2] == 105)[0] + 1

    #behaviour (remove the wrong answer trials and seperate the in three conditions)
    condition = mat['behaviour']['condition'][0]
    response = mat['behaviour']['response'][0]
    a = np.hstack((condition[0], response[0]))

    df = pd.DataFrame({'condition': a[:, 0], 'response': a[:, 1]})
    df.index = df.index + 1

    hyper = df.loc[(df['condition'] == 1) & (df['response'] == 0)]
    normal = df.loc[(df['condition'] == 2) & (df['response'] == 0)]
    hypo = df.loc[(df['condition'] == 3) & (df['response'] == 0)]

    events = mne.find_events(raw, stim_channel='trial_no')
    hyper = np.intersect1d(events_, hyper.index.values)
    normal = np.intersect1d(events_, normal.index.values)
    hypo = np.intersect1d(events_, hypo.index.values)

    hyper = events[hyper - 1]
    hyper[:, 2] = 1
    normal = events[normal - 1]
    normal[:, 2] = 2
    hypo = events[hypo - 1]
    hypo[:, 2] = 3
    a = np.vstack((hyper, normal, hypo))
    events = np.sort(a, axis=0)

    # epoching
    reject = dict(eeg=180e-6)
    event_id = {'hyper': 1, 'normal': 2, 'hypo': 3}
    # Set up indices of channels to include in analysis
    picks = mne.pick_types(raw.info,
                           meg=False,
                           eeg=True,
                           stim=False,
                           eog=False,
                           misc=False)
    epochs_params = dict(events=events,
                         picks=picks,
                         event_id=event_id,
                         tmin=Tmin,
                         tmax=Tmax,
                         reject=reject,
                         preload=True)
    epochs = mne.Epochs(raw, **epochs_params)
    return epochs
コード例 #13
0
def main():

    # Initialize to keep track of how many events each subject has
    n_kept_events = []

    # Set the MNE print out level
    set_log_level('ERROR')

    # Get list of available subjects
    subnums = [name for name in os.listdir(DATA_PATH) if name[0] is not '.']

    # Loop across all subjects
    for idx, subnum in enumerate(subnums):

        # Add status updates
        print('\nRunning Subject # ', subnum)

        ## DATA LOADING

        print('\tData Wrangling')

        # Get the files that match the desired task, load and concatenate data
        subj_path = os.path.join(DATA_PATH, subnum, 'EEG', 'raw', 'raw_format')
        subj_files = list(fms.loc[(fms["SUBNUM"] == subnum)
                                  & (fms["TASK"] == TASK)]["FILE"].values)

        # Stop if subject doesn't have enough data, defined in terms of number of blocks ('runs')
        if len(subj_files) < N_BLOCKS:
            print('Subject does not have enough data')
            continue

        raws = [
            mne.io.read_raw_egi(os.path.join(subj_path, raw_file),
                                preload=True) for raw_file in subj_files
        ]

        # Set montage, drop misc channels, and filter
        montage = mne.channels.read_montage('GSN-HydroCel-129',
                                            ch_names=raws[0].ch_names)
        for raw in raws:
            raw.set_montage(montage)
            raw.set_channel_types(EOG_MAPPINGS)
            raw.drop_channels(raw.ch_names[128:-1])
            raw.filter(l_freq=L_FREQ, h_freq=H_FREQ, fir_design='firwin')

        # Concatenate raw objects into single new raw object
        raw = mne.concatenate_raws(raws)

        ## EVENT MANAGEMENT

        print('\tEvent Management')

        try:
            events = mne.find_events(raw, verbose=False)
        except:
            print('Subject has weird shortest_event error...skipping')
            continue

        # Create correct-response events
        new_events = []

        for trgt, rspn, corr in zip(TRGT_EVCS, RSPN_EVCS, CORR_EVCS):

            tmp, _ = define_target_events(events,
                                          rspn,
                                          trgt,
                                          raw.info['sfreq'],
                                          tmin=-2.,
                                          tmax=0.,
                                          new_id=corr,
                                          fill_na=None)
            new_events.append(tmp)

        # Collapse new events into an array
        new_events = np.concatenate(
            (np.array(new_events[0]), np.array(new_events[1])), axis=0)
        new_event_ids = dict(left=21, right=22)

        # Check if subject has enough epochs to continue
        if not np.any(new_events):
            print('Subject has no correct trials...')
            continue
        elif new_events.shape[0] < N_EPOCHS:
            print('Subject has too few trials for analysis: %d' %
                  new_events.shape[0])
            continue

        # Create epochs object
        epochs = mne.Epochs(raw,
                            new_events,
                            new_event_ids,
                            tmin=TMIN,
                            tmax=TMAX,
                            picks=None,
                            baseline=BASELINE,
                            reject=None,
                            preload=True)

        ## BAD CHANNELS & RE-REFERENCING

        print('\tBad Channels & Re-Referencing')

        # Mark bad channels via scrappy kurtosis z-score method
        bad_channels = faster_bad_channels(epochs, thres=5)
        raw.info['bads'] = bad_channels
        epochs.info['bads'] = bad_channels

        # Re-reference to average reference
        raw.set_eeg_reference('average', projection=False)
        epochs.set_eeg_reference('average', projection=False)

        ## ICA

        if RUN_ICA:

            print('\tRunning ICA')

            # High-pass filter for the purpose of ICA de-noising
            raw_hpf = raw.copy()
            raw_hpf.filter(l_freq=1.,
                           h_freq=None,
                           fir_design='firwin',
                           verbose=False)
            epochs_hpf = mne.Epochs(raw_hpf,
                                    new_events,
                                    new_event_ids,
                                    tmin=TMIN,
                                    tmax=TMAX,
                                    picks=None,
                                    baseline=BASELINE,
                                    reject=None,
                                    preload=True)

            # Get the EEG picks (eeg electrode indices), ignoring bad channels as marked by Faster
            eeg_ica_picks = mne.pick_types(epochs_hpf.info,
                                           meg=False,
                                           eeg=True)
            ica = ICA(random_state=1)
            ica.fit(epochs_hpf, picks=eeg_ica_picks)

            # Define bad components by correlating with channels near eyes
            eog_chs = [ch for ch in EOG_CHS if ch not in raw.info['bads']]

            bad_ica_comps = []
            for ch in eog_chs:
                inds, scores = ica.find_bads_eog(raw_hpf,
                                                 ch_name=ch,
                                                 threshold=4,
                                                 l_freq=1,
                                                 h_freq=8)
                bad_ica_comps.extend(inds)

            ica.exclude = list(set(bad_ica_comps))

            # Plot and save bad components
            if len(bad_ica_comps) > 0:
                fig = ica.plot_components(picks=np.array(bad_ica_comps),
                                          show=False)
                fig_name = os.path.join(FIG_PATH,
                                        subnum + '_ica_scalp_maps.png')
                fig.savefig(fig_name, dpi=150)

            # Save out ICA decomposition
            ica_filename = os.path.join(ICA_PATH, subnum + '-ica.fif')
            ica.save(ica_filename)

            # Apply ICA to both epoched and raw data
            epochs = ica.apply(epochs)
            raw = ica.apply(raw)

            raw_filename = os.path.join(PROC_PATH, subnum + '-raw.fif')
            raw.save(raw_filename, overwrite=True)

        ## Autoreject

        if RUN_AUTOREJECT:

            print('\tRunning AutoReject')

            # Use AutoReject to reject bad epochs and interpolate bad channels
            ar = AutoReject(n_jobs=4, random_state=1, verbose=False, cv=3)
            epochs, rej_log = ar.fit_transform(epochs, return_log=True)
            epochs.info['bads'] = [
            ]  # no need for bad channels after AutoReject

            # Save out autoreject log, as a pickled object
            pickle.dump(rej_log,
                        open(os.path.join(ICA_PATH, subnum + "-ar.p"), "wb"))

        ## SAVE OUT DATA

        # Enforce consistencty in the number of events per condition
        epochs.equalize_event_counts(new_event_ids, method='mintime')

        # Don't save the subject's data if they don't have enough epochs
        if len(epochs) < N_EPOCHS:
            print(
                'Subject has too few trials for analysis after preprocessing')
            continue

        # Save out pre-processed data
        epochs_filename = os.path.join(PROC_PATH,
                                       subnum + '_preprocessed-epo.fif')
        epochs.save(epochs_filename)

        print('\tData Saved - {:2d} kept events'.format(len(epochs)))

        n_kept_events.append((subnum, len(epochs)))

        print('\nGreat Success.\n')

    # Save out the log file of number of good events per subject
    with open('event_log.csv', 'w') as csvfile:
        writer = csv.writer(csvfile)
        for ev_info in n_kept_events:
            writer.writerow(list(ev_info))