Esempio n. 1
0
def _convert_to_epochs(raw_data):
    """Converts the raw object to an Epochs
    Input:
    - raw_data: instance of mne.RawArray, that has been previously cropped to tmin=in_bed_time
    returns: mne.Epochs
    """
    return mne.make_fixed_length_epochs(
        raw=raw_data,
        duration=EPOCH_DURATION,
        preload=True,
        verbose=False,
    )
Esempio n. 2
0
def plot_topo_hack(normalized_topo):
    ref_sens = mne.io.read_raw_fif('/fast/ICA/CAMCAN/sub-CC621184_ses-rest_task-rest_proc-sss_300srate.fif')
    ref_sens.crop(0, 2)
    ref_sens.pick_types(meg='mag')
    ref_sens.load_data()    
    epochs = mne.make_fixed_length_epochs(ref_sens)    
    evoked = epochs.average()
    if normalized_topo.shape.__len__() == 1:
         evoked._data[:,0]=normalized_topo
         evoked.plot_topomap(times=evoked.times[0], colorbar=False)
    else:
        evoked._data[:,:25]=normalized_topo
        evoked.plot_topomap(times=evoked.times[0:25], ncols=5, nrows=5, colorbar=False)
Esempio n. 3
0
def creating_epochs(raw, tmin=0, tmax=60, verbose=True, duration=0.5):
    '''Function to transform dataset into epochs 

  Parameters
  ----------
  raw : raw file to transform 
  tmin : minimum time to take into account (default = 0)
  tmax: maximum time to take into account (default = 60)
  duration : duration of each epoch (default = 0.5)

  Return
  ------
  Epoch object (mne)
'''

    epochs = make_fixed_length_epochs(raw.crop(tmin=tmin, tmax=tmax),
                                      duration=duration,
                                      verbose=verbose)
    return epochs
Esempio n. 4
0
def creating_epochs(raw, tmin=0, verbose=True, duration=0.5):
    '''
    Plot EEG 

    Parameters
    ----------
    raw : Raw file
    tmin: minimum time to consider
    duration : length of epochs

    Return 
    ------
    Epochs 

    '''
    epochs = make_fixed_length_epochs(raw.crop(tmin=tmin),
                                      duration=duration,
                                      verbose=verbose)
    return epochs
raw.crop(tmax=150).resample(100).pick('meg')
ecg_proj, _ = compute_proj_ecg(raw, ch_name='MEG 0511')  # No ECG chan
raw.add_proj(ecg_proj)
raw.apply_proj()

###############################################################################
# To create fixed length epochs, we simply call the function and provide it
# with the appropriate parameters indicating the desired duration of epochs in
# seconds, whether or not to preload data, whether or not to reject epochs that
# overlap with raw data segments annotated as bad, whether or not to include
# projectors, and finally whether or not to be verbose. Here, we choose a long
# epoch duration (30 seconds). To conserve memory, we set ``preload`` to
# ``False``.

epochs = mne.make_fixed_length_epochs(raw, duration=30, preload=False)

###############################################################################
# Characteristics of Fixed Length Epochs
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# Fixed length epochs are generally unsuitable for event-related analyses. This
# can be seen in an image map of our fixed length
# epochs. When the epochs are averaged, as seen at the bottom of the plot,
# misalignment between onsets of event-related activity results in noise.

event_related_plot = epochs.plot_image(picks=['MEG 1142'])

###############################################################################
# For information about creating epochs for event-related analyses, please see
# :ref:`tut-epochs-class`.
#
Esempio n. 6
0
# Apply common average reference on EEG channels
raw.pick_types(meg=False, eeg=True).apply_proj()

# Select two EEG channels for the example, preferably without artifact at the
# beginning, to have a reliable calibration
ch_names = ['EEG 010', 'EEG 015']

# Apply band-pass filter between 1 and 35 Hz
raw.filter(1., 35., method='iir', picks=ch_names)

# Epoch time-series with a sliding window
duration = 2.5  # duration of epochs
interval = 0.2  # interval between successive epochs
epochs = make_fixed_length_epochs(raw,
                                  duration=duration,
                                  overlap=duration - interval,
                                  verbose=False)
epochs_data = 5e5 * epochs.get_data(picks=ch_names)

# Estimate spatial covariance matrices
covs = Covariances(estimator='lwf').transform(epochs_data)

###############################################################################
# Offline Calibration of Potato
# -----------------------------
#
# 2D projection of the z-score map of the Riemannian potato, for 2x2 covariance
# matrices (in blue if clean, in red if artifacted) and their reference matrix
# (in black). The colormap defines the z-score and a chosen isocontour defines
# the potato. It reproduces Fig 1 of reference [2]_.
# Riemannian potato
# -----------------
#
# Riemannian potato (RP) [2]_ selects all channels and filter between 1 and
# 35 Hz.

# RP definition
z_th = 2.0  # z-score threshold
low_freq, high_freq = 1., 35.
rp = Potato(metric='riemann', threshold=z_th)

# EEG processing for RP
rp_sig = filter_bandpass(raw, low_freq, high_freq)  # band-pass filter
rp_epochs = make_fixed_length_epochs(  # epoch time-series
    rp_sig,
    duration=duration,
    overlap=duration - interval,
    verbose=False)
rp_covs = Covariances(estimator='scm').transform(rp_epochs.get_data())

# RP training
train_covs = 45  # nb of matrices for training
train_set = range(train_covs)
rp.fit(rp_covs[train_set])

###############################################################################
# Riemannian potato field
# -----------------------
#
# Riemannian potato field (RPF) [1]_ combines several potatoes of low
# dimensionality, each one designed to capture a different kind of artifact
Esempio n. 8
0
# %%
# We will now load in the raw data from the bdf file downloaded from OpenNeuro
# and, since this is resting-state data without any events, make regularly
# spaced events with which to epoch the raw data. In the averaged plot,
# we can see that there may be some eyeblink
# artifact contamination but, overall, the data is typical of
# resting-state EEG.

raw_fname = op.join(target_dir, f'sub-{subject_id}', 'ses-off', 'eeg',
                    'sub-pd14_ses-off_task-rest_eeg.bdf')
raw = mne.io.read_raw_bdf(raw_fname, preload=True)
dig_montage = mne.channels.make_standard_montage('biosemi32')
raw.drop_channels(
    [ch for ch in raw.ch_names if ch not in dig_montage.ch_names])
raw.set_montage(dig_montage)  # use the standard montage
epochs = mne.make_fixed_length_epochs(raw, duration=3, preload=True)

# plot the data
epochs.average().detrend().plot_joint()

# %%
# Autoreject without any other preprocessing
# ------------------------------------------
# Now, we'll naively apply autoreject as our first preprocessing step.
#
# As we can see in the plot of the rejected epochs, there are many eyeblinks
# that caused the epoch to be dropped. This resulted in a lot of the data
# being lost.
#
# The data looks fairly clean already and we don't want to interpolate
# more than a few sensors since we only have 32 to start, so the
Esempio n. 9
0
def ica_raw(raw, montage):
    """Automatic artifacts identification - raw data is modified in place
    Parameters:
    ----------:
    raw:    mne.io.Raw
            raw object of EEG data after processing by previous steps (filter
            and bad channels removal)
    montage:    str
                montage
    Returns:
    ----------
    raw:   mne.io.Raw
           instance of raw data after removing artifacts (eog)
    output_dict_ica:  dictionary
                      dictionary with relevant ica information
    """

    # set montage
    # return if value is invalid
    try:
        raw.set_montage(montage)
    except ValueError:
        print("Invalid value for the 'montage' parameter. Allowed values are\
        'EGI_256', 'GSN-HydroCel-128', 'GSN-HydroCel-129', 'GSN-HydroCel-256',\
        'GSN-HydroCel-257', 'GSN-HydroCel-32', 'GSN-HydroCel-64_1.0',\
        'GSN-HydroCel-65_1.0', 'biosemi128', 'biosemi16', 'biosemi160',\
        'biosemi256', 'biosemi32', 'biosemi64', 'easycap-M1', 'easycap-M10',\
        'mgh60', 'mgh70', 'standard_1005', 'standard_1020',\
        'standard_alphabetic', 'standard_postfixed', 'standard_prefixed',\
        'standard_primed', 'artinis-octamon', and 'artinis-brite23'.")

        montage_error = "Invalid value for the 'montage' parameter. Allowed values are 'EGI_256', \
        'GSN-HydroCel-128', 'GSN-HydroCel-129', 'GSN-HydroCel-256',\
        'GSN-HydroCel-257', 'GSN-HydroCel-32', 'GSN-HydroCel-64_1.0',\
        'GSN-HydroCel-65_1.0', 'biosemi128', 'biosemi16', 'biosemi160',\
        'biosemi256', 'biosemi32', 'biosemi64', 'easycap-M1', 'easycap-M10',\
        'mgh60', 'mgh70', 'standard_1005', 'standard_1020',\
        'standard_alphabetic', 'standard_postfixed', 'standard_prefixed',\
        'standard_primed', 'artinis-octamon', and 'artinis-brite23'."

        ica_details = {"ERROR": montage_error}
        return raw, {"Ica": ica_details}

    # prepica - step1 - filter
    # High-pass with 1. Hz
    raw_filtered_1 = raw.copy()
    raw_filtered_1 = raw_filtered_1.load_data().filter(l_freq=1, h_freq=None)

    # prepica - step2 - segment continuous EEG into arbitrary 1-second epochs
    epochs_prep = mne.make_fixed_length_epochs(raw_filtered_1,
                                               duration=1.0,
                                               preload=True,
                                               overlap=0.0)
    # compute the number of original epochs
    epochs_original = epochs_prep.__len__()

    # drop epochs that are excessively “noisy”
    reject_criteria = dict(eeg=1000e-6)       # 1000 µV
    epochs_prep.drop_bad(reject=reject_criteria)

    # compute the number of epochs after removal
    epochs_bads_removal = epochs_prep.__len__()
    epochs_bads = epochs_original - epochs_bads_removal

    # ica
    method = 'picard'
    max_iter = 500
    fit_params = dict(fastica_it=5)
    ica = mne.preprocessing.ICA(n_components=None,
                                method=method,
                                max_iter=max_iter,
                                fit_params=fit_params)
    ica.fit(epochs_prep)

    # exclude eog
    # Note: should be edited later for "ch_name"
    eog_indices, eog_scores = ica.find_bads_eog(epochs_prep, ch_name='FC1')
    ica.exclude = eog_indices

    # reapplying the matrix back to raw data -- modify in place
    raw_icaed = ica.apply(raw.load_data())

    ica_details = {"original epochs": epochs_original,
                   "bad epochs": epochs_bads,
                   "bad epochs rate": epochs_bads / epochs_original,
                   "eog indices": eog_indices,
                   "eog_scores": eog_scores}

    return raw_icaed, {"Ica": ica_details}
Esempio n. 10
0
analy_duration = 60
between_duration = 60
filelist = listdir(proc_dir)
epolen = 10
min_bad = 25
picks = ["Fz", "AFz", "Fp1", "Fp2", "FC1", "FC2", "Cz"]
n_jobs = 8

subjs = ["1001", "1002"]
conds = ["Stim"]

for subj in subjs:
    for cond in conds:
        raw = mne.io.Raw("{}of_EPI_{}_{}-raw.fif".format(proc_dir, subj, cond),
                         preload=True)
        epo = mne.make_fixed_length_epochs(raw, duration=epolen)
        power = tfr_morlet(epo, [0.75],
                           n_cycles=3,
                           picks=picks,
                           average=False,
                           return_itc=False,
                           n_jobs=n_jobs)

        tfr = np.zeros(0)
        for epo_tfr in power.__iter__():
            tfr = np.concatenate((tfr, np.mean(epo_tfr[:, 0, ], axis=0)))
        tfr_aschan = np.zeros(len(raw))
        tfr_aschan[:len(tfr)] = tfr

        winner_std = np.inf
        for tfr_upper_thresh in tfr_thresh_range:
Esempio n. 11
0
def main(filename=None, subjid=None, trans=None, info=None, line_freq=None, 
         emptyroom_filename=None, subjects_dir=None):
    
    raw = hcp.read_raw(subjid, 'rest', hcp_path='/data/EnigmaMeg/HCP/HCP_MEG')
    raw.load_data()
    
    eraw = hcp.read_raw(subjid, 'noise_empty_room',hcp_path='/data/EnigmaMeg/HCP/HCP_MEG')
    eraw.load_data()
    
    hcp.preprocessing.apply_ref_correction(raw)
    hcp.preprocessing.apply_ref_correction(eraw)
    #Below may be useful for testing ICA components
    #ica_mat = hcp.read_ica(subjid, 'rest')
    #annotations_dict=hcp.read_annot(subjid, 'rest')
    #hcp.preprocessing.apply_ica_hcp(raw, ica_mat, annotations_dict['ica']['ecg_eog_ic'])
    
    ## Load and prefilter continuous data
    #raw=load_data(filename)
    #eraw=load_data(emptyroom_filename)
    
    if type(raw)==mne.io.ctf.ctf.RawCTF:
        raw.apply_gradient_compensation(3)
    
    ## Test SSS bad channel detection for non-Elekta data
    # !!!!!!!!!!!  Currently no finecal or crosstalk used  !!!!!!!!!!!!!!!
    # if filename[-3:]=='fif':
    #     raw_bads_dict = assess_bads(filename)
    #     eraw_bads_dict = assess_bads(emptyroom_filename, is_eroom=True)
        
    #     raw.info['bads']=raw_bads_dict['noisy'] + raw_bads_dict['flat']
    #     eraw.info['bads']=eraw_bads_dict['noisy'] + eraw_bads_dict['flat']
    
    resample_freq=300
    
    raw.resample(resample_freq)
    eraw.resample(resample_freq)
    
    raw.filter(0.5, 140)
    eraw.filter(0.5, 140)
    
    if line_freq==None:
        try:
            line_freq = raw.info['line_freq']  # this isn't present in all files
        except:
            raise(ValueError('Could not determine line_frequency'))
    notch_freqs = np.arange(line_freq, 
                            resample_freq/2, 
                            line_freq)
    raw.notch_filter(notch_freqs)
    
    
    ## Create Epochs and covariance 
    epochs = mne.make_fixed_length_epochs(raw, duration=4.0, preload=True)
    epochs.apply_baseline(baseline=(0,None))
    cov = mne.compute_covariance(epochs)
    
    er_epochs=mne.make_fixed_length_epochs(eraw, duration=4.0, preload=True)
    er_epochs.apply_baseline(baseline=(0,None))
    er_cov = mne.compute_covariance(er_epochs)
    
    os.environ['SUBJECTS_DIR']=subjects_dir
    src = mne.read_source_spaces(info.src_filename)
    bem = mne.read_bem_solution(info.bem_sol_filename)
    fwd = mne.make_forward_solution(epochs.info, trans, src, bem)
    
    data_info = epochs.info
    
    from mne.beamformer import make_lcmv, apply_lcmv_epochs
    filters = make_lcmv(epochs.info, fwd, cov, reg=0.01,
                        noise_cov=er_cov, pick_ori='max-power',
                        weight_norm='unit-noise-gain', rank=None)
    
    labels_lh=mne.read_labels_from_annot(subjid, parc='aparc_sub',
                                        subjects_dir=subjects_dir, hemi='lh') 
    labels_rh=mne.read_labels_from_annot(subjid, parc='aparc_sub',
                                        subjects_dir=subjects_dir, hemi='rh') 
    labels=labels_lh + labels_rh 
    
    results_stcs = apply_lcmv_epochs(epochs, filters, return_generator=True)#, max_ori_out='max_power')
    
    #Monkey patch of mne.source_estimate to perform 15 component SVD
    label_ts = mod_source_estimate.extract_label_time_course(results_stcs, 
                                                             labels, 
                                                             fwd['src'],
                                                             mode='pca15_multitaper')
    
    #Convert list of numpy arrays to ndarray (Epoch/Label/Sample)
    label_stack = np.stack(label_ts)

    #HACK HARDCODED FREQ BINS
    freq_bins = np.linspace(1,45,177)    ######################################3######### FIX

    #Initialize 
    label_power = np.zeros([len(labels), len(freq_bins)])  
    alpha_peak = np.zeros(len(labels))
    
    #Create PSD for each label
    for label_idx in range(len(labels)):
        print(str(label_idx))
        current_psd = label_stack[:,label_idx, :].mean(axis=0) 
        label_power[label_idx,:] = current_psd
        
        spectral_image_path = os.path.join(info.outfolder, 'Spectra_'+
                                            labels[label_idx].name + '.png')

        try:
            tmp_fmodel = calc_spec_peak(freq_bins, current_psd, 
                            out_image_path=spectral_image_path)
            
            #FIX FOR MULTIPLE ALPHA PEAKS
            potential_alpha_idx = np.where((8.0 <= tmp_fmodel.peak_params[:,0] ) & \
                                    (tmp_fmodel.peak_params[:,0] <= 12.0 ) )[0]
            if len(potential_alpha_idx) != 1:
                alpha_peak[label_idx] = np.nan         #############FIX ###########################3 FIX     
            else:
                alpha_peak[label_idx] = tmp_fmodel.peak_params[potential_alpha_idx[0]][0]
        except:
            alpha_peak[label_idx] = np.nan  #Fix <<<<<<<<<<<<<<    
        
    #Save the label spectrum to assemble the relative power
    freq_bin_names=[str(binval) for binval in freq_bins]
    label_spectra_dframe = pd.DataFrame(label_power, columns=[freq_bin_names])
    label_spectra_dframe.to_csv( os.path.join(info.outfolder, 'label_spectra.csv') , index=False)
    # with open(os.path.join(info.outfolder, 'label_spectra.npy'), 'wb') as f:
    #     np.save(f, label_power)
    
    relative_power = label_power / label_power.sum(axis=1, keepdims=True)

    #Define bands
    bands = [[1,3], [3,6], [8,12], [13,35], [35,55]]
    band_idxs = get_freq_idx(bands, freq_bins)

    #initialize output
    band_means = np.zeros([len(labels), len(bands)]) 
    #Loop over all bands, select the indexes assocaited with the band and average    
    for mean_band, band_idx in enumerate(band_idxs):
        band_means[:, mean_band] = relative_power[:, band_idx].mean(axis=1) 
    
    output_filename = os.path.join(info.outfolder, 'Band_rel_power.csv')
    

    bands_str = [str(i) for i in bands]
    label_names = [i.name for i in labels]
    
    output_dframe = pd.DataFrame(band_means, columns=bands_str, 
                                 index=label_names)
    output_dframe['AlphaPeak'] = alpha_peak
    output_dframe.to_csv(output_filename, sep='\t')    
Esempio n. 12
0
def test_beamformer():
   
    #Load filenames from test datasets
    from enigmeg.test_data.get_test_data import datasets
    test_dat = datasets().ctf

    meg_filename = test_dat['meg_rest'] 
    subjid = test_dat['subject']
    subjects_dir = test_dat['SUBJECTS_DIR'] 
    trans_fname = test_dat['trans']
    src_fname = test_dat['src']
    bem = test_dat['bem']
    
    outfolder = './tmp'  #<<< Change this ############################
    
    raw = mne.io.read_raw_ctf(meg_filename, preload=True)
    trans = mne.read_trans(trans_fname)
    # info.subjid, info.subjects_dir = subjid, subjects_dir
    
    raw.apply_gradient_compensation(3)
    raw.resample(300)
    raw.filter(1.0, None)
    raw.notch_filter([60,120])
    eraw.notch_filter([60,120])
    
    epochs = mne.make_fixed_length_epochs(raw, duration=4.0, preload=True)

    data_cov = mne.compute_covariance(epochs, method='empirical')  
    
    eroom_filename = test_dat['meg_eroom'] 
    eroom_raw = mne.io.read_raw_ctf(eroom_filename, preload=True)
    eroom_raw.resample(300)
    eroom_raw.notch_filter([60,120])
    eroom_raw.filter(1.0, None)
    
    eroom_epochs = mne.make_fixed_length_epochs(eroom_raw, duration=4.0)
    noise_cov = mne.compute_covariance(eroom_epochs)

    fwd = mne.make_forward_solution(epochs.info, trans, src_fname, 
                                    bem)
    
    from mne.beamformer import make_lcmv, apply_lcmv_epochs
    filters = make_lcmv(epochs.info, fwd, data_cov, reg=0.01,
                        noise_cov=noise_cov, pick_ori='max-power',
                        weight_norm='unit-noise-gain', rank=None)
    
    labels_lh=mne.read_labels_from_annot(subjid, parc='aparc',
                                        subjects_dir=subjects_dir, hemi='lh') 
    labels_rh=mne.read_labels_from_annot(subjid, parc='aparc',
                                        subjects_dir=subjects_dir, hemi='rh') 
    labels=labels_lh + labels_rh 
    
    # labels[1].center_of_mass()
    
    results_stcs = apply_lcmv_epochs(epochs, filters, return_generator=True)#, max_ori_out='max_power')
    
    #Monkey patch of mne.source_estimate to perform 15 component SVD
    label_ts = mod_source_estimate.extract_label_time_course(results_stcs, labels, 
                                                         fwd['src'],
                                       mode='pca15_multitaper')
    
    #Convert list of numpy arrays to ndarray (Epoch/Label/Sample)
    label_stack = np.stack(label_ts)
    # label_stack = np.mean(label_stack, axis=0)

#    freq_bins, _ = label_psd(label_stack[:,0, :], raw.info['sfreq'])
    freq_bins = np.linspace(1,45,177)    ######################################3######### FIX

    #Initialize 
    label_power = np.zeros([len(labels), len(freq_bins)])  
    alpha_peak = np.zeros(len(labels))
    
    #Create PSD for each label
    for label_idx in range(len(labels)):
        print(str(label_idx))
        current_psd = label_stack[:,label_idx, :].mean(axis=0) 
        label_power[label_idx,:] = current_psd
        
        spectral_image_path = os.path.join(outfolder, 'Spectra_'+
                                            labels[label_idx].name + '.png')

        try:
            tmp_fmodel = calc_spec_peak(freq_bins, current_psd, 
                            out_image_path=spectral_image_path)
            
            #FIX FOR MULTIPLE ALPHA PEAKS
            potential_alpha_idx = np.where((8.0 <= tmp_fmodel.peak_params[:,0] ) & \
                                    (tmp_fmodel.peak_params[:,0] <= 12.0 ) )[0]
            if len(potential_alpha_idx) != 1:
                alpha_peak[label_idx] = np.nan         #############FIX ###########################3 FIX     
            else:
                alpha_peak[label_idx] = tmp_fmodel.peak_params[potential_alpha_idx[0]][0]
        except:
            alpha_peak[label_idx] = np.nan  #Fix <<<<<<<<<<<<<<