def score(p, subjects, run_indices): """Default scoring function that just passes event numbers through""" for si, subj in enumerate(subjects): print(' Scoring subject %s... ' % subj) raw_fnames = get_raw_fnames(p, subj, 'raw', False, False, run_indices[si]) eve_fnames = get_event_fnames(p, subj, run_indices[si]) for raw_fname, eve_fname in zip(raw_fnames, eve_fnames): with warnings.catch_warnings(record=True): raw = mne.io.read_raw_fif(raw_fname, allow_maxshield='yes') events_raw = mne.find_events(raw, stim_channel='STI101', shortest_event=0) # where event trigs = 1 start_inds = np.where(events_raw[:, -1] == 1)[0] # generate empty matrix with len events, 3 for event type event_type = np.zeros((len(start_inds), 3)) # take all samples marked with 1 trigger (stim onset) events = events_raw[start_inds, :] assert len(events) == 200 # otherwise something is wrong # :100 - pitch 100: - speech 100 trials for full data set event_type[:100, -1] = 10 # 'pitch' event_type[100:, -1] = 20 # 'speech' events[:, -1] = event_type[:, -1] mne.write_events(eve_fname, events)
def run_ssp(subject): print("processing subject: %s" % subject) meg_subject_dir = op.join(config.meg_dir, subject) print(" Loading runs") for run in config.runs: extension = run + '_sss_raw' raw_fname_in = op.join(meg_subject_dir, config.base_fname.format(**locals())) proj_fname_out = op.splitext(raw_fname_in)[0] + '-proj.fif' eve_ecg_fname_out = op.splitext(raw_fname_in)[0] + '_ecg-eve.fif' eve_eog_fname_out = op.splitext(raw_fname_in)[0] + '_eog-eve.fif' print("Input: ", raw_fname_in) print("Output: ", proj_fname_out) print("Output: ", eve_ecg_fname_out) print("Output: ", eve_eog_fname_out) raw = mne.io.read_raw_fif(raw_fname_in) ecg_projs, ecg_events = \ compute_proj_ecg(raw, n_grad=1, n_mag=1, n_eeg=0, average=True) eog_projs, eog_events = \ compute_proj_eog(raw, n_grad=1, n_mag=1, n_eeg=1, average=True) mne.write_proj(proj_fname_out, eog_projs + ecg_projs) mne.write_events(eve_ecg_fname_out, ecg_events) mne.write_events(eve_eog_fname_out, eog_events)
def test_io_events(): """Test IO for events """ events = mne.read_events(fname) mne.write_events('events.fif', events) events2 = mne.read_events('events.fif') assert_array_almost_equal(events, events2)
def run_evoked(subject_id): subject = "sub%03d" % subject_id print("processing subject: %s" % subject) data_path = op.join(meg_dir, subject) for run in range(1, 7): run_fname = op.join(data_path, 'run_%02d_filt_sss_raw.fif' % run) if not os.path.exists(run_fname): continue raw = mne.io.Raw(run_fname) events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', min_duration=0.003, verbose=True) for key in all_events_id: events = mne.merge_events(events, all_events_id[key], events_id[key]) mask = (events[:, 2] == 1) | (events[:, 2] == 2) | (events[:, 2] == 3) events = events[mask] # df = pd.read_csv(data_path + '/Trials/run_%02d_trldef.txt' % run, sep='\t', header=None) # ev = np.c_[df[1], np.zeros_like(df[0]), le.transform(df[3])] # ev[:, 0] = np.round(ev[:, 0] / 3.) # decimation by 3 # ev[:, 0] += raw.first_samp # ev[:, 0] -= 452 # ev[ev[:, 2] == 3, 2] = 4 # ev[ev[:, 2] == 2, 2] = 3 # ev[ev[:, 2] == 4, 2] = 2 # print events - ev print "S %s - R %s" % (subject, run) # print (events - ev)[:, 2] # assert not np.any((events - ev)[:, 1:]) # assert np.max(np.abs((events - ev)[:, 0])) == 1 mne.write_events(op.join(data_path, 'run_%02d_filt_sss-eve.fif' % run), events)
def save_data_and_events_as_fif(df, name, fs, channels, montage_kind="standard_1020"): """ save_data_and_events_as_fif(df, name, fs, channels, montage_kind="standard_1020") Saves raw data and events, extracted from a photoelectric sensor and block names as readable for Brainstorm .fif (See https://neuroimage.usc.edu/brainstorm/Introduction). :param df: pandas data frame returned from nfblab data by standard util :param name: save path or name :param fs: data sampling rate :param channels: channels to save :param montage_kind: mne style montage name or array :return: raw data fif """ # make an appropriate data name if name[-1] == os.sep: name = name[-1] # delete a photoelectric sensor from raw data if channels.count("Photo") > 0: channels.remove("Photo") # save raw data as readable for Brainstorm .fif montage = mne.channels.read_montage(kind="standard_1020") info = mne.create_info(ch_names=channels, sfreq=fs, ch_types="eeg", montage=montage) raw = mne.io.RawArray(df[channels].T, info) raw.save("{}-raw.fif".format(name), overwrite=True) # make events from a photoelectric sensor triggers = df['Photo'].diff(1) events = np.array(triggers.where(triggers > 0.00005).dropna().index) events_ids = np.array(["Photo" for i in range(events.shape[0])]) events_channels = np.zeros([events.shape[0]]) photo_events = np.stack([events, events_channels, events_ids], axis=1) # make events from block names le = preprocessing.LabelEncoder() le.fit(df["block_name"].unique()) triggers = pd.Series(le.transform(df["block_name"])).diff(1) events = np.array(triggers.where(abs(triggers) > 0).dropna().index) events_ids = df["block_name"].iloc[events] events_channels = np.zeros([events.shape[0]]) block_events = np.stack([events, events_channels, events_ids], axis=1) # block_events = np.array([[i, 0, df["block_name"][i]] for i in range(df["block_name"].shape[0] - 1) # if df["block_name"][i] != df["block_name"][i + 1]]) # concatenate events of different types events_list = np.concatenate([photo_events, block_events]) # save events data as readable for Brainstorm .fif mne.write_events("{}-eve.fif".format(name), events_list)
def score(p, subjects): """Scoring function""" for subj in subjects: print(" Running subject %s... " % subj, end="") # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for run_name in p.run_names: fname = op.join(p.work_dir, subj, p.raw_dir, (run_name % subj) + p.raw_fif_tag) events, presses = extract_expyfun_events(fname)[:2] for ii in range(len(events)): events[ii, 2] = _expyfun_dict[events[ii, 2]] fname_out = op.join(out_dir, "ALL_" + (run_name % subj) + "-eve.lst") mne.write_events(fname_out, events) # get subject performance devs = events[:, 2] % 2 == 1 has_presses = np.array([len(pr) > 0 for pr in presses], bool) n_devs = np.sum(devs) hits = np.sum(has_presses[devs]) fas = np.sum(has_presses[~devs]) misses = n_devs - hits crs = (len(devs) - n_devs) - fas print("HMFC: %s, %s, %s, %s" % (hits, misses, fas, crs))
def test_io_cov(): """Test IO for noise covariance matrices """ events = mne.read_events(fname) mne.write_events('events.fif', events) events2 = mne.read_events(fname) assert_array_almost_equal(events, events2)
def score(p, subjects): """Scoring function""" for subj in subjects: print(' Running subject %s... ' % subj, end='') # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for run_name in p.run_names: fname = op.join(p.work_dir, subj, p.raw_dir, (run_name % subj) + p.raw_fif_tag) events, presses = extract_expyfun_events(fname)[:2] for ii in range(len(events)): events[ii, 2] = _expyfun_dict[events[ii, 2]] fname_out = op.join(out_dir, 'ALL_' + (run_name % subj) + '-eve.lst') mne.write_events(fname_out, events) # get subject performance devs = (events[:, 2] % 2 == 1) has_presses = np.array([len(pr) > 0 for pr in presses], bool) n_devs = np.sum(devs) hits = np.sum(has_presses[devs]) fas = np.sum(has_presses[~devs]) misses = n_devs - hits crs = (len(devs) - n_devs) - fas print('HMFC: %s, %s, %s, %s' % (hits, misses, fas, crs))
def score(p, subjects, run_indices): """Default scoring function that just passes event numbers through""" for si, subj in enumerate(subjects): print(' Scoring subject %s... ' % subj, end='') # Figure out what our filenames should be raw_fnames = get_raw_fnames(p, subj, 'raw', False, False, run_indices[si]) eve_fnames = get_event_fnames(p, subj, run_indices[si]) for raw_fname, eve_fname in zip(raw_fnames, eve_fnames): raw = mne.io.read_raw_fif(raw_fname, allow_maxshield='yes', verbose=False) events = mne.find_events(raw, stim_channel='STI101', shortest_event=1, mask=128, mask_type='not_and', verbose=False) events[:, 2] += 10 if subj.startswith('f2f_108'): # weird triggering mask = (events[:, 2] == 19) events[mask, 2] = 13 n_bad = (~np.in1d(events[:, 2], [11, 13, 15])).sum() if n_bad > 0: print('Found %d unknown events!' % n_bad) else: print() mne.write_events(eve_fname, events)
def reconstruct_events(p, subjects): """Reconstruct events from expyfun tab file """ for subj in subjects: print(' Running subject %s... ' % subj, end='') # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) for run_name in p.run_names: print(subj) fname = op.join(p.work_dir, subj, p.raw_dir, (run_name % subj) + p.raw_fif_tag) tab_file = op.join(p.work_dir, subj, p.list_dir, (run_name % subj + '.tab')) evs, _ = extract_expyfun_events(fname)[:2] raw = mne.io.read_raw_fif(fname, allow_maxshield='yes') data = read_tab(tab_file) new_evs = np.zeros(evs.shape, dtype=np.int) for i in range(len(evs)): new_evs[i, 0] = raw.time_as_index(data[i]['play'][0][1]) # classify event type based on expyfun stimulus new_evs[i, 1] = 0 if data[i]['trial_id'][0][0] == 'Dp01bw6-rms': trigger = 103 elif data[i]['trial_id'][0][0] == 'Dp01bw1-rms': trigger = 104 elif data[i]['trial_id'][0][0] == 'Dp01bw10-rms': trigger = 105 new_evs[i, 2] = trigger fname_out = op.join(out_dir, 'ALL_' + (run_name % subj) + '-eve.lst') mne.write_events(fname_out, new_evs)
def test_io_set(): """Test importing EEGLAB .set files""" from scipy import io _test_raw_reader(read_raw_eeglab, input_fname=raw_fname, montage=montage) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') _test_raw_reader(read_raw_eeglab, input_fname=raw_fname_onefile, montage=montage) raw = read_raw_eeglab(input_fname=raw_fname_onefile, montage=montage) raw2 = read_raw_eeglab(input_fname=raw_fname, montage=montage) assert_array_equal(raw[:][0], raw2[:][0]) # one warning per each preload=False or str with raw_fname_onefile assert_equal(len(w), 3) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') epochs = read_epochs_eeglab(epochs_fname) epochs2 = read_epochs_eeglab(epochs_fname_onefile) # 3 warnings for each read_epochs_eeglab because there are 3 epochs # associated with multiple events assert_equal(len(w), 6) assert_array_equal(epochs.get_data(), epochs2.get_data()) # test different combinations of events and event_ids temp_dir = _TempDir() out_fname = op.join(temp_dir, 'test-eve.fif') write_events(out_fname, epochs.events) event_id = {'S255/S8': 1, 'S8': 2, 'S255/S9': 3} epochs = read_epochs_eeglab(epochs_fname, epochs.events, event_id) epochs = read_epochs_eeglab(epochs_fname, out_fname, event_id) assert_raises(ValueError, read_epochs_eeglab, epochs_fname, None, event_id) assert_raises(ValueError, read_epochs_eeglab, epochs_fname, epochs.events, None) # test if .dat file raises an error eeg = io.loadmat(epochs_fname, struct_as_record=False, squeeze_me=True)['EEG'] eeg.data = 'epochs_fname.dat' bad_epochs_fname = op.join(temp_dir, 'test_epochs.set') io.savemat(bad_epochs_fname, {'EEG': {'trials': eeg.trials, 'srate': eeg.srate, 'nbchan': eeg.nbchan, 'data': eeg.data, 'epoch': eeg.epoch, 'event': eeg.event, 'chanlocs': eeg.chanlocs}}) shutil.copyfile(op.join(base_dir, 'test_epochs.fdt'), op.join(temp_dir, 'test_epochs.dat')) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') assert_raises(NotImplementedError, read_epochs_eeglab, bad_epochs_fname) assert_equal(len(w), 3)
def run_events(subject, run=None, session=None): """Extract events and save as fif.""" print("Processing subject: %s" % subject) # Construct the search path for the data file. `sub` is mandatory subject_path = op.join('sub-{}'.format(subject)) # `session` is optional if session is not None: subject_path = op.join(subject_path, 'ses-{}'.format(session)) subject_path = op.join(subject_path, config.kind) bids_basename = make_bids_basename(subject=subject, session=session, task=config.task, acquisition=config.acq, run=run, processing=config.proc, recording=config.rec, space=config.space ) # Prepare a name to save the data fpath_deriv = op.join(config.bids_root, 'derivatives', config.PIPELINE_NAME, subject_path) if config.use_maxwell_filter: raw_fname_in = \ op.join(fpath_deriv, bids_basename + '_sss_raw.fif') else: raw_fname_in = \ op.join(fpath_deriv, bids_basename + '_filt_raw.fif') eve_fname_out = op.join(fpath_deriv, bids_basename + '-eve.fif') raw = mne.io.read_raw_fif(raw_fname_in) events, event_id = mne.events_from_annotations(raw) if config.trigger_time_shift: events = mne.event.shift_time_events(events, np.unique(events[:, 2]), config.trigger_time_shift, raw.info['sfreq']) print("Input: ", raw_fname_in) print("Output: ", eve_fname_out) mne.write_events(eve_fname_out, events) if config.plot: # plot events mne.viz.plot_events(events, sfreq=raw.info['sfreq'], first_samp=raw.first_samp) plt.show()
def _compute_add_eog(p, subj, raw_orig, projs, eog_nums, kind, pca_dir, flat, extra_proj, old_kwargs, p_sl): assert kind in ('EOG', 'HEOG', 'VEOG') bk = dict(EOG='blink').get(kind, kind.lower()) eog_eve = op.join(pca_dir, f'preproc_{bk}-eve.fif') eog_epo = op.join(pca_dir, f'preproc_{bk}-epo.fif') eog_proj = op.join(pca_dir, f'preproc_{bk}-proj.fif') eog_t_lims = _handle_dict(getattr(p, f'{kind.lower()}_t_lims'), subj) eog_f_lims = _handle_dict(getattr(p, f'{kind.lower()}_f_lims'), subj) eog_channel = _handle_dict(getattr(p, f'{kind.lower()}_channel'), subj) thresh = _handle_dict(getattr(p, f'{kind.lower()}_thresh'), subj) if eog_channel is None and kind != 'EOG': eog_channel = 'EOG061' if kind == 'HEOG' else 'EOG062' if eog_nums.any(): if p.disp_files: print(f' Computing {kind} projectors...', end='') raw = raw_orig.copy() raw.filter(eog_f_lims[0], eog_f_lims[1], n_jobs=p.n_jobs_fir, method='fir', filter_length=p.filter_length, l_trans_bandwidth=0.5, h_trans_bandwidth=0.5, phase='zero-double', fir_window='hann', skip_by_annotation='edge', **old_kwargs) raw.add_proj(projs) raw.apply_proj() eog_events = find_eog_events( raw, ch_name=eog_channel, reject_by_annotation=True, thresh=thresh) use_reject, use_flat = _restrict_reject_flat( _handle_dict(p.ssp_eog_reject, subj), flat, raw) eog_epochs = Epochs( raw, eog_events, 998, eog_t_lims[0], eog_t_lims[1], baseline=None, reject=use_reject, flat=use_flat, preload=True) print(' obtained %d epochs from %d events.' % (len(eog_epochs), len(eog_events))) del eog_events if len(eog_epochs) >= 5: write_events(eog_eve, eog_epochs.events) eog_epochs.save(eog_epo, **_get_epo_kwargs()) desc_prefix = f'{kind}-%s-%s' % tuple(eog_t_lims) pr = compute_proj_wrap( eog_epochs, p.proj_ave, n_grad=eog_nums[0], n_mag=eog_nums[1], n_eeg=eog_nums[2], desc_prefix=desc_prefix, **extra_proj) assert len(pr) == np.sum(eog_nums[::p_sl]) write_proj(eog_proj, pr) projs.extend(pr) else: warnings.warn('Only %d usable EOG events!' % len(eog_epochs)) _safe_remove([eog_proj, eog_eve, eog_epo]) del raw, eog_epochs else: _safe_remove([eog_proj, eog_eve, eog_epo])
def save_event_file_ctl(subject, data_folder): """ Parameters ---------- subject : subject name data_folder : string Returns : None Save the events file to the data_folder. ------- """ test_trigger = { "P21": 251, "P22": 251, "P23": 251, "P24": 251, "P25": 251, "P26": 251, "P26": 251, "P27": 254, "P28": 252, "P29": 252, "P30": 254, "P31": 254, "P32": 254, "P33": 254, "P34": 254, "P35": 254, "P36": 254, "P37": 254, "P38": 254 } # extract only the test trails, remember python is 0 index! idx = np.concatenate( [np.arange(30, 89, 2), np.arange(120, 179, 2), np.arange(210, 269, 2)]) raw = mne.io.Raw(data_folder + "%s_bp_ica-raw.fif" % subject, preload=False) events = mne.find_events(raw) # find the events that match the individual triggers test_events = events[:, 2] == test_trigger[subject] events = events[test_events] # select the events events = events[idx] # select the test trials # save events file mne.write_events(data_folder + "%s-eve.fif" % subject, events)
def run_events(subject): print("processing subject: %s" % subject) meg_subject_dir = op.join(config.meg_dir, subject) raw_fnames_in = [op.join(meg_subject_dir, '%s_audvis_filt_raw.fif' % subject)] eve_fnames_out = [op.join(meg_subject_dir, '%s_audvis_filt-eve.fif' % subject)] for raw_fname_in, eve_fname_out in zip(raw_fnames_in, eve_fnames_out): raw = mne.io.read_raw_fif(raw_fname_in) events = mne.find_events(raw) print("subject: %s - file: %s" % (subject, raw_fname_in)) mne.write_events(eve_fname_out, events)
def run_events(raw, events_fname=None): mask = 4096 + 256 # mask for excluding high order bits events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='not_and', min_duration=0.0001, verbose=True) if events_fname is None: events_fname = raw._filenames[0][:-4] + '-pyimpress-eve.fif' mne.write_events(events_fname, events) return events, events_fname
def run_events(raw, events_fname=None, mask=255): events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='and', min_duration=2. / raw.info['sfreq'], verbose=True) if events_fname is None: events_fname = raw._filenames[0][:-4] + '-pyimpress-eve.fif' mne.write_events(events_fname, events) return events, events_fname
def default_score(p, subjects, run_indices): """Default scoring function that just passes event numbers through""" for si, subj in enumerate(subjects): print(' Scoring subject %s... ' % subj) # Figure out what our filenames should be raw_fnames = get_raw_fnames(p, subj, 'raw', False, False, run_indices[si]) eve_fnames = get_event_fnames(p, subj, run_indices[si]) for raw_fname, eve_fname in zip(raw_fnames, eve_fnames): with warnings.catch_warnings(record=True): raw = read_raw_fif(raw_fname, allow_maxshield='yes') events = find_events(raw, stim_channel='STI101', shortest_event=1) write_events(eve_fname, events)
def save_event_file(subject, data_folder): """ Parameters ---------- subject : subject name data_folder : string Returns ------- """ raw = mne.io.Raw(data_folder + "%s_bp_ica-raw.fif" % subject, preload=True) events = mne.find_events(raw) mne.write_events(data_folder + "%s-eve.fif" % subject, events)
def test_io_events(): """Test IO for events """ events = read_events(fname) write_events("events.fif", events) events2 = read_events("events.fif") assert_array_almost_equal(events, events2) a = read_events("events.fif", include=1) b = read_events("events.fif", include=[1]) c = read_events("events.fif", exclude=[2, 3, 4, 5, 32]) d = read_events("events.fif", include=1, exclude=[2, 3]) assert_array_equal(a, b) assert_array_equal(a, c) assert_array_equal(a, d)
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 score(p, subjects, run_indices): # run_indices = [r for ri, r in enumerate(run_indices) if ri in p.subject_indices] for subj, ri in zip(subjects, run_indices): if ri is None: runs = p.run_names else: runs = ['%s_%d' % (subj, r + 1) for r in ri] print('Running subject %s...' % subj) # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) raw_names = get_raw_fnames(p, subj, 'raw', True, False, ri) # assert len(raw_names) == 8 out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for rii, fname in zip(runs, raw_names): print(' Run %s...' % op.basename(fname)) # sjjoo_20160810: To incorporate session that has incomplete data # if not op.isfile(fname): # raise RuntimeError(' File %s not found' % fname) if op.isfile(fname): # read events, make sure we got the correct number of trials raw = mne.io.Raw(fname, allow_maxshield=True) ev = mne.find_events(raw, stim_channel='STI101', shortest_event=1) if op.basename(fname).__contains__('_1_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_2_raw'): ev[:, 2] += 200 elif op.basename(fname).__contains__('_3_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_4_raw'): ev[:, 2] += 200 elif op.basename(fname).__contains__('_5_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_6_raw'): ev[:, 2] += 200 elif op.basename(fname).__contains__('_7_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_8_raw'): ev[:, 2] += 200 fname_out = 'ALL_' + safe_inserter(rii, subj) + '-eve.lst' fname_out = op.join(out_dir, fname_out) mne.write_events(fname_out, ev)
def default_score(p, subjects, run_indices): """Default scoring function that just passes event numbers through""" for si, subj in enumerate(subjects): print(' Scoring subject %s... ' % subj) # Figure out what our filenames should be raw_fnames = get_raw_fnames(p, subj, 'raw', False, False, run_indices[si]) eve_fnames = get_event_fnames(p, subj, run_indices[si]) for raw_fname, eve_fname in zip(raw_fnames, eve_fnames): with warnings.catch_warnings(record=True): raw = mne.io.read_raw_fif(raw_fname, allow_maxshield=True) events = mne.find_events(raw, stim_channel='STI101', shortest_event=1) mne.write_events(eve_fname, events)
def score(p, subjects, run_indices): # run_indices = [r for ri, r in enumerate(run_indices) if ri in p.subject_indices] for subj, ri in zip(subjects, run_indices): if ri is None: runs = p.run_names else: runs = ['%s_%d' % (subj, r+1) for r in ri] print('Running subject %s...' % subj) # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) raw_names = get_raw_fnames(p, subj, 'raw', True, False, ri) # assert len(raw_names) == 8 out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for rii, fname in zip(runs, raw_names): print(' Run %s...' % op.basename(fname)) # sjjoo_20160810: To incorporate session that has incomplete data # if not op.isfile(fname): # raise RuntimeError(' File %s not found' % fname) if op.isfile(fname): # read events, make sure we got the correct number of trials raw = mne.io.Raw(fname, allow_maxshield=True) ev = mne.find_events(raw, stim_channel='STI101', shortest_event=1) if op.basename(fname).__contains__('_1_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_2_raw'): ev[:, 2] += 200 elif op.basename(fname).__contains__('_3_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_4_raw'): ev[:, 2] += 200 elif op.basename(fname).__contains__('_5_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_6_raw'): ev[:, 2] += 200 elif op.basename(fname).__contains__('_7_raw'): ev[:, 2] += 100 elif op.basename(fname).__contains__('_8_raw'): ev[:, 2] += 200 fname_out = 'ALL_' + safe_inserter(rii, subj) + '-eve.lst' fname_out = op.join(out_dir, fname_out) mne.write_events(fname_out, ev)
def run_events(subject_id, data_path, filter_path): print('processing subject: {}'.format(subject_id)) in_path = os.path.join(data_path, 'sub-{:02}'.format(subject_id), 'ses-meg', 'meg') filter_path = os.path.join(filter_path, 'sub-{:02}'.format(subject_id), 'ses-meg', 'meg') os.makedirs(filter_path, exist_ok=True) for run in range(1, 7): run_fname = os.path.join(in_path, 'sub-{:02}_ses-meg_task-facerecognition_run-{:02}_meg.fif'.format(subject_id, run)) raw = mne.io.read_raw_fif(run_fname) mask = 4096 + 256 # mask for excluding high order bits events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='not_and', min_duration=0.003) print(' S {} - R {}'.format(subject_id, run)) fname_events = os.path.join(filter_path, 'run_{:02}-eve.fif'.format(run)) mne.write_events(fname_events, events)
def test_io_set_epochs_events(tmpdir): """Test different combinations of events and event_ids.""" tmpdir = str(tmpdir) out_fname = op.join(tmpdir, 'test-eve.fif') events = np.array([[4, 0, 1], [12, 0, 2], [20, 0, 3], [26, 0, 3]]) write_events(out_fname, events) event_id = {'S255/S8': 1, 'S8': 2, 'S255/S9': 3} out_fname = op.join(tmpdir, 'test-eve.fif') epochs = read_epochs_eeglab(epochs_fname_mat, events, event_id) assert_equal(len(epochs.events), 4) assert epochs.preload assert epochs._bad_dropped epochs = read_epochs_eeglab(epochs_fname_mat, out_fname, event_id) pytest.raises(ValueError, read_epochs_eeglab, epochs_fname_mat, None, event_id) pytest.raises(ValueError, read_epochs_eeglab, epochs_fname_mat, epochs.events, None)
def score(p, subjects): """Scoring function""" for subj in subjects: print(' Running subject %s... ' % subj, end='') # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for run_name in p.run_names: print(subj) fname = op.join(p.work_dir, subj, p.raw_dir, (run_name % subj) + p.raw_fif_tag) events, _ = extract_expyfun_events(fname)[:2] events[:, 2] += 100 fname_out = op.join(out_dir, 'ALL_' + (run_name % subj) + '-eve.lst') mne.write_events(fname_out, events)
def score(p, subjects, run_indices): # run_indices = [r for ri, r in enumerate(run_indices) if ri in p.subject_indices] for subj, ri in zip(subjects, run_indices): if ri is None: runs = p.run_names else: runs = ["%s_%d" % (subj, r + 1) for r in ri] print("Running subject %s..." % subj) # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) raw_names = get_raw_fnames(p, subj, "raw", True, False, ri) # assert len(raw_names) == 8 out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for rii, fname in zip(runs, raw_names): print(" Run %s..." % op.basename(fname)) if not op.isfile(fname): raise RuntimeError(" File %s not found" % fname) # read events, make sure we got the correct number of trials raw = mne.io.Raw(fname, allow_maxshield=True) ev = mne.find_events(raw, stim_channel="STI101", shortest_event=1) if op.basename(fname).__contains__("_1_raw"): ev[:, 2] += 100 elif op.basename(fname).__contains__("_2_raw"): ev[:, 2] += 200 elif op.basename(fname).__contains__("_3_raw"): ev[:, 2] += 100 elif op.basename(fname).__contains__("_4_raw"): ev[:, 2] += 200 elif op.basename(fname).__contains__("_5_raw"): ev[:, 2] += 100 elif op.basename(fname).__contains__("_6_raw"): ev[:, 2] += 200 elif op.basename(fname).__contains__("_7_raw"): ev[:, 2] += 100 elif op.basename(fname).__contains__("_8_raw"): ev[:, 2] += 200 fname_out = "ALL_" + safe_inserter(rii, subj) + "-eve.lst" fname_out = op.join(out_dir, fname_out) mne.write_events(fname_out, ev)
def compute_proj_ecg(in_path, in_fif_fname, h_tmin, h_tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, filter_length, n_jobs, ch_name, reject, avg_ref): # Reading fif File raw_in = fiff.Raw(in_fif_fname) prefix = in_fif_fname[:-8] # prefix = 'sc3_BaleenLPRun4' # print prefix in_fif_fname = in_path + in_fif_fname print in_fif_fname out_path = os.path.join(in_path + 'ssp/') out_fif_fname = in_path + 'ssp/' + prefix + '_clean_ecg_raw.fif' ecg_proj_fname = in_path + prefix + '_ecg_proj.fif' ecg_event_fname = in_path + 'ssp/' + prefix + '_ecg-eve.fif' flag = 0 print 'Implementing ECG artifact rejection on data' ecg_events, _, _ = mne.artifacts.find_ecg_events(raw_in) if not len(ecg_events) < 30: # print ecg_event_fname print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) # Making projector print 'Computing ECG projector' print out_path #os.chdir('/cluster/kuperberg/SemPrMM/MEG/data/ac1/ssp') #os.getcwd() command = ( 'mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin -0.2 --projtmax 0.2 --saveprojtag _ecg_proj ' '--projnmag 1 --projngrad 1 --projneeg 0 --projevent 999 --highpass 5 ' '--lowpass 35 --projmagrej 8000 --projgradrej 5000 --projeegrej 600' % (in_path, in_fif_fname, ecg_event_fname) ) ##6/1/13 CU:projectors fixed 1 1 0 st = os.system(command) if st != 0: print "Error while running : %s" % command return in_fif_fname, ecg_proj_fname, len(ecg_event_fname), out_fif_fname
def run_events_concatenate(subject_id): subject = "sub%03d" % subject_id print("processing subject: %s" % subject) in_path = op.join(data_path, subject, 'MEG') out_path = in_path raw_list = list() events_list = list() print(" Loading raw data") for run in range(1, 7): run_fname = op.join(ica_dir % (run, subject), 'ica', 'run_%02d_sss_filt_ica.fif' % run) print(run_fname) raw = mne.io.read_raw_fif(run_fname, preload=False) mask = 4096 + 256 # mask for excluding high order bits events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='not_and', min_duration=0.003) print(" S %s - R %s" % (subject, run)) fname_events = op.join(out_path, 'run_%02d-eve.fif' % run) mne.write_events(fname_events, events) delay = int(round(0.0345 * raw.info['sfreq'])) events[:, 0] = events[:, 0] + delay events_list.append(events) raw_list.append(raw) raw, events = mne.concatenate_raws(raw_list, events_list=events_list) raw.set_eeg_reference(projection=True) raw.save(op.join(out_path, '{}_sss_filt_ica-raw.fif'.format(subject)), overwrite=True) mne.write_events( op.join(out_path, '{}_sss_filt_ica-raw-eve.fif'.format(subject)), events) del raw_list del raw
def run_events(subject_id): subject = "sub%03d" % subject_id print("processing subject: %s" % subject) data_path = op.join(meg_dir, subject) for run in range(1, 7): run_fname = op.join(data_path, 'run_%02d_filt_sss_raw.fif' % run) if not os.path.exists(run_fname): continue raw = mne.io.Raw(run_fname, add_eeg_ref=False) mask = 4096 + 256 # mask for excluding high order bits events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='not_and', min_duration=0.003, verbose=True) print("S %s - R %s" % (subject, run)) fname_events = op.join(data_path, 'run_%02d_filt_sss-eve.fif' % run) mne.write_events(fname_events, events)
def run_events(subject_id): subject = "sub%03d" % subject_id print("processing subject: %s" % subject) in_path = op.join(study_path, 'ds117', subject, 'MEG') out_path = op.join(meg_dir, subject) for run in range(1, 7): run_fname = op.join(in_path, 'run_%02d_raw.fif' % (run, )) raw = mne.io.read_raw_fif(run_fname) mask = 4096 + 256 # mask for excluding high order bits events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='not_and', min_duration=0.003) print(" S %s - R %s" % (subject, run)) fname_events = op.join(out_path, 'run_%02d-eve.fif' % run) mne.write_events(fname_events, events)
def default_score(p, subjects): """Default scoring function that just passes event numbers through""" for subj in subjects: print(' Scoring subject %s... ' % subj) # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for run_name in p.run_names: fname = op.join(p.work_dir, subj, p.raw_dir, (run_name % subj) + p.raw_fif_tag) with warnings.catch_warnings(record=True): raw = mne.io.read_raw_fif(fname, allow_maxshield=True) events = mne.find_events(raw, stim_channel='STI101', shortest_event=1) fname_out = op.join(out_dir, 'ALL_' + (run_name % subj) + '-eve.lst') mne.write_events(fname_out, events)
def compute_proj_ecg(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, filter_length, n_jobs, ch_name, reject, avg_ref, bads): # Reading fif File raw_in = fiff.Raw(in_fif_fname) if in_fif_fname.endswith('_raw.fif') or in_fif_fname.endswith('-raw.fif'): prefix = in_fif_fname[:-8] else: prefix = in_fif_fname[:-4] print in_fif_fname if out_fif_fname is None: out_fif_fname = prefix + '_clean_ecg_raw.fif' if ecg_proj_fname is None: ecg_proj_fname = prefix + '_ecg_proj.fif' if ecg_event_fname is None: ecg_event_fname = prefix + '_ecg-eve.fif' flag = 0 print 'Implementing ECG artifact rejection on data' ecg_events, _, _ = mne.artifacts.find_ecg_events(raw_in) if not len(ecg_events) < 30: print ecg_event_fname print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) # Making projector print 'Computing ECG projector' command = ( 'mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin tmin --projtmax tmax --saveprojtag _ecg_proj ' '--projnmag %d --projngrad 1 --projneeg 1 --projevent 999 --highpass h_freq ' '--lowpass l_freq --projmagrej 4000 --projgradrej 3000 --projeegrej 500' % (in_path, in_fif_fname, ecg_event_fname, n_mag)) st = os.system(command) if st != 0: print "Error while running : %s" % command
def score(p, subjects): """Scoring function""" for subj in subjects: print(' Running subject %s... ' % subj, end='') # Figure out what our filenames should be out_dir = op.join(p.work_dir, subj, p.list_dir) if not op.isdir(out_dir): os.mkdir(out_dir) for run_name in p.run_names: print(subj) fname = op.join(p.work_dir, subj, p.raw_dir, (run_name % subj) + p.raw_fif_tag) # events = mne.find_events(mne.io.Raw(fname, allow_maxshield='True'), stim_channel='STI101') #events = events[events[:, 2] == 1] events, _ = extract_expyfun_events(fname)[:2] events[:, 2] += 10 fname_out = op.join(out_dir, 'ALL_' + (run_name % subj) + '-eve.lst') mne.write_events(fname_out, events)
def make_events_files(sub_id, session): """ This function read in a fiff file and write out a event file """ # SETUP AND LOAD FILES #### # name with subject id & session fname = "sub_%d_%s" % (sub_id, session) # load the raw fif print '\nLoading raw file' raw = fiff.Raw(fname + "_tsss_mc_autobad.fif", preload=False) # EPOCHS #### events = mne.find_events(raw, stim_channel="STI101") events_classic = [] events_interupt = [] for i in range(len(events)): if i > 0: if events[i, 2] == 1 and events[i - 1, 2] == 1: events_classic.append(i) elif events[i, 2] == 1 and events[i - 1, 2] == 2: events_interupt.append(i) if len(events_classic) > 0: outname = "sub_%d_%s.eve" % (sub_id, session) mne.write_events(outname, events[events_classic]) if len(events_interupt) is not 0: outname_classic = "sub_%d_%s_%s.eve" % (sub_id, session, "classic") outname_interrupt = "sub_%d_%s_%s.eve" % (sub_id, session, "interrupt") mne.write_events(outname_classic, events[events_classic]) mne.write_events(outname_interrupt, events[events_interupt])
def compute_proj_ecg(in_path, in_fif_fname, h_tmin, h_tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, filter_length, n_jobs, ch_name, reject, avg_ref): # Reading fif File raw = io.Raw(in_fif_fname) prefix = in_fif_fname[:-8] # prefix = 'sc3_BaleenLPRun4' print prefix in_fif_fname = in_path + in_fif_fname print in_fif_fname out_path = os.path.join(in_path + 'ssp/mne/') out_fif_fname = in_path + 'ssp/mne/' + prefix + '_clean_ecg_raw.fif' ecg_proj_fname = in_path + prefix + '_ecg_proj.fif' ecg_event_fname = in_path + 'ssp/mne/' + prefix + '_ecg-eve.fif' flag = 0 event_id = 999 print 'Implementing ECG artifact rejection on data' ecg_events, _, _ = mne.preprocessing.find_ecg_events(raw,event_id, ch_name=None) print ecg_events if not len(ecg_events) < 30: # print ecg_event_fname print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) # Making projector print 'Computing ECG projector' print out_path #os.chdir('/cluster/kuperberg/SemPrMM/MEG/data/ac1/ssp') #os.getcwd() command = ('mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin %s --projtmax %s --saveprojtag _ecg_proj ' '--projnmag %s --projngrad %s --projevent 999 --highpass 5 ' '--lowpass 35 --projmagrej 8000 --projgradrej 5000' % (in_path, in_fif_fname, ecg_event_fname, h_tmin, h_tmax, n_mag, n_grad)) ##6/1/13 CU:projectors fixed 1 1 0 print command st = os.system(command) if st != 0: print "Error while running : %s" % command return in_fif_fname, ecg_proj_fname, len(ecg_event_fname), out_fif_fname
def run_events(subject): print("processing subject: %s" % subject) meg_subject_dir = op.join(config.meg_dir, subject) for run in config.runs: extension = run + '_sss_raw' raw_fname_in = op.join(meg_subject_dir, config.base_fname.format(**locals())) eve_fname_out = op.splitext(raw_fname_in)[0] + '-eve.fif' raw = mne.io.read_raw_fif(raw_fname_in) events = mne.find_events(raw, stim_channel=config.stim_channel) print("Input: ", raw_fname_in) print("Output: ", eve_fname_out) mne.write_events(eve_fname_out, events) if config.plot: # plot events figure = mne.viz.plot_events(events) figure.show()
def run_events(subject): print("Processing subject: %s" % subject) meg_subject_dir = op.join(config.meg_dir, subject) for run in config.runs: if config.use_maxwell_filter: extension = run + '_sss_raw' else: extension = run + '_filt_raw' raw_fname_in = op.join(meg_subject_dir, config.base_fname.format(**locals())) eve_fname_out = op.splitext(raw_fname_in)[0] + '-eve.fif' raw = mne.io.read_raw_fif(raw_fname_in) events = mne.find_events(raw, stim_channel=config.stim_channel, consecutive=True, min_duration=config.min_event_duration, shortest_event=config.shortest_event) if config.trigger_time_shift: events = mne.event.shift_time_events(events, np.unique(events[:, 2]), config.trigger_time_shift, raw.info['sfreq']) print("Input: ", raw_fname_in) print("Output: ", eve_fname_out) mne.write_events(eve_fname_out, events) if config.plot: # plot events figure = mne.viz.plot_events(events, sfreq=raw.info['sfreq'], first_samp=raw.first_samp) figure.show()
def compute_proj_eog(in_path, in_fif_fname, e_tmin, e_tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, filter_length, n_jobs, ch_name, reject, avg_ref): # Reading fif File raw = io.Raw(in_fif_fname) prefix = in_fif_fname[:-8] # prefix = 'sc3_BaleenLPRun4' print prefix in_fif_fname = in_path + in_fif_fname print in_fif_fname out_path = os.path.join(in_path + 'ssp/mne/') out_fif_fname = in_path + 'ssp/mne/' + prefix + '_clean_eog_raw.fif' eog_proj_fname = in_path + prefix + '_eog_proj.fif' eog_event_fname = in_path + 'ssp/mne/' + prefix + '_eog-eve.fif' flag=0 event_id = 998 print 'Finding EOG events from the data...' eog_events= mne.preprocessing.find_eog_events(raw,event_id, ch_name = 'EOG061') if not len(eog_events)<20: print eog_event_fname print "Writing EOG events in %s" % eog_event_fname mne.write_events(eog_event_fname, eog_events) print eog_proj_fname print "Computing the EOG projector" command = ('mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin %s --projtmax %s --saveprojtag _eog_proj ' '--projnmag %s --projngrad %s --projevent 998 --highpass 0.3 ' '--lowpass 35 --filtersize 8192 --projmagrej 8000 --projgradrej 7000' ###filtersize 8192, projeegrej was 500 by default!!! % (in_path, in_fif_fname, eog_event_fname, e_tmin, e_tmax, n_mag, n_grad)) #e_tmin, e_tmax, #1/15/13 CU: refer the speadsheet(projectors custom made for each subject) --projmagrej 5500 --projgradrej 3000 --projeegrej 900 print command st = os.system(command) if st != 0: print "Error while running : %s" % command return in_fif_fname, eog_proj_fname, len(eog_event_fname), out_fif_fname
def compute_proj_ecg(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, filter_length, n_jobs, ch_name, reject, avg_ref, bads): # Reading fif File raw_in = fiff.Raw(in_fif_fname) if in_fif_fname.endswith('_raw.fif') or in_fif_fname.endswith('-raw.fif'): prefix = in_fif_fname[:-8] else: prefix = in_fif_fname[:-4] print in_fif_fname if out_fif_fname is None: out_fif_fname = prefix + '_clean_ecg_raw.fif' if ecg_proj_fname is None: ecg_proj_fname = prefix + '_ecg_proj.fif' if ecg_event_fname is None: ecg_event_fname = prefix + '_ecg-eve.fif' flag = 0 print 'Implementing ECG artifact rejection on data' ecg_events, _, _ = mne.artifacts.find_ecg_events(raw_in) if not len(ecg_events) < 30: print ecg_event_fname print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) # Making projector print 'Computing ECG projector' command = ('mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin tmin --projtmax tmax --saveprojtag _ecg_proj ' '--projnmag %d --projngrad 1 --projneeg 1 --projevent 999 --highpass h_freq ' '--lowpass l_freq --projmagrej 4000 --projgradrej 3000 --projeegrej 500' % (in_path, in_fif_fname, ecg_event_fname, n_mag)) st = os.system(command) if st != 0: print "Error while running : %s" % command
def compute_proj_ecg(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, filter_length, n_jobs, ch_name, reject, avg_ref): # Reading fif File raw_in = fiff.Raw(in_fif_fname) prefix = in_fif_fname[:-8] print prefix in_fif_fname = in_path + in_fif_fname print in_fif_fname out_path = os.path.join(in_path + 'ssp/') out_fif_fname = in_path + 'ssp/' + prefix + '_clean_ecg_raw.fif' ecg_proj_fname = in_path + prefix + '_ecg_proj.fif' ecg_event_fname = in_path + 'ssp/' + prefix + '_ecg-eve.fif' flag = 0 print 'Implementing ECG artifact rejection on data' ecg_events, _, _ = mne.artifacts.find_ecg_events(raw_in) if not len(ecg_events) < 30: print ecg_event_fname print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) # Making projector print 'Computing ECG projector' print out_path #os.chdir('/cluster/kuperberg/SemPrMM/MEG/data/ac1/ssp') #os.getcwd() command = ('mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin %s --projtmax %s --saveprojtag _ecg_proj ' '--projnmag %s --projngrad %s --projneeg 0 --projevent 999 --highpass 5 ' '--lowpass 35 --projmagrej 4000 --projgradrej 3000 --projeegrej 250 ' % (in_path, in_fif_fname, ecg_event_fname, tmin, tmax, n_mag, n_grad)) ##10/1/12 CU after changing the number of projectors for ECG(mag1, grad1, eeg0) st = os.system(command) if st != 0: print "Error while running : %s" % command ## return in_fif_fname, ecg_proj_fname, len(ecg_event_fname), out_fif_fname
def test_io_events(): """Test IO for events.""" tempdir = _TempDir() # Test binary fif IO events = read_events(fname) # Use as the gold standard write_events(op.join(tempdir, 'events-eve.fif'), events) events2 = read_events(op.join(tempdir, 'events-eve.fif')) assert_array_almost_equal(events, events2) # Test binary fif.gz IO events2 = read_events(fname_gz) # Use as the gold standard assert_array_almost_equal(events, events2) write_events(op.join(tempdir, 'events-eve.fif.gz'), events2) events2 = read_events(op.join(tempdir, 'events-eve.fif.gz')) assert_array_almost_equal(events, events2) # Test new format text file IO write_events(op.join(tempdir, 'events.eve'), events) events2 = read_events(op.join(tempdir, 'events.eve')) assert_array_almost_equal(events, events2) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') events2 = read_events(fname_txt_mpr, mask=0, mask_type='not_and') assert_true(sum('first row of' in str(ww.message) for ww in w) == 1) assert_array_almost_equal(events, events2) # Test old format text file IO events2 = read_events(fname_old_txt) assert_array_almost_equal(events, events2) write_events(op.join(tempdir, 'events.eve'), events) events2 = read_events(op.join(tempdir, 'events.eve')) assert_array_almost_equal(events, events2) # Test event selection a = read_events(op.join(tempdir, 'events-eve.fif'), include=1) b = read_events(op.join(tempdir, 'events-eve.fif'), include=[1]) c = read_events(op.join(tempdir, 'events-eve.fif'), exclude=[2, 3, 4, 5, 32]) d = read_events(op.join(tempdir, 'events-eve.fif'), include=1, exclude=[2, 3]) assert_array_equal(a, b) assert_array_equal(a, c) assert_array_equal(a, d) # test reading file with mask=None events2 = events.copy() events2[:, -1] = range(events2.shape[0]) write_events(op.join(tempdir, 'events-eve.fif'), events2) events3 = read_events(op.join(tempdir, 'events-eve.fif'), mask=None) assert_array_almost_equal(events2, events3) # Test binary file IO for 1 event events = read_events(fname_1) # Use as the new gold standard write_events(op.join(tempdir, 'events-eve.fif'), events) events2 = read_events(op.join(tempdir, 'events-eve.fif')) assert_array_almost_equal(events, events2) # Test text file IO for 1 event write_events(op.join(tempdir, 'events.eve'), events) events2 = read_events(op.join(tempdir, 'events.eve')) assert_array_almost_equal(events, events2) # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') fname2 = op.join(tempdir, 'test-bad-name.fif') write_events(fname2, events) read_events(fname2) assert_naming(w, 'test_event.py', 2)
def run(): """Run command.""" from mne.commands.utils import get_optparser parser = get_optparser(__file__) parser.add_option("-i", "--in", dest="raw_in", help="Input raw FIF file", metavar="FILE") parser.add_option("--tmin", dest="tmin", type="float", help="Time before event in seconds", default=-0.2) parser.add_option("--tmax", dest="tmax", type="float", help="Time after event in seconds", default=0.2) parser.add_option("-g", "--n-grad", dest="n_grad", type="int", help="Number of SSP vectors for gradiometers", default=2) parser.add_option("-m", "--n-mag", dest="n_mag", type="int", help="Number of SSP vectors for magnetometers", default=2) parser.add_option("-e", "--n-eeg", dest="n_eeg", type="int", help="Number of SSP vectors for EEG", default=2) parser.add_option("--l-freq", dest="l_freq", type="float", help="Filter low cut-off frequency in Hz", default=1) parser.add_option("--h-freq", dest="h_freq", type="float", help="Filter high cut-off frequency in Hz", default=35) parser.add_option("--eog-l-freq", dest="eog_l_freq", type="float", help="Filter low cut-off frequency in Hz used for " "EOG event detection", default=1) parser.add_option("--eog-h-freq", dest="eog_h_freq", type="float", help="Filter high cut-off frequency in Hz used for " "EOG event detection", default=10) parser.add_option("-p", "--preload", dest="preload", help="Temporary file used during computation (to " "save memory)", default=True) parser.add_option("-a", "--average", dest="average", action="store_true", help="Compute SSP after averaging", default=False) # XXX: change to default=True in 0.17 parser.add_option("--proj", dest="proj", help="Use SSP projections from a fif file.", default=None) parser.add_option("--filtersize", dest="filter_length", type="int", help="Number of taps to use for filtering", default=2048) parser.add_option("-j", "--n-jobs", dest="n_jobs", type="int", help="Number of jobs to run in parallel", default=1) parser.add_option("--rej-grad", dest="rej_grad", type="float", help="Gradiometers rejection parameter in fT/cm (peak " "to peak amplitude)", default=2000) parser.add_option("--rej-mag", dest="rej_mag", type="float", help="Magnetometers rejection parameter in fT (peak to " "peak amplitude)", default=3000) parser.add_option("--rej-eeg", dest="rej_eeg", type="float", help="EEG rejection parameter in uV (peak to peak " "amplitude)", default=50) parser.add_option("--rej-eog", dest="rej_eog", type="float", help="EOG rejection parameter in uV (peak to peak " "amplitude)", default=1e9) parser.add_option("--avg-ref", dest="avg_ref", action="store_true", help="Add EEG average reference proj", default=False) parser.add_option("--no-proj", dest="no_proj", action="store_true", help="Exclude the SSP projectors currently in the " "fiff file", default=False) parser.add_option("--bad", dest="bad_fname", help="Text file containing bad channels list " "(one per line)", default=None) parser.add_option("--event-id", dest="event_id", type="int", help="ID to use for events", default=998) parser.add_option("--event-raw", dest="raw_event_fname", help="raw file to use for event detection", default=None) parser.add_option("--tstart", dest="tstart", type="float", help="Start artifact detection after tstart seconds", default=0.) parser.add_option("-c", "--channel", dest="ch_name", type="string", help="Custom EOG channel(s), comma separated", default=None) options, args = parser.parse_args() raw_in = options.raw_in if raw_in is None: parser.print_help() sys.exit(1) tmin = options.tmin tmax = options.tmax n_grad = options.n_grad n_mag = options.n_mag n_eeg = options.n_eeg l_freq = options.l_freq h_freq = options.h_freq eog_l_freq = options.eog_l_freq eog_h_freq = options.eog_h_freq average = options.average preload = options.preload filter_length = options.filter_length n_jobs = options.n_jobs reject = dict(grad=1e-13 * float(options.rej_grad), mag=1e-15 * float(options.rej_mag), eeg=1e-6 * float(options.rej_eeg), eog=1e-6 * float(options.rej_eog)) avg_ref = options.avg_ref no_proj = options.no_proj bad_fname = options.bad_fname event_id = options.event_id proj_fname = options.proj raw_event_fname = options.raw_event_fname tstart = options.tstart ch_name = options.ch_name if bad_fname is not None: with open(bad_fname, 'r') as fid: bads = [w.rstrip() for w in fid.readlines()] print('Bad channels read : %s' % bads) else: bads = [] if raw_in.endswith('_raw.fif') or raw_in.endswith('-raw.fif'): prefix = raw_in[:-8] else: prefix = raw_in[:-4] eog_event_fname = prefix + '_eog-eve.fif' if average: eog_proj_fname = prefix + '_eog_avg-proj.fif' else: eog_proj_fname = prefix + '_eog-proj.fif' raw = mne.io.read_raw_fif(raw_in, preload=preload) if raw_event_fname is not None: raw_event = mne.io.read_raw_fif(raw_event_fname) else: raw_event = raw flat = None # XXX : not exposed to the user projs, events = mne.preprocessing.compute_proj_eog( raw=raw, raw_event=raw_event, tmin=tmin, tmax=tmax, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg, l_freq=l_freq, h_freq=h_freq, average=average, filter_length=filter_length, n_jobs=n_jobs, reject=reject, flat=flat, bads=bads, avg_ref=avg_ref, no_proj=no_proj, event_id=event_id, eog_l_freq=eog_l_freq, eog_h_freq=eog_h_freq, tstart=tstart, ch_name=ch_name, copy=False) raw.close() if raw_event_fname is not None: raw_event.close() if proj_fname is not None: print('Including SSP projections from : %s' % proj_fname) # append the eog projs, so they are last in the list projs = mne.read_proj(proj_fname) + projs if isinstance(preload, string_types) and os.path.exists(preload): os.remove(preload) print("Writing EOG projections in %s" % eog_proj_fname) mne.write_proj(eog_proj_fname, projs) print("Writing EOG events in %s" % eog_event_fname) mne.write_events(eog_event_fname, events)
import mne from mne.datasets import sample print(__doc__) data_path = sample.data_path() fname = data_path + '/MEG/sample/sample_audvis_raw.fif' # Reading events raw = mne.io.read_raw_fif(fname) events = mne.find_events(raw, stim_channel='STI 014') # Writing events mne.write_events('sample_audvis_raw-eve.fif', events) for ind, before, after in events[:5]: print("At sample %d stim channel went from %d to %d" % (ind, before, after)) # Plot the events to get an idea of the paradigm # Specify colors and an event_id dictionary for the legend. event_id = { 'aud_l': 1, 'aud_r': 2, 'vis_l': 3, 'vis_r': 4, 'smiley': 5, 'button': 32 }
events_out_path = op.join(meg_subj_path, "{}-eve.fif".format(str(ix).zfill(3))) ica_out_path = op.join(meg_subj_path, "{}-ica.fif".format(str(ix).zfill(3))) n_components = 50 method = "fastica" reject = dict(mag=4e-12) ica = ICA(n_components=n_components, method=method) ica.fit(raw, picks=picks_meg, reject=reject, verbose=verb) print(subj, ix, "ICA_fit") raw.save(raw_out_path, overwrite=True) mne.write_events(events_out_path, events) ica.save(ica_out_path) print(subj, ix, "saved") if pipeline_params["apply_ICA"]: ica_json = files.get_files(meg_subj_path, "", "ica-rej.json")[2][0] raw_files = files.get_files(meg_subj_path, "raw", "-raw.fif", wp=False)[2] comp_ICA_json_path = op.join(meg_subj_path, "{}-ica-rej.json".format(str(subj).zfill(3))) ica_files = files.get_files(meg_subj_path, "", "-ica.fif", wp=False)[2] with open(ica_json) as data: components_rej = json.load(data)
def compute_proj_ecg(in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, preload, filter_length, n_jobs, ch_name, reject, avg_ref, bads): """Compute SSP/PCA projections for ECG artifacts Parameters ---------- in_fif_fname: string Raw fif File XXX """ # Reading fif File raw = mne.fiff.Raw(in_fif_fname, preload=preload) if in_fif_fname.endswith('_raw.fif') or in_fif_fname.endswith('-raw.fif'): prefix = in_fif_fname[:-8] else: prefix = in_fif_fname[:-4] ecg_event_fname = prefix + '_ecg-eve.fif' if average: ecg_proj_fname = prefix + '_ecg_avg_proj.fif' else: ecg_proj_fname = prefix + '_ecg_proj.fif' print 'Running ECG SSP computation' ecg_events, _, _ = mne.artifacts.find_ecg_events(raw, ch_name=ch_name) print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) if avg_ref: print "Adding average EEG reference projection." eeg_proj = mne.fiff.proj.make_eeg_average_ref_proj(raw.info) raw.info['projs'].append(eeg_proj) print 'Computing ECG projector' # Handler rejection parameters if len(mne.fiff.pick_types(raw.info, meg='grad', eeg=False, eog=False)) == 0: del reject['grad'] if len(mne.fiff.pick_types(raw.info, meg='mag', eeg=False, eog=False)) == 0: del reject['mag'] if len(mne.fiff.pick_types(raw.info, meg=False, eeg=True, eog=False)) == 0: del reject['eeg'] if len(mne.fiff.pick_types(raw.info, meg=False, eeg=False, eog=True)) == 0: del reject['eog'] picks = mne.fiff.pick_types(raw.info, meg=True, eeg=True, eog=True, exclude=raw.info['bads'] + bads) if l_freq is None and h_freq is not None: raw.high_pass_filter(picks, h_freq, filter_length, n_jobs) if l_freq is not None and h_freq is None: raw.low_pass_filter(picks, h_freq, filter_length, n_jobs) if l_freq is not None and h_freq is not None: raw.band_pass_filter(picks, l_freq, h_freq, filter_length, n_jobs) epochs = mne.Epochs(raw, ecg_events, None, tmin, tmax, baseline=None, picks=picks, reject=reject, proj=True) projs_init = raw.info['projs'] if average: evoked = epochs.average() projs = mne.compute_proj_evoked(evoked, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) else: projs = mne.compute_proj_epochs(epochs, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) if preload is not None and os.path.exists(preload): os.remove(preload) print "Writing ECG projections in %s" % ecg_proj_fname mne.write_proj(ecg_proj_fname, projs + projs_init) print 'Done.'
raw.filter(l_freq=1.0, h_freq=90, picks=np.arange(0, 306, 1)) # raw.apply_proj() fs = raw.info['sfreq'] removeblinks = True if removeblinks: # SSP for blinks blinks = find_blinks(raw, ch_name=[ 'EOG062', ], l_trans_bandwidth='auto') blinkname = (fpath + subj + '_' + para + '_blinks_erp' + ssstag + '-eve.fif') mne.write_events(blinkname, blinks) epochs_blinks = mne.Epochs(raw, blinks, 998, tmin=-0.25, tmax=0.25, proj=True, baseline=(-0.25, 0), reject=dict(grad=8000e-13, mag=8e-12)) blink_projs = compute_proj_epochs(epochs_blinks, n_grad=2, n_mag=2, n_eeg=0, verbose='DEBUG') raw.add_proj(blink_projs)
def make_ecr_events(raw_file, data_file, out_file, pattern=False): #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# ### Load behavioral file. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# data = read_csv(data_file) n_events, _ = data.shape n_trials, _ = data[ data.Condition != 0 ].shape # <--- Excludes rest trials. n_responses = (~np.isnan(data[data.Condition!=0].ResponseOnset)).sum() #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# ### Load raw file. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# raw = Raw(raw_file,preload=False,verbose=False) stim_onsets = find_events(raw, stim_channel='STI001', output='onset', verbose=False) response_onsets = find_events(raw, stim_channel='STI002', output='onset', verbose=False) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# ### Error catching. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# if n_events != stim_onsets.shape[0]: raise ValueError('Number of trial onsets in %s and %s do not match!' %(data_file,raw_file)) elif n_responses != response_onsets.shape[0]: raise ValueError('Number of responses in %s and %s do not match!' %(data_file,raw_file)) else: pass #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# ### Make events file. #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~# # Ammend Conflict and ResponseAccuracy Categories. data['Conflict'] = np.where( data.Conflict == 0, 1, data.Conflict ) # No Conflict: [0,1] --> 1 data['ResponseAccuracy'] = np.where( data.ResponseAccuracy == 0, 2, data.ResponseAccuracy ) # Accuracy: 0 --> 2 data['ResponseAccuracy'] = np.where( data.ResponseAccuracy ==99, 3, data.ResponseAccuracy ) # Accuracy:99 --> 3 # Append Word Valence category. data['WordValence'] = np.where(# Con + Angry Face = Angry Word (data.Condition == 1) & (data.Valence == 1), 1, np.where( # Con + Happy Face = Happy Word (data.Condition == 1) & (data.Valence == 2), 2, np.where( # Incon + Angry Face = Happy Word (data.Condition == 2) & (data.Valence == 1), 2, np.where( # Incon + Happy Face = Angry Word (data.Condition == 2) & (data.Valence == 2), 1, 99 )))) # Make unique identifiers. data['StimIDs'] = '1' + data.Condition.map(str) + data.Conflict.map(str) + data.Valence.map(str) +\ data.WordValence.map(str) + data.ResponseAccuracy.map(str) data['RespIDs'] = '2' + data.Condition.map(str) + data.Conflict.map(str) + data.Valence.map(str) +\ data.WordValence.map(str) + data.ResponseAccuracy.map(str) # Add identifiers to onset arrays. stim_onsets = stim_onsets[np.where(data.Condition == 0, False, True), :] stim_onsets[:,2] = data.StimIDs[data.Condition != 0].astype(int) response_onsets[:,2] = data.RespIDs[data.ResponseKey != 99].astype(int) # Merge and sort. events = np.concatenate([stim_onsets,response_onsets]) events = events[events[:,0].argsort(),:] # Reduce to pattern. if pattern: p = re.compile(pattern) idx, = np.where( [True if re.findall(p,event.astype(str)) else False for event in events[:,2]] ) events = events[idx,:] # Insert first sample. events = np.insert(events, 0, [raw.first_samp, 0, 0], 0) # Write to fif file. write_events(out_file, events) # Write to text file. if out_file.endswith('.fif'): out_file = out_file[:-4] + '.txt' else: out_file = out_file + '.txt' for n, p in enumerate(pattern): events[:,2] = np.where( [True if re.findall(p,event.astype(str)) else False for event in events[:,2]], n+1, events[:,2]) events = np.insert(events, 1, raw.index_as_time(events[:,0]), 1) np.savetxt(out_file, events, fmt = '%s', header = 'pattern = %s' %' | '.join(pattern))
############################################################################### # Plotting events # --------------- # # We can also plot events in order to visualize how events occur over the # course of our recording session. Below we'll plot our three event types # to see which ones were included. fig, axs = plt.subplots(1, 3, figsize=(15, 5)) mne.viz.plot_events(events_1, axes=axs[0], show=False) axs[0].set(title="restricted to event 1") mne.viz.plot_events(events_1_2, axes=axs[1], show=False) axs[1].set(title="restricted to event 1 or 2") mne.viz.plot_events(events_not_4_32, axes=axs[2], show=False) axs[2].set(title="keep all but 4 and 32") plt.setp([ax.get_xticklabels() for ax in axs], rotation=45) plt.tight_layout() plt.show() ############################################################################### # Writing events # -------------- # # Finally, we can write events to disk. Remember to use the naming convention # ``-eve.fif`` for your file. mne.write_events('example-eve.fif', events_1)
def clean_eog_ecg(in_fif_fname, out_fif_fname=None, ecg_proj_fname=None, eog_proj_fname=None, ecg_event_fname=None, eog_event_fname=None, in_path='.'): """Clean ECG from raw fif file Parameters ---------- in_fif_fname: string Raw fif File eog_event_fname: string name of EOG event file required. ecg_event_fname: string name of ECG event file required. in_path: Path where all the files are. """ # Reading fif File raw_in = fiff.Raw(in_fif_fname) if in_fif_fname.endswith('_raw.fif') or in_fif_fname.endswith('-raw.fif'): prefix = in_fif_fname[:-8] else: prefix = in_fif_fname[:-4] print in_fif_fname if out_fif_fname is None: out_fif_fname = prefix + '_clean_ecg_eog_raw.fif' if ecg_proj_fname is None: ecg_proj_fname = prefix + '_ecg_proj.fif' if eog_proj_fname is None: eog_proj_fname = prefix + '_eog_proj.fif' if ecg_event_fname is None: ecg_event_fname = prefix + '_ecg-eve.fif' if eog_event_fname is None: eog_event_fname = prefix + '_eog-eve.fif' flag = 0 print 'Implementing ECG and EOG artifact rejection on data' ecg_events, _, _ = mne.artifacts.find_ecg_events(raw_in) if not len(ecg_events) < 30: print ecg_event_fname print "Writing ECG events in %s" % ecg_event_fname mne.write_events(ecg_event_fname, ecg_events) # Making projector print 'Computing ECG projector' command = ('mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin -0.08 --projtmax 0.08 --saveprojtag _ecg_proj ' '--projnmag 1 --projngrad 1 --projneeg 1 --projevent 999 --highpass 5 ' '--lowpass 35 --projmagrej 4000 --projgradrej 3000 --projeegrej 500' % (in_path, in_fif_fname, ecg_event_fname)) st = os.system(command) if st != 0: print "Error while running : %s" % command #################################################### eog_events = mne.artifacts.find_eog_events(raw_in) if not len(eog_events) < 20: print "Writing EOG events in %s" % eog_event_fname mne.write_events(eog_event_fname, eog_events) print 'Computing EOG projector' command = ('mne_process_raw --cd %s --raw %s --events %s --makeproj ' '--projtmin -0.15 --projtmax 0.15 --saveprojtag _eog_proj ' '--projnmag 1 --projngrad 1 --projneeg 1 --projevent 998 --lowpass 35 ' '--projmagrej 4000 --projgradrej 3000 --projeegrej 500' % (in_path, in_fif_fname, eog_event_fname)) print 'Running : %s' % command st = os.system(command) if st != 0: raise ValueError('Problem while running : %s' % command) ################################################################# if (not len(ecg_events) < 30) and (not len(eog_events) < 20): # Applying the ECG EOG projector print 'Applying ECG EOG projector' command = ('mne_process_raw --cd %s --raw %s --proj %s --proj %s --proj %s ' '--projoff --save %s --filteroff' % (in_path, in_fif_fname, ecg_proj_fname, eog_proj_fname, in_fif_fname, out_fif_fname)) print 'Command executed: %s' % command st = os.system(command) if st != 0: raise ValueError('Pb while running : %s' % command) print ('Done removing ECG and EOG artifacts. ' 'IMPORTANT : Please eye-ball the data !!') print "ECG ",len(ecg_events), "EOG ", len(eog_events) elif (not len(eog_events) < 20): out_fif_fname = prefix + '_clean_eog_raw.fif' print 'Applying EOG projector' command = ('mne_process_raw --cd %s --raw %s --proj %s --proj %s ' '--projoff --save %s --filteroff' % (in_path, in_fif_fname, eog_proj_fname, in_fif_fname, out_fif_fname)) print 'Command executed: %s' % command st = os.system(command) if st != 0: raise ValueError('Pb while running : %s' % command) print ('Done removing EOG artifacts. ' 'IMPORTANT : Please eye-ball the data !!') print "EOG ", len(eog_events) elif (not len(ecg_events) < 30): out_fif_fname = prefix + '_clean_ecg_raw.fif' print 'Applying ECG projector' command = ('mne_process_raw --cd %s --raw %s --proj %s --proj %s ' '--projoff --save %s --filteroff' % (in_path, in_fif_fname, ecg_proj_fname, in_fif_fname, out_fif_fname)) print 'Command executed: %s' % command st = os.system(command) if st != 0: raise ValueError('Pb while running : %s' % command) print ('Done removing ECG artifacts. ' 'IMPORTANT : Please eye-ball the data !!') print "ECG ", len(ecg_events)
raw_event = raw flat = None # XXX : not exposed to the user projs, events = mne.preprocessing.compute_proj_eog(raw=raw, raw_event=raw_event, tmin=tmin, tmax=tmax, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg, l_freq=l_freq, h_freq=h_freq, average=average, filter_length=filter_length, n_jobs=n_jobs, reject=reject, flat=flat, bads=bads, avg_ref=avg_ref, no_proj=no_proj, event_id=event_id, eog_l_freq=eog_l_freq, eog_h_freq=eog_h_freq, tstart=tstart, ch_name=ch_name, copy=False) raw.close() if raw_event_fname is not None: raw_event.close() if proj_fname is not None: print('Including SSP projections from : %s' % proj_fname) # append the eog projs, so they are last in the list projs = mne.read_proj(proj_fname) + projs if isinstance(preload, string_types) and os.path.exists(preload): os.remove(preload) print("Writing EOG projections in %s" % eog_proj_fname) mne.write_proj(eog_proj_fname, projs) print("Writing EOG events in %s" % eog_event_fname) mne.write_events(eog_event_fname, events)
Find events from the stimulation/trigger channel in the raw data. The plot them to get an idea of the paradigm. """ # Author: Alexandre Gramfort <*****@*****.**> # # License: BSD (3-clause) print(__doc__) import mne from mne.datasets import sample from mne.io import Raw data_path = sample.data_path() fname = data_path + '/MEG/sample/sample_audvis_raw.fif' # Reading events raw = Raw(fname) events = mne.find_events(raw, stim_channel='STI 014') # Writing events mne.write_events('events.fif', events) for ind, before, after in events[:5]: print("At sample %d stim channel went from %d to %d" % (ind, before, after)) # Plot the events to get an idea of the paradigm mne.viz.plot_events(events, raw.info['sfreq'], raw.first_samp)
def clean_ecg_eog(in_fif_fname, out_fif_fname=None, eog=True, ecg=True, ecg_proj_fname=None, eog_proj_fname=None, ecg_event_fname=None, eog_event_fname=None, in_path='.', quiet=False): """Clean ECG from raw fif file Parameters ---------- in_fif_fname : str Raw fif File eog_event_fname : str name of EOG event file required. eog : bool Reject or not EOG artifacts. ecg : bool Reject or not ECG artifacts. ecg_event_fname : str name of ECG event file required. in_path : str Path where all the files are. """ if not eog and not ecg: raise Exception("EOG and ECG cannot be both disabled") # Reading fif File raw_in = mne.io.read_raw_fif(in_fif_fname) if in_fif_fname.endswith('_raw.fif') or in_fif_fname.endswith('-raw.fif'): prefix = in_fif_fname[:-8] else: prefix = in_fif_fname[:-4] if out_fif_fname is None: out_fif_fname = prefix + '_clean_ecg_eog_raw.fif' if ecg_proj_fname is None: ecg_proj_fname = prefix + '_ecg-proj.fif' if eog_proj_fname is None: eog_proj_fname = prefix + '_eog-proj.fif' if ecg_event_fname is None: ecg_event_fname = prefix + '_ecg-eve.fif' if eog_event_fname is None: eog_event_fname = prefix + '_eog-eve.fif' print('Implementing ECG and EOG artifact rejection on data') kwargs = dict() if quiet else dict(stdout=None, stderr=None) if ecg: ecg_events, _, _ = mne.preprocessing.find_ecg_events(raw_in) print("Writing ECG events in %s" % ecg_event_fname) mne.write_events(ecg_event_fname, ecg_events) print('Computing ECG projector') command = ('mne_process_raw', '--cd', in_path, '--raw', in_fif_fname, '--events', ecg_event_fname, '--makeproj', '--projtmin', '-0.08', '--projtmax', '0.08', '--saveprojtag', '_ecg-proj', '--projnmag', '2', '--projngrad', '1', '--projevent', '999', '--highpass', '5', '--lowpass', '35', '--projmagrej', '4000', '--projgradrej', '3000') mne.utils.run_subprocess(command, **kwargs) if eog: eog_events = mne.preprocessing.find_eog_events(raw_in) print("Writing EOG events in %s" % eog_event_fname) mne.write_events(eog_event_fname, eog_events) print('Computing EOG projector') command = ('mne_process_raw', '--cd', in_path, '--raw', in_fif_fname, '--events', eog_event_fname, '--makeproj', '--projtmin', '-0.15', '--projtmax', '0.15', '--saveprojtag', '_eog-proj', '--projnmag', '2', '--projngrad', '2', '--projevent', '998', '--lowpass', '35', '--projmagrej', '4000', '--projgradrej', '3000') mne.utils.run_subprocess(command, **kwargs) if out_fif_fname is not None: # Applying the ECG EOG projector print('Applying ECG EOG projector') command = ('mne_process_raw', '--cd', in_path, '--raw', in_fif_fname, '--proj', in_fif_fname, '--projoff', '--save', out_fif_fname, '--filteroff', '--proj', ecg_proj_fname, '--proj', eog_proj_fname) mne.utils.run_subprocess(command, **kwargs) print('Done removing artifacts.') print("Cleaned raw data saved in: %s" % out_fif_fname) print('IMPORTANT : Please eye-ball the data !!') else: print('Projection not applied to raw data.')