예제 #1
0
def apply_ICA(subject, runlist, badEEG, badICA):

    from mne.io.pick import _picks_by_type as picks_by_type
    from mne.preprocessing import read_ica
    from mne.io import Raw

    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'

    for run in runlist:
        raw_fname = (data_path + subject + '/' + run + '_trans_sss.fif')
        # read raw data
        raw = []
        raw = Raw(raw_fname, preload=True)
        raw.info['bads'] = badEEG

        # Load ICA file for each data type (MEG and EEG)
        for ch_type, picks in picks_by_type(raw.info, meg_combined=True):
            # Read ICA file
            ica = []
            ica = read_ica(data_path + subject + '/mne_python/ICA/ICA_' +
                           ch_type + '_allRuns')
            # Apply ICA projection
            ica.exclude += badICA[ch_type]
            raw = ica.apply(raw, copy=True)
        # Interpolate bad EEG channels
        raw = raw.copy().interpolate_bads()
        # Save artifacts-corrected raw data
        raw.save((data_path + subject + '/' + run + 'ICAcorr_trans_sss.fif'),
                 overwrite='True')
예제 #2
0
def browse_components(subject, runlist, badEEG):

    import numpy as np
    import mne
    import os.path as op
    import os
    from mne.report import Report
    from mne.io.pick import _picks_by_type as picks_by_type
    from mne.preprocessing import read_ica
    from mne.io import Raw

    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'
    report = Report(subject)

    raw_fnames = []
    for run in runlist:
        raw_fnames.append(data_path + subject + '/' + run + '_trans_sss.fif')
    # read raw data
    raw = Raw(raw_fnames, preload=True)
    raw.info['bads'] = badEEG
    # Highpass filter 1Hz on EOG/ECG channels
    #picks =  mne.pick_types(raw.info, meg=False,eeg=False, eog=True, ecg=True)
    #raw.filter(l_freq = 1, h_freq=30, picks = picks)
    #picks =  mne.pick_types(raw.info, meg=True,eeg=True, eog=True, ecg=True)
    #raw.filter(l_freq = None, h_freq=30, picks = picks,n_jobs = 4)
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=False, ecg=True)
    raw.filter(l_freq=1, h_freq=30, picks=picks)
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=True, ecg=False)
    raw.filter(l_freq=1, h_freq=5, picks=picks)
    picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True)
    raw.filter(l_freq=None, h_freq=30, picks=picks, n_jobs=4)

    results_dir = op.join(data_path, subject, 'artefactICA')
    if not op.exists(results_dir):
        os.makedirs(results_dir)

    # Plot all ICA component
    for ch_type, picks in picks_by_type(
            raw.info, meg_combined=True):  # bad EEG channels are excluded
        # Read ICA file for each data type (MEG and EEG) - created in preprocessing_ica
        ica = read_ica(data_path + subject + '/mne_python/ICA/ICA_' + ch_type +
                       '_allRuns')

        print subject + ' ' + ch_type
        #fig = ica.plot_sources(raw, block = True) # the viewer will open and ICA component can be screened
        #report.add_figs_to_section(fig, 'All ICA sources (%s)' % ch_type,'ICA decomposition')
        fig = ica.plot_components(colorbar=True)
        for i in range(np.shape(fig)[0]):
            report.add_figs_to_section(fig[i],
                                       'All ICA components (%s)' % ch_type,
                                       'ICA decomposition')

    report.save((results_dir + '/AllRuns_checkcomponents.html'),
                open_browser=False,
                overwrite=True)
    del raw
        #                                                   contrast['name']))
        #     continue

        # Prepare plot delta (subtraction, or regression)
        fig1, ax1 = plt.subplots(1, len(chan_types))
        if type(ax1) not in [list, np.ndarray]:
            ax1 = [ax1]
        # Prepare plot all conditions at top level of contrast
        fig2, ax2 = plt.subplots(len(evokeds), len(chan_types))
        ax2 = np.reshape(ax2, len(evokeds) * len(chan_types))

        # Loop across channels
        for ch, chan_type in enumerate(chan_types):
            # Select specific types of sensor
            info = delta.info
            picks = [i for k, p in picks_by_type(info)
                          for i in p if k in chan_type['name']]
            # --------------------------------------------------------------
            # Plot delta (subtraction, or regression)
            # adjust color scale
            mM = np.percentile(np.abs(delta.data[picks,:]), 99.)

            # plot mean sensors x time
            ax1[ch].imshow(delta.data[picks,:], vmin=-mM, vmax=mM,
                              interpolation='none', aspect='auto',
                              cmap='RdBu_r', extent=[min(delta.times),
                                  max(delta.times), 0, len(picks)])
            # add t0
            ax1[ch].plot([0, 0], [0, len(picks)], color='black')
            ax1[ch].set_title(chan_type['name'] + ': ' + delta.comment)
            ax1[ch].set_xlabel('Time')
예제 #4
0
def ICA_denoising(subject, run, badEEG):

    import mne
    import os.path as op
    import os
    import matplotlib
    matplotlib.use('Agg')

    from mne.report import Report
    from mne.io import Raw
    from meeg_preprocessing import compute_ica
    from mne.io.pick import _picks_by_type as picks_by_type

    ###########################################################################
    # jobs and runtime performance
    n_components = 0.99
    decim = 5  # decimation
    n_max_ecg, n_max_eog = 5, 5  # limit components detected due to ECG / EOG
    ica_reject = {'mag': 5e-12, 'grad': 5000e-13, 'eeg': 200e-6}

    ###########################################################################
    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'
    raw_fname = data_path + subject + '/' + run + '_trans_sss.fif'
    report = Report(subject)
    # read raw data
    raw = Raw(raw_fname, preload=True)
    raw.info['bads'] = badEEG
    # Highpass filter 1Hz on EOG/ECG channels
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=True, ecg=True)
    raw.filter(l_freq=1, h_freq=30, picks=picks)
    picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True)
    raw.filter(l_freq=None, h_freq=30, picks=picks)

    ###########################################################################
    # Create the report directory if it doesn't exist
    results_dir = op.join(data_path, subject, 'artefactICA')
    if not op.exists(results_dir):
        os.makedirs(results_dir)

    # Create the results directory if it doesn't exist
    ica_dir = op.join(data_path, subject, 'mne_python/ICA')
    if not op.exists(ica_dir):
        os.makedirs(ica_dir)

    report = mne.report.Report(subject)

    ###########################################################################
    for ch_type, picks in picks_by_type(
            raw.info, meg_combined=True):  # bad EEG channels are excluded
        ica, _ = compute_ica(raw,
                             picks=picks,
                             subject=subject,
                             n_components=n_components,
                             n_max_ecg=n_max_ecg,
                             n_max_eog=n_max_eog,
                             reject=ica_reject,
                             random_state=666,
                             decim=decim,
                             report=report)

        ica.save((ica_dir + '/ICA_' + ch_type + '_' + run))

    report.save((results_dir + '/' + run + '.html'),
                open_browser=False,
                overwrite=True)
    del raw
예제 #5
0
    for filePath in glob.glob(nip_path + '*_auto_ica_sss_raw.fif'):
        temp = mne.io.Raw(filePath, preload=True, add_eeg_ref=False)
        if 'MISC005' in temp.ch_names and nip in ['vV100048', 'Pb160320']:
            temp.drop_channels(['MISC005'])
        if not 'raw' in locals():
            raw = temp
        else:
            raw.append(temp)

    # Exclude bad EEG channels
    raw.info['bads'] = bad_EEG[nip]
    
    ###########################################################################
    ####################### Cycle across channel type #########################
    ###########################################################################    
    for ch_type, picks in picks_by_type(raw.info, meg_combined=True):
        
        #######################################################################
        ##################### Reject head-speaker artifacts ###################
        #######################################################################
       
        # Choose good channels (ch_types + misc)
        picks_misc = np.concatenate([picks, mne.pick_types(raw.info, misc=True, meg=False)])

        # Plot before transformation                   
        misc_epochs = create_misc_epochs(raw, reject=reject, picks=picks_misc, 
                                         baseline=(None, 0), ch_name='MISC004', 
                                         tmax=0.5)

        misc_average = misc_epochs.average()
        report.add_figs_to_section(misc_average.plot(), 
예제 #6
0
def check_ICA_Cleaning(subject, runlist, badEEG, badICA):

    import mne
    import os.path as op
    import os
    from mne.report import Report
    from mne.io.pick import _picks_by_type as picks_by_type
    from mne.preprocessing import read_ica
    from mne.io import Raw
    from mne.preprocessing import create_ecg_epochs, create_eog_epochs

    data_path = '/neurospin/meg/meg_tmp/MTT_MEG_Baptiste/MEG/'
    report = Report(subject)

    raw_fnames = []
    for run in runlist:
        raw_fnames.append(data_path + subject + '/' + run + '_trans_sss.fif')
    # read raw data
    raw = Raw(raw_fnames, preload=True)
    raw.info['bads'] = badEEG
    # Highpass filter 1Hz on EOG/ECG channels
    picks = mne.pick_types(raw.info, meg=False, eeg=False, eog=True, ecg=False)
    raw.filter(l_freq=1, h_freq=2, picks=picks)
    picks = mne.pick_types(raw.info, meg=True, eeg=True, eog=True, ecg=True)
    raw.filter(l_freq=None, h_freq=30, picks=picks, n_jobs=4)

    results_dir = op.join(data_path, subject, 'artefactICA')
    if not op.exists(results_dir):
        os.makedirs(results_dir)

    # Create ICA file for each data type (MEG and EEG)
    for ch_type, picks in picks_by_type(
            raw.info, meg_combined=True):  # bad EEG channels are excluded
        # Read ICA file
        ica = []
        ica = read_ica(data_path + subject + '/mne_python/ICA/ICA_' + ch_type +
                       '_allRuns')

        for badICAsel in badICA[ch_type]:
            # Select bad component and assess artefact rejection quality
            ica_exclude = badICAsel

            ecg_evoked = create_ecg_epochs(raw, tmin=-.5, tmax=.5,
                                           picks=picks).average()
            fig = ica.plot_overlay(ecg_evoked,
                                   exclude=ica_exclude)  # plot ECG cleaning
            report.add_figs_to_section(
                fig,
                str(badICAsel) + ' Rejection overlay (%s)' % ch_type, 'ECG')

            eog_evoked = create_eog_epochs(raw, tmin=-.5, tmax=.5,
                                           picks=picks).average()
            fig = ica.plot_overlay(eog_evoked,
                                   exclude=ica_exclude)  # plot EOG cleaning
            report.add_figs_to_section(
                fig,
                str(badICAsel) + ' Rejection overlay (%s)' % ch_type, 'EOG')

            fig = ica.plot_overlay(raw)
            report.add_figs_to_section(
                fig,
                str(badICAsel) + ' Rejection overlay (%s)' % ch_type,
                'Workroad artefact')

    report.save((results_dir + '/AllRunsChecked.html'),
                open_browser=False,
                overwrite=True)