Skip to content

MRG, FIX: Test round-trip, fix EEGLAB ICA - #8326

Merged
hoechenberger merged 7 commits into
mne-tools:masterfrom
larsoner:ica
Oct 4, 2020
Merged

hoechenberger merged 7 commits into
mne-tools:masterfrom
larsoner:ica

Conversation

@larsoner

@larsoner larsoner commented Sep 30, 2020 •

Copy link
Copy Markdown
Member

Closes #8325
Closes #8308
Closes #7657

@larsoner

larsoner commented Oct 1, 2020

Copy link
Copy Markdown
Member Author

On this branch I get the same plots as in #8308:

Screenshot from 2020-10-01 14-18-38

@christian-oreilly @marcpabst can you try it on your datasets to see if it makes sense? I was able to unify the code so that no branching is required, which is nice.

@larsoner larsoner changed the title WIP, FIX: Dont destabilize pinv MRG, FIX: Test round-trip, fix EEGLAB ICA Oct 1, 2020
Comment thread mne/preprocessing/tests/test_ica.py Resolved
Comment thread mne/preprocessing/tests/test_ica.py Resolved
@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member Author

Okay this one is ready for review @agramfort @hoechenberger . We should wait to hear from @christian-oreilly and @marcpabst before merge, though

@hoechenberger hoechenberger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a question, otherwise LGTM.
We need to do the same thing for n_components, for sake of consistency.

Comment thread mne/utils/tests/test_numerics.py Resolved
@larsoner

larsoner commented Oct 2, 2020 •

Copy link
Copy Markdown
Member Author

Argh, I went to update the documentation, now I'm confused why we need n_components, n_pca_components, and max_pca_components. I thought we'd only need two (n_pca_components and max_pca_components, and that max_pca_components determined signal reconstruction and n_pca_components was what was passed to the ICA algorithm, but this is not at all what our docs say. I'll revert my doc changes (other than the refs) and hopefully we can fix it later.

@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member Author

@hoechenberger I'm beginning to wonder if we need max_pca_componetns to be float. Really it should be enough to allow n_components to be a float I think. The instability of the inversion comes from dividing by np.sqrt(self.pca_explained_variance_[:self.n_components_]). So if n_components is set appropriately, it seems like there shouldn't be a problem.

@hoechenberger

hoechenberger commented Oct 2, 2020 •

Copy link
Copy Markdown
Member

@larsoner

@hoechenberger I'm beginning to wonder if we need max_pca_componetns to be float. Really it should be enough to allow n_components to be a float I think. The instability of the inversion comes from dividing by np.sqrt(self.pca_explained_variance_[:self.n_components_]). So if n_components is set appropriately, it seems like there shouldn't be a problem.

All max_pca_components are submitted to ICA for fitting; after fitting, only n_componentsICA components are retained.

If it is like you say and we can get rid of the instability caused by choosing inappropriate max_pca_components and produce sane results, then things should be okay… Still I'm a little worried. Also, max_pca_components being a float could be just another way to limit how many components to fit ICA on – would be consistent with n_components and n_pca_components

@hoechenberger

Copy link
Copy Markdown
Member

Argh, I went to update the documentation, now I'm confused why we need n_components, n_pca_components, and max_pca_components. I thought we'd only need two (n_pca_components and max_pca_components, and that max_pca_components determined signal reconstruction and n_pca_components was what was passed to the ICA algorithm, but this is not at all what our docs say. I'll revert my doc changes (other than the refs) and hopefully we can fix it later.

Yet another example of our API being way too complicated. We've had so many issues with this…

@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member Author

Okay let me see if I can write the docs properly

@hoechenberger

hoechenberger commented Oct 2, 2020 •

Copy link
Copy Markdown
Member

Maybe to clarify, my understanding is:

  • max_pca_components: PCA components to keep
  • n_components: number of ICA components that are retained after fitting ICA on data with dimensionality reduced to max_pca_components --> the docs say otherwise, so either the docs are wrong or I made a mistake reading the sources
  • n_pca_components: number of components (ICA + PCA!!!!) to use for signal reconstruction -> maybe this kwarg is just poorly named

@hoechenberger

Copy link
Copy Markdown
Member

(edited my above post a couple times)

@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member Author

n_components: number of ICA components that are retained after fitting ICA on data with dimensionality reduced to max_pca_components

The code has this:

        sel = slice(0, self.n_components_)
        if self.method == 'fastica':
            from sklearn.decomposition import FastICA
            ica = FastICA(whiten=False, random_state=random_state,
                          **self.fit_params)
            ica.fit(data_transformed[:, sel])

So I think the docs are correct -- ICA is fitted on the first n_components of the PCA.

n_pca_components isn't even used until apply so it only has to do with the apply step. I'll add this to the docs.

@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member Author

Now I wonder if using n_components as float would have been enough and max_pca_components as float is overkill... I'll push a commit that shows setting n_components to a float also gives the behavior we want, even with max_pca_components=n_chan

@hoechenberger

Copy link
Copy Markdown
Member

So I think the docs are correct -- ICA is fitted on the first n_components of the PCA.

Oh ok, I got this mixed up then.

@larsoner larsoner mentioned this pull request Oct 4, 2020
3 of 7 tasks

@hoechenberger hoechenberger left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once CI is green

Comment thread mne/preprocessing/ica.py Resolved
@hoechenberger
hoechenberger merged commit 47ce37e into mne-tools:master Oct 4, 2020
@hoechenberger

Copy link
Copy Markdown
Member

Thanks @larsoner! I'll want to try this on some of our data now before we move forward with the API redesign, ok?

@larsoner
larsoner deleted the ica branch October 4, 2020 15:38
marsipu pushed a commit to marsipu/mne-python that referenced this pull request Oct 14, 2020
* FIX: Dont destabilize pinv

* FIX: sklearn

* FIX: Fix for EEGLAB

* TST: Better tests

* FIX: Better logic for n_pca_components

* FIX: Better tests

* Fix typo [skip travis]

Co-authored-by: Richard Höchenberger <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG?: ICA can blow up components The computation of the mixing matrix for ICA imported from EEGLAB ICA is broken

2 participants