from mne_bids import read_raw_bids bids_path = BIDSPath( subject='001', session='01', task='visual', suffix='eeg', root=bids_root, )
raw = read_raw_bids(bids_path, verbose=True) raw.load_data() # now in memory - Channel locations (from .tsv) - Events (from events.tsv) - Bad channels (from channels.tsv) print(raw) Step 3: Preprocessing Pipeline A typical preprocessing pipeline in MNE for BIDS data:
with open(args.config, 'r') as f: config = yaml.safe_load(f) main(args.subject, config)
For group analysis, save evoked data in BIDS-derivatives:
from mne_bids import write_anat write_anat(bids_root, subject='001', t1w_anat='sub-001_T1w.nii.gz') Assuming you have one evoked response per subject per condition:
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
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, )