예제 #1
0
def ica_convert2mne(unmixing, pca, info=None, method='fastica'):

    # create MNE-type of ICA object
    from mne.preprocessing.ica import ICA
    n_comp = unmixing.shape[1]

    if method == 'extended-infomax':
        ica_method = 'infomax'
        fit_params = dict(extended=True)
    else:
        ica_method = method
        fit_params = None

    ica = ICA(n_components=n_comp, method=ica_method, fit_params=fit_params)

    # add PCA object
    ica.pca = pca

    # PCA info to be used bei MNE-Python
    ica.pca_mean_ = pca.mean_
    ica.pca_components_ = pca.components_
    exp_var = pca.explained_variance_
    ica.pca_explained_variance_ = exp_var
    ica.pca_explained_variance_ratio_ = pca.explained_variance_ratio_

    # ICA info
    ica.n_components_ = n_comp
    ica.n_components = n_comp
    ica.components_ = unmixing  # compatible with sklearn
    ica.unmixing_ = ica.components_  # as used by sklearn
    ica.mixing_ = pinv(ica.unmixing_)  # as used by sklearn
    ica.unmixing_matrix_ = ica.unmixing_ / np.sqrt(
        exp_var[0:n_comp])[None, :]  # as used by MNE-Python
    ica.mixing_matrix_ = pinv(ica.unmixing_matrix_)  # as used by MNE-Python
    ica._ica_names = ['ICA%03d' % ii for ii in range(n_comp)]
    ica.fun = method
    if info:
        ica.info = info

    return ica
예제 #2
0
def eeglab2mne(fname, montage='standard_1020', event_id=None, load_ica=False):
    """Get an EEGLAB dataset into a MNE Raw object.

    Parameters
    ----------
    input_fname : str
        Path to the .set file. If the data is stored in a separate .fdt file,
        it is expected to be in the same folder as the .set file.
    montage : str | None | instance of montage
        Path or instance of montage containing electrode positions.
        If None, sensor locations are (0,0,0). See the documentation of
        :func:`mne.channels.read_montage` for more information.
    event_id : dict
        Map event id (integer) to string name of corresponding event.
        This allows to smoothly load EEGLAB event structure when overlapping
        events (e.g. boundaries) occur.
    load_ica : bool
        Default to False. Load ica matrices from eeglab structure if available
        and attempt to transfer them into the ica structure of MNE.

    Returns
    -------
    raw : Instance of RawEEGLAB
        A Raw object containing EEGLAB .set data.
    ica : Instance of ICA
        If load_ica True

    Note
    ----
    ICA matrices in ICA MNE object might not entirely capture the decomposition.
    To apply projections (i.e. remove some components from observed EEG data) it
    might be better to load directly the matrices and do it by hand, where:

        - icawinv = pinv(icaweights * icasphere)
        - ica_act = icaweights * icasphere * eegdata

    References
    ----------
    .. [#] https://benediktehinger.de/blog/science/ica-weights-and-invweights/
    .. [#] https://github.com/mne-tools/mne-python/pull/5114/files
    """
    montage_mne = mne.channels.make_standard_montage(montage)

    try:
        raw = mne.io.read_raw_eeglab(input_fname=fname, preload=True)
    except NotImplementedError:
        print("Version 7.3 matlab file detected, will load 'by hand'")
        eeg, srateate, _, _, _, ch_names = _load_eeglab_data(fname)
        info = mne.create_info(ch_names=ch_names,
                               sfreq=srateate,
                               ch_types='eeg')
        raw = mne.io.RawArray(eeg.T, info)
    # set up montage:
    raw.set_montage(montage_mne)

    if load_ica:
        weights, winv, sphere = load_ica_matrices(fname)
        ica = ICA(n_components=winv.shape[1], max_pca_components=winv.shape[1])
        ica.fit(raw, decim=2, start=1., stop=60.)
        ica.unmixing_matrix_ = weights.dot(sphere.dot(ica.pca_components_.T))
        ica.mixing_matrix_ = np.linalg.pinv(ica.unmixing_matrix_)

        return raw, ica
    return raw