from sklearn.model_selection import cross_validate from sklearn.preprocessing import StandardScaler from sklearn.svm import LinearSVC # Read and filter the data raw_fname = '/projects/training/MNE/MNE-sample-data/MEG/sample/sample_audvis_raw.fif' raw = mne.io.read_raw_fif(raw_fname, preload=True) raw.filter(0.5, 40) # Get events and epochs event_id = dict(aud_l=1, aud_r=2, vis_l=3, vis_r=4) events = mne.find_events(raw) epochs = mne.Epochs(raw, events, event_id, -0.1, 0.4, proj=True, baseline=(None, 0), preload=True, decim=4, reject=dict(mag=3e-12, grad=300e-12, eog=200e-6)) epochs.pick_types(meg='grad', exclude='bads') # Assign data and labels to X, y X = epochs.get_data() # has time as last extra dimension y = epochs.events[:, -1] # combine the processing steps into a scikit-learn pipeline object: scaler = StandardScaler() # scales the data svc = LinearSVC() clf = make_pipeline(scaler, svc) # To get results over time, we need a sliding estimator, which # will handle each time instant as a separate sample:
raw = mne.io.read_raw_fif(raw_fname) events = mne.find_events(raw, stim_channel='STI 014') event_id = dict(aud_r=1) # event trigger and conditions tmin = -0.2 # start of each epoch (200ms before the trigger) tmax = 0.5 # end of each epoch (500ms after the trigger) raw.info['bads'] = ['MEG 2443', 'EEG 053'] picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=True, exclude='bads') baseline = (None, 0) # means from the first instant to t = 0 reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=baseline, reject=reject) ############################################################################### # Compute regularized noise covariance # ------------------------------------ # # For more details see :ref:`tut_compute_covariance`. noise_cov = mne.compute_covariance(epochs, tmax=0., method=['shrunk', 'empirical'])
include = [] raw.info['bads'] += ['EEG 053'] # bads + 1 more # pick MEG channels picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # define frequencies of interest fmin, fmax = 0., 70. bandwidth = 4. # bandwidth of the windows in Hz ############################################################################### # Compute source space PSD in label # --------------------------------- # # ..note:: By using "return_generator=True" stcs will be a generator object # instead of a list. This allows us so to iterate without having to # keep everything in memory.
# Set up pick list: EEG + MEG - bad channels (modify to your needs) raw.info['bads'] += ['MEG 2443', 'EEG 053'] # bads + 2 more picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=True, eog=True, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0.), preload=True, reject=dict(grad=4000e-13, eog=150e-6), decim=10) epochs.pick_types(meg=True, exclude='bads') ############################################################################### # Temporal decoding # ----------------- # # We'll use a Logistic Regression for a binary classification as machine # learning model. # We will train the classifier on all left visual vs auditory trials on MEG
def test_experiment_class(): import mne from mne.io import concatenate_raws # 5,6,7,10,13,14 are codes for executed and imagined hands/feet subject_id = 1 event_codes = [5, 6, 9, 10, 13, 14] # This will download the files if you don't have them yet, # and then return the paths to the files. physionet_paths = mne.datasets.eegbci.load_data(subject_id, event_codes) # Load each of the files parts = [ mne.io.read_raw_edf(path, preload=True, stim_channel='auto', verbose='WARNING') for path in physionet_paths ] # Concatenate them raw = concatenate_raws(parts) # Find the events in this dataset events = mne.find_events(raw, shortest_event=0, stim_channel='STI 014') # Use only EEG channels eeg_channel_inds = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') # Extract trials, only using EEG channels epoched = mne.Epochs(raw, events, dict(hands=2, feet=3), tmin=1, tmax=4.1, proj=False, picks=eeg_channel_inds, baseline=None, preload=True) import numpy as np from braindecode.datautil.signal_target import SignalAndTarget from braindecode.datautil.splitters import split_into_two_sets # Convert data from volt to millivolt # Pytorch expects float32 for input and int64 for labels. X = (epoched.get_data() * 1e6).astype(np.float32) y = (epoched.events[:, 2] - 2).astype(np.int64) # 2,3 -> 0,1 train_set = SignalAndTarget(X[:60], y=y[:60]) test_set = SignalAndTarget(X[60:], y=y[60:]) train_set, valid_set = split_into_two_sets(train_set, first_set_fraction=0.8) from braindecode.models.shallow_fbcsp import ShallowFBCSPNet from torch import nn from braindecode.torch_ext.util import set_random_seeds from braindecode.models.util import to_dense_prediction_model # Set if you want to use GPU # You can also use torch.cuda.is_available() to determine if cuda is available on your machine. cuda = False set_random_seeds(seed=20170629, cuda=cuda) # This will determine how many crops are processed in parallel input_time_length = 450 n_classes = 2 in_chans = train_set.X.shape[1] # final_conv_length determines the size of the receptive field of the ConvNet model = ShallowFBCSPNet(in_chans=in_chans, n_classes=n_classes, input_time_length=input_time_length, final_conv_length=12).create_network() to_dense_prediction_model(model) if cuda: model.cuda() from torch import optim optimizer = optim.Adam(model.parameters()) from braindecode.torch_ext.util import np_to_var # determine output size test_input = np_to_var( np.ones((2, in_chans, input_time_length, 1), dtype=np.float32)) if cuda: test_input = test_input.cuda() out = model(test_input) n_preds_per_input = out.cpu().data.numpy().shape[2] print("{:d} predictions per input/trial".format(n_preds_per_input)) from braindecode.experiments.experiment import Experiment from braindecode.datautil.iterators import CropsFromTrialsIterator from braindecode.experiments.monitors import RuntimeMonitor, LossMonitor, \ CroppedTrialMisclassMonitor, MisclassMonitor from braindecode.experiments.stopcriteria import MaxEpochs import torch.nn.functional as F import torch as th from braindecode.torch_ext.modules import Expression # Iterator is used to iterate over datasets both for training # and evaluation iterator = CropsFromTrialsIterator(batch_size=32, input_time_length=input_time_length, n_preds_per_input=n_preds_per_input) # Loss function takes predictions as they come out of the network and the targets # and returns a loss loss_function = lambda preds, targets: F.nll_loss( th.mean(preds, dim=2, keepdim=False), targets) # Could be used to apply some constraint on the models, then should be object # with apply method that accepts a module model_constraint = None # Monitors log the training progress monitors = [ LossMonitor(), MisclassMonitor(col_suffix='sample_misclass'), CroppedTrialMisclassMonitor(input_time_length), RuntimeMonitor(), ] # Stop criterion determines when the first stop happens stop_criterion = MaxEpochs(4) exp = Experiment(model, train_set, valid_set, test_set, iterator, loss_function, optimizer, model_constraint, monitors, stop_criterion, remember_best_column='valid_misclass', run_after_early_stop=True, batch_modifier=None, cuda=cuda) # need to setup python logging before to be able to see anything import logging import sys logging.basicConfig(format='%(asctime)s %(levelname)s : %(message)s', level=logging.DEBUG, stream=sys.stdout) exp.run() import pandas as pd from io import StringIO compare_df = pd.read_csv( StringIO( u'train_loss,valid_loss,test_loss,train_sample_misclass,valid_sample_misclass,' 'test_sample_misclass,train_misclass,valid_misclass,test_misclass\n' '0,0.8692976435025532,0.7483791708946228,0.6975634694099426,' '0.5389371657754011,0.47103386809269165,0.4425133689839572,' '0.6041666666666667,0.5,0.4\n1,2.3362590074539185,' '2.317707061767578,2.1407743096351624,0.4827874331550802,' '0.5,0.4666666666666667,0.5,0.5,0.4666666666666667\n' '2,0.5981490015983582,0.785034716129303,0.7005959153175354,' '0.3391822638146168,0.47994652406417115,0.41996434937611404,' '0.22916666666666663,0.41666666666666663,0.43333333333333335\n' '3,0.6355261653661728,0.785034716129303,' '0.7005959153175354,0.3673351158645276,0.47994652406417115,' '0.41996434937611404,0.2666666666666667,0.41666666666666663,' '0.43333333333333335\n4,0.625280424952507,' '0.802731990814209,0.7048938572406769,0.3367201426024955,' '0.43137254901960786,0.4229946524064171,0.3666666666666667,' '0.5833333333333333,0.33333333333333337\n')) for col in compare_df: np.testing.assert_allclose(np.array(compare_df[col]), exp.epochs_df[col], rtol=1e-4, atol=1e-5)
# Set epoch rejection threshold a bit larger than for SQUIDs reject = dict(mag=2e-10) tmin, tmax = -0.5, 1 # Find median nerve stimulator trigger event_id = dict(Median=257) events = mne.find_events(raw, stim_channel='STI101', mask=257, mask_type='and') picks = mne.pick_types(raw.info, meg=True, eeg=False) # We use verbose='error' to suppress warning about decimation causing aliasing, # ideally we would low-pass and then decimate instead epochs = mne.Epochs(raw, events, event_id, tmin, tmax, verbose='error', reject=reject, picks=picks, proj=False, decim=10, preload=True) evoked = epochs.average() evoked.plot() cov = mne.compute_covariance(epochs, tmax=0.) del epochs, raw # %% # Examine our coordinate alignment for source localization and compute a # forward operator: # # .. note:: The Head<->MRI transform is an identity matrix, as the
data = dataset._get_single_subject_data(subject) for session in data.keys(): raw = data[session]['run_3'] # filter data and resample fmin = 1 fmax = 24 raw.filter(fmin, fmax, verbose=False) # detect the events and cut the signal into epochs events = mne.find_events(raw=raw, shortest_event=1, verbose=False) event_id = {'NonTarget': 33286, 'Target': 33285} epochs = mne.Epochs(raw, events, event_id, tmin=0.0, tmax=1.0, baseline=None, verbose=False, preload=True) epochs.pick_types(eeg=True) # get trials and labels X = epochs.get_data() y = events[:, -1] y[y == 33286] = 0 y[y == 33285] = 1 # cross validation skf = StratifiedKFold(n_splits=5) clf = make_pipeline(ERPCovariances(estimator='lwf', classes=[1]), MDM()) scr = cross_val_score(clf, X, y, cv=skf, scoring='roc_auc') # print results of classification scores[subject][session] = scr.mean()
# Set picks picks = mne.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') # Read epochs event_id, tmin, tmax = 1, -0.2, 0.5 events = mne.read_events(event_fname) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, mag=4e-12)) evoked = epochs.average() # Read forward operator forward = mne.read_forward_solution(fname_fwd, surf_ori=True) # Computing the data and noise cross-spectral density matrices # The time-frequency window was chosen on the basis of spectrograms from # example time_frequency/plot_time_frequency.py # As fsum is False csd_epochs returns a list of CrossSpectralDensity # instances than can then be passed to dics_source_power data_csds = csd_epochs(epochs,
# Read the raw data raw = mne.io.read_raw_fif(raw_fname) raw.info['bads'] = ['MEG 2443'] # bad MEG channel # Set up the epoching event_id = 1 # those are the trials with left-ear auditory stimuli tmin, tmax = -0.2, 0.5 events = mne.find_events(raw) # pick relevant channels raw.pick(['meg', 'eog']) # pick channels of interest # Create epochs proj = False # already applied epochs = mne.Epochs(raw, events, event_id, tmin, tmax, baseline=(None, 0), preload=True, proj=proj, reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6)) # Visualize averaged sensor space data evoked = epochs.average() evoked.plot_joint() del raw # save memory ############################################################################### # Computing the covariance matrices # --------------------------------- # Spatial filters use the data covariance to estimate the filter # weights. The data covariance matrix will be `inverted`_ during the spatial # filter computation, so it is valuable to plot the covariance matrix and its # eigenvalues to gauge whether matrix inversion will be possible.
'Mismatch_8': 5, 'Match_8': 4, 'Mismatch_16': 8, 'Match_16': 7 } tmin = -0.5 tmax = 1.5 include = [] # or stim channels ['STI 014'] raw.info['bads'] += [ 'EEG 030', 'EEG 040', 'MEG 0813', 'MEG 1731', 'MEG 1412', 'MEG 1921' ] # bads + 1 more picks = mne.fiff.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=True, include=include, exclude='bads') epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(eeg=80e-6, eog=150e-6))
# raw.apply_proj() fs = raw.info['sfreq'] removeblinks = True if removeblinks: # SSP for blinks blinks = find_blinks(raw, ch_name=[ 'A1', ]) blinkname = (fpath + subj + '_' + para + '_blinks_erp' + '-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(eeg=500e-6)) blink_projs = compute_proj_epochs(epochs_blinks, n_grad=0, n_mag=0, n_eeg=2, verbose='DEBUG') raw.add_proj(blink_projs) evokeds = [] condnames = ['coh07', 'coh14', 'coh20'] condlists = [3, 2, 1] eves2 = np.zeros((eves.shape[0] * 2, 3), dtype=np.int) fs_int = int(raw.info['sfreq'])
from autoreject import get_rejection_threshold from autoreject import AutoReject import mne from params import (event_id, raw_maxfiltered_file, raw_er_maxfiltered_file, epochs_file, evoked_file, AUTOREJECT, FIND_EVENTS_KWARGS) raw = mne.io.read_raw_fif(raw_maxfiltered_file) raw_er = mne.io.read_raw_fif(raw_er_maxfiltered_file) # %% # Construct epochs from MEG data events = mne.find_events(raw, **FIND_EVENTS_KWARGS) epochs = mne.Epochs( raw, events, event_id=event_id, on_missing='ignore', ) fig = mne.viz.plot_events( events, sfreq=raw.info['sfreq'], first_samp=raw.first_samp, event_id=event_id, on_missing='ignore', ) fig.subplots_adjust(right=0.7) # make room for legend for (e, i) in event_id.items(): a = (events[:, -1] == i).sum() print(f"event {e} is present {a} times")
raw_sim = mne.simulation.simulate_raw(info, source_simulator, forward=fwd) raw_sim.set_eeg_reference(projection=True) mne.simulation.add_noise(raw_sim, cov=noise_cov, random_state=0) mne.simulation.add_eog(raw_sim, random_state=0) mne.simulation.add_ecg(raw_sim, random_state=0) # Plot original and simulated raw data. raw_sim.plot(title='Simulated raw data') ############################################################################### # Extract epochs and compute evoked responsses # -------------------------------------------- # epochs = mne.Epochs(raw_sim, events, event_id, tmin=-0.2, tmax=0.3, baseline=(None, 0)) evoked_aud_left = epochs['auditory/left'].average() evoked_vis_right = epochs['visual/right'].average() # Visualize the evoked data evoked_aud_left.plot(spatial_colors=True) evoked_vis_right.plot(spatial_colors=True) ############################################################################### # Reconstruct simulated source time courses using dSPM inverse operator # --------------------------------------------------------------------- # # Here, source time courses for auditory and visual areas are reconstructed # separately and their difference is shown. This was done merely for better # visual representation of source reconstruction. # As expected, when high activations appear in primary auditory areas, primary
import mne import mne_features import numpy as np from mne_features.feature_extraction import extract_features from moabb.datasets import physionet_mi if __name__ == '__main__': # get dataset ds = physionet_mi.PhysionetMI() raw = ds.get_data([2])[2]['session_0']['run_4'].pick_channels(['C3', 'C4']) events = mne.events_from_annotations(raw) s_freq = raw.info['sfreq'] # get x and save to file X = mne.Epochs(raw, events[0]).get_data() # extract features extract_features() params = { 'pow_freq_bands__freq_bands': np.arange(1, int(s_freq / 2), 1), } selected_funcs = {'mean', 'ptp_amp', 'std', 'pow_freq_bands'} features_array = mne_features.feature_extraction.extract_features( X, s_freq, selected_funcs, params) # save features to file np.savetxt('../data/mne/1/features.csv', features_array, delimiter=',') # get y and save to file y = np.asarray([e[2] for e in events[0]])
# raw.apply_proj() raw = raw.set_eeg_reference(['TP9', 'TP10']) # Filter raw = raw.notch_filter(np.arange(50, 251, 50), picks=eeg_channels, fir_design='firwin') raw = raw.filter(1, 30) # ============================================================================= # Epoching # ============================================================================= epochs = mne.Epochs(raw, events=events, event_id=event_id, tmin=-0.2, tmax=0.8, detrend=1, preload=True) epochs.resample(200, npad="auto") # Autoreject ransac = autoreject.Ransac(verbose='progressbar', picks=eeg_channels, n_jobs=1) epochs = ransac.fit_transform(epochs) reject = autoreject.get_rejection_threshold(epochs) # ============================================================================= # ICA # =============================================================================
'EXG3', ] # Filter the data for ERPs raw.filter(l_freq=1.0, h_freq=20, l_trans_bandwidth=0.15, picks=np.arange(0, 32, 1)) # raw.apply_proj() fs = raw.info['sfreq'] # SSP for blinks blinks = find_blinks(raw) epochs_blinks = mne.Epochs(raw, blinks, 998, tmin=-0.25, tmax=0.25, proj=True, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) blink_projs = compute_proj_epochs(epochs_blinks, n_grad=0, n_mag=0, n_eeg=2, verbose='DEBUG') # raw.del_proj(0) # Removing average reference projection raw.add_proj(blink_projs) # Epoching events of type epochs = mne.Epochs(raw, eves, condlist,
ica_raw_name = ica_dir + "%s_%s_%s_after_ica_raw.fif" % (subj, sesh, fname_suffix) raw = mne.io.Raw(ica_raw_name, preload=True) ##read the events from the event files instead of the stim channels in the data. fname = event_dir + "%s_%s_WORD.eve" % (subj, sesh) events = mne.read_events(fname) tmin, tmax = -0.1, 0.3 baseline = (-0.1, 0.0) ##the baseline period is set to equal to the length of the epoch. The epoch is set to be longer ##than usual because of the potential time-frequency analysis that comes later. #However, the isi in the experiment is, which means that there is?? epochs = mne.Epochs(raw, events=events, baseline=baseline, event_id=event_id, tmin=tmin, tmax=tmax) ##In this step, we will also reject the bad trials that have 15 standard deviations ##from the average picks = mne.pick_types(epochs.info, eeg=True, stim=False, eog=False) epoch_mat = epochs.get_data()[:, picks, :] ##epoch_mat is 3-D where 0 is n_events, 1 is n_channels and 2 is n_time points #The following code rejects epochs that are alpha std beyong the average. Serves as an #Automatic trial rejection step ranges_each_trial = np.max(epoch_mat, axis=2) - np.min(epoch_mat, axis=2) ranges_zscore = scipy.stats.zscore(ranges_each_trial, axis=0) bad_trials = np.any(ranges_zscore > alpha, axis=1) print "# bad_trials %d" % bad_trials.sum() epochs.drop(np.where(bad_trials == 1)[0])
events = mne.read_events(fname_event) # %% # By default, CSD matrices are computed using all MEG/EEG channels. When # interpreting a CSD matrix with mixed sensor types, be aware that the # measurement units, and thus the scalings, differ across sensors. In this # example, for speed and clarity, we select a single channel type: # gradiometers. picks = mne.pick_types(raw.info, meg='grad') # Make some epochs, based on events with trigger code 1 epochs = mne.Epochs(raw, events, event_id=1, tmin=-0.2, tmax=1, picks=picks, baseline=(None, 0), reject=dict(grad=4000e-13), preload=True) # %% # Computing CSD matrices using short-term Fourier transform and (adaptive) # multitapers is straightforward: csd_fft = csd_fourier(epochs, fmin=15, fmax=20, n_jobs=n_jobs) csd_mt = csd_multitaper(epochs, fmin=15, fmax=20, adaptive=True, n_jobs=n_jobs) # %% # When computing the CSD with Morlet wavelets, you specify the exact # frequencies at which to compute it. For each frequency, a corresponding # wavelet will be constructed and convolved with the signal, resulting in a
conditions = 'faces', 'scrambled' snr = 3.0 lambda2 = 1.0 / snr**2 clim = dict(kind='value', lims=[0, 2.5, 5]) # %% # Estimate covariances samples_epochs = 5, 15, method = 'empirical', 'shrunk' colors = 'steelblue', 'red' epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, baseline=baseline, preload=True, reject=reject, decim=8) del raw noise_covs = list() evokeds = list() stcs = list() methods_ordered = list() for n_train in samples_epochs: # estimate covs based on a subset of samples # make sure we have the same number of conditions. idx = np.sort( np.concatenate([
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif' tmin, tmax = -0., 1 event_id = dict(aud_l=1, aud_r=2, vis_l=3, vis_r=4) # Setup for reading the raw data raw = io.Raw(raw_fname, preload=True, verbose=False) raw.filter(2, None, method='iir') # replace baselining with high-pass events = mne.read_events(event_fname) raw.info['bads'] = ['MEG 2443'] # set bad channels picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=False, picks=picks, baseline=None, preload=True, verbose=False) labels = epochs.events[:, -1] # extract raw data. scale by 1000 due to scaling sensitivity in deep learning X = epochs.get_data()*1000 # format is in (trials, channels, samples) y = labels kernels, chans, samples = 1, 60, 151 # take 50/25/25 percent of the data to train/validate/test X_train = X[0:144,] Y_train = y[0:144] X_validate = X[144:216,] Y_validate = y[144:216] X_test = X[216:,] Y_test = y[216:]
def main(): # Initialize FOOOFGroup to use for fitting fg = FOOOFGroup(*FOOOF_SETTINGS, verbose=False) # Save out a settings file fg.save(file_name=GROUP + '_fooof_group_settings', file_path=PATHS.fooofs_path, save_settings=True) for sub in SUBJ_NUMS: print('Current Subject' + str(sub)) # load subject data subj_fname = str(sub) + "_resampled.set" full_path = os.path.join(PATHS.data_path, subj_fname) path_check = Path(full_path) if path_check.is_file(): #eeg_data = mne.io.read_raw_eeglab(full_path, event_id_func=None, preload=True) eeg_data = mne.io.read_raw_eeglab(full_path, preload=True) evs = mne.io.eeglab.read_events_eeglab(full_path, EV_DICT) new_evs = np.empty(shape=(0, 3)) for ev_label in BLOCK_EVS: ev_code = EV_DICT[ev_label] temp = evs[evs[:, 2] == ev_code] new_evs = np.vstack([new_evs, temp]) eeg_data.add_events(new_evs) # Set EEG to average reference eeg_data.set_eeg_reference() ## PRE-PROCESSING: ICA if RUN_ICA: # ICA settings method = 'fastica' n_components = 0.99 random_state = 47 reject = {'eeg': 20e-4} # Initialize ICA object ica = ICA(n_components=n_components, method=method, random_state=random_state) # High-pass filter data for running ICA eeg_data.filter(l_freq=1., h_freq=None, fir_design='firwin') # Fit ICA ica.fit(eeg_data, reject=reject) # Find components to drop, based on correlation with EOG channels drop_inds = [] for chi in EOG_CHS: inds, scores = ica.find_bads_eog(eeg_data, ch_name=chi, threshold=2.5, l_freq=1, h_freq=10, verbose=False) drop_inds.extend(inds) drop_inds = list(set(drop_inds)) # Set which components to drop, and collect record of this ica.exclude = drop_inds # Save out ICA solution ica.save(pjoin(PATHS.ica_path, str(sub) + '-ica.fif')) # Apply ICA to data eeg_data = ica.apply(eeg_data) ## EPOCH BLOCKS events = mne.find_events(eeg_data) rest_epochs = mne.Epochs(eeg_data, events=events, event_id=REST_EVENT_ID, tmin=5, tmax=125, baseline=None, preload=True) trial_epochs = mne.Epochs(eeg_data, events=events, event_id=TRIAL_EVENT_ID, tmin=5, tmax=125, baseline=None, preload=True) ## PRE-PROCESSING: AUTO-REJECT if RUN_AUTOREJECT: # Initialize and run autoreject across epochs ar = AutoReject(n_jobs=4, verbose=False) epochs, rej_log = ar.fit_transform(epochs, True) # Drop same trials from filtered data rest_epochs.drop(rej_log.bad_epochs) trial_epochs.drop(rej_log.bad_epochs) # Collect list of dropped trials dropped_trials[s_ind, 0:sum(rej_log.bad_epochs)] = np.where( rej_log.bad_epochs)[0] # Set montage chs = mne.channels.read_montage('standard_1020', rest_epochs.ch_names[:-1]) rest_epochs.set_montage(chs) trial_epochs.set_montage(chs) # Calculate power spectra rest_psds, rest_freqs = mne.time_frequency.psd_welch(rest_epochs, fmin=1., fmax=50., n_fft=2000, n_overlap=250, n_per_seg=500) trial_psds, trial_freqs = mne.time_frequency.psd_welch( trial_epochs, fmin=1., fmax=50., n_fft=2000, n_overlap=250, n_per_seg=500) # Setting frequency range freq_range = [3, 30] ## FOOOF the Data # Rest Data for ind, entry in enumerate(rest_psds): rest_fooof_psds = rest_psds[ind, :, :] fg.fit(rest_freqs, rest_fooof_psds, freq_range) fg.save(file_name=str(sub) + 'fooof_group_results' + str(ind), file_path=PATHS.fooofs_rest_path, save_results=True) # Trial Data for ind, entry in enumerate(trial_psds): trial_fooof_psds = trial_psds[ind, :, :] fg.fit(trial_freqs, trial_fooof_psds, freq_range) fg.save(file_name=str(sub) + 'fooof_group_results' + str(ind), file_path=PATHS.fooofs_trial_path, save_results=True) print('Subject Saved') else: print('Current Subject' + str(sub) + ' does not exist') print(path_check) print('Pre-processing Complete')
if subject == 'S207': data_eeg.info['bads'].append('A15') if subject == 'S228': data_eeg.info['bads'].append('A24') #%% Blink Removal blinks = find_blinks(data_eeg, ch_name=['A1'], thresh=100e-6, l_trans_bandwidth=0.5, l_freq=1.0) blink_epochs = mne.Epochs(data_eeg, blinks, 998, tmin=-0.25, tmax=0.25, proj=False, baseline=(-0.25, 0), reject=dict(eeg=500e-6)) Projs = compute_proj_epochs(blink_epochs, n_grad=0, n_mag=0, n_eeg=8, verbose='DEBUG') if subject == 'S207': ocular_projs = [Projs[0]] elif subject == 'S228': ocular_projs = [Projs[0]] elif subject == 'S236': ocular_projs = [Projs[0]]
resp=1, chpi=1e-4) scalings['eeg'] = 40e-6 scalings['misc'] = 150e-6 #raw.plot(events =blinks, scalings = scalings, proj = False) #eog_events = mne.preprocessing.find_eog_events(raw,ch_name='EXG5') #eog_epochs = mne.preprocessing.create_eog_epochs(raw,ch_name='EXG5') from mne.preprocessing.ssp import compute_proj_epochs epochs_blinksHB = mne.Epochs( rawHB, blinksHB, 998, tmin=-0.25, tmax=0.25, proj=False, # why is proj true is we havent generated one yet? baseline=(-0.25, 0), # what is point of baseline correction for blinks reject=dict(eeg=500e-6)) epochs_blinksRS = mne.Epochs( rawRS, blinksRS, 998, tmin=-0.25, tmax=0.25, proj=False, # why is proj true is we havent generated one yet? baseline=(-0.25, 0), # what is point of baseline correction for blinks reject=dict(eeg=500e-6)) blink_projsHB = compute_proj_epochs(epochs_blinksHB,
# Simulate the raw data, with a lowpass filter on the noise stcs = [(stc_signal, unit_impulse(n_samp, dtype=int) * 1), (stc_noise, unit_impulse(n_samp, dtype=int) * 2)] # stacked in time duration = (len(stc_signal.times) * 2) / sfreq raw = simulate_raw(info, stcs, forward=fwd) add_noise(raw, cov, iir_filter=[4, -4, 0.8], random_state=rand) ############################################################################### # We create an :class:`mne.Epochs` object containing two trials: one with # both noise and signal and one with just noise events = mne.find_events(raw, initial_event=True) tmax = (len(stc_signal.times) - 1) / sfreq epochs = mne.Epochs(raw, events, event_id=dict(signal=1, noise=2), tmin=0, tmax=tmax, baseline=None, preload=True) assert len(epochs) == 2 # ensure that we got the two expected events # Plot some of the channels of the simulated data that are situated above one # of our simulated sources. picks = mne.pick_channels(epochs.ch_names, mne.read_selection('Left-frontal')) epochs.plot(picks=picks) ############################################################################### # Power mapping # ------------- # With our simulated dataset ready, we can now pretend to be researchers that # have just recorded this from a real subject and are going to study what parts # of the brain communicate with each other. # # First, we'll create a source estimate of the MEG data. We'll use both a
raw_fname = op.join(data_path, 'sub-{}'.format(subject), 'meg', 'sub-{}_task-{}_meg.fif'.format(subject, task)) fwd_fname = op.join(data_path, 'derivatives', 'sub-{}'.format(subject), 'sub-{}_task-{}-fwd.fif'.format(subject, task)) # Read evoked raw = mne.io.read_raw_fif(raw_fname) raw.pick_types(meg=True, eog=True, stim=True) events = mne.find_events(raw, stim_channel='STI 014') reject = dict(grad=4000e-13, eog=350e-6) event_id, tmin, tmax = dict(unknown=1), -0.5, 0.5 epochs = mne.Epochs(raw, events, event_id, tmin, tmax, reject=reject, baseline=(None, 0)) evoked = epochs.average() evoked.crop(tmin=0.0, tmax=0.2) # Compute noise covariance matrix cov = mne.compute_covariance(epochs, rank='info', tmax=0.) del epochs, raw # Handling forward solution forward = mne.read_forward_solution(fwd_fname) ###############################################################################
############################################################################### # Set parameters raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif' # Setup for reading the raw data raw = fiff.Raw(raw_fname) event_id = 998 eog_events = mne.preprocessing.find_eog_events(raw, event_id) # Read epochs picks = fiff.pick_types(raw.info, meg=False, eeg=False, stim=False, eog=True, exclude='bads') tmin, tmax = -0.2, 0.2 epochs = mne.Epochs(raw, eog_events, event_id, tmin, tmax, picks=picks) data = epochs.get_data() print "Number of detected EOG artifacts : %d" % len(data) ############################################################################### # Plot EOG artifacts plt.plot(1e3 * epochs.times, np.squeeze(data).T) plt.xlabel('Times (ms)') plt.ylabel('EOG (muV)') plt.show()
def main(): # Initialize fg fg = FOOOFGroup(verbose=False, peak_width_limits=[1, 6], min_peak_amplitude=0.075, max_n_peaks=6, peak_threshold=1) # Save out a settings file fg.save(file_name='PBA_fooof_group_settings', file_path=save_path, save_settings=True) # START LOOP for sub in subj_dat_num: print('Current Subject' + str(sub)) # load subjec data subj_dat_fname = str(sub) + "_resampled.set" full_path = os.path.join(base_path, subj_dat_fname) path_check = Path(full_path) if path_check.is_file(): eeg_dat = mne.io.read_raw_eeglab(full_path, event_id_func=None, preload=True) evs = mne.io.eeglab.read_events_eeglab(full_path, EV_DICT) new_evs = np.empty(shape=(0, 3)) #for ev_code in [3000, 5000]: for ev_label in ['Rest_Start', 'Exp_Block_Start']: ev_code = EV_DICT[ev_label] temp = evs[evs[:, 2] == ev_code] new_evs = np.vstack([new_evs, temp]) eeg_dat.add_events(new_evs) # set EEG average reference eeg_dat.set_eeg_reference() ## PRE-PROCESSING: ICA if RUN_ICA: # ICA settings method = 'fastica' n_components = 0.99 random_state = 47 reject = {'eeg': 20e-4} # Initialize ICA object ica = ICA(n_components=n_components, method=method, random_state=random_state) # High-pass filter data for running ICA eeg_dat.filter(l_freq=1., h_freq=None, fir_design='firwin') # Fit ICA ica.fit(eeg_dat, reject=reject) # Find components to drop, based on correlation with EOG channels drop_inds = [] for chi in EOG_CHS: inds, scores = ica.find_bads_eog(eeg_dat, ch_name=chi, threshold=2.5, l_freq=1, h_freq=10, verbose=False) drop_inds.extend(inds) drop_inds = list(set(drop_inds)) # Set which components to drop, and collect record of this ica.exclude = drop_inds dropped_components[s_ind, 0:len(drop_inds)] = drop_inds # Save out ICA solution ica.save(pjoin(RES_PATH, 'ICA', subj_label + '-ica.fif')) # Apply ICA to data eeg_dat = ica.apply(eeg_dat) events = mne.find_events(eeg_dat) rest_event_id = {'Rest_Start': 3000} trial_event_id = {'Exp_Block_Start': 5000} epochs = mne.Epochs(eeg_dat, events=events, tmin=5, tmax=125, baseline=None, preload=True) rest_epochs = mne.Epochs(eeg_dat, events=events, event_id=rest_event_id, tmin=5, tmax=125, baseline=None, preload=True) trial_epochs = mne.Epochs(eeg_dat, events=events, event_id=trial_event_id, tmin=5, tmax=125, baseline=None, preload=True) ## PRE-PROCESSING: AUTO-REJECT if RUN_AUTOREJECT: # Initialize and run autoreject across epochs ar = AutoReject(n_jobs=4, verbose=False) epochs, rej_log = ar.fit_transform(epochs, True) # Drop same trials from filtered data rest_epochs.drop(rej_log.bad_epochs) trial_epochs.drop(rej_log.bad_epochs) # Collect list of dropped trials dropped_trials[s_ind, 0:sum(rej_log.bad_epochs)] = np.where( rej_log.bad_epochs)[0] # Set montage chs = mne.channels.read_montage('standard_1020', rest_epochs.ch_names[:-1]) rest_epochs.set_montage(chs) trial_epochs.set_montage(chs) # Calculate PSDs rest_psds, rest_freqs = mne.time_frequency.psd_welch(rest_epochs, fmin=1., fmax=50., n_fft=2000, n_overlap=250, n_per_seg=500) trial_psds, trial_freqs = mne.time_frequency.psd_welch( trial_epochs, fmin=1., fmax=50., n_fft=2000, n_overlap=250, n_per_seg=500) # Setting frequency range freq_range = [3, 32] # FOOOFing Data # Rest for ind, entry in enumerate(rest_psds): rest_fooof_psds = rest_psds[ind, :, :] fg.fit(rest_freqs, rest_fooof_psds, freq_range) fg.save(file_name=str(sub) + 'fooof_group_results' + str(ind), file_path=rest_save_path, save_results=True) for ind, entry in enumerate(trial_psds): trial_fooof_psds = trial_psds[ind, :, :] fg.fit(trial_freqs, trial_fooof_psds, freq_range) fg.save(file_name=str(sub) + 'fooof_group_results' + str(ind), file_path=trial_save_path, save_results=True) print('Subject Saved') else: print('Current Subject' + str(sub) + ' does not exist') print(path_check) print('Pre-processing Complete')
# paths to mne datasets - sample sEEG and FreeSurfer's fsaverage subject # which is in MNI space misc_path = mne.datasets.misc.data_path() sample_path = mne.datasets.sample.data_path() subjects_dir = sample_path / 'subjects' # use mne-python's fsaverage data fetch_fsaverage(subjects_dir=subjects_dir, verbose=True) # downloads if needed # %% # Let's load some sEEG data with channel locations and make epochs. raw = mne.io.read_raw(misc_path / 'seeg' / 'sample_seeg_ieeg.fif') events, event_id = mne.events_from_annotations(raw) epochs = mne.Epochs(raw, events, event_id, detrend=1, baseline=None) epochs = epochs['Response'][0] # just process one epoch of data for speed # %% # Let use the Talairach transform computed in the Freesurfer recon-all # to apply the Freesurfer surface RAS ('mri') to MNI ('mni_tal') transform. montage = epochs.get_montage() # first we need a head to mri transform since the data is stored in "head" # coordinates, let's load the mri to head transform and invert it this_subject_dir = misc_path / 'seeg' head_mri_t = mne.coreg.estimate_head_mri_t('sample_seeg', this_subject_dir) # apply the transform to our montage montage.apply_trans(head_mri_t)
events = mne.events_from_annotations(raw) event_id = {'CS': events[1]['8Bit 1'] + 10} #,'US': events[1]['8Bit 2']+10 events = events[0] for i in range(len(events) - 1): if events[i, 2] == 1 and events[i + 1, 2] == 2 and ( events[i + 1, 0] - events[i, 0]) < 1000: events[i, 2] = events[i, 2] + 10 events[i + 1, 2] = events[i + 1, 2] + 10 if os.path.isfile(fname.replace('.edf', "-bads.txt")): with open(fname.replace('.edf', "-bads.txt"), 'r') as filehandle: raw.info['bads'] = json.load(filehandle) if os.path.isfile(fname.replace('.edf', "-annot.fif")): raw.set_annotations( mne.read_annotations(fname.replace('.edf', "-annot.fif"))) raw.interpolate_bads() raw.filter(0.5, 30) raw.set_eeg_reference('average', projection=False) epochs = mne.Epochs(raw, events=events, event_id=event_id, tmin=-0.1, tmax=0.5, baseline=None) epochs.save(fname.replace(".edf", "-epo.fif"), overwrite=True)
# then, select the blink pattern data_dir = op.realpath(op.join("..","..","data")) ################################################################################ name = names[0] raw = mne.io.read_raw_fif(op.join(temp_dir,name+".fif"),preload=True) epochs,eye_raw = epochs_for_blink_search(raw,reject=dict(eeg=150e-6),window_size=2,return_filtered=True) blinks,channel_average = search_for_blinks(epochs,thresh=50e-6,window_size=2,return_average=True) blink_events = np.stack([blinks , np.zeros(len(blinks)), np.ones(len(blinks))]).astype('int_').T blink_epochs = mne.Epochs(raw,blink_events,tmin=-0.4,tmax=0.4,baseline=None) ica = ICA(n_components=25,method='extended-infomax',random_state=1983) ica.fit(epochs,decim=2) # ica.plot_components() # ica.plot_properties(blink_epochs,picks=[10,18,20,21,22,23]) N = 10 raw_noblinks = ica.apply(raw.copy(),exclude=[N]) raw_noblinks.save(op.join(temp_dir,name+"_noblink.fif")) ################################################################################ name = names[1] raw = mne.io.read_raw_fif(op.join(temp_dir,name+".fif"),preload=True)