Skip to content

ICA arguments confusion #4856

Closed
Closed

Description

@cbrnr

The ICA object takes three arguments that are somehow related to the number of components:

  • n_components
  • max_pca_components
  • n_pca_components

Every time I use ICA in MNE I get confused. Why are there three seemingly related arguments? The only thing I ever needed to do is to reduce the number of PCA components prior to ICA (e.g. for average-referenced data the number of PCA components should be set to the number of channels minus one). Now since PCA is the first step in any ICA algorithm, I thought that n_components would set this value. But from the docs, it looks like this is really done by max_pca_components - or is it? And when do I need n_pca_components? I'm curious which use cases require these three arguments.

BTW, both EEGLAB's runica and scikit-learn's FastICA functions/objects only have n_components arguments.

Related to this question: what does the noise_cov argument do? Is the purpose to whiten the data if noise_cov is some covariance matrix or not whiten (i.e. only decorrelate) if noise_cov=None? If so, wouldn't it make more sense to introduce a boolean whiten argument instead? Here, it seems like MNE's ICA does not whiten by default, whereas EEGLAB and scikit-learn do whiten by default.

Activity

  1. mmagnuski commented on Jan 9, 2018

    @mmagnuski
    Member

    There was once an issue about that but I forgot to fix the docs. I made some notes about these args at that time however. I found these notes on my computer about yesterday so your timing is excellent :) Below I copy-paste the notes:

    pca_components_ - pca_components x channels matrix
    unmixing_matrix_ - ica_components x pca_components
    mixing_matrix_ - pca_components x ica_components (inverse of unmixing matrix)

    max_pca_components - the number of components used in PCA
    n_components - the number of pca components that are further used in ICA,
    can be a float - then selects first N components that
    explain ...
    This is also the number of ICA components that you will get
    n_pca_components - the number of PCA components used when reconstructing the
    data from ICA and PCA

    max_pca_components >= n_pca_components >= n_components

    if max_pca_components is set then n_components must be set too (but this is not checked for)
    if n_components is None all components are used.

    picks is used to infer max_pca_components if it is None
    picks are on the other hand set with _pick_data_channels if None

    n_components can be float - selecting PCA components used in ICA by exaplained variance, but n_components_ is always int indicating the number of selected PCA components

    I also found following remark in these notes (not sure whether it was true at the time of writing that is about 1 yr ago or if it was true whether it still is the case):
    it seems to me that there is a little too much freedom in the three components arguments. One can set max_pca_components < n_channels or set n_components < n_channels and get the same results. Similar situation is with setting max_pca_components and n_pca_components

  2. mmagnuski commented on Jan 9, 2018

    @mmagnuski
    Member

    Generally when going from data to ICs:
    data --[ PCA and max_pca_components]--> PCs --[ICA and n_components] --> ICs
    n_pca_components is used when going in the reverse order (IIRC to drop some PCs - ignore them in reconstructing the data?)

  3. cbrnr commented on Jan 11, 2018

    @cbrnr
    ContributorAuthor

    This is confusing. Can we combine max_pca_components and n_components into one argument (called n_components)? It seems like both arguments affect the number of ICA components I get back. Internally, this is achieved by setting the number of PCA components prior to ICA. However, as a user I'm usually only interested in setting the number of ICs I get back. This is what the n_components argument in sklearn.decomposition.FastICA or the pca argument in runica do. In my opinion, if you want to do something else than specify the number of ICs you want, you could run PCA explicitly before ICA (e.g. if you want to decompose a PCA subspace that accounts for 99% of the variance). Or we let n_components accept both int and float.

    I think part of the confusion comes from the fact that in MNE, the unmixing matrix is ica_components x pca_components, whereas EEGLAB calls this weights and defines the unmixing matrix as weights @ sphere. And I still don't see the need for a n_pca_components argument - if someone really needs to drop PCA components in the reverse case, one could always do this manually with pca_components_ and unmixing_matrix_.

  4. dengemann commented on Jan 11, 2018

    @dengemann
    Member
  5. cbrnr commented on Jan 11, 2018

    @cbrnr
    ContributorAuthor

    Where is this feature (controlling ICA and PCA components separately) actually used?

  6. dengemann commented on Jan 11, 2018

    @dengemann
    Member
  7. cbrnr commented on Jan 11, 2018

    @cbrnr
    ContributorAuthor

    Yes, this may all be true, but still I find the usage rather confusing, especially for people used to most other ICA APIs.

    Anyway, since an API change seems to be out of the question, I'll add a paragraph to the documentation to clarify these arguments.

  8. dengemann commented on Jan 11, 2018

    @dengemann
    Member
  9. mmagnuski commented on Jan 11, 2018

    @mmagnuski
    Member

    I'm also in favor of improving the docs and not changing the API.

    Or we let n_components accept both int and float.

    n_components already allows that - this is actually clearly stated in the docs. I mosly use only n_components - you don't need the other params for basic use cases.

  10. cbrnr commented on Jan 11, 2018

    @cbrnr
    ContributorAuthor

    n_components already allows that - this is actually clearly stated in the docs. I mosly use only n_components - you don't need the other params for basic use cases.

    Yes, I saw that, but I wasn't sure if it does the right thing (given that the percentage really applies to the number of PCA components it made more sense to me to have this in the max_pca_components argument).

    So are you saying that if I want to compute ICA on a PCA subspace (say I have 64 EEG channels but due to average reference I want to compute ICA only on 63 components), n_components and max_pca_components actually do the same thing? If yes, this would be great in a way, because everyone else is using the n_components argument exactly for this purpose. Then we only need to make it clear in the docs that the other two arguments are for more specific use cases.

  11. mmagnuski commented on Jan 11, 2018

    @mmagnuski
    Member

    @cbrnr Yes, you will get the same number of ICs in ICA object, but different number of PCs IIRC (some will be thrown away). Then during the reconstruction of the signal this could lead to differences (because you will have different number of PCs, notice that max_pca_components >= n_pca_components).

  12. cbrnr commented on Jan 12, 2018

    @cbrnr
    ContributorAuthor

    I played around with the arguments a bit. The most common use case (which is implemented in EEGLAB and sklearn) is to compute ICs only on a PCA subspace, i.e. only a subset of PCA components is used to compute the ICs.

    Here's an example. Let's assume we have an EEG data set consisting of 64 channels, i.e. the shape of x is (64, N). Let's further assume we want to compute only 60 ICs.

    In EEGLAB, I'd specify pca=60 for this purpose. The results I'd get are of the following shapes:

    • icaweights: (60, 64)
    • icasphere: (64, 64)
    • icawinv: (64, 60)

    This works out, because the unmixing matrix can be computed by unmixing = icaweights @ icasphere and is of shape (60, 64). This unmixing matrix can then be applied to the data to get the sources as unmixing @ x, which results in the expected shape (60, N).

    How do we achieve the same thing with MNE? First I thought that n_components=60 does the trick, but it results in the following matrices:

    • unmixing_matrix_: (60, 60)
    • pca_components_: (64, 64)
    • mixing_matrix_: (60, 60)

    In contrast to what I'd expect, I can't compute the sources by multiplying the unmixing matrix with the data: unmixing_matrix_ @ x has incompatible dimensions. Apparently, I have to do unmixing_matrix_ @ pca_components_[:60] @ x. This is completely unsatisfiable from a user perspective (why do I have to slice the PCA components?).

    It looks like max_pca_components=60 actually does what I want:

    • unmixing_matrix_: (60, 60)
    • pca_components_: (60, 64)
    • mixing_matrix_: (60, 60)
      But again, I'd say that the term unmixing matrix is not correct because I still have to do unmixing_matrix_ @ pca_components_ @ x. In fact, this is identical to what EEGLAB does, but EEGLAB correctly calls the matrices weights and sphere (and their product is unmixing).

    Finally, I have still no idea why n_pca_components is needed.

    All this is to say that I still don't find calculating ICA in MNE intuitive. What exactly can not be done when there is only one argument (as in EEGLAB)? PCA and ICA are intrinsically linked, so if someone wants to to some special PCA preprocessing, I suggest to do this outside of ICA.

    But if it's not possible to change the API, please confirm that max_pca_components is the counterpart to EEGLAB's n_components (and scikit-learn's n_components).

  13. cbrnr commented on Jan 12, 2018

    @cbrnr
    ContributorAuthor

    Oh, and also, how do I turn on whitening? I think whitening is disabled by default, whereas it is enabled in EEGLAB and scikit-learn.

  14. mmagnuski commented on Jan 12, 2018

    @mmagnuski
    Member

    I'll reply later, I'm in a bit of a hurry now. But you can safely just use n_components (n_components=60) and it will work just like in eeglab - using a subset of PCs to use in ICA.

  15. cbrnr commented on Jan 12, 2018

    @cbrnr
    ContributorAuthor

    Not quite. If I just use get_sources, then yes, but what if I want the (true) unmixing matrix? I'd have to compute it via unmixing_matrix_ @ pca_components_[:60]. Setting max_pca_components=60 seems to be more what I expect, because then the unmixing matrix is unmixing_matrix_ @ pca_components_.

  16. 51 remaining items

  17. cbrnr commented on Jan 19, 2018

    @cbrnr
    ContributorAuthor

    Do you have such a large file for me to test? Is there something in the sample data?

  18. dengemann commented on Jan 19, 2018

    @dengemann
    Member
  19. dengemann commented on Jan 19, 2018

    @dengemann
    Member
  20. dengemann commented on Jan 19, 2018

    @dengemann
    Member
  21. cbrnr commented on Jan 19, 2018

    @cbrnr
    ContributorAuthor

    I tiled my EEG data to create a 2.4GB (4935680, 64) array and compared full PCA vs. randomized PCA:

    Keep all components: randomized: 193.0s, full: 48.8s.
    Keep only 10 components: randomized: 41.8s, full: 48.5s.

    I guess we can safely move to full PCA, but feel free to test this with real data.

  22. sappelhoff commented on Mar 29, 2018

    @sappelhoff
    Member

    Migrating from #5054 ...

    Like @cbrnr, I have issues with the ICA API and would appreciate a quick check on my intuition about it:

    My current understanding is:

    1. calculate PCA on data X
    2. retain max_pca_components and project X to X_reduced
    3. at this point, probably n_components is doing something ... but what?
    4. Using X_reduced, we compute an ICA and have X_reduced_ica
    5. From X_reduced_ica, we retain/reject components
    6. We back-project X_reduced_ica using the ICA weights and have X_reduced'
      • ( the ' indicating that some components were rejected in the ICA ... but the dimensions are the same)
    7. X_reduced' can now be back-projected to X' using the PCA weights of dimension max_pca_components
      • (It would be X' instead of the original X because of two reasons: (a) some ICA components were missing (rejected) to get the same X_reduced and (b) X_reduced does not contain all pca components so we cannot recover all the data)
    8. Here, at step 7., n_pca_components comes into play by using more than just max_pca_components to construct X' from X_reduced'
      Is my intuition about the procedure correct? Given that yes, ... are there any references that suggest that step 8. (concerning max_pca_components) is sensible? And I still don't know what n_components is doing ... but for now, I will not do any dimension reduction anyway and leave the arguments at None.

    Furthermore, given the discussion about PCA before ICA, there is a recent paper that warns about performing dimension reduction prior to ICA

    Maybe we should include a warning that performing dimensionality reduction (even only by 1% variance) before ICA can lead to unreliable results? Just as @cbrnr has pointed out in #5054 we can mention the case that dimension reduction is necessary in the case of e.g., the average reference. That information might fit well next to the warning note about filters, wouldn't it? :-)

  23. jeythekey commented on Oct 17, 2018

    @jeythekey
    Contributor

    Hi,
    I have just made some comments to this #5114 docstring commit, after having dived through the ICA code the whole day in order to get a more profound understanding of what is going on with the different component-settings. What actually provoked this undertaking were two mistakes in the specification of the pca_components_-shape in the doc.

    @cbrnr, maybe you could review and consider it? See here: 1a09859
    (I am not sure, if this is the right way to contribute anything to this issue; if not, please let me know the most proper way.)

  24. larsoner commented on Oct 17, 2018

    @larsoner
    Member

    @jeythekey can you make a PR to fix the problems you've identified?

  25. jeythekey commented on Oct 17, 2018

    @jeythekey
    Contributor

    OK, done so (my first one ;): #5620

  26. larsoner commented on Nov 13, 2018

    @larsoner
    Member

    Closing as (optimistically!) fixed, @sappelhoff or others if it's not, feel free to reopen

  27. cbrnr commented on Nov 13, 2018

    @cbrnr
    ContributorAuthor

    That's indeed optimistic 😄 - when I have more time I'll come back to this.

  28. sappelhoff commented on Nov 13, 2018

    @sappelhoff
    Member

    That's indeed optimistic smile - when I have more time I'll come back to this.

    +1, same here :-)

    ... at the latest the next time I want to use it. Fair enough to close it for now.

  29. cbrunner commented on Nov 13, 2018

    @cbrunner
  30. larsoner commented on Nov 13, 2018

    @larsoner
    Member

    Ahh yes sorry for the noise @cbrunner, we meant to tag @cbrnr -- you should be able to follow the "mute this thread" link in your email to disable notifications (or do so via the GitHub web interface)

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