def downsample(self): raw = self.raw sfreq = self.downsample_sfreq print """ from doc: WARNING: The intended purpose of this function is primarily to speed up computations (e.g., projection calculation) when precise timing of events is not required, as downsampling raw data effectively jitters trigger timings. It is generally recommended not to epoch downsampled data, but instead epoch and then downsample, as epoching downsampled data jitters triggers. NOTE: event onset collisions will be reported as warnings in that case, it might be a good idea to pick either the trial onset or audio onset events and delete the other ones before downsampling """ print 'down-sampling raw and events stim channel ...' fast_resample_mne(raw, sfreq, res_type='sinc_best', preserve_events=True, verbose=True) # fast_resample_mne(raw, sfreq, res_type='sinc_fastest', preserve_events=True, verbose=False) # resample epochs print 'down-sampling epochs ...' self.eog_epochs.resample(sfreq) self._downsample_epochs() print 'TODO: down-sampling events (not in stim channel) ...' # TODO: resample events self.downsampled = True
def load_and_preprocess_raw(subject, onsets='audio', interpolate_bad_channels=True, reference_mastoids=True, l_freq=0.5, h_freq=30, sfreq=None, ica_cleaning=True, ica_name='100p_64c', l_freq2=None, h_freq2=None, verbose=None, n_jobs=4 ): # load the imported fif data, use the specified onsets raw = load_raw(subject, onsets=onsets, interpolate_bad_channels=interpolate_bad_channels, reference_mastoids=reference_mastoids, verbose=verbose, ) # apply bandpass filter, use 4 processes to speed things up log.info('Applying filter: low_cut_freq={} high_cut_freq={}'.format(l_freq, h_freq)) eeg_picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=False, stim=False) raw.filter(l_freq=l_freq, h_freq=h_freq, picks=eeg_picks, filter_length='10s', l_trans_bandwidth=0.1, h_trans_bandwidth=0.5, method='fft', n_jobs=n_jobs, verbose=verbose) # extract events # this comprises 240 trials, 60 noise events (1111) and 60 feedback events (2000=No, 2001=Yes) trial_events = mne.find_events(raw, stim_channel='STI 014', shortest_event=0, verbose=verbose) if verbose: log.debug('trial events: {}'.format(trial_events.shape)) # resample data and eventa if sfreq is not None: orig_sfreq = raw.info['sfreq'] fast_resample_mne(raw, sfreq, res_type='sinc_fastest', preserve_events=True, verbose=False) # IMPORTANT: extracted events have to be resampled, too - otherwise misalignment trial_events = resample_mne_events(trial_events, orig_sfreq, sfreq) if ica_cleaning: # load ica ica = load_ica(subject, description=ica_name) if verbose: log.info('Applying ICA: {}'.format(ica)) log.info('Excluding ICA components: {}'.format(ica.exclude)) raw = ica.apply(raw, exclude=ica.exclude) if l_freq2 is not None or h_freq2 is not None: log.info('Applying additional filter: low_cut_freq={} high_cut_freq={}'.format(l_freq2, h_freq2)) raw.filter(l_freq=l_freq2, h_freq=h_freq2, picks=eeg_picks, filter_length='10s', l_trans_bandwidth=0.1, h_trans_bandwidth=0.5, method='fft', n_jobs=n_jobs, verbose=verbose) return raw, trial_events
def load_and_preprocess_raw(subject, onsets='audio', interpolate_bad_channels=True, reference_mastoids=True, l_freq=0.5, h_freq=30, sfreq=None, ica_cleaning=True, l_freq2=None, h_freq2=None, verbose=None, n_jobs=4 ): # load the imported fif data, use the specified onsets raw = load_raw(subject, onsets=onsets, interpolate_bad_channels=interpolate_bad_channels, reference_mastoids=reference_mastoids, verbose=verbose, ) # apply bandpass filter, use 4 processes to speed things up log.info('Applying filter: low_cut_freq={} high_cut_freq={}'.format(l_freq, h_freq)) eeg_picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=False, stim=False) raw.filter(l_freq=l_freq, h_freq=h_freq, picks=eeg_picks, filter_length='10s', l_trans_bandwidth=0.1, h_trans_bandwidth=0.5, method='fft', n_jobs=n_jobs, verbose=verbose) # extract events # this comprises 240 trials, 60 noise events (1111) and 60 feedback events (2000=No, 2001=Yes) trial_events = mne.find_events(raw, stim_channel='STI 014', shortest_event=0, verbose=verbose) if verbose: log.debug('trial events: {}'.format(trial_events.shape)) # resample data and eventa if sfreq is not None: orig_sfreq = raw.info['sfreq'] fast_resample_mne(raw, sfreq, res_type='sinc_fastest', preserve_events=True, verbose=False) # IMPORTANT: extracted events have to be resampled, too - otherwise misalignment trial_events = resample_mne_events(trial_events, orig_sfreq, sfreq) if ica_cleaning: # load ica ica = load_ica(subject, description='100p_64c') if verbose: log.info('Applying ICA: {}'.format(ica)) log.info('Excluding ICA components: {}'.format(ica.exclude)) raw = ica.apply(raw, exclude=ica.exclude, copy=False) if l_freq2 is not None or h_freq2 is not None: log.info('Applying additional filter: low_cut_freq={} high_cut_freq={}'.format(l_freq2, h_freq2)) raw.filter(l_freq=l_freq2, h_freq=h_freq2, picks=eeg_picks, filter_length='10s', l_trans_bandwidth=0.1, h_trans_bandwidth=0.5, method='fft', n_jobs=n_jobs, verbose=verbose) return raw, trial_events
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))
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))