Mne Bids Pipeline |verified| Review
src = mne.setup_source_space('sub-001', spacing='oct6', subjects_dir=subjects_dir) fwd = mne.make_forward_solution( raw.info, trans=None, src=src, bem=bem_sol, meg=False, eeg=True ) 4. Inverse operator (dSPM or MNE) inverse_operator = mne.minimum_norm.make_inverse_operator( epochs.info, fwd, cov, loose=0.2, depth=0.8 ) 5. Apply to evoked data stc = mne.minimum_norm.apply_inverse( evoked_face, inverse_operator, lambda2=1/9., method='dSPM' ) Plot on cortical surface stc.plot(subject='sub-001', subjects_dir=subjects_dir, initial_time=0.1)
all_evoked_faces = [] all_evoked_cars = [] for sub in subject_list: bids_path = BIDSPath(subject=sub, task='visual', suffix='eeg', root=bids_root) raw = read_raw_bids(bids_path) # ... preprocessing (same as above) ... epochs = mne.Epochs(raw, events, event_id, tmin, tmax, baseline) all_evoked_faces.append(epochs['face'].average()) all_evoked_cars.append(epochs['car'].average()) X = [evoked.data for evoked in all_evoked_faces] # subjects x channels x times Y = [evoked.data for evoked in all_evoked_cars] Reshape to (n_subjects, n_channels, n_times) import numpy as np X = np.array(X) Y = np.array(Y) Threshold-free cluster enhancement (TFCE) or cluster-based permutation threshold = 2.0 # t-value threshold cluster_stats = mne.stats.permutation_cluster_test( [X, Y], threshold=threshold, n_permutations=1024, tail=0, # two-tailed n_jobs=-1, )
if == ' main ': parser = argparse.ArgumentParser() parser.add_argument('--subject', required=True) parser.add_argument('--config', default='config.yaml') args = parser.parse_args() mne bids pipeline
Run in parallel:
import mne def preprocess_raw(raw, l_freq=0.1, h_freq=40, notch=50): """ Apply standard EEG preprocessing. Adjust parameters for MEG (e.g., high-pass 1 Hz, low-pass 100 Hz). """ # 1. Filter (bandpass) raw.filter(l_freq, h_freq, fir_design='firwin', verbose=True) src = mne
t_obs, clusters, p_values, H0 = cluster_stats Use a configuration file (YAML) # config.yaml subjects: ['001', '002', '003'] task: 'visual' preprocessing: l_freq: 0.1 h_freq: 40 notch: 50 epochs: tmin: -0.2 tmax: 0.8 baseline: [-0.2, 0] Python script with argparse import yaml, argparse from mne_bids import BIDSPath, read_raw_bids def main(subject, config): # load config # run pipeline for one subject pass
Save source estimates in BIDS derivatives using mne-bids : preprocessing (same as above)
# 4. Set average reference (EEG) if 'eeg' in raw: raw.set_eeg_reference('average', projection=False)