def _load_raw(subject, mne_data_root=None, verbose=False, onsets=None, interpolate_bad_channels=False, has_mastoid_channels=None, # None=True, False, or callable(subject) returning True/False reference_mastoids=True): if mne_data_root is None: # use default data root import deepthought data_root = os.path.join(deepthought.DATA_PATH, 'OpenMIIR') mne_data_root = os.path.join(data_root, 'eeg', 'mne') mne_data_filepath = os.path.join(mne_data_root, '{}-raw.fif'.format(subject)) log.info('Loading raw data for subject "{}" from {}'.format(subject, mne_data_filepath)) raw = mne.io.Raw(mne_data_filepath, preload=True, verbose=verbose) ## referencing to mastoids if has_mastoid_channels is None \ or has_mastoid_channels is True \ or has_mastoid_channels(subject) is True: if reference_mastoids: log.info('Referencing to mastoid channels: {}'.format(MASTOID_CHANNELS)) mne.io.set_eeg_reference(raw, MASTOID_CHANNELS, copy=False) # inplace else: log.info('This recording has unused mastoid channels: {} ' 'To use them, re-run with reference_mastoids=True.'.format(MASTOID_CHANNELS)) raw.drop_channels(MASTOID_CHANNELS) ## optional event merging if onsets == 'audio': merge_trial_and_audio_onsets(raw, use_audio_onsets=True, inplace=True, stim_channel='STI 014', verbose=verbose) elif onsets == 'trials': merge_trial_and_audio_onsets(raw, use_audio_onsets=True, inplace=True, stim_channel='STI 014', verbose=verbose) # else: keep both bads = raw.info['bads'] if bads is not None and len(bads) > 0: if interpolate_bad_channels: log.info('Interpolating bad channels: {}'.format(bads)) raw.interpolate_bads() else: log.info('This file contains some EEG channels marked as bad: {}\n' 'To interpolate bad channels run load_raw() with interpolate_bad_channels=True.' ''.format(bads)) return raw
def merge_trial_and_audio_onsets(self, use_audio_onsets=True): raw = self.raw # save original events self.orig_trial_events = self.trial_events # merge merge_trial_and_audio_onsets(raw, use_audio_onsets=use_audio_onsets, inplace=True) # recompute trial_events and times trial_events = mne.find_events(raw, stim_channel='STI 014', shortest_event=0) trial_event_times = raw.index_as_time(trial_events[:,0]) self.trial_events = trial_events self.trial_event_times = trial_event_times
def merge_trial_and_audio_onsets(self, use_audio_onsets=True): raw = self.raw # save original events self.orig_trial_events = self.trial_events # merge merge_trial_and_audio_onsets(raw, use_audio_onsets=use_audio_onsets, inplace=True) # recompute trial_events and times trial_events = mne.find_events(raw, stim_channel='STI 014', shortest_event=0) trial_event_times = raw.times[trial_events[:,0]] self.trial_events = trial_events self.trial_event_times = trial_event_times
def check_trial_audio_onset_merge(self, use_audio_onsets=True, verbose=None): # assert self.filtered is False assert self.downsampled is False raw = self.raw ## check whether trial and audio events are merged correctly merged_events = merge_trial_and_audio_onsets(raw, use_audio_onsets=use_audio_onsets, inplace=False) if verbose: for event in merged_events: print event plt.figure(figsize=(17,10)) axes = plt.gca() mne.viz.plot_events(merged_events, raw.info['sfreq'], raw.first_samp, axes=axes)
def _load_raw( subject, mne_data_root=None, verbose=False, onsets=None, interpolate_bad_channels=False, has_mastoid_channels=None, # None=True, False, or callable(subject) returning True/False apply_reference=True, # by default, reference the data reference_mastoids=True): if mne_data_root is None: # use default data root import deepthought data_root = os.path.join(deepthought.DATA_PATH, 'OpenMIIR') mne_data_root = os.path.join(data_root, 'eeg', 'mne') mne_data_filepath = os.path.join(mne_data_root, '{}-raw.fif'.format(subject)) log.info('Loading raw data for subject "{}" from {}'.format( subject, mne_data_filepath)) raw = mne.io.Raw(mne_data_filepath, preload=True, verbose=verbose) if apply_reference: if has_mastoid_channels is None \ or has_mastoid_channels is True \ or has_mastoid_channels(subject) is True: ## referencing to mastoids if reference_mastoids: log.info('Referencing to mastoid channels: {}'.format( MASTOID_CHANNELS)) mne.io.set_eeg_reference(raw, MASTOID_CHANNELS, copy=False) # inplace else: log.info( 'This recording has unused mastoid channels: {} ' 'To use them, re-run with reference_mastoids=True.'.format( MASTOID_CHANNELS)) raw.drop_channels(MASTOID_CHANNELS) else: ## referencing to average log.info('Referencing to average.') mne.io.set_eeg_reference(raw, copy=False) ## optional event merging if onsets == 'audio': merge_trial_and_audio_onsets(raw, use_audio_onsets=True, inplace=True, stim_channel='STI 014', verbose=verbose) elif onsets == 'trials': merge_trial_and_audio_onsets(raw, use_audio_onsets=True, inplace=True, stim_channel='STI 014', verbose=verbose) # else: keep both bads = raw.info['bads'] if bads is not None and len(bads) > 0: if interpolate_bad_channels: log.info('Interpolating bad channels: {}'.format(bads)) raw.interpolate_bads() else: log.info( 'This file contains some EEG channels marked as bad: {}\n' 'To interpolate bad channels run load_raw() with interpolate_bad_channels=True.' ''.format(bads)) return raw