Exemplo n.º 1
0
def epoch_raw(sub, t_epoch, baseline, reject, flat, autoreject_interpolation, consensus_percs, n_interpolates,
              autoreject_threshold, overwrite_ar, decim, n_jobs):
    raw = sub.load_filtered()
    events = sub.load_events()

    picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False,
                           eog=False, ecg=False, exclude=sub.bad_channels)

    epochs = mne.Epochs(raw, events, sub.event_id, t_epoch[0], t_epoch[1], baseline,
                        preload=True, picks=picks, proj=False, reject=None,
                        decim=decim, on_missing='ignore', reject_by_annotation=True)

    if autoreject_interpolation:
        ar_object = ar.AutoReject(n_interpolates, consensus_percs, random_state=8,
                                  n_jobs=n_jobs)
        epochs, reject_log = ar_object.fit_transform(epochs, return_log=True)
        sub.save_reject_log(reject_log)

    elif autoreject_threshold:
        reject = ut.autoreject_handler(sub.name, epochs, sub.p["highpass"], sub.p["lowpass"],
                                       sub.pr.pscripts_path, overwrite_ar=overwrite_ar)
        print(f'Autoreject Rejection-Threshold: {reject}')
        epochs.drop_bad(reject=reject, flat=flat)
    else:
        print(f'Chosen Rejection-Threshold: {reject}')
        epochs.drop_bad(reject=reject, flat=flat)

    sub.save_epochs(epochs)
def autoreject_epochs(ft_file, out_file, log_file):
    import mne
    import autoreject
    import json

    # Load the file
    #info = mne.io.read_info(raw_file)
    epochs = mne.read_epochs_fieldtrip(ft_file, info=None)

    # Resample the data
    #epochs.resample(500,npad='auto')

    # Apply autoreject
    ar = autoreject.AutoReject()
    epochs, reject_log = ar.fit_transform(epochs, return_log=True)

    reject_log = reject_log.bad_epochs
    reject_log = reject_log.tolist()

    # Write to disk
    with open(log_file, 'w') as f:
        json.dump(reject_log, f)

    #Save data to file
    epochs.save(out_file)
    return
def autoreject_log(ft_file, out_file):
    import mne
    import autoreject
    import json

    # Load the file
    #info = mne.io.read_info(raw_file)
    epochs = mne.read_epochs_fieldtrip(ft_file, info=None)

    # Resample the data - we're only going to use the thresholds
    #epochs.resample(400,npad='auto')

    # Apply autoreject to find bad channels
    ar = autoreject.AutoReject()
    ar.fit(epochs)
    reject_log = ar.get_reject_log(epochs)

    reject_log = reject_log.bad_epochs
    reject_log = reject_log.tolist()

    # Write to disk
    with open(out_file, 'w') as f:
        json.dump(reject_log, f)

    return
Exemplo n.º 4
0
def final_reject_epoch(epochs):
    """Final and automatic rejection of  epochs
    Parameters
    ----------
    epochs: mne.Epochs object
            instance of the mne.Epochs,

    Returns
    ----------
    epochs_clean:   mne.Epochs object
                    instance of "cleaned" epochs using autoreject

    output_dict_finalRej:   dictionary
                            dictionary with epochs droped per channel and
                            channels interpolated
    """

    # creates the output dictionary to store the function output
    output_dict_finalRej = collections.defaultdict(dict)
    output_dict_finalRej['interpolatedChannels'] = []

    # fit and clean epoch data using autoreject
    autoRej = ar.AutoReject()
    try:
        autoRej.fit(epochs)
    except ValueError:
        fr_error = "The least populated class in y has only 1 member, which is too\
             few. The minimum number of groups for any class cannot be\
             less than 2."

        print(fr_error)

        ica_details = {"ERROR": fr_error}
        return epochs, {"Final Reject": ica_details}

    epochs_clean = autoRej.transform(epochs)

    # Create a rejection log
    reject_log = autoRej.get_reject_log(epochs)

    # get channel names
    ch_names = epochs.info.ch_names

    # Create dataframe with the labels of which channel was interpolated
    df = pd.DataFrame(data=reject_log.labels, columns=ch_names)
    for ch in ch_names:
        if df[df[ch] == 2][ch].count() > 0:
            output_dict_finalRej['interpolatedChannels'].append(ch)

    for ch in ch_names:
        # store amount of epochs dropped for each channel
        output_dict_finalRej['epochsDropped'][ch] = str(
            epochs_clean.drop_log.count((ch,)))

    return epochs_clean, output_dict_finalRej
Exemplo n.º 5
0
epochs.average().detrend().plot_joint()

# %%
# Autoreject without any other preprocessing
# ------------------------------------------
# Now, we'll naively apply autoreject as our first preprocessing step.
#
# As we can see in the plot of the rejected epochs, there are many eyeblinks
# that caused the epoch to be dropped. This resulted in a lot of the data
# being lost.
#
# The data looks fairly clean already and we don't want to interpolate
# more than a few sensors since we only have 32 to start, so the
# number of channels to interpolate was set to check some low numbers
ar = autoreject.AutoReject(n_interpolate=[1, 2, 3, 4],
                           random_state=11,
                           n_jobs=1,
                           verbose=True)
ar.fit(epochs[:20])  # fit on a few epochs to save time
epochs_ar, reject_log = ar.transform(epochs, return_log=True)

# %%
# visualize the dropped epochs
epochs[reject_log.bad_epochs].plot(scalings=dict(eeg=100e-6))

# %%
# and the reject log
reject_log.plot('horizontal')

# %%
# Autoreject with high-pass filter
# --------------------------------