def get_data_2a(data_name, n_classes, num_channels=22):
    # # # Reads in raw edf (or gdf) file from BCI Competition 2a and returns the signal, time array,
    # # # and start of each (labeled) motor imagery event
    # Returns: signal, time array, events

    freq = 250

    raw = read_raw_edf(data_name,
                       preload=True,
                       stim_channel='auto',
                       verbose='WARNING')

    events = find_edf_events(raw)
    events.pop(0)
    time = events.pop(0)
    events1 = events.pop(0)
    events2 = events.pop(0)
    events3 = events.pop(0)

    # raw_train.plot_psd(area_mode='range', tmax=10.0)
    raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')
    signal = np.transpose(raw.get_data()[:num_channels])

    events = np.transpose(np.vstack([time, events1, events2, events3]))
    time = raw.times * freq

    return np.asarray(signal), time, events
示例#2
0
文件: eeg.py 项目: ifranjic/EEG
    def manage_edf_events(raw):
        # find_edf_events returns the list of five elements:
        # n_events - the number of all elements (this element
        #            excluded in order to transform list to array)
        # pos      - beginning of the events in samples
        # typ      - the event identifiers
        # chn      - the associated channels (0 for all)
        # dur      - duration of the events

        event = find_edf_events(raw)
        event1 = [event[1][:]]
        event2 = [event[2][:]]
        event3 = [event[3][:]]
        events = np.vstack((event1, event3, event2))
        events = events.transpose()
        return events
def get_label_data_2a(data_name,
                      n_classes,
                      num_channels=22,
                      remove_rest=True,
                      training_data=True,
                      reuse_data=False,
                      mult_data=False,
                      noise_data=False):

    freq = 250

    raw = read_raw_edf(data_name,
                       preload=True,
                       stim_channel='auto',
                       verbose='WARNING')

    events = find_edf_events(raw)
    events.pop(0)
    time = events.pop(0)
    events1 = events.pop(0)
    events2 = events.pop(0)
    events3 = events.pop(0)

    # raw_train.plot_psd(area_mode='range', tmax=10.0)
    raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')
    signal = np.transpose(raw.get_data()[:num_channels])

    events = np.transpose(np.vstack([time, events1, events2, events3]))
    time = raw.times * freq

    if training_data:
        signal_out, labels = label_data_2a(signal,
                                           time,
                                           events,
                                           remove_rest,
                                           n_classes,
                                           freq,
                                           reuse_data=reuse_data,
                                           mult_data=mult_data,
                                           noise_data=noise_data)
        # if remove_rest:
        signal = signal_out
    else:
        labels = np.zeros(signal.shape[0])

    return np.asarray(signal), labels
def load_data(file_path):
    event_id = dict(left_hand=769, right_hand=770, both_feet=771, tongue=772)
    
    tmin, tmax = -0.5, 4
    
    raw = read_raw_edf(file_path, eog= [22, 23, 24], stim_channel="auto", preload=True)
    raw.filter(0.5, 38.)
    events = find_edf_events(raw)
    events = np.stack((events[1], events[3], events[2]), 1).astype(np.int64)
    picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
                       exclude='bads')
    epochs_train = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                    baseline=None, preload=True)
    labels = epochs_train.events[:, -1] - 769
    
    indices = np.arange(labels.shape[0])
    np.random.shuffle(indices)
    
    return epochs_train.get_data()[:, :, :, np.newaxis][indices], labels[indices]
示例#5
0
def get_data_2a(data_name, n_classes, num_channels=22):

    freq = 250

    raw = read_raw_edf(data_name,
                       preload=True,
                       stim_channel='auto',
                       verbose='WARNING')

    events = find_edf_events(raw)
    events.pop(0)
    time = events.pop(0)
    events1 = events.pop(0)
    events2 = events.pop(0)
    events3 = events.pop(0)

    # raw_train.plot_psd(area_mode='range', tmax=10.0)
    raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')
    signal = np.transpose(raw.get_data()[:num_channels])

    events = np.transpose(np.vstack([time, events1, events2, events3]))
    time = raw.times * freq

    return np.asarray(signal), time, events
示例#6
0
def get_data_2a(data_name, n_classes, num_channels=22):
    # # # Collects data from edf file and returns it as an np array along with the events
    # # # This has been specifically designed to handle data from the BCI Competition IV 2a dataset
    # Returns: signal as np array, time array, and the events/prompts
    
    freq = 250

    raw = read_raw_edf(data_name, preload=True, stim_channel='auto', verbose='WARNING')

    events = find_edf_events(raw)
    events.pop(0)
    time = events.pop(0)
    events1 = events.pop(0)
    events2 = events.pop(0)
    events3 = events.pop(0)

    # raw_train.plot_psd(area_mode='range', tmax=10.0)
    raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')
    signal = np.transpose(raw.get_data()[:num_channels])

    events = np.transpose(np.vstack([time, events1, events2, events3]))
    time = raw.times * freq

    return np.asarray(signal), time, events
示例#7
0
    def prepare_eeg_csv():

        root = './data'
        classes = [
            'img_lhand', 'img_rhand', 'img_bfeet', 'img_tongue', 'img_none'
        ]

        class_mapping = {
            'img_lhand': 769,
            'img_rhand': 770,
            'img_bfeet': 771,
            'img_tongue': 772,
        }

        # load recording gdf and extract its headers
        bci_record_names = []
        for file_name in os.listdir(root):
            bci_record_names.append(file_name)

        # print('Filename: {}'.format(bci_record_names[0]))
        # stim channel is required for gdf files
        gdf_file = io.read_raw_edf(root + '/' + bci_record_names[0],
                                   stim_channel=1,
                                   preload=True)
        recording_headers = io.find_edf_events(gdf_file)
        recording_positions = recording_headers[1]
        recording_types = recording_headers[2]
        recording_durations = recording_headers[4]

        # convert to dataframe (recording time series)
        recording_ts = gdf_file.to_data_frame()

        # drop EOG cols
        eog_cols = ['EOG-left', 'EOG-central', 'EOG-right']
        recording_ts.drop(eog_cols, axis=1, inplace=True)

        # fill nans with 0
        recording_ts.fillna(0.0, inplace=True)

        # normalize
        recording_ts = (recording_ts - recording_ts.min()) / recording_ts.std()

        # extract class ranges
        class_ranges = EEGPrep.get_class_ranges(class_mapping, recording_types,
                                                recording_positions,
                                                recording_durations)

        # add a new column for labels
        recording_ts_labeled = EEGPrep.label_recording(recording_ts,
                                                       class_ranges)

        # fill all nans with 'none'
        recording_ts_labeled['class_label'].fillna('img_none', inplace=True)
        recording_ts_labeled['class_label'].replace(0.0,
                                                    'img_none',
                                                    inplace=True)

        # convert to one-hot
        recording_ts_oh = EEGDataUtils.one_hot(recording_ts_labeled,
                                               ['class_label'])

        # shuffle data
        recording_ts_oh = recording_ts_oh.sample(frac=1)

        return recording_ts_oh