def writeList(filename, data): myFile = open(filename, "w") myFile.close() myFile = open(filename, "a") for row in data: myFile.write(str(row)) myFile.write("\n") myFile.close() make_lingua(filename)
def writeTable(filename, data): myFile = open(filename, "w") myFile.close() myFile = open(filename, "a") for row in data: for item in row: myFile.write(str(item)) myFile.write("\t") myFile.write("\n") myFile.close() make_lingua(filename)
def compute_proj_ecgeog(in_path, 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 average: ecg_proj_fname = in_path + 'ssp/' +prefix + '_ecg_avg_proj.fif' eog_proj_fname = in_path + 'ssp/' +prefix + '_eog_avg_proj.fif' out_fif_fname = in_path + 'ssp/' +prefix + '_ecgeog_avg_proj_raw.fif' else: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_proj.fif' eog_proj_fname = in_path + 'ssp/' + prefix + '_eog_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecgeog_proj_raw.fif' ##Reading raw file in_fif_fname = in_path + in_fif_fname raw = mne.fiff.Raw(in_fif_fname) #, preload=preload) print 'Applying ECG and 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, eog_proj_fname, ecg_proj_fname, in_fif_fname, out_fif_fname)) make_lingua(out_fif_fname) print 'Command executed: %s' % command print 'Running : %s' % command st = os.system(command) if st != 0: raise ValueError('Problem while running : %s' % command) else: print 'Done' print ('Computed ECG EOG Rejection fif file. Saved result as %s' % out_fif_fname)
def compute_proj_ecgeog(in_path, 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 average: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_avg_proj.fif' eog_proj_fname = in_path + 'ssp/' + prefix + '_eog_avg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecgeog_avg_proj_raw.fif' else: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_proj.fif' eog_proj_fname = in_path + 'ssp/' + prefix + '_eog_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecgeog_proj_raw.fif' ##Reading raw file in_fif_fname = in_path + in_fif_fname raw = mne.fiff.Raw(in_fif_fname) #, preload=preload) print 'Applying ECG and 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, eog_proj_fname, ecg_proj_fname, in_fif_fname, out_fif_fname)) make_lingua(out_fif_fname) print 'Command executed: %s' % command print 'Running : %s' % command st = os.system(command) if st != 0: raise ValueError('Problem while running : %s' % command) else: print 'Done' print('Computed ECG EOG Rejection fif file. Saved result as %s' % out_fif_fname)
def compute_proj_ecg(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, avg_ref, bads, preload, flat): """Compute SSP/PCA projections for ECG/EOG artifacts Parameters ---------- in_fif_fname: string Raw fif File XXX """ 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 = in_path + 'ssp/' + prefix + '_ecg-eve.fif' if average: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_avg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecg_avg_proj_raw.fif' else: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecg_proj_raw.fif' print 'Reading fif File' in_fif_fname = in_path + in_fif_fname raw = mne.fiff.Raw(in_fif_fname, preload=preload) # if (in_path == '/cluster/kuperberg/SemPrMM/MEG/data/ya30/'): # save after 84s of MEG data in FIF file # raw = raw[: , 84:] #raw.save('sample_audvis_meg_raw.fif', tmin=84) 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) make_lingua(ecg_event_fname) 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_ecg = mne.Epochs(raw, ecg_events, None, tmin, tmax, baseline=None, picks=picks, reject=reject, proj=True) print epochs_ecg projs_init = raw.info['projs'] if average: evoked_ecg = epochs_ecg.average() projs_ecg = mne.compute_proj_evoked(evoked_ecg, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) else: print epochs_ecg, n_grad, n_mag, n_eeg projs_ecg = mne.compute_proj_epochs(epochs_ecg, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) print "Writing ECG projections in %s" % ecg_proj_fname mne.write_proj( ecg_proj_fname, projs_ecg + projs_init ) ## Original Projections written along with the ecg projections. print "Writing ECG projections in %s" % ecg_proj_fname mne.write_proj(ecg_proj_fname, projs_ecg) return in_fif_fname, ecg_proj_fname, out_fif_fname
def compute_proj_eog(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, avg_ref, bads, preload): #####Defining filenames 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] eog_event_fname = in_path + 'ssp/' + prefix + '_PYeog-eve.fif' if average: eog_proj_fname = in_path + 'ssp/' + prefix + '_eog_avg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_eog_avg_proj_raw.fif' else: eog_proj_fname = in_path + 'ssp/' + prefix + '_PYeog_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_PYeog_proj_raw.fif' ####Reading in raw data in_fif_fname = in_path + in_fif_fname raw = mne.fiff.Raw(in_fif_fname, preload=preload) print 'Running EOG SSP computation' eog_events, _ = mne.artifacts.find_eog_events( raw ) # since our copy of the mne.artifacts.events.py script returns two parameters. print "Writing EOG events in %s" % eog_event_fname mne.write_events(eog_event_fname, eog_events) make_lingua(eog_event_fname) print 'Computing EOG projector' # Handler rejection parameters if len(mne.fiff.pick_types(raw.info, meg='grad', eeg=False, ecg=False)) == 0: del reject['grad'] if len(mne.fiff.pick_types(raw.info, meg='mag', eeg=False, ecg=False)) == 0: del reject['mag'] if len(mne.fiff.pick_types(raw.info, meg=False, eeg=True, ecg=False)) == 0: del reject['eeg'] if len(mne.fiff.pick_types(raw.info, meg=False, eeg=False, ecg=True)) == 0: del reject['ecg'] picks_eog = mne.fiff.pick_types(raw.info, meg=True, eeg=True, ecg=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_eog, l_freq, h_freq, filter_length, n_jobs) epochs_eog = mne.Epochs(raw, eog_events, None, tmin, tmax, baseline=None, picks=picks_eog, reject=reject, proj=True) projs_init = raw.info['projs'] if average: evoked_eog = epochs_eog.average() projs_eog = mne.compute_proj_evoked(evoked_eog, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) else: print epochs_eog, n_grad, n_mag, n_eeg projs_eog = mne.compute_proj_epochs(epochs_eog, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) print "Writing EOG projections in %s" % eog_proj_fname mne.write_proj(eog_proj_fname, projs_eog) return in_fif_fname, eog_proj_fname, out_fif_fname
bad_fname = in_path + bad_fname print bad_fname if bad_fname is not None: bads = [w.rstrip().split() for w in open(bad_fname).readlines()] print 'Bad channels read : %s' % bads else: bads = [] print type(tmin) if tag == 'ecg': in_fif_fname, ecg_proj_fname, out_fif_fname = compute_proj_ecg( in_path, raw_in, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, avg_ref, bads, preload, flat) make_lingua(in_fif_fname) make_lingua(ecg_proj_fname) 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)) st = os.system(command) if st != 0: raise ValueError('Problem while running : %s' % command) else: print 'Command executed: %s' % command print 'Done' print('Computed ECG Rejection fif file. Saved result as %s' % out_fif_fname) make_lingua(out_fif_fname)
subject_lines = [] for sub in subjects: if sub in v: if len(sub) > 3: ##This makes the tabbing pretty for the table; different for longer subject names beg = '%s\t' % sub else: beg = '%s\t\t' % sub results = '\t\t'.join(['%i' % ( v[sub][code][1]) for code in codes]) subject_lines.append('%s%s' % (beg, results)) #write out dat for each paradigm f = open(fname, 'w') f.write(code_line + '\n') f.write('\n'.join(subject_lines)) f.close() print("Saved total table to %s" % fname) make_lingua(fname) ##make combined rejTable for k,v in all_data.items(): if k == 'BaleenHP': fname = '/%s/kuperberg/SemPrMM/MEG/results/artifact_rejection/combined_rejection/ssp/%s_%s_remaining_events_ecgeog.txt' % (pre, subjType, k) codes = sorted(v[subjType+'8'].keys(), cmp=lambda x,y: cmp(int(x), int(y))) ##This is the codes for the events for the paradigm code_line = '\t\t%s' % '\t\t'.join(codes) ##This just puts them together to head the table subject_lines = [] for sub in subjects: subjLine = [] if sub in v: if len(sub) > 3: ##This makes the tabbing pretty for the table; different for longer subject names beg = '%s\t' % sub else: beg = '%s\t\t' % sub
print subjects study_results = parse_study(study, subjects) logf = '/cluster/kuperberg/SemPrMM/MEG/results/behavioral_accuracy/R/MEG_%s_%s_accuracy.log' % ( listPrefix, study) with open(logf, 'w') as f: f.write('sub:\t\t') good_keys = sorted([key for key in codes[study].keys()], lambda x, y: cmp(int(x), int(y))) #write out header f.write('CondCode\t\tAccuracy\tUnknown\n') for sub in sorted(study_results.keys()): results = study_results[sub] total_num = 0 total_den = 0 for key in good_keys: v = results[key] total_num += v['c'] total_den += v['t'] if v['rts']: avg_rt = sum(v['rts']) / float(len(v['rts'])) else: avg_rt = 0 f.write(sub + '\t\t') f.write(codes[study][key][0] + '\t\t') f.write( "%1.3f\t\t%1.3f\t\n" % (round(float(v['c']) / v['t'], 3), round(avg_rt, 3))) ## f.write('ALL TASKS\n') ## f.write("{0}\n".format(round(float(total_num)/total_den,3))) make_lingua(logf)
def compute_proj_ecg(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, avg_ref, bads, preload, flat): """Compute SSP/PCA projections for ECG/EOG artifacts Parameters ---------- in_fif_fname: string Raw fif File XXX """ 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 = in_path + 'ssp/' + prefix + '_ecg-eve.fif' if average: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_avg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecg_avg_proj_raw.fif' else: ecg_proj_fname = in_path + 'ssp/' + prefix + '_ecg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_ecg_proj_raw.fif' print 'Reading fif File' in_fif_fname = in_path + in_fif_fname raw = mne.fiff.Raw(in_fif_fname, preload=preload) # if (in_path == '/cluster/kuperberg/SemPrMM/MEG/data/ya30/'): # save after 84s of MEG data in FIF file # raw = raw[: , 84:] #raw.save('sample_audvis_meg_raw.fif', tmin=84) 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) make_lingua(ecg_event_fname) 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_ecg = mne.Epochs(raw, ecg_events, None, tmin, tmax, baseline=None, picks=picks, reject=reject, proj=True) print epochs_ecg projs_init = raw.info['projs'] if average: evoked_ecg = epochs_ecg.average() projs_ecg = mne.compute_proj_evoked(evoked_ecg, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) else: print epochs_ecg, n_grad, n_mag, n_eeg projs_ecg = mne.compute_proj_epochs(epochs_ecg, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) print "Writing ECG projections in %s" % ecg_proj_fname mne.write_proj(ecg_proj_fname, projs_ecg + projs_init) ## Original Projections written along with the ecg projections. print "Writing ECG projections in %s" % ecg_proj_fname mne.write_proj(ecg_proj_fname, projs_ecg) return in_fif_fname, ecg_proj_fname, out_fif_fname
def compute_proj_eog(in_path, in_fif_fname, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, avg_ref, bads, preload): #####Defining filenames 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] eog_event_fname = in_path + 'ssp/'+ prefix + '_PYeog-eve.fif' if average: eog_proj_fname = in_path + 'ssp/' + prefix + '_eog_avg_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_eog_avg_proj_raw.fif' else: eog_proj_fname = in_path + 'ssp/' + prefix + '_PYeog_proj.fif' out_fif_fname = in_path + 'ssp/' + prefix + '_PYeog_proj_raw.fif' ####Reading in raw data in_fif_fname = in_path + in_fif_fname raw = mne.fiff.Raw(in_fif_fname, preload=preload) print 'Running EOG SSP computation' eog_events, _= mne.artifacts.find_eog_events(raw) # since our copy of the mne.artifacts.events.py script returns two parameters. print "Writing EOG events in %s" % eog_event_fname mne.write_events(eog_event_fname, eog_events) make_lingua(eog_event_fname) print 'Computing EOG projector' # Handler rejection parameters if len(mne.fiff.pick_types(raw.info, meg='grad', eeg=False, ecg=False)) == 0: del reject['grad'] if len(mne.fiff.pick_types(raw.info, meg='mag', eeg=False, ecg=False)) == 0: del reject['mag'] if len(mne.fiff.pick_types(raw.info, meg=False, eeg=True, ecg=False)) == 0: del reject['eeg'] if len(mne.fiff.pick_types(raw.info, meg=False, eeg=False, ecg=True)) == 0: del reject['ecg'] picks_eog = mne.fiff.pick_types(raw.info, meg=True, eeg=True, ecg=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_eog, l_freq, h_freq, filter_length, n_jobs) epochs_eog = mne.Epochs(raw, eog_events, None, tmin, tmax, baseline=None, picks=picks_eog, reject=reject, proj=True) projs_init = raw.info['projs'] if average: evoked_eog = epochs_eog.average() projs_eog = mne.compute_proj_evoked(evoked_eog, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) else: print epochs_eog, n_grad, n_mag, n_eeg projs_eog = mne.compute_proj_epochs(epochs_eog, n_grad=n_grad, n_mag=n_mag, n_eeg=n_eeg) print "Writing EOG projections in %s" % eog_proj_fname mne.write_proj(eog_proj_fname, projs_eog) return in_fif_fname, eog_proj_fname, out_fif_fname
bad_fname = options.bad_fname bad_fname = in_path + bad_fname print bad_fname if bad_fname is not None: bads = [w.rstrip().split() for w in open(bad_fname).readlines()] print 'Bad channels read : %s' % bads else: bads = [] print type(tmin) if tag == 'ecg': in_fif_fname, ecg_proj_fname, out_fif_fname = compute_proj_ecg(in_path, raw_in, tmin, tmax, n_grad, n_mag, n_eeg, l_freq, h_freq, average, filter_length, n_jobs, ch_name, reject, avg_ref, bads, preload, flat) make_lingua(in_fif_fname) make_lingua(ecg_proj_fname) 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)) st = os.system(command) if st != 0: raise ValueError('Problem while running : %s' % command) else: print 'Command executed: %s' % command print 'Done' print ('Computed ECG Rejection fif file. Saved result as %s' % out_fif_fname) make_lingua(out_fif_fname)
if len( sub ) > 3: ##This makes the tabbing pretty for the table; different for longer subject names beg = '%s\t' % sub else: beg = '%s\t\t' % sub results = '\t\t'.join( ['%i' % (v[sub][code][1]) for code in codes]) subject_lines.append('%s%s' % (beg, results)) #write out dat for each paradigm f = open(fname, 'w') f.write(code_line + '\n') f.write('\n'.join(subject_lines)) f.close() print("Saved total table to %s" % fname) make_lingua(fname) ##make combined rejTable for k, v in all_data.items(): if k == 'BaleenHP': fname = '/%s/kuperberg/SemPrMM/MEG/results/artifact_rejection/combined_rejection/ssp/%s_%s_remaining_events_ecgeog.txt' % ( pre, subjType, k) codes = sorted( v[subjType + '8'].keys(), cmp=lambda x, y: cmp(int(x), int( y))) ##This is the codes for the events for the paradigm code_line = '\t\t%s' % '\t\t'.join( codes) ##This just puts them together to head the table subject_lines = [] for sub in subjects: subjLine = [] if sub in v: