Example #1
0
def save_beat_times(beats, stimulus_id, cue=False, data_root=None, offset=None, overwrite=False, version=None):

    if version is None:
        version = DEFAULT_VERSION

    if data_root is None:
        data_root = os.path.join(deepthought.DATA_PATH, 'OpenMIIR')

    if cue:
        assert offset is None   # no offset in cue files
        beats_filepath = os.path.join(data_root, 'meta',
                                      'beats.v{}'.format(version),
                                      '{}_cue_beats.txt'.format(stimulus_id))
    else:
        beats_filepath = os.path.join(data_root, 'meta',
                                      'beats.v{}'.format(version),
                                      '{}_beats.txt'.format(stimulus_id))

    if os.path.exists(beats_filepath) and not overwrite:
        log.info('Skipping existing {}'.format(beats_filepath))
    else:
        ensure_parent_dir_exists(beats_filepath)
        with open(beats_filepath, 'w') as f:
            if offset is not None:
                f.write('# offset of cue: {}\n'.format(offset))
            for beat in beats:
                f.write('{}\n'.format(beat))
        log.info('Saved beat times in {}'.format(beats_filepath))
Example #2
0
def clean_data(mne_data_root, subject, verbose=True, overwrite=False):

    ## check whether output already exists
    output_filepath = os.path.join(mne_data_root,
                                   '{}_filtered-raw.fif'.format(subject))

    if os.path.exists(output_filepath):
        if not overwrite:
            log.info('Skipping existing {}'.format(output_filepath))
            return

    input_filepath = os.path.join(mne_data_root, '{}-raw.fif'.format(subject))

    raw = mne.io.Raw(input_filepath, preload=True, verbose=verbose)

    ## apply bandpass filter
    raw.filter(0.5,
               30,
               filter_length='10s',
               l_trans_bandwidth=0.1,
               h_trans_bandwidth=0.5,
               method='fft',
               iir_params=None,
               picks=None,
               n_jobs=1,
               verbose=verbose)

    ensure_parent_dir_exists(output_filepath)
    raw.save(output_filepath, overwrite=overwrite, verbose=False)
Example #3
0
def save_beat_times(beats, stimulus_id, cue=False, data_root=None, offset=None, overwrite=False, version=None):

    if version is None:
        version = DEFAULT_VERSION

    if data_root is None:
        data_root = os.path.join(deepthought.DATA_PATH, 'OpenMIIR')

    if cue:
        assert offset is None   # no offset in cue files
        beats_filepath = os.path.join(data_root, 'meta',
                                      'beats.v{}'.format(version),
                                      '{}_cue_beats.txt'.format(stimulus_id))
    else:
        beats_filepath = os.path.join(data_root, 'meta',
                                      'beats.v{}'.format(version),
                                      '{}_beats.txt'.format(stimulus_id))

    if os.path.exists(beats_filepath) and not overwrite:
        log.info('Skipping existing {}'.format(beats_filepath))
    else:
        ensure_parent_dir_exists(beats_filepath)
        with open(beats_filepath, 'w') as f:
            if offset is not None:
                f.write('# offset of cue: {}\n'.format(offset))
            for beat in beats:
                f.write('{}\n'.format(beat))
        log.info('Saved beat times in {}'.format(beats_filepath))
Example #4
0
def extract_events_from_file(data_root, subject, verbose=True):
    bdf_filepath = os.path.join(data_root, 'eeg', 'biosemi', '{}.bdf'.format(subject))
    markers_filepath = os.path.join(data_root, 'eeg', 'biosemi', '{}_EEG_Data.mat'.format(subject))

    raw = read_raw_edf(bdf_filepath, preload=True, verbose=verbose)

    log.debug(raw)
    log.debug(raw.info)

    events = extract_events_from_raw(raw, markers_filepath, subject, verbose)

    # Writing events
    output_filepath = os.path.join(data_root, 'eeg', 'imported', subject, '{}-eve.fif.gz'.format(subject))
    ensure_parent_dir_exists(output_filepath)
    mne.write_events(output_filepath, events)
def generate_plots(y_real, y_pred, output, dataset_name, output_path):
    font = {
        #         'family' : 'normal',
        'weight': 'normal',
        'size': 24
    }

    mpl.rc('font', **font)

    # Compute confusion matrix
    cm = confusion_matrix(y_real, y_pred)

    shuffle_classes = LabelConverter().shuffle_classes
    cmt = np.zeros([24, 24])
    for i in xrange(24):
        for j in xrange(24):
            cmt[shuffle_classes[i], shuffle_classes[j]] = cm[i, j]
    labels = swapped_meta_labels

    print cmt
    print classification_report(y_real, y_pred)

    misclass = (y_real != y_pred).mean()
    print 'misclassification rate: {:.4f}'.format(misclass)
    print 'accuracy: {:.4f}'.format(100. * (1 - misclass))

    fig = plt.figure(1, figsize=(10, 10), dpi=600)
    axes = fig.add_subplot(111)

    # Show confusion matrix in a separate
    axes.matshow(cmt)
    #     plt.title('Confusion matrix')
    #     axes.colorbar()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

    axes.set_xticks(xrange(24))
    axes.set_xticklabels(labels, rotation=90, fontsize=15)
    axes.set_yticks(xrange(24))
    axes.set_yticklabels(labels, fontsize=15)

    plot_file = os.path.join(experiment_root, 'confusion',
                             '{}_confusion.pdf'.format(dataset_name))
    ensure_parent_dir_exists(plot_file)
    plt.savefig(plot_file, bbox_inches='tight')
Example #6
0
def extract_events_from_file(data_root, subject, verbose=True):
    bdf_filepath = os.path.join(data_root, 'eeg', 'biosemi',
                                '{}.bdf'.format(subject))
    markers_filepath = os.path.join(data_root, 'eeg', 'biosemi',
                                    '{}_EEG_Data.mat'.format(subject))

    raw = read_raw_edf(bdf_filepath, preload=True, verbose=verbose)

    log.debug(raw)
    log.debug(raw.info)

    events = extract_events_from_raw(raw, markers_filepath, subject, verbose)

    # Writing events
    output_filepath = os.path.join(data_root, 'eeg', 'imported', subject,
                                   '{}-eve.fif.gz'.format(subject))
    ensure_parent_dir_exists(output_filepath)
    mne.write_events(output_filepath, events)
Example #7
0
def generate_plots(y_real, y_pred, output, dataset_name, output_path):
    font = {
#         'family' : 'normal',
        'weight' : 'normal',
        'size'   : 24}

    mpl.rc('font', **font)

    # Compute confusion matrix
    cm = confusion_matrix(y_real, y_pred);
    
    shuffle_classes = LabelConverter().shuffle_classes;
    cmt = np.zeros([24,24]);
    for i in xrange(24):
        for j in xrange(24):
            cmt[shuffle_classes[i],shuffle_classes[j]] = cm[i,j];
    labels = swapped_meta_labels;

    print cmt  
    print classification_report(y_real, y_pred)
    
    misclass = (y_real != y_pred).mean();
    print 'misclassification rate: {:.4f}'.format(misclass);
    print 'accuracy: {:.4f}'.format(100. * (1 - misclass));

    fig = plt.figure(1, figsize=(10, 10), dpi=600)
    axes = fig.add_subplot(111)
    
    # Show confusion matrix in a separate 
    axes.matshow(cmt)
#     plt.title('Confusion matrix')
#     axes.colorbar()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    
    axes.set_xticks(xrange(24));
    axes.set_xticklabels(labels, rotation=90, fontsize=15)
    axes.set_yticks(xrange(24));
    axes.set_yticklabels(labels, fontsize=15)
    
    plot_file = os.path.join(experiment_root, 'confusion', '{}_confusion.pdf'.format(dataset_name));
    ensure_parent_dir_exists(plot_file);
    plt.savefig(plot_file, bbox_inches='tight')
Example #8
0
def clean_data(mne_data_root, subject, verbose=True, overwrite=False):

    ## check whether output already exists
    output_filepath = os.path.join(mne_data_root,
                                   '{}_filtered-raw.fif'.format(subject))

    if os.path.exists(output_filepath):
        if not overwrite:
            log.info('Skipping existing {}'.format(output_filepath))
            return

    input_filepath = os.path.join(mne_data_root,
                                   '{}-raw.fif'.format(subject))

    raw = mne.io.Raw(input_filepath, preload=True, verbose=verbose)

    ## apply bandpass filter
    raw.filter(0.5, 30, filter_length='10s',
                l_trans_bandwidth=0.1, h_trans_bandwidth=0.5,
                method='fft', iir_params=None,
                picks=None, n_jobs=1, verbose=verbose)

    ensure_parent_dir_exists(output_filepath)
    raw.save(output_filepath, overwrite=overwrite, verbose=False)
Example #9
0
def import_and_process_metadata(biosemi_data_root,
                                mne_data_root,
                                subject,
                                verbose=True,
                                overwrite=False):

    ## check whether output already exists
    output_filepath = os.path.join(mne_data_root, '{}-raw.fif'.format(subject))

    if os.path.exists(output_filepath):
        if not overwrite:
            log.info('Skipping existing {}'.format(output_filepath))
            return

    ## import raw BDF file from biosemi
    bdf_filepath = os.path.join(biosemi_data_root, '{}.bdf'.format(subject))

    ## NOTE: marks EXT1-4 channels as EOG channels during import
    log.info('Importing raw BDF data from: {}'.format(bdf_filepath))
    raw = read_raw_edf(bdf_filepath,
                       eog=RAW_EOG_CHANNELS,
                       preload=True,
                       verbose=verbose)
    log.info('Imported raw data: {}'.format(raw))

    sfreq = raw.info['sfreq']
    if sfreq != 512:
        log.warn('Unexpected sample rate: {} Hz'.format(sfreq))
        log.warn('Re-sampling to 512 Hz')
        fast_resample_mne(raw,
                          512,
                          res_type='sinc_best',
                          preserve_events=True,
                          verbose=True)

    ## mark all unused channels as bad
    raw.info['bads'] += [
        u'C1', u'C2', u'C3', u'C4', u'C5', u'C6', u'C7', u'C8', u'C9', u'C10',
        u'C11', u'C12', u'C13', u'C14', u'C15', u'C16', u'C17', u'C18', u'C19',
        u'C20', u'C21', u'C22', u'C23', u'C24', u'C25', u'C26', u'C27', u'C28',
        u'C29', u'C30', u'C31', u'C32', u'D1', u'D2', u'D3', u'D4', u'D5',
        u'D6', u'D7', u'D8', u'D9', u'D10', u'D11', u'D12', u'D13', u'D14',
        u'D15', u'D16', u'D17', u'D18', u'D19', u'D20', u'D21', u'D22', u'D23',
        u'D24', u'D25', u'D26', u'D27', u'D28', u'D29', u'D30', u'D31', u'D32',
        u'E1', u'E2', u'E3', u'E4', u'E5', u'E6', u'E7', u'E8', u'E9', u'E10',
        u'E11', u'E12', u'E13', u'E14', u'E15', u'E16', u'E17', u'E18', u'E19',
        u'E20', u'E21', u'E22', u'E23', u'E24', u'E25', u'E26', u'E27', u'E28',
        u'E29', u'E30', u'E31', u'E32', u'F1', u'F2', u'F3', u'F4', u'F5',
        u'F6', u'F7', u'F8', u'F9', u'F10', u'F11', u'F12', u'F13', u'F14',
        u'F15', u'F16', u'F17', u'F18', u'F19', u'F20', u'F21', u'F22', u'F23',
        u'F24', u'F25', u'F26', u'F27', u'F28', u'F29', u'F30', u'F31', u'F32',
        u'G1', u'G2', u'G3', u'G4', u'G5', u'G6', u'G7', u'G8', u'G9', u'G10',
        u'G11', u'G12', u'G13', u'G14', u'G15', u'G16', u'G17', u'G18', u'G19',
        u'G20', u'G21', u'G22', u'G23', u'G24', u'G25', u'G26', u'G27', u'G28',
        u'G29', u'G30', u'G31', u'G32', u'H1', u'H2', u'H3', u'H4', u'H5',
        u'H6', u'H7', u'H8', u'H9', u'H10', u'H11', u'H12', u'H13', u'H14',
        u'H15', u'H16', u'H17', u'H18', u'H19', u'H20', u'H21', u'H22', u'H23',
        u'H24', u'H25', u'H26', u'H27', u'H28', u'H29', u'H30', u'H31', u'H32',
        u'EXG7', u'EXG8', u'GSR1', u'GSR2', u'Erg1', u'Erg2', u'Resp', u'Plet',
        u'Temp'
    ]
    log.info('Marked unused channels as bad: {}'.format(raw.info['bads']))

    if not recording_has_mastoid_channels(subject):
        raw.info['bads'] += [u'EXG5', u'EXG6']

    picks = mne.pick_types(raw.info,
                           meg=False,
                           eeg=True,
                           eog=True,
                           stim=True,
                           exclude='bads')

    ## process events
    markers_filepath = os.path.join(biosemi_data_root,
                                    '{}_EEG_Data.mat'.format(subject))
    log.info('Processing events, external source: {}'.format(markers_filepath))
    events = extract_events_from_raw(raw, markers_filepath, subject, verbose)
    raw._data[-1, :].fill(0)  # delete data in stim channel
    raw.add_events(events)

    # crop to first event - 1s ... last event + 20s (longer than longest trial)
    onesec = raw.info['sfreq']
    tmin, tmax = raw.times[[
        events[0, 0] - onesec, events[-1, 0] + 20 * onesec
    ]]
    log.info('Cropping raw inplace to {:.3f}s - {:.3f}s'.format(tmin, tmax))
    raw.crop(tmin=tmin, tmax=tmax, copy=False)
    # fix sample offser -> 0
    raw.last_samp -= raw.first_samp
    raw.first_samp = 0

    ensure_parent_dir_exists(output_filepath)
    log.info('Saving raw fif data to: {}'.format(output_filepath))
    raw.save(output_filepath, picks=picks, overwrite=overwrite, verbose=False)

    del raw

    raw = fix_channel_infos(output_filepath, verbose=verbose)

    log.info('Imported {}'.format(raw))
    log.info('Metadata: {}'.format(raw.info))
Example #10
0
 def save_ica(self, description):
     ica_data_root = self.get_ica_data_root()
     ica_filepath = os.path.join(
         ica_data_root, '{}-{}-ica.fif'.format(self.subject, description))
     ensure_parent_dir_exists(ica_filepath)
     self.ica.save(ica_filepath)
Example #11
0
def import_and_process_metadata(biosemi_data_root, mne_data_root, subject, verbose=True, overwrite=False):

    ## check whether output already exists
    output_filepath = os.path.join(mne_data_root,
                                   '{}-raw.fif'.format(subject))

    if os.path.exists(output_filepath):
        if not overwrite:
            log.info('Skipping existing {}'.format(output_filepath))
            return

    ## import raw BDF file from biosemi
    bdf_filepath = os.path.join(biosemi_data_root, '{}.bdf'.format(subject))

    ## NOTE: marks EXT1-4 channels as EOG channels during import
    log.info('Importing raw BDF data from: {}'.format(bdf_filepath))
    raw = read_raw_edf(bdf_filepath, eog=RAW_EOG_CHANNELS, preload=True, verbose=verbose)
    log.info('Imported raw data: {}'.format(raw))

    sfreq = raw.info['sfreq']
    if sfreq != 512:
        log.warn('Unexpected sample rate: {} Hz'.format(sfreq))
        log.warn('Re-sampling to 512 Hz')
        fast_resample_mne(raw, 512, res_type='sinc_best', preserve_events=True, verbose=True)

    ## mark all unused channels as bad
    raw.info['bads'] += [u'C1', u'C2', u'C3', u'C4', u'C5', u'C6', u'C7', u'C8', u'C9', u'C10',
                u'C11', u'C12', u'C13', u'C14', u'C15', u'C16', u'C17', u'C18', u'C19', u'C20',
                u'C21', u'C22', u'C23', u'C24', u'C25', u'C26', u'C27', u'C28', u'C29', u'C30',
                u'C31', u'C32', u'D1', u'D2', u'D3', u'D4', u'D5', u'D6', u'D7', u'D8',
                u'D9', u'D10', u'D11', u'D12', u'D13', u'D14', u'D15', u'D16', u'D17', u'D18',
                u'D19', u'D20', u'D21', u'D22', u'D23', u'D24', u'D25', u'D26', u'D27', u'D28',
                u'D29', u'D30', u'D31', u'D32', u'E1', u'E2', u'E3', u'E4', u'E5', u'E6',
                u'E7', u'E8', u'E9', u'E10', u'E11', u'E12', u'E13', u'E14', u'E15',
                u'E16', u'E17', u'E18', u'E19', u'E20', u'E21', u'E22', u'E23', u'E24',
                u'E25', u'E26', u'E27', u'E28', u'E29', u'E30', u'E31', u'E32', u'F1',
                u'F2', u'F3', u'F4', u'F5', u'F6', u'F7', u'F8', u'F9', u'F10', u'F11',
                u'F12', u'F13', u'F14', u'F15', u'F16', u'F17', u'F18', u'F19', u'F20',
                u'F21', u'F22', u'F23', u'F24', u'F25', u'F26', u'F27', u'F28', u'F29',
                u'F30', u'F31', u'F32', u'G1', u'G2', u'G3', u'G4', u'G5', u'G6', u'G7',
                u'G8', u'G9', u'G10', u'G11', u'G12', u'G13', u'G14', u'G15', u'G16', u'G17',
                u'G18', u'G19', u'G20', u'G21', u'G22', u'G23', u'G24', u'G25', u'G26', u'G27',
                u'G28', u'G29', u'G30', u'G31', u'G32', u'H1', u'H2', u'H3', u'H4', u'H5',
                u'H6', u'H7', u'H8', u'H9', u'H10', u'H11', u'H12', u'H13', u'H14', u'H15',
                u'H16', u'H17', u'H18', u'H19', u'H20', u'H21', u'H22', u'H23', u'H24', u'H25',
                u'H26', u'H27', u'H28', u'H29', u'H30', u'H31', u'H32',
                u'EXG7', u'EXG8',
                u'GSR1', u'GSR2', u'Erg1', u'Erg2', u'Resp', u'Plet', u'Temp']
    log.info('Marked unused channels as bad: {}'.format(raw.info['bads']))

    if not recording_has_mastoid_channels(subject):
        raw.info['bads'] += [u'EXG5', u'EXG6']

    picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=True, stim=True, exclude='bads')

    ## process events
    markers_filepath = os.path.join(biosemi_data_root, '{}_EEG_Data.mat'.format(subject))
    log.info('Processing events, external source: {}'.format(markers_filepath))
    events = extract_events_from_raw(raw, markers_filepath, subject, verbose)
    raw._data[-1,:].fill(0)     # delete data in stim channel
    raw.add_events(events)

    # crop to first event - 1s ... last event + 20s (longer than longest trial)
    onesec = raw.info['sfreq']
    tmin, tmax = raw.index_as_time([events[0,0]-onesec, events[-1,0]+20*onesec])
    log.info('Cropping raw inplace to {:.3f}s - {:.3f}s'.format(tmin, tmax))
    raw.crop(tmin=tmin, tmax=tmax, copy=False)
    # fix sample offser -> 0
    raw.last_samp -= raw.first_samp
    raw.first_samp = 0

    ensure_parent_dir_exists(output_filepath)
    log.info('Saving raw fif data to: {}'.format(output_filepath))
    raw.save(output_filepath, picks=picks, overwrite=overwrite, verbose=False)

    del raw

    raw = fix_channel_infos(output_filepath, verbose=verbose)

    log.info('Imported {}'.format(raw))
    log.info('Metadata: {}'.format(raw.info))
Example #12
0
 def save_ica(self, description):
     ica_data_root = self.get_ica_data_root()
     ica_filepath = os.path.join(ica_data_root,
                                 '{}-{}-ica.fif'.format(self.subject, description))
     ensure_parent_dir_exists(ica_filepath)
     self.ica.save(ica_filepath)