def test_set_eeg_reference(): """Test rereference eeg data.""" raw = read_raw_fif(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(ref_data is None) # Test setting an average reference when one was already present with warnings.catch_warnings(record=True): # weight tables reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref)
def set_reference(self): """Set reference. """ dialog = ReferenceDialog(self) if dialog.exec_(): if dialog.average.isChecked(): tmp, _ = mne.set_eeg_reference(self.all.current.raw, None) tmp.apply_proj() name = self.all.current.name + " (average ref)" new = DataSet(raw=tmp, name=name, reference="average", events=self.all.current.events) else: ref = [c.strip() for c in dialog.channellist.text().split(",")] refstr = ",".join(ref) if set(ref) - set(self.all.current.raw.info["ch_names"]): # add new reference channel(s) to data try: tmp = mne.add_reference_channels(self.all.current.raw, ref) except RuntimeError: QMessageBox.critical(self, "Cannot add new channels", "Cannot add new channels to " "average referenced data.") return else: # re-reference to existing channel(s) tmp, _ = mne.set_eeg_reference(self.all.current.raw, ref) name = self.all.current.name + " (ref {})".format(refstr) new = DataSet(raw=tmp, name=name, reference=refstr, events=self.all.current.events) self._update_datasets(new)
def set_eeg_reference(inst, ref_channels, ref_old=None, **kwargs): """ Reference to new channels. MNE raw object is modified in-place for efficiency. Parameters ---------- inst : mne.io.Raw | mne.io.RawArray | mne.Epochs | mne.Evoked MNE instance of Raw | Epochs | Evoked. Assumes the 'eeg' type is correctly set for EEG channels. ref_channels : list of str | str Can be: - The name(s) of the channel(s) used to construct the reference. - 'average' to apply an average reference (CAR) - 'REST' to use the reference electrode standardization technique infinity reference (requires instance with montage forward kwarg). ref_old : list of str | str Channel(s) to recover. **kwargs : Additional arguments are passed to mne.set_eeg_reference() c.f. https://mne.tools/dev/generated/mne.set_eeg_reference.html """ if not (all(isinstance(ref_ch, str) for ref_ch in ref_channels) or isinstance(ref_channels, str)): logger.error("The new reference channel must be a list of strings " "or 'average' or 'REST'.") raise ValueError if ref_old is not None: mne.add_reference_channels(inst, ref_old, copy=False) mne.set_eeg_reference(inst, ref_channels, copy=False, **kwargs)
def test_set_eeg_reference(): """Test rereference eeg data.""" raw = read_raw_fif(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(not reref.info['projs'][0]['active']) assert_true(ref_data is None) reref.apply_proj() eeg_chans = [ raw.ch_names[ch] for ch in pick_types(raw.info, meg=False, eeg=True) ] _test_reference(raw, reref, ref_data, [ch for ch in eeg_chans if ch not in raw.info['bads']]) # Test setting an average reference when one was already present with warnings.catch_warnings(record=True): reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Test setting an average reference on non-preloaded data raw_nopreload = read_raw_fif(fif_fname, preload=False) raw_nopreload.info['projs'] = [] reref, ref_data = set_eeg_reference(raw_nopreload) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(not reref.info['projs'][0]['active']) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref) # Test moving from custom to average reference reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002']) reref, _ = set_eeg_reference(reref) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_equal(reref.info['custom_ref_applied'], False) # When creating an average reference fails, make sure the # custom_ref_applied flag remains untouched. reref = raw.copy() reref.info['custom_ref_applied'] = True reref.pick_types(eeg=False) # Cause making average ref fail assert_raises(ValueError, set_eeg_reference, reref) assert_true(reref.info['custom_ref_applied']) # Test moving from average to custom reference reref, ref_data = set_eeg_reference(raw) reref, _ = set_eeg_reference(reref, ['EEG 001', 'EEG 002']) assert_true(not _has_eeg_average_ref_proj(reref.info['projs'])) assert_equal(reref.info['custom_ref_applied'], True)
def test_set_eeg_reference(): """Test rereference eeg data.""" raw = read_raw_fif(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(not reref.info['projs'][0]['active']) assert_true(ref_data is None) reref.apply_proj() eeg_chans = [raw.ch_names[ch] for ch in pick_types(raw.info, meg=False, eeg=True)] _test_reference(raw, reref, ref_data, [ch for ch in eeg_chans if ch not in raw.info['bads']]) # Test setting an average reference when one was already present with warnings.catch_warnings(record=True): reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Test setting an average reference on non-preloaded data raw_nopreload = read_raw_fif(fif_fname, preload=False) raw_nopreload.info['projs'] = [] reref, ref_data = set_eeg_reference(raw_nopreload) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(not reref.info['projs'][0]['active']) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref) # Test moving from custom to average reference reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002']) reref, _ = set_eeg_reference(reref) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_equal(reref.info['custom_ref_applied'], False) # When creating an average reference fails, make sure the # custom_ref_applied flag remains untouched. reref = raw.copy() reref.info['custom_ref_applied'] = True reref.pick_types(eeg=False) # Cause making average ref fail assert_raises(ValueError, set_eeg_reference, reref) assert_true(reref.info['custom_ref_applied']) # Test moving from average to custom reference reref, ref_data = set_eeg_reference(raw) reref, _ = set_eeg_reference(reref, ['EEG 001', 'EEG 002']) assert_true(not _has_eeg_average_ref_proj(reref.info['projs'])) assert_equal(reref.info['custom_ref_applied'], True)
def test_set_eeg_reference_ch_type(ch_type): """Test setting EEG reference for ECoG.""" # gh-6454 rng = np.random.RandomState(0) data = rng.randn(3, 1000) raw = RawArray(data, create_info(3, 1000., ['ecog'] * 2 + ['misc'])) with catch_logging() as log: reref, ref_data = set_eeg_reference(raw.copy(), ch_type=ch_type, verbose=True) assert 'Applying a custom ECoG' in log.getvalue() assert reref.info['custom_ref_applied'] # gh-7350 _test_reference(raw, reref, ref_data, ['0', '1']) with pytest.raises(ValueError, match='No channels supplied'): set_eeg_reference(raw, ch_type='eeg')
def robust_avg_ref(epochs, ransac_parameters, apply=True): """ Create a robust average reference by first interpolating the bad channels to exclude outliers. The reference is applied as a projection. Return epochs with reference projection applied if apply=True """ ransac = Ransac(**ransac_parameters, verbose="tqdm") epochs_tmp = epochs.copy() epochs_tmp = ransac.fit_transform(epochs) set_eeg_reference(epochs_tmp, ref_channels="average", projection=True) robust_avg_proj = epochs_tmp.info["projs"][0] del epochs_tmp epochs.info["projs"].append(robust_avg_proj) if apply: epochs.apply_proj() return epochs
def car(bufferGen, channels, **kargs): while True: raw = next(bufferGen) inst, data = set_eeg_reference(raw, verbose=False, ref_channels=channels, **kargs) yield inst
def re_refernce(self, ): self.raw_ref, _ = mne.set_eeg_reference( self.raw, ref_channels='average', projection=True, ) self.raw_ref.apply_proj( ) # it might tell you it already has been re-referenced, but do it anyway
def rereference(raw, ref_new, ref_old=None, **kwargs): """ Apply rereferencing. """ # For numpy array if isinstance(raw, np.ndarray): # Check if isinstance(ref_new, int): ref_new = [ref_new] if not (all(isinstance(ref_ch, int) for ref_ch in ref_new) and all(0 <= ref_ch <= raw.shape[0] for ref_ch in ref_new)): raise ValueError( 'The new reference channel indices {} are not in raw.shape {}.' .format(ref_new, raw.shape[0])) if ref_old is not None: # Number of channel to recover if isinstance(ref_old, (list, tuple, np.ndarray)): ref_old = len(ref_old) # Add blank (zero-valued) channel(s) refs = np.zeros((ref_old, raw.shape[1])) raw = np.vstack((raw, refs)) # this can not be done in-place # Re-reference raw -= np.mean(raw[ref_new], axis=0) # For MNE raw else: # Check if not (all(isinstance(ref_ch, str) for ref_ch in ref_new) or isinstance(ref_new, str)): raise ValueError( "The new reference channel must be a list of strings or 'average' or 'REST'." ) if ref_old is not None: # Add blank (zero-valued) channel(s) mne.add_reference_channels(raw, ref_old, copy=False) # Re-reference mne.set_eeg_reference(raw, ref_new, copy=False, **kwargs) return raw
def process(self): data = self.args["Raw"] reference = list(self.parameters["target"].keys()) if reference == ["Average"]: reference = "average" data.set_eeg_reference('average', projection=False) else: data, _ = mne.set_eeg_reference(data, reference) return {"Re-referenced Raw": data}
def load_raw_mne_from_fif(data_type, subject='adelie', recording=0, montage='standard_1020'): """loads the data and returns an instance of mne.Raw Parameters ---------- data_type : string type of the data, right now two options are valid: `baseline` or `meditation` subject: string name of the subject recording: int number of recording, if you have multiple of same type and subject montage: string the type of montage that was used for the recording see: https://mne.tools/dev/generated/mne.channels.make_standard_montage.html Returns ------- a mne.Raw instance that has the correct montage and info and is ready to be plotted """ subject_paths = config.value('paths', 'subjects', subject) base_path = config.value('paths', 'base') recording_id = subject_paths['recordings'][data_type][recording] file_path = f"{base_path}{subject_paths['prefix']}/offline/fif/{recording_id}-raw.fif" # Create a digitization of the montage digitization = mne.channels.make_standard_montage(montage) channels = get_channelsList(subject=subject) # Read from fif file raw = mne.io.read_raw_fif(file_path, preload=True) # I hope that this is correct raw, _ = mne.set_eeg_reference(raw, [config['reference_electrode']]) # finding events events = mne.find_events(raw, stim_channel='TRIGGER') # Create info with some useful information raw.info = mne.create_info(channels, sfreq=config['sampling_frequency'], ch_types='eeg') # set the montage raw.set_montage(digitization) raw = raw.pick_types(eeg=True, stim=False) raw.set_eeg_reference(projection=True).apply_proj() return raw, events
def re_reference(self): # tutorial: https://github.com/mne-tools/mne-python/blob/master/tutorials/preprocessing/ # plot_55_setting_eeg_reference.py # get rid of the default average reference raw_no_ref, _ = mne.set_eeg_reference(self.raw, []) # add new reference channel (all zero) (because A1 is not saved in the recording - this will be flat) raw_new_ref = mne.add_reference_channels(self.raw, ref_channels=['ch99_A1']) # raw_new_ref.plot(block=True, scalings=dict(eeg=50, grad=1e13, mag=1e15, eog=50), n_channels=31) # set reference to average of A1 and A2 raw_new_ref.set_eeg_reference(ref_channels=['ch4_A2'])
def getEvokedPot(predicted_eeg_samp,client_to_eeg,offset, myBias, raw): bias = int(round(myBias / (1/.3))) #predicted_eeg_samp,client_to_eeg,offset = fit_timestamps(matchdf, msgs, mad_scale=mad_scale, correct_delay=True) syn_event_df = pd.DataFrame([(i+bias,0,1) for i in predicted_eeg_samp], columns=['ts','diff','code']) raw_no_ref,_ = mne.set_eeg_reference(raw.load_data().filter(l_freq=None, h_freq=45), []) #raw_no_ref, _ = mne.set_eeg_reference(raw.load_data(), []) reject = dict(eeg=150e-6) # 180e-6, eog=150e-6) event_id, tmin, tmax = {'visual': 1}, -0.10, 0.5 epochs_params = dict(events=syn_event_df.values, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject) evoked_no_ref = mne.Epochs(raw_no_ref, **epochs_params).average() syn_event_df = pd.DataFrame([(i+bias,0,1) for i in predicted_eeg_samp], columns=['ts','diff','code']) epochs_params = dict(events=syn_event_df.values, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject) evoked_no_ref_uncorrected = mne.Epochs(raw_no_ref, **epochs_params).average() return evoked_no_ref_uncorrected
def test_set_eeg_reference(): """Test rereference eeg data.""" raw = read_raw_fif(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference assert_true(not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_true(ref_data is None) # Test setting an average reference when one was already present with warnings.catch_warnings(record=True): # weight tables reref, ref_data = set_eeg_reference(raw, copy=False) assert_true(ref_data is None) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert_true(reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert_true(raw is reref) # Test moving from custom to average reference reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002']) reref, _ = set_eeg_reference(reref) assert_true(_has_eeg_average_ref_proj(reref.info['projs'])) assert_equal(reref.info['custom_ref_applied'], False) # When creating an average reference fails, make sure the # custom_ref_applied flag remains untouched. reref = raw.copy() reref.info['custom_ref_applied'] = True reref.pick_types(eeg=False) # Cause making average ref fail assert_raises(ValueError, set_eeg_reference, reref) assert_true(reref.info['custom_ref_applied'])
# To apply a montage on your data use the ``set_montage`` method. # function. Here don't actually call this function as our demo dataset # already contains good EEG channel locations. # # Next we'll explore the definition of the reference. ############################################################################### # Setting EEG reference # --------------------- # # Let's first remove the reference from our Raw object. # # This explicitly prevents MNE from adding a default EEG average reference # required for source localization. raw_no_ref, _ = mne.set_eeg_reference(raw, []) ############################################################################### # We next define Epochs and compute an ERP for the left auditory condition. reject = dict(eeg=180e-6, eog=150e-6) event_id, tmin, tmax = {'left/auditory': 1}, -0.2, 0.5 events = mne.read_events(event_fname) epochs_params = dict(events=events, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject) evoked_no_ref = mne.Epochs(raw_no_ref, **epochs_params).average() del raw_no_ref # save memory title = 'EEG Original reference' evoked_no_ref.plot(titles=dict(eeg=title), time_unit='s') evoked_no_ref.plot_topomap(times=[0.1], size=3., title=title, time_unit='s')
def split(raw_merge: mne.io.Raw) -> mne.io.Raw: """ Splits merged Raw data into 2 subjects Raw data. Arguments: raw_merge: Raw data for the dyad with data from subject 1 and data from subject 2 (channels name are defined with the suffix S1 and S2 respectively). Note: Subject's Raw data is set to the standard montage 1020 available in MNE. An average is computed to avoid reference bias (see MNE documentation about set_eeg_reference). Returns: raw_1020_S1, raw_1020_S2: Raw data for each subject separately. Raws are MNE objects. """ ch_S1 = [] ch_S2 = [] ch = [] for name in raw_merge.info['ch_names']: if name.endswith('S1'): ch_S1.append(name) ch.append(name.split('_')[0]) elif name.endswith('S2'): ch_S2.append(name) # picking individual subject data data_S1 = raw_merge.get_data(picks=ch_S1) data_S2 = raw_merge.get_data(picks=ch_S2) # creating info for raws info = mne.create_info(ch, raw_merge.info['sfreq'], ch_types='eeg', montage=None, verbose=None) raw_S1 = mne.io.RawArray(data_S1, info) raw_S2 = mne.io.RawArray(data_S2, info) # setting info about channels and task raw_S1.info['bads'] = [ ch.split('_')[0] for ch in ch_S1 if ch in raw_merge.info['bads'] ] raw_S2.info['bads'] = [ ch.split('_')[0] for ch in ch_S2 if ch in raw_merge.info['bads'] ] for raws in (raw_S1, raw_S2): raws.info['description'] = raw_merge.info['description'] raws.info['events'] = raw_merge.info['events'] # setting montage 94 electrodes (ignore somes to correspond to our data) for ch in raws.info['chs']: if ch['ch_name'].startswith('MOh') or ch['ch_name'].startswith( 'MOb'): # print('emg') ch['kind'] = FIFF.FIFFV_EOG_CH else: ch['kind'] = FIFF.FIFFV_EEG_CH montage = mne.channels.make_standard_montage('standard_1020') raw_1020_S1 = raw_S1.copy().set_montage(montage) raw_1020_S2 = raw_S2.copy().set_montage(montage) # raw_1020_S1.plot_sensors() # set reference to electrodes average # (instate of initial ref to avoid ref biais) # and storing it in raw.info['projs']: applied when Epochs raw_1020_S1, _ = mne.set_eeg_reference(raw_1020_S1, 'average', projection=True) raw_1020_S2, _ = mne.set_eeg_reference(raw_1020_S2, 'average', projection=True) # TODO: annotations, subj name, events # task description different across subj # raw_1020_S1.plot() # raw_1020_S1.plot_psd() return raw_1020_S1, raw_1020_S2
import sys import os import os.path as op import numpy as np import mne execfile("local_settings.py") bdf_dir = op.join(data_dir,"bdf") # the location for all intermediate processing stages # (put in a temporary directory) for name in names: if op.isfile(op.join(temp_dir,name+".fif")): print op.join(temp_dir,name+".fif")+" already generated, skipping..." continue print "Processing ",name raw = mne.io.read_raw_edf(op.join(bdf_dir,name+".bdf"), preload=True,misc=['Erg1'], eog=['IO1','IO2','LO1','LO2'], montage=op.join(data_dir,'..','acnlbiosemi64.sfp')) raw.filter(0.01,30,l_trans_bandwidth='auto', h_trans_bandwidth='auto', filter_length='auto',phase='zero') mne.set_eeg_reference(raw,copy=False,ref_channels=['M1','M2']) raw.save(op.join(temp_dir,name+".fif"))
# # # The ``copy`` parameter # ---------------------- # # Above we saw an example of using the `~mne.io.Raw.copy` method to facilitate # comparing data before and after processing. This is not needed when using # certain MNE-Python *functions*, because they have a *function parameter* # where you can specify ``copy=True`` (return a modified copy of the data) or # ``copy=False`` (operate in-place). For example, `mne.set_eeg_reference` is # one such function; notice that here we plot ``original_raw`` *after* the # rereferencing has been done, but ``original_raw`` is unaffected because # we specified ``copy=True``: # sphinx_gallery_thumbnail_number=2 rereferenced_raw, ref_data = mne.set_eeg_reference(original_raw, ['EEG 003'], copy=True) fig_orig = original_raw.plot() fig_reref = rereferenced_raw.plot() # %% # Another example is the picking function `mne.pick_info`, which operates on # `mne.Info` dictionaries rather than on data objects. See # :ref:`tut-info-class` for details. # # # Summary # ------- # # Generally speaking, you should expect that *methods of data objects* will # operate in-place, and *functions that take a data object as a parameter* will # operate on a copy of the data (unless the function has a ``copy`` parameter
def run(self): eog = self.info['channel_info']['EOG'] misc = self.info['channel_info']['Misc'] stim = self.info['channel_info']['Stim'] try: ext_files = glob.glob(self.info['ext_file_folder'] + '/' + self.participant + '/*axis0.dat') except: pass tmin = self.t_epoch[0] tmax = self.t_epoch[1] raw = read_raw_edf(self.file, eog=eog, misc=misc) self.raw = cp.deepcopy(raw) raw.load_data() # marker detection (one marker continous trial) if self.info['marker_detection'] == True: starts = find_trialstart(raw, stim_channel=raw.ch_names[stim[0]], new_samplin_rate=self.sr_new) try: starts[1] = starts[0] + 30 * 200 except: starts = np.r_[starts, (starts[0] + 30 * 200)] events = np.zeros((len(starts), 3)) events[:, 0] = starts events[:, 2] = list(self.info['event_dict'].values()) events = events.astype(np.int) # event detection (one marker regular events) if self.info['event_detection'] == True: starts = find_trialstart(raw, stim_channel=raw.ch_names[stim[0]], new_samplin_rate=self.sr_new) events = force_events(ext_files, self.info['event_dict'], self.sr_new, self.info['trial_length'], self.info['trials'], starts[:len(self.info['event_dict'])]) if self.info['ICA'] == True: ica = ICA(method='fastica') if self.info['Autoreject'] == True: ar = AutoReject() ## EEG preprocessing options will applied if parameters are set in object #read montage try: montage = make_standard_montage(self.montage) raw.set_montage(montage) except: pass #resampling try: raw.resample(sfreq=self.sr_new) except: pass #rereferencing try: raw, _ = mne.set_eeg_reference(raw, ref_channels=['EXG5', 'EXG6']) except: pass #filter try: low = self.filter_freqs[0] high = self.filter_freqs[1] raw.filter(low, high, fir_design='firwin') except: pass # occular correction try: ica.fit(raw) ica.exclude = [] eog_indices, eog_scores = ica.find_bads_eog(raw) ica.exclude = eog_indices ica.apply(raw) self.ica = ica except: pass picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') event_id = self.info['event_dict'] epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, baseline=None, preload=True, picks=picks) #epoch rejection try: epochs = epochs.drop(indices=self.bads) except: pass try: epochs, self.autoreject_log = ar.fit_transform(epochs, return_log=True) except: pass bads = np.asarray( [l == ['USER'] or l == ['AUTOREJECT'] for l in epochs.drop_log]) self.bads = np.where(bads == True) self.epochs = epochs return (self)
#bad channels raw.info['bads'] = [] #input your own bad channels here picks = mne.pick_types( raw.info, meg=False, eeg=True, eog=False, stim=False, exclude='bads' ) # setting for eeg data, will have to change values to other #values for any other types of data to True. ### Sensor mapping raw.plot_sensors() #plotting the sensor map in 2d raw.plot_sensors(kind='3d', ch_type='eeg', ch_groups='position') #sensor map in 3d ### Projection mapping mne.set_eeg_reference(raw) #prevent MNE from setting reference automatically ### Power plot #raw.plot_psd(average=False) ### Filtering of data raw.filter( l_freq=1, h_freq=55 ) #band-pass filter at 55 Hz (set your own frequencies here if you prefer something else) raw.notch_filter(freqs=[50, 100], picks=picks, filter_length='auto', phase='zero') # run cell twice
def test_set_eeg_reference(): """Test rereference eeg data.""" raw = read_raw_fif(fif_fname, preload=True) raw.info['projs'] = [] # Test setting an average reference projection assert (not _has_eeg_average_ref_proj(raw.info['projs'])) reref, ref_data = set_eeg_reference(raw, projection=True) assert (_has_eeg_average_ref_proj(reref.info['projs'])) assert (not reref.info['projs'][0]['active']) assert (ref_data is None) reref.apply_proj() eeg_chans = [raw.ch_names[ch] for ch in pick_types(raw.info, meg=False, eeg=True)] _test_reference(raw, reref, ref_data, [ch for ch in eeg_chans if ch not in raw.info['bads']]) # Test setting an average reference when one was already present with pytest.warns(RuntimeWarning, match='untouched'): reref, ref_data = set_eeg_reference(raw, copy=False, projection=True) assert ref_data is None # Test setting an average reference on non-preloaded data raw_nopreload = read_raw_fif(fif_fname, preload=False) raw_nopreload.info['projs'] = [] reref, ref_data = set_eeg_reference(raw_nopreload, projection=True) assert (_has_eeg_average_ref_proj(reref.info['projs'])) assert (not reref.info['projs'][0]['active']) # Rereference raw data by creating a copy of original data reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=True) assert (reref.info['custom_ref_applied']) _test_reference(raw, reref, ref_data, ['EEG 001', 'EEG 002']) # Test that data is modified in place when copy=False reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002'], copy=False) assert (raw is reref) # Test moving from custom to average reference reref, ref_data = set_eeg_reference(raw, ['EEG 001', 'EEG 002']) reref, _ = set_eeg_reference(reref, projection=True) assert (_has_eeg_average_ref_proj(reref.info['projs'])) assert_equal(reref.info['custom_ref_applied'], False) # When creating an average reference fails, make sure the # custom_ref_applied flag remains untouched. reref = raw.copy() reref.info['custom_ref_applied'] = True reref.pick_types(eeg=False) # Cause making average ref fail pytest.raises(ValueError, set_eeg_reference, reref, projection=True) assert (reref.info['custom_ref_applied']) # Test moving from average to custom reference reref, ref_data = set_eeg_reference(raw, projection=True) reref, _ = set_eeg_reference(reref, ['EEG 001', 'EEG 002']) assert not _has_eeg_average_ref_proj(reref.info['projs']) assert len(reref.info['projs']) == 0 assert_equal(reref.info['custom_ref_applied'], True) # Test that disabling the reference does not change the data assert _has_eeg_average_ref_proj(raw.info['projs']) reref, _ = set_eeg_reference(raw, []) assert_array_equal(raw._data, reref._data) assert not _has_eeg_average_ref_proj(reref.info['projs']) # make sure ref_channels=[] removes average reference projectors assert _has_eeg_average_ref_proj(raw.info['projs']) reref, _ = set_eeg_reference(raw, []) assert (not _has_eeg_average_ref_proj(reref.info['projs'])) # Test that average reference gives identical results when calculated # via SSP projection (projection=True) or directly (projection=False) raw.info['projs'] = [] reref_1, _ = set_eeg_reference(raw.copy(), projection=True) reref_1.apply_proj() reref_2, _ = set_eeg_reference(raw.copy(), projection=False) assert_allclose(reref_1._data, reref_2._data, rtol=1e-6, atol=1e-15) # Test average reference without projection reref, ref_data = set_eeg_reference(raw.copy(), ref_channels="average", projection=False) _test_reference(raw, reref, ref_data, eeg_chans) # projection=True only works for ref_channels='average' pytest.raises(ValueError, set_eeg_reference, raw, [], True, True) pytest.raises(ValueError, set_eeg_reference, raw, ['EEG 001'], True, True)
# To apply a montage on your data use the ``set_montage`` method. # function. Here don't actually call this function as our demo dataset # already contains good EEG channel locations. # # Next we'll explore the definition of the reference. ############################################################################### # Setting EEG reference # --------------------- # # Let's first remove the reference from our Raw object. # # This explicitly prevents MNE from adding a default EEG average reference # required for source localization. raw_no_ref, _ = mne.set_eeg_reference(raw, []) ############################################################################### # We next define Epochs and compute an ERP for the left auditory condition. reject = dict(eeg=180e-6, eog=150e-6) event_id, tmin, tmax = {'left/auditory': 1}, -0.2, 0.5 events = mne.read_events(event_fname) epochs_params = dict(events=events, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject) evoked_no_ref = mne.Epochs(raw_no_ref, **epochs_params).average() del raw_no_ref # save memory title = 'EEG Original reference' evoked_no_ref.plot(titles=dict(eeg=title)) evoked_no_ref.plot_topomap(times=[0.1], size=3., title=title)
numOfRejectedEpochs=[] #%% for b in range(numOfTrainBlocks): for r in range(runNum): print "********preparing data for the stimuli in block", b+1,"run#", r+1,"****************\ *********************" trial=AudioTactile_runs[b][r] trial.n_times trial.load_data() trial.info['bads']=bads trial.set_montage(montage) trial_rerefrenced, _= mne.set_eeg_reference(trial,[]) # trial.filter(0.5,12,method='iir') trial.resample(sfreq=resamp_freq) trial_Epoch=mne.Epochs(trial_rerefrenced, exported_events, tmin=-.4,baseline=(-.16,None), tmax=1.5, decim=decim, reject_by_annotation=True) #TODO: make tmin and tmax a variable, make sure thredhold for eeg is appropriate trial_Epoch.load_data() trial_Epoch.crop(tmin=tmin,tmax=tmax) trial_Epoch.drop_channels(trial_Epoch.info['bads']) #trial_Epoch.drop_bad() oddball_str=str(oddball_stim[b,0][r,0]+7)
ch_type = ['stim'] + ['eeg'] * 30 info = mne.create_info(ch_names, montage='easycap-M1', sfreq=500.0, ch_types=ch_type) raweeg = mne.io.RawArray(raweeg, info, verbose=False) return raweeg #### OPENING rec1 = filedialog.askopenfilename() #запросить файл print('File_1 is opened') #### CREATING RAW raweeg = dat2mne(rec1) ##### PREPROC mne.set_eeg_reference(raweeg) raweeg.filter(0.1, 30, fir_design='firwin', skip_by_annotation='edge') events = mne.find_events(raweeg, initial_event=True) event_id = dict(fes=1, rest=2, before=3, after=4) epochs1 = mne.Epochs(raweeg, events, event_id, tmin=0.0, tmax=3.0, baseline=(0, 0), preload=True) ####### MULTITAPER FULL SPECTRUM def multitaper(eeg): for key in event_id:
# To apply a montage on your data use the ``set_montage`` method. # function. Here don't actually call this function as our demo dataset # already contains good EEG channel locations. # # Next we'll explore the definition of the reference. ############################################################################### # Setting EEG reference # --------------------- # # Let's first remove the reference from our Raw object. # # This explicitly prevents MNE from adding a default EEG average reference # required for source localization. raw_no_ref, _ = mne.set_eeg_reference(raw, []) ############################################################################### # We next define Epochs and compute an ERP for the left auditory condition. reject = dict(eeg=180e-6, eog=150e-6) event_id, tmin, tmax = {'left/auditory': 1}, -0.2, 0.5 events = mne.read_events(event_fname) epochs_params = dict(events=events, event_id=event_id, tmin=tmin, tmax=tmax, reject=reject) evoked_no_ref = mne.Epochs(raw_no_ref, **epochs_params).average() del raw_no_ref # save memory
data_collection_flag = False #Data could be saved to a pickle object at this point, but was commented out to make the process faster #collected_data_df = pd.DataFrame(collected_data) #collected_data_df.to_pickle('Raw_data.pkl') for data in collected_data: # Each trial is assigned a montage and notch and band pass filtered data = np.array(data) data = np.transpose(data) data = convert(data) data = filter_data(data) # If Laplacian filtering was not used the external RPA electrode will be used to reference the data if filter_lapl == False: raw = mne.set_eeg_reference(raw, ['RPA']) raw = raw[0] #events maps the timestep to corresponding stimulus codes events = mne.find_events(data, stim_channel='STI 014', verbose=False, initial_event=False) print('Unique event codes:', np.unique(events[:, 2])) event_id = {'33040': 33040} # epoching is performed using a uniform 33040 stimulus code which occurs 0.2 sec after the visual stimulus disappears for 600 ms # the mne.Epoching function provides the functionality of artefact removal but was not used to avoid a disbalanced dataset epochs = mne.Epochs(data, events, event_id, tmin=0.4, tmax=1,
scalings={'eeg': .0003}, title='Select channels to interpolate') bads = two_sec_eps.info['bads'] bads_msg = input("The channels you marked as bad are: " + str(bads) + " Are you sure you want to continue? [y/n]") if bads_msg == 'y': two_sec_eps.interpolate_bads() else: print('no interpolation') #layout=mne.channels.read_montage(kind='GSN-HydroCel-128') layout = mne.channels.find_layout(two_sec_eps.info) two_sec_eps, r = mne.set_eeg_reference(two_sec_eps, ref_channels=dataChannels + extChannels) ica = ICA(n_components=90, random_state=25, method='infomax') picks = mne.pick_types(two_sec_eps.info, meg=False, eeg=True, eog=False, ecg=False) ica.fit(two_sec_eps, picks=picks) eog_ic = [] for ch in ['E25', 'E17', 'E8', 'E21', 'E14', 'E125', 'E126', 'E127', 'E128']: #insert EOG channels #ecg_epochs=create_ecg_epochs(data,ch_name=ch) # ? eog_idx, scores = ica.find_bads_eog(two_sec_eps, ch_name=ch) eog_ic.append(eog_idx)