Skip to content

BUG?: ICA can blow up components #8325

Closed

Description

@larsoner

@hoechenberger not 100% sure this is related to #8313 and #8316 since those seem to have to do with ICA taking too long rather than doing something totally wrong. To me this code should work / be a no-op:

import numpy as np
import mne

data = np.random.RandomState(0).randn(10, 100000)
info = mne.create_info(10, 1000., 'eeg')
raw = mne.io.RawArray(data, info)
raw.set_eeg_reference()
assert np.linalg.matrix_rank(raw.get_data()) == 9

ica = mne.preprocessing.ICA(
    max_pca_components=10, n_pca_components=10, max_iter=1, verbose=True)
raw_new = ica.fit(raw).apply(raw.copy())
np.testing.assert_allclose(raw.get_data(), raw_new.get_data())

gives an AssertionError:

Mismatched elements: 1000000 / 1000000 (100%)
Max absolute difference: 4.53435389
Max relative difference: 70047.35032413
 x: array([[ 1.481445,  0.391694,  1.081697, ...,  0.370815, -1.556122,
        -1.275782],
       [-0.766405,  1.279594, -0.02692 , ...,  0.312448, -1.409486,...
 y: array([[ 6.455399e-05,  6.455399e-05,  6.455399e-05, ...,  6.455399e-05,
         6.455399e-05,  6.455399e-05],
       [ 3.581095e-03,  3.581095e-03,  3.581095e-03, ...,  3.581095e-03,...

It seems like this should work even if you get the number of components wrong. It suggests that something is wrong with the mixing/unmixing step that when the number of components equals the number of channels you don't get an identity transform here.

It also fails for max_pca_components=10, n_pca_components=9 but passes for any value of max_pca_components (8, 9, 10) so long as max_pca_components=9.

If I remove this call:

        self.unmixing_matrix_ /= np.sqrt(
            self.pca_explained_variance_[sel])[None, :]  # whitening

Then things work okay.

Activity

  1. hoechenberger commented on Sep 30, 2020

    @hoechenberger
    Member

    @hoechenberger not 100% sure this is related to #8313 and #8316
    ...
    It also fails for max_pca_components=10, n_pca_components=9 but passes for any value of max_pca_components (8, 9, 10) so long as max_pca_components=9.

    I believe this is related, and in fact may be the true explanation for super bad ICA results @agramfort and I witnessed when working with rank-deficient data (i.e., without setting max_pca_components to an appropriate value)

    Reducing the rank always fixed things for us.

    This is why I started working on #8321, to make it easier to select a relative number of components (explaining x% of variance).

  2. hoechenberger commented on Sep 30, 2020

    @hoechenberger
    Member

    . It suggests that something is wrong with the mixing/unmixing step that when the number of components equals the number of channels

    I think it's really because your data is rank-deficient here thanks to the avg reference.

  3. hoechenberger commented on Oct 1, 2020

    @hoechenberger
    Member

    I think it's really because your data is rank-deficient here thanks to the avg reference.

    import numpy as np
    import mne
    
    data = np.random.RandomState(0).randn(10, 100000)
    info = mne.create_info(10, 1000., 'eeg')
    raw = mne.io.RawArray(data, info)
    # raw.set_eeg_reference()
    # assert np.linalg.matrix_rank(raw.get_data()) == 9
    assert np.linalg.matrix_rank(raw.get_data()) == 10
    
    ica = mne.preprocessing.ICA(
        max_pca_components=10, n_pca_components=10, max_iter=1, verbose=True)
    raw_new = ica.fit(raw).apply(raw.copy())
    np.testing.assert_allclose(raw.get_data(), raw_new.get_data())

    Commented-out the avg reference step, and the assertion holds…

  4. larsoner commented on Oct 1, 2020

    @larsoner
    MemberAuthor

    Commented-out the avg reference step, and the assertion holds…

    Yes it's 100% because it's rank deficient. But my point is that if we can make it so that this doesn't blow up the data without any special intervention (such as trying to detect the rank), we should. That's what #8326 does.

  5. hoechenberger commented on Oct 1, 2020

    @hoechenberger
    Member

    Ok, got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions