def preprocess_raw_ica_only(sub_id, session):
    """ This function removes the ICA component that correlates woth the 
    EOG channel(s) best.
    No filtering or downsampling is applied!
    """

    # SETUP AND LOAD FILES ####
    # name with subject id & session name
    fname = "sub_%d_%s" % (sub_id, session)

    # load the raw fif
    print '\nLoading raw file'
    raw = fiff.Raw(fname + "_tsss_mc.fif", preload=True)

    picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False, eog=False,
                                stim=False, exclude='bads')

    # ICA ####
    print '\nRun ICA'
    ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100,
              noise_cov=None, random_state=0)

    start, stop = None, None

    # decompose sources for raw data
    ica.decompose_raw(raw, start=start, stop=stop, picks=picks)

    corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0]

    eog_scores_1 = ica.find_sources_raw(raw, target='EOG001',
                                        score_func=corr)
    eog_scores_2 = ica.find_sources_raw(raw, target='EOG002',
                                        score_func=corr)

    # get maximum correlation index for EOG
    eog_source_idx_1 = np.abs(eog_scores_1).argmax()
    eog_source_idx_2 = np.abs(eog_scores_2).argmax()

    # We now add the eog artifacts to the ica.exclusion list
    if eog_source_idx_1 ==  eog_source_idx_2:
        ica.exclude += [eog_source_idx_1]

    elif eog_source_idx_1 !=  eog_source_idx_2:
        ica.exclude += [eog_source_idx_1, eog_source_idx_2]

    print eog_source_idx_1, eog_source_idx_2
    print ica.exclude

    # Restore sensor space data
    raw_ica = ica.pick_sources_raw(raw, include=None)

    # SAVE FILES ####
    raw_ica.save(fname + '_tsss_mc_preproc_ica.fif', overwrite=True)
Exemplo n.º 2
0
# variance explained by the PCA components.

ica = ICA(n_components=0.90,
          n_pca_components=None,
          max_pca_components=None,
          random_state=0)

# Also we decide to use all PCA components before mixing back to sensor space.
# You can again use percentages (float) or set the total number of components
# to be kept directly (int) which allows to control the amount of additional
# denoising.

ica.n_pca_components = 1.0

# decompose sources for raw data using each third sample.
ica.decompose_raw(raw, picks=picks, decim=3)
print(ica)

# plot reasonable time window for inspection
start_plot, stop_plot = 100., 103.
ica.plot_sources_raw(raw, range(30), start=start_plot, stop=stop_plot)

###############################################################################
# Automatically find the ECG component using correlation with ECG signal.

# Defining a customized distance function.

# You can pass any function object that
# takes a n_sources X n_samples vector and, optionally, a second
# n_samples vector, and returns a score vector of length n_sources.
# Let's illustrate this by creating a function that, when passed as
Exemplo n.º 3
0
# Instead of the actual number of components here we pass a float value
# between 0 and 1 to select n_components by a percentage of
# explained variance. Also we decide to use 64 PCA components before mixing
# back to sensor space. These include the PCA components supplied to ICA plus
# additional PCA components up to rank 64 of the MEG data.
# This allows to control the trade-off between denoising and preserving signal.

ica = ICA(n_components=0.90, n_pca_components=None, max_pca_components=100,
          random_state=0)

# 1 minute exposure should be sufficient for artifact detection.
# However, rejection performance may significantly improve when using
# the entire data range

# decompose sources for raw data using each third sample.
ica.decompose_raw(raw, picks=picks, decim=3)
print ica

# plot reasonable time window for inspection
start_plot, stop_plot = 100., 103.
ica.plot_sources_raw(raw, range(30), start=start_plot, stop=stop_plot)

###############################################################################
# Automatically find the ECG component using correlation with ECG signal.

# First, we create a helper function that iteratively applies the pearson
# correlation function to sources and returns an array of r values
# This is to illustrate the way ica.find_sources_raw works. Actually, this is
# the default score_func.

from scipy.stats import pearsonr
# Setup ICA seed decompose data, then access and plot sources.

# Sign and order of components is non deterministic.
# setting the random state to 0 makes the solution reproducible.
# Instead of the actual number of components we pass a float value
# between 0 and 1 to select n_components by a percentage of
# explained variance.

ica = ICA(n_components=0.90, max_pca_components=100, noise_cov=None,
          random_state=0)

# For maximum rejection performance we will compute the decomposition on
# the entire time range

# decompose sources for raw data, select n_components by explained variance
ica.decompose_raw(raw, start=None, stop=None, picks=picks)
print ica

sources = ica.get_sources_raw(raw)

# setup reasonable time window for inspection
start_plot, stop_plot = raw.time_as_index([100, 103])

# plot components
ica.plot_sources_raw(raw, start=start_plot, stop=stop_plot)

###############################################################################
# Automatically find the ECG component using correlation with ECG signal

# As we don't have an ECG channel we use one that correlates a lot with heart
# beats: 'MEG 1531'. We can directly pass the name to the find_sources method.
def preprocess_raw(sub_id, session):
    """ This function preprocessess data
    """

    # SETUP AND LOAD FILES ####
    # name with subject id & session name
    fname = "sub_%d_%s" % (sub_id, session)

    # load the raw fif
    print '\nLoading raw file'
    raw = fiff.Raw(fname + "_tsss_mc.fif", preload=True)

    picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False, eog=False,
                                stim=False, exclude='bads')

    print 'Computing Covariance matrix'
    cov = mne.compute_raw_data_covariance(raw, picks=picks, reject=None)

    # FILTER ####
    # filter raw, lp 128, bp at 50 & 100
    raw.filter(None, 128, n_jobs=n_jobs, verbose=True)

#    steps = np.arange(50, 151, 50)
#    print '\nBand stop filter at %s' % steps
#    raw.notch_filter(steps, n_jobs=n_jobs, verbose=True)

    # ICA ####
    print '\nRun ICA'
    ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100,
              noise_cov=None, random_state=0)

    start, stop = None, None

    # decompose sources for raw data
    ica.decompose_raw(raw, start=start, stop=stop, picks=picks)

    corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0]

    eog_scores_1 = ica.find_sources_raw(raw, target='EOG001',
                                        score_func=corr)
    eog_scores_2 = ica.find_sources_raw(raw, target='EOG002',
                                        score_func=corr)

    # get maximum correlation index for EOG
    eog_source_idx_1 = np.abs(eog_scores_1).argmax()
    eog_source_idx_2 = np.abs(eog_scores_2).argmax()

    # We now add the eog artifacts to the ica.exclusion list
    if eog_source_idx_1 == eog_source_idx_2:
        ica.exclude += [eog_source_idx_1]
    elif eog_source_idx_1 != eog_source_idx_2:
        ica.exclude += [eog_source_idx_1, eog_source_idx_2]

    print eog_source_idx_1, eog_source_idx_2
    print ica.exclude

    # Restore sensor space data
    raw_ica = ica.pick_sources_raw(raw, include=None)

    # EPOCHS ####
    events = mne.find_events(raw_ica, stim_channel="STI101")
    events_classic = []
    events_interupt = []
    for i in range(len(events)):
        if i > 0:
            if events[i, 2] == 1 and events[i - 1, 2] == 1:
                events_classic.append(i)
            elif events[i, 2] == 1 and events[i - 1, 2] == 2:
                events_interupt.append(i)

    picks = mne.fiff.pick_types(raw_ica.info, meg=True, eeg=False, eog=False,
                                emg=True, stim=False, exclude='bads')

    reject = dict(grad=4000e-13)
    epochs = mne.Epochs(raw_ica, events[events_classic], event_id, tmin, tmax,
                        proj=True, picks=picks, baseline=baseline,
                        preload=False, reject=reject)

    # SAVE FILES ####
    raw_ica.save(fname + '_tsss_mc_ica.fif', overwrite=True)
    cov.save((fname + '_tsss_mc_cov.fif'))
    epochs.save(fname + '_tsss_mc_ica_epochs.fif')
Exemplo n.º 6
0
# explained variance. Also we decide to use 64 PCA components before mixing
# back to sensor space. These include the PCA components supplied to ICA plus
# additional PCA components up to rank 64 of the MEG data.
# This allows to control the trade-off between denoising and preserving signal.

ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100,
          noise_cov=None, random_state=0)
print ica

# 1 minute exposure should be sufficient for artifact detection.
# However, rejection performance may significantly improve when using
# the entire data range
start, stop = raw.time_as_index([100, 160])

# decompose sources for raw data
ica.decompose_raw(raw, start=start, stop=stop, picks=picks)
print ica

sources = ica.get_sources_raw(raw, start=start, stop=stop)

# setup reasonable time window for inspection
start_plot, stop_plot = raw.time_as_index([100, 103])

# plot components
ica.plot_sources_raw(raw, start=start_plot, stop=stop_plot)

###############################################################################
# Automatically find the ECG component using correlation with ECG signal.

# First, we create a helper function that iteratively applies the pearson
# correlation function to sources and returns an array of r values
Exemplo n.º 7
0
# This allows to control the trade-off between denoising and preserving signal.

ica = ICA(n_components=0.90,
          n_pca_components=64,
          max_pca_components=100,
          noise_cov=None,
          random_state=0)
print ica

# 1 minute exposure should be sufficient for artifact detection.
# However, rejection performance may significantly improve when using
# the entire data range
start, stop = raw.time_as_index([100, 160])

# decompose sources for raw data
ica.decompose_raw(raw, start=start, stop=stop, picks=picks)
print ica

sources = ica.get_sources_raw(raw, start=start, stop=stop)

# setup reasonable time window for inspection
start_plot, stop_plot = raw.time_as_index([100, 103])

# plot components
ica.plot_sources_raw(raw, start=start_plot, stop=stop_plot)

###############################################################################
# Automatically find the ECG component using correlation with ECG signal.

# First, we create a helper function that iteratively applies the pearson
# correlation function to sources and returns an array of r values