Example #1
0
def apply_ica_cleaning(fname_ica, n_pca_components=None,
                       name_ecg='ECG 001', flow_ecg=10, fhigh_ecg=20,
                       name_eog_hor='EOG 001', name_eog_ver='EOG 002',
                       flow_eog=1, fhigh_eog=10, threshold=0.3,
                       unfiltered=False, notch_filter=True, notch_freq=50,
                       notch_width=None):

    ''' Performs artifact rejection based on ICA to a list of (ICA) files. '''

    fnlist = get_files_from_list(fname_ica)

    # loop across all filenames
    for fnica in fnlist:
        name = os.path.split(fnica)[1]
        #basename = fnica[0:len(fnica)-4]
        basename = fnica[:fnica.rfind(ext_ica)]
        fnfilt = basename + ext_raw
        fnclean = basename + ext_clean
        fnica_ar = basename + ext_icap
        print ">>>> perform artifact rejection on :"
        print '   ' + name

        # load filtered data
        meg_raw = mne.io.Raw(fnfilt, preload=True)
        picks = mne.pick_types(meg_raw.info, meg=True, ref_meg=False, exclude='bads')
        # ICA decomposition
        ica = mne.preprocessing.read_ica(fnica)

        # get ECG and EOG related components
        ic_ecg = get_ics_cardiac(meg_raw, ica,
                                 flow=flow_ecg, fhigh=fhigh_ecg, thresh=threshold)
        ic_eog = get_ics_ocular(meg_raw, ica,
                                flow=flow_eog, fhigh=fhigh_eog, thresh=threshold)
        ica.exclude += list(ic_ecg) + list(ic_eog)
        # ica.plot_topomap(ic_artefacts)
        ica.save(fnica)  # save again to store excluded

        # clean and save MEG data
        if n_pca_components:
            npca = n_pca_components
        else:
            npca = picks.size

        # check if cleaning should be applied
        # to unfiltered data
        if unfiltered:
            # adjust filenames to unfiltered data
            basename = basename[:basename.rfind(',')]
            fnfilt = basename + ext_raw
            fnclean = basename + ext_clean
            fnica_ar = basename + ext_icap

            # load raw unfiltered data
            meg_raw = mne.io.Raw(fnfilt, preload=True)

            # apply notch filter
            if notch_filter:

                from jumeg.filter import jumeg_filter

                # generate and apply filter
                # check if array of frequencies is given
                if type(notch_freq) in (tuple, list):
                    notch = np.array(notch_freq)
                elif type(np.ndarray) == np.ndarray:
                    notch = notch_freq
                # or a single frequency
                else:
                    notch = np.array([])

                fi_mne_notch = jumeg_filter(filter_method="mne", filter_type='notch',
                                            remove_dcoffset=False,
                                            notch=notch, notch_width=notch_width)

                # if only a single frequency is given generate optimal
                # filter parameter to also remove the harmonics
                if not type(notch_freq) in (tuple, list, np.ndarray):
                    fi_mne_notch.calc_notches(notch_freq)

                fi_mne_notch.apply_filter(meg_raw._data, picks=picks)

        # apply cleaning
        meg_clean = ica.apply(meg_raw, exclude=ica.exclude,
                              n_pca_components=npca, copy=True)
        meg_clean.save(fnclean, overwrite=True)

        # plot ECG, EOG averages before and after ICA
        print ">>>> create performance image..."
        plot_performance_artifact_rejection(meg_raw, ica, fnica_ar,
                                            show=False, verbose=False)
Example #2
0
# -------------------------------------------------------
# apply ICA cleaning on original unfiltered data !!
# -------------------------------------------------------
raw_new = ica.apply(raw_orig.copy(),
                    exclude=ica.exclude,
                    n_pca_components=0.99)
# check your new raw cleaned data, and reject bad channels by visual inspection
#raw_new.plot(start=0,duration=120)
# -------------------------------------------------------
# save ICA object and plot ICA rejection performance
# -------------------------------------------------------
fn_ica = fn_raw[:-8] + ',ica.fif'
fn_perf = fn_ica[:-4] + ',perf'
ica.save(fn_ica)
# Note, there is a problem with the MNE peak detection of EOG peaks
# therefore the we only find a few peaks in the EOG signal
#raw_orig = mne.io.read_raw_fif(fn_raw, preload=True)
#plot_performance_artifact_rejection(raw_orig, ica, fn_perf, meg_clean=raw_new, show=True)
plot_performance_artifact_rejection(raw_orig, ica, fn_perf, show=True)

# -------------------------------------------------------
# save results
# -------------------------------------------------------
# maybe you keep a copy of the original OCARTA raw fileimport shutil
#import shutil
#shutil.copy2(fn_raw,fn_raw+'.orig' )

# save/overwrite cleaned data
raw_new.save(fn_raw_oca, overwrite=True)

# now create epochs and check for bad epochs
Example #3
0
def apply_ica_and_plot_performance(raw,
                                   ica,
                                   name_ecg,
                                   name_eog,
                                   raw_fname,
                                   clean_fname,
                                   picks=None,
                                   reject=None,
                                   replace_pre_whitener=True,
                                   save=False):
    """
    Applies ICA to the raw object and plots the performance of rejecting ECG and EOG artifacts.

    Parameters
    ----------
    raw : mne.io.Raw()
        Raw object ICA is applied to
    ica : ICA object
        ICA object being applied d to the raw object
    name_ecg : str
        Name of the ECG channel in the raw data
    name_eog : str
        Name of the (vertical) EOG channel in the raw data
    raw_fname : str | None
        Path for saving the raw object
    clean_fname : str | None
        Path for saving the ICA cleaned raw object
    picks : array-like of int | None
        Channels to be included for the calculation of pca_mean_ and _pre_whitener.
        This selection SHOULD BE THE SAME AS the one used in ica.fit().
    reject : dict | None
        Rejection parameters based on peak-to-peak amplitude. This parameter SHOULD BE
        THE SAME AS the one used in ica.fit().
        Valid keys are 'grad', 'mag', 'eeg', 'seeg', 'ecog', 'eog', 'ecg',
        'hbo', 'hbr'.
        If reject is None then no rejection is done. Example::

            reject = dict(grad=4000e-13, # T / m (gradiometers)
                          mag=4e-12, # T (magnetometers)
                          eeg=40e-6, # V (EEG channels)
                          eog=250e-6 # V (EOG channels)
                          )

        It only applies if `inst` is of type Raw.
    replace_pre_whitener : bool
        If True, pre_whitener is replaced when applying ICA to
        unfiltered data otherwise the original pre_whitener is used.
    save : bool
        Save the raw object and cleaned raw object

    Returns
    -------
    raw_clean : mne.io.Raw()
        Raw object after ICA cleaning
    """

    # apply_ica_replace_mean_std processes in place -> need copy to plot performance
    raw_copy = raw.copy()
    ica = ica.copy()

    raw_clean = apply_ica_replace_mean_std(
        raw,
        ica,
        picks=picks,
        reject=reject,
        exclude=ica.exclude,
        n_pca_components=None,
        replace_pre_whitener=replace_pre_whitener)
    if save:
        if raw_fname is not None:
            raw_copy.save(raw_fname, overwrite=True)
        raw_clean.save(clean_fname, overwrite=True)

    overview_fname = clean_fname.rsplit('-raw.fif')[0] + ',overview-plot'
    plot_performance_artifact_rejection(raw_copy,
                                        ica,
                                        overview_fname,
                                        meg_clean=raw_clean,
                                        show=False,
                                        verbose=False,
                                        name_ecg=name_ecg,
                                        name_eog=name_eog)
    print('Saved ', overview_fname)

    raw_copy.close()

    return raw_clean
Example #4
0
# exclude bad components identified by MLICA
ica.exclude = bads_MLICA

fnout_fig = '109925_CAU01A_100715_0842_2_c,rfDC,0-45hz,ar-perf'
ica_filtered_chop = ica_update_mean_std(raw_filtered_chop,
                                        ica,
                                        picks=picks,
                                        reject=reject)
raw_filtered_chop_clean = ica_filtered_chop.apply(raw_filtered_chop,
                                                  exclude=ica.exclude,
                                                  n_pca_components=None)

ica_unfiltered_chop = ica_update_mean_std(raw_chop,
                                          ica,
                                          picks=picks,
                                          reject=reject)
raw_unfiltered_chop_clean = ica_unfiltered_chop.apply(raw_chop,
                                                      exclude=ica.exclude,
                                                      n_pca_components=None)

# create copy of original data since apply_ica_replace_mean_std changes the input data in place (raw and ica)
raw_copy = raw.copy().crop(tmin=tmin * 4. / 1000, tmax=tmax * 4. / 1000)
plot_performance_artifact_rejection(raw_copy,
                                    ica,
                                    fnout_fig,
                                    meg_clean=raw_unfiltered_chop_clean,
                                    show=False,
                                    verbose=False,
                                    name_ecg=ecg_ch,
                                    name_eog=eog2_ch)
Example #5
0
fig = ica.plot_sources(raw_filtered_chop,
                       picks=ic_order,
                       title='MLICA',
                       show=False)
#fig.savefig('MLICA_ica-sources.png')

# plot artifact rejection performance
fnout_fig = '109925_CAU01A_100715_0842_2_c,rfDC,0-45hz,ar-perf'
ica_filtered_chop = ica_update_mean_std(raw_filtered_chop,
                                        ica,
                                        picks=picks,
                                        reject=reject)
raw_filtered_chop_clean = ica_filtered_chop.apply(raw_filtered_chop,
                                                  exclude=ica.exclude,
                                                  n_pca_components=None)
ica_unfiltered_chop = ica_update_mean_std(raw_chop,
                                          ica,
                                          picks=picks,
                                          reject=reject)
raw_unfiltered_chop_clean = ica_unfiltered_chop.apply(raw_chop,
                                                      exclude=ica.exclude,
                                                      n_pca_components=None)
plot_performance_artifact_rejection(raw.copy().crop(tmin=tmin, tmax=tmax),
                                    ica_unfiltered_chop,
                                    fnout_fig,
                                    meg_clean=raw_unfiltered_chop_clean,
                                    show=True,
                                    verbose=False,
                                    name_ecg=ecg_ch,
                                    name_eog=eog2_ch)