events = np.zeros((N_events, 3)) events[:, 0] = N_start + N_start * np.arange(N_events) # Events sample. events[:, 1] = np.concatenate([[[0] * Nf], [[1] * Nf], [[2] * Nf], [[3] * Nf], [[4] * Nf], [[5] * Nf]], 1) events[:, 2] = np.concatenate([np.arange(0, 40)] * Nb) # All events have the sample id. events = events.astype(int) # epochs lEpochs = [] for i in range(Ns): data = list_subject_data[i] data = data.swapaxes(2, 3) data = data.reshape([Ne, Nt, Nb * Nf]) data = data.swapaxes(1, 2) data = data.swapaxes(0, 1) lEpochs.append(mne.EpochsArray(data, info, events, tmin=-0.5, verbose=False)) lEpochs[i].set_montage(mne_montage) ## Simulated referenc signals with harmonics mat_Y = np.zeros([Nf, Nh * 2, N_stim]) # [Frequency, Harmonics * 2, Samples] for k in range(0, Nf): for i in range(1, Nh + 1): mat_Y[k, i - 1, :] = np.sin(2 * np.pi * i * vec_freq[k] * vec_t[N_start:N_stop] + vec_phase[k]) mat_Y[k, i, :] = np.cos(2 * np.pi * i * vec_freq[k] * vec_t[N_start:N_stop] + vec_phase[k]) ### Frequency detection using CCA list_result_cca = [] # list to store the subject wise results list_time_cca = [] num_iter = 0
# Scale the noise to be in the ballpark of MEG data noise_scaling = np.linalg.norm(sensor_data) / np.linalg.norm(noise) noise *= noise_scaling # Mix noise and signal with the given signal-to-noise ratio. sensor_data = SNR * sensor_data + noise ############################################################################### # We create an :class:`mne.EpochsArray` object containing two trials: one with # just noise and one with both noise and signal. The techniques we'll be # using in this tutorial depend on being able to contrast data that contains # the signal of interest versus data that does not. epochs = mne.EpochsArray( data=np.concatenate( (noise[np.newaxis, :, :], sensor_data[np.newaxis, :, :]), axis=0), info=info, events=np.array([[0, 0, 1], [10, 0, 2]]), event_id=dict(noise=1, signal=2), ) # Plot the simulated data epochs.plot() ############################################################################### # 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
from xffect.communication import send_data, receive_data from xffect.training import logistic_regression, linear_regression, ridge_regression from xffect.training import lda, svm, qda, cross_validation from xffect.training import train, test import torch import torch.nn as nn import torch.optim as optim from xffect.datasets import emotion_loader info = mne.create_info(2, 100.) event_id = dict(left=0, right=1) A = np.random.randn(10, 2, 40) b = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0] events = np.array([[i, 0, b[i]] for i in range(10)]) epochs = mne.EpochsArray(A, info=info, events=events, event_id=event_id) print(epochs.get_data().shape) transform = True fitted = pca(epochs, info, transform=transform) if transform: print('pca_fit:', fitted.get_data().shape) ica_fit = ica(epochs, info, transform=transform) if transform: print('ica fit:', ica_fit.get_data().shape)
def preprocessEpoch(eeg, info, downsample, tmin, reject=None, mne_reject=1, reject_ch=None, flat=None, bad_channels=[], opt_detrend=1, HP=0, LP=40, phase='zero-double'): n_samples = eeg.shape[0] n_channels = eeg.shape[1] eeg = np.reshape(eeg.T, (1, n_channels, n_samples)) # Baseline start, i.e. 200 ms before stimulus onset # Temporal detrending: if opt_detrend == 1: eeg = detrend(eeg, axis=2, type='linear') epoch = mne.EpochsArray(eeg, info, tmin=tmin, baseline=None, verbose=False) # Drop list of channels known to be problematic: if reject_ch == True: # label of channels to remove bads = ['RAW_CQ', 'GYROX', 'GYROY', 'TIMESTAMP'] badSet = set(bads) # list of all channel names allSet = set(epoch.ch_names) # find the intersection of all available channels and bad channels badSet = badSet.intersection(allSet) badSet = list(badSet) epoch.drop_channels(badSet) # Lowpass epoch.filter(HP, LP, fir_design='firwin', phase=phase, verbose=False) # Downsample epoch.resample(downsample, npad='auto', verbose=False) # Apply baseline correction epoch.apply_baseline(baseline=(None, 0), verbose=False) if reject is not None: # Rejection of channels, either manually defined or based on MNE analysis. Currently not # used. if mne_reject == 1: # Use MNE method to reject+interpolate bad channels from mne.epochs import _is_good from mne.io.pick import channel_indices_by_type # reject=dict(eeg=100) idx_by_type = channel_indices_by_type(epoch.info) A, bad_channels = _is_good(epoch.get_data()[0], epoch.ch_names, channel_type_idx=idx_by_type, reject=reject, flat=flat, full_report=True) print(A) if A == False: epoch.info['bads'] = bad_channels epoch.interpolate_bads(reset_bads=True, verbose=False) else: # Predefined bad_channels epoch.drop_channels(bad_channels) # Re-referencing epoch.set_eeg_reference(verbose=False) # Apply baseline after re-reference epoch.apply_baseline(baseline=(None, 0), verbose=False) epoch = epoch.get_data()[0] return epoch
# (re)load the data to save memory raw = mne.io.read_raw_fif(raw_fname, preload=True) raw.pick_types(meg='grad', eog=True) # we just look at gradiometers # bandpass filter and compute Hilbert raw.filter(fmin, fmax, n_jobs=1, # use more jobs to speed up. l_trans_bandwidth=1, # make sure filter params are the same h_trans_bandwidth=1, # in each band and skip "auto" option. fir_design='firwin') raw.apply_hilbert(n_jobs=1, envelope=False) epochs = mne.Epochs(raw, events, event_id, tmin, tmax, baseline=baseline, reject=dict(grad=4000e-13, eog=350e-6), preload=True) # remove evoked response and get analytic signal (envelope) epochs.subtract_evoked() # for this we need to construct new epochs. epochs = mne.EpochsArray( data=np.abs(epochs.get_data()), info=epochs.info, tmin=epochs.tmin) # now average and move on frequency_map.append(((band, fmin, fmax), epochs.average())) ############################################################################### # Now we can compute the Global Field Power # We can track the emergence of spatial patterns compared to baseline # for each frequency band, with a bootstrapped confidence interval. # # We see dominant responses in the Alpha and Beta bands. fig, axes = plt.subplots(4, 1, figsize=(10, 7), sharex=True, sharey=True) colors = plt.get_cmap('winter_r')(np.linspace(0, 1, 4)) for ((freq_name, fmin, fmax), average), color, ax in zip( frequency_map, colors, axes.ravel()[::-1]): times = average.times * 1e3
fig, ax = plt.subplots(figsize=(6, 4)) mne.viz.plot_compare_evokeds(evokeds, axes=ax, **style_plot) plt.show() ############################################################################### # And finally, for the interaction between concreteness and continuous length # in letters: evokeds = dict() query = "is_concrete == '{0}' & NumberOfLetters == {1}" for concreteness in ("Concrete", "Abstract"): for n_letters in letters: subset = epochs[query.format(concreteness, n_letters)] evokeds["/".join((concreteness, n_letters))] = subset.average() style_plot["linestyles"] = {"Concrete": "-", "Abstract": ":"} fig, ax = plt.subplots(figsize=(6, 4)) mne.viz.plot_compare_evokeds(evokeds, axes=ax, **style_plot) plt.show() ############################################################################### # .. note:: # # Creating an :class:`mne.Epochs` object with metadata is done by passing # a :class:`pandas.DataFrame` to the ``metadata`` kwarg as follows: data = epochs.get_data() metadata = epochs.metadata.copy() epochs_new = mne.EpochsArray(data, epochs.info, metadata=metadata)
def preprocEEG(eeg, EEGfile, SSP=True, threshold=0.1, SSP_start_end=None, reject=None, events_list=None, reject_ch=None, set_ref=1, prefilter=0, flat=None): ''' Preprocesses epoched EEG data. # Input - eeg: Epoched EEG data in the following format: (trials, time samples, channels). - EEGfile: .csv file with raw EEG recordings (for computing SSP projection vectors). If EEGfile == [], the SSP projection vectors are computed based on epochs. - SSP: Whether to apply SSP projections. - Threshold: Value between 0 and 1, only uses the SSP projection vector if it explains more than the pre-defined threshold. - SSP_start_end: Pre-defined "start" and "end" time points (array consisting of two floats) for the length of raw EEG to use for SSP projection vector analysis. Acquired from extractEpochs function. - reject_ch: Whether to reject pre-defined channels. - set_ref: Use average reference - prefilter: Filter before epoch extraction - flat: reject channels having close to no variation # Preprocessing - Linear detrending (channel-wise) - EpochsArray format in MNE - Reject channels (if reject_ch == True) - Bandpass filter - Resample to 100Hz - SSP (if ssp == True) - Reject bad channels/epochs (if ??) - Baseline correction - Rereference to average - Baseline correction # Output - epochs: Epoched EEG data in MNE EpochsArray - projs: SSP projection vectors ''' # INFO channel_names = [ 'P7', 'P4', 'Cz', 'Pz', 'P3', 'P8', 'O1', 'O2', 'T8', 'F8', 'C4', 'F4', 'Fp2', 'Fz', 'C3', 'F3', 'Fp1', 'T7', 'F7', 'Oz', 'PO3', 'AF3', 'FC5', 'FC1', 'CP5', 'CP1', 'CP2', 'CP6', 'AF4', 'FC2', 'FC6', 'PO4' ] channel_types = ['eeg'] * 32 sfreq = 500 # in Hertz montage = 'standard_1020' # Or 1010 info = mne.create_info(channel_names, sfreq, channel_types, montage) info['description'] = 'Enobio' #if reject_ch == 'true': # info['bads'] = ['Fp1','Fp2','Fz','AF3','AF4','T7','T8','F7','F8'] eeg = detrend(eeg, axis=1, type='linear') no_trials = eeg.shape[0] axis_move = np.moveaxis(eeg, [1, 2], [-1, -2]) # Rearrange into MNE format tmin = -0.1 if events_list is not None: # if categories are included event_id = dict(scene=0, face=1) n_epochs = len(events_list) events_list = [int(i) for i in events_list] events = np.c_[np.arange(n_epochs), np.zeros(n_epochs, int), events_list] else: event_id = None events = None epochs = mne.EpochsArray(axis_move, info, events=events, tmin=tmin, event_id=event_id, baseline=None) if reject_ch == True: bads = ['Fp1', 'Fp2', 'Fz', 'AF3', 'AF4', 'T7', 'T8', 'F7', 'F8'] epochs.drop_channels(bads) # Bandpass filtering epochs.filter(HP, LP, fir_design='firwin', phase=phase) # Resampling to 100 Hz epochs.resample(100, npad='auto') # Apply SSP (computed based on the raw/not epoched EEG) if SSP == True: if not EEGfile: print('Computing SSP based on epochs') all_projs = mne.compute_proj_epochs(epochs, n_eeg=10, n_jobs=1, verbose=True) p = [all_projs[i]['explained_var'] for i in range(10)] # If variance explained is above a certain threshold, use the SSP vector for projection threshold_idx = analyzeVar(p, threshold) projs = [] # List with projections above a chosen threshold for idx in threshold_idx: projs.append(all_projs[idx]) else: if reject_ch == None: projs = computeSSP(EEGfile, threshold, SSP_start_end, reject_ch=None) if reject_ch == True: projs = computeSSP(EEGfile, threshold, SSP_start_end, reject_ch=True) # Apply projection to the epochs already defined epochs.add_proj(projs) epochs.apply_proj() else: projs = [] if reject is not None: epochs_copy = epochs.copy() epochs_copy.drop_bad(reject=reject, flat=flat, verbose=None) log = epochs_copy.drop_log not_blank = np.concatenate([x for x in (log) if x]) bad_channels, bad_counts = np.unique(not_blank, return_counts=True) #bad_channels=[bad_channels[a] for a in range(len(bad_channels))] thres = 0.05 * no_trials bad_above_thres = bad_channels[bad_counts > thres] if len(bad_above_thres): print(1) epochs.info['bads'] = bad_above_thres print('Dropping channels:', bad_above_thres) epochs.interpolate_bads(reset_bads=True) # repeat looking for bad epochs epochs_copy = epochs.copy() epochs_copy.drop_bad(reject=reject, flat=flat, verbose=None) log = epochs_copy.drop_log bad_epochs = [i for i, x in enumerate(log) if x] epochs.drop(bad_epochs, reason='Reject') epochs.apply_baseline(baseline=(None, 0)) # Rereferencing epochs.set_eeg_reference() # Apply baseline after filtering epochs.apply_baseline(baseline=(None, 0)) return epochs, projs
def computeWSMI(file_to_compute, word_to_compute, categoria): #nombres de archivo MAT_FULLNAME = file_to_compute MAT_BASENAME = op.basename(MAT_FULLNAME).split('.')[0] MAT_VAR = word_to_compute FIF_FILENAME = '../data/' + categoria + '/' + MAT_BASENAME + '-' + word_to_compute + '-epo.fif' HDF5_FILENAME = '../data/' + categoria + '/' + MAT_BASENAME + '-' + word_to_compute + '-markers.hdf5' MAT_OUTPUT = '../data/' + categoria + '/' + MAT_BASENAME + '-' + word_to_compute + '-wsmi.mat' start_time = time.time() #importamos la matriz desde .mat y la guardo en healthyData print('Loading mat file: ' + MAT_BASENAME + " - " + word_to_compute) #Esto no funciona para mat de ciertas versiones #se importa como samples x channel healthy = {} sio.loadmat(MAT_FULLNAME, healthy) healthyData = np.array(healthy[MAT_VAR]) #Esto funciona para mat version 7.3 #pero este nuevo metodo importa channel x samples entonces transponemos para mantener todo consistente #with h5py.File(MAT_FULLNAME, 'r') as f: # healthyData = np.array(f[MAT_VAR]).transpose() #eliminamos la ultima columna, es decir, el canal Cz healthyData = np.delete(healthyData, 256, 1) #creamos la informacion para el mne container montage = mne.channels.make_standard_montage('GSN-HydroCel-256') channel_names = montage.ch_names sfreq = 1000 info = mne.create_info(channel_names, sfreq, ch_types='eeg', montage=montage) info['description'] = 'egi/256' #hacemos reshape para que quede trials x samples x channel healthyData = np.reshape(healthyData, (30, 4000, 256)) #transponemos para que quede trials x channels x samples healthyData = np.transpose(healthyData, (0, 2, 1)) #epochsarray toma trials x channels x samples epochs = mne.EpochsArray(healthyData, info) epochs.save(FIF_FILENAME, overwrite=True) #importamos el archivo fif epochs = mne.read_epochs(FIF_FILENAME, preload=True) #computamos wsmi m_list = [ SymbolicMutualInformation(tmin=None, tmax=0.6, method='weighted', backend='python', tau=16, method_params={ 'nthreads': 'auto', 'bypass_csd': False }, comment='weighted'), ] mc = Markers(m_list) mc.fit(epochs) #guardamos el archivo mc.save(HDF5_FILENAME, overwrite=True) print('Converting hdf5 to mat...') filename = HDF5_FILENAME with h5py.File(filename, "r") as f: # List all groups a_group_key = list(f.keys())[0] # Get the data data_labels = list(f[a_group_key]) data = f['nice'] values = list(data['marker']['SymbolicMutualInformation']['weighted'] ['key_data_']) sio.savemat(MAT_OUTPUT, {'data': values}) #eliminamos el fif para que no ocupe espacio os.remove(FIF_FILENAME) print('Execution time: ', str(time.time() - start_time), 'sec')
'headmodel_036.mat', 'headmodel_059.mat', 'headmodel_047.mat' ] for s, subject in enumerate(subjects[6:]): fname = os.path.join(root_dir, subject, 'data', eegfiles[s + 6]) mat = loadmat(fname, squeeze_me=True) print(mat['AmpsAl'].shape) print(mat['AmpsM'].shape) print(mat['XAl'].shape) print(mat['Xremoved'].shape) del mat ch_names = mat['EEG']['chanlocs'][()]['labels'] ch_types = ['eeg' for _ in ch_names] sfreq = mat['EEG']['srate'] info = mne.create_info(ch_names=list(ch_names), ch_types=ch_types, sfreq=sfreq) info['bads'] = list(ch_names[np.bool_(mat['rmch'])]) #info.set_montage('standard_1005') data = np.moveaxis(mat['EEG']['data'][()], 2, 0) tmin = float(mat['EEG']['xmin']) selected_epochs = np.nonzero(mat['goodTrialsEEGrest'])[0] epochs = mne.EpochsArray(data, info, tmin=tmin)
############################################################################### # Creating `~mne.Epochs` objects # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # # To create an `~mne.Epochs` object from scratch, you can use the # `mne.EpochsArray` class constructor, which takes an `~mne.Info` object and a # :class:`NumPy array <numpy.ndarray>` of shape ``(n_epochs, n_channels, # n_samples)``. Here we'll create 5 epochs of our 2-channel data, and plot it. # Notice that we have to pass ``picks='misc'`` to the `~mne.Epochs.plot` # method, because by default it only plots :term:`data channels`. data = np.array([[0.2 * sine, 1.0 * cosine], [0.4 * sine, 0.8 * cosine], [0.6 * sine, 0.6 * cosine], [0.8 * sine, 0.4 * cosine], [1.0 * sine, 0.2 * cosine]]) simulated_epochs = mne.EpochsArray(data, info) simulated_epochs.plot(picks='misc', show_scrollbars=False) ############################################################################### # Since we did not supply an events array, the `~mne.EpochsArray` constructor # automatically created one for us, with all epochs having the same event # number: print(simulated_epochs.events[:, -1]) ############################################################################### # If we want to simulate having different experimental conditions, we can pass # an event array (and an event ID dictionary) to the constructor. Since our # epochs are 1 second long and have 200 samples/second, we'll put our events # spaced 200 samples apart, and pass ``tmin=-0.5``, so that the events # land in the middle of each epoch (the events are always placed at time=0 in
############################################################################### # More information about the event codes: subject was either smiling or # frowning event_id = dict(smiling=1, frowning=2) ############################################################################### # Finally, we must specify the beginning of an epoch (the end will be inferred # from the sampling frequency and n_samples) # Trials were cut from -0.1 to 1.0 seconds tmin = -0.1 ############################################################################### # Now we can create the :class:`mne.EpochsArray` object custom_epochs = mne.EpochsArray(data, info, events, tmin, event_id) print(custom_epochs) # We can treat the epochs object as we would any other _ = custom_epochs['smiling'].average().plot(time_unit='s') ############################################################################### # ------------------------------------- # Creating :class:`~mne.Evoked` Objects # ------------------------------------- # If you already have data that is collapsed across trials, you may also # directly create an evoked array. Its constructor accepts an array of # `shape(n_chans, n_times)` in addition to some bookkeeping parameters. # The proper units of measure for the data are listed above.
event_id = events_red[:, 2] # This is used to identify the events. # First column is for the sample number. # Here a data set of 700 ms epochs from 2 channels is # created from sin and cos data. # Any data in shape (n_epochs, n_channels, n_times) can be used. epochs_data = data info = mne.create_info(ch_names=ch_names, sfreq=sfreq, ch_types=['eeg' for x in range(71)]) epochs = mne.EpochsArray( epochs_data, info=info, events=events_red, ) #%% noise_cov = mne.compute_covariance(epochs, method=['shrunk', 'empirical']) #%% trans = mne.read_trans('E:/sub-06_free-trans.fif') subject = 'sub-06_free' conductivity_base = (0.3, 0.006, 0.3) src = mne.setup_source_space(subject, spacing='oct6', add_dist='patch', subjects_dir=subjects_dir) #%% model = mne.make_bem_model(subject=subject,