import os.path as op import mne import numpy as np from config import bem_ico, excludes, map_subjects, meg_dir, spacing conditions = { 'aud_left_minus_right', 'vis_left_minus_right', 'aud_left_eq', 'aud_right_eq', 'vis_left_eq', 'vis_right_eq' } subjects = [s for s in map_subjects.values() if s not in excludes] for condition in conditions: stcs = list() for subject in subjects: stc_fname = op.join( meg_dir, subject, '-'.join([ f'{subject}_audvis', 'dSPM', spacing, 'inverse', 'morph', 'filt', 'sss', condition ])) stc = mne.read_source_estimate(stc_fname) stcs.append(stc) data = np.average([s.data for s in stcs], axis=0) stc_ave = mne.VectorSourceEstimate(data, stcs[0].vertices, stcs[0].tmin, stcs[0].tstep, 'fsaverage') ave_fname = op.join( meg_dir, '-'.join( [spacing, f'ico{bem_ico}', 'dSPM', 'morph', 'condition', 'ave'])) stc_ave.save(ave_fname)
trans_fname = op.join(meg_dir, subject, f'{subject}_audvis_raw-trans.fif') # If you follow the step in 2_setup_source_space.py src_fname = op.join(subjects_dir, subject, 'bem', f'{subject}-{spacing}-src.fif') bem_fname = op.join(subjects_dir, subject, 'bem', f'{subject}-ico{bem_ico}-bem-sol.fif') # If you are practicing with the sample dataset # src_fname = op.join(subjects_dir, subject, 'bem', # f'{subject}-oct-6-src.fif') # bem_fname = op.join(subjects_dir, subject, 'bem', # f'{subject}-5120-5120-5120-bem-sol.fif') info = mne.io.read_info(evoked_fname) fwd = mne.make_forward_solution(info, trans_fname, src_fname, bem_fname, meg=True, eeg=False) mne.write_forward_solution(fwd_fname, fwd, overwrite=True) print(f'Computed forward solution for {subject}') parallel, run_func, _ = parallel_func(run_forward, n_jobs=n_jobs) subjects = [ subject for subject in map_subjects.values() if subject not in excludes ] parallel(run_func(subject) for subject in subjects)
picks=picks, event_id=event_id, tmin=tmin, tmax=tmax, baseline=baseline, decim=4, # raw sampling rate is 600 Hz, subsample to 150 Hz preload=True, # for autoreject reject_tmax=reject_tmax, reject_by_annotation=True) del raw, annot # autoreject (local) if autoreject: # local reject # keep the bad sensors/channels because autoreject can repair it via # interpolation picks = mne.pick_types(epochs.info, meg=True, exclude=[]) ar = AutoReject(picks=picks, n_jobs=n_jobs, verbose=False) print(f'Run autoreject (local) for {subject} (it takes a long time)') ar.fit(epochs) print(f'Drop bad epochs and interpolate bad sensors for {subject}') epochs = ar.transform(epochs) print(f'Dropped {round(epochs.drop_log_stats(), 2)}% epochs for {subject}') epochs.save(epo_fname, overwrite=True) for subject in map_subjects.values(): if subject not in excludes: run_epochs(subject)