Skip to content

ICA questions #4882

Closed
Closed

Description

@cbrnr

I don't understand some choices in the ICA calculation process, maybe someone could answer these questions:

  1. Why is a so-called pre-whitener (by default channels are divided by their standard deviations) used before PCA/ICA? I think this is not necessary because the next step is PCA with whiten=True.
  2. Speaking of PCA and whitening, sphering seems to be more optimal for ICA, and sphering is also used in EEGLAB by default. AFAIK the only change necessary would be another rotation by the eigenvector matrix to convert the PCA whitening transform to a sphering transform. Should we care to implement it?
  3. I don't understand this line - why are the elements of the unmixing matrix divided by the square root of the explained variances?

Activity

  1. larsoner commented on Jan 19, 2018

    @larsoner
    Member

    Why is a so-called pre-whitener (by default channels are divided by their standard deviations) used before PCA/ICA? I think this is not necessary because the next step is PCA with whiten=True.

    My guess is that it's for numerical stability. If you have channels on drastically different scales then even PCA can fail.

    Speaking of PCA and whitening, sphering seems to be more optimal for ICA, and sphering is also used in EEGLAB by default. AFAIK the only change necessary would be another rotation by the eigenvector matrix to convert the PCA whitening transform to a sphering transform. Should we care to implement it?

    I have no problem with sphering=False default (for backward compat) but I'm guessing @agramfort or Pierre (can't remember GitHub name) might have thoughts.

    I don't understand this line - why are the elements of the unmixing matrix divided by the square root of the explained variances?

    This one I do not know, except to say that it has been there since 2014 (0.8) and was added by @dengemann

  2. agramfort commented on Jan 19, 2018

    @agramfort
    SponsorMember
  3. cbrnr commented on Jan 19, 2018

    @cbrnr
    ContributorAuthor

    if you do PCA dim reduction and you combine grad and mag then you will only
    use the channel type with highest absolute amplitude unless you prewhiten.

    Of course! Feature normalization prior to PCA is essential - somehow I didn't make the connection (prewhitening, z-score - we should update the docs and just call normalization standardization by its name).

    Infomax does not require sphering but FastICA does. So you need to do:

    prewhiten -> PCA for dim red with whiten=True -> FastICA (or Infomax)

    clear?

    No 😄 - white=True is not sphering, it's whitening. Or are you saying that normalization (aka pre-whitening) plus whitening equals sphering?

  4. agramfort commented on Jan 21, 2018

    @agramfort
    SponsorMember
  5. cbrnr commented on Jan 22, 2018

    @cbrnr
    ContributorAuthor

    to do this you need W to be the inverse of a square root of cov(X). Let's
    call the svd of this covariance U S^2 U^T then W can be S^{-1} U^T or
    U S^{-1} U^T. What you call (I think) sphering is U S^{-1} U^T and whitening
    S^{-1} U^T but they are just 2 choices to whiten the data.

    Yes, the terminology seems to be inconsistent. Whitening is a transform that decorrelates the data (i.e. the covariance matrix becomes the identity matrix), e.g. via PCA. There are infinitely many ways to whiten due to rotation. Sphering is a special kind of whitening that rotates the data back to the original space (some call that ZCA). So exactly like you say, but currently we (and scikit-learn) use PCA whitening, but EEGLAB uses sphering (and I remember having read somewhere that ZCA is more optimal for Infomax than PCA whitening, but I can't find that anymore).

    In any case, since I can't find relevant references, any kind of whitening seems to work just fine, so if I find more on that topic I'll create a new issue.

    So only the last question left - @dengemann?

  6. cbrnr commented on Jan 22, 2018

    @cbrnr
    ContributorAuthor

    Improved docstring in #4886.

  7. cbrnr commented on Jan 26, 2018

    @cbrnr
    ContributorAuthor

    OK, I figured it out myself. The line

    self.unmixing_matrix_ /= np.sqrt(exp_var[sel])[None, :]
    

    does the whitening, which is not part of the PCA transform matrix self.pca_components_.

    Kind of confusing, I think it would be much easier if all transforms were accessible as matrices, i.e. (1) the pre-whitening (standardization) (a diagonal matrix), (2) the PCA transform matrix, (3) the whitening (again a diagonal matrix), and (4) the ICA unmixing matrix. Oh, and the mean of the data should also be removed prior to PCA I guess. Currently, the matrices accessible as ica.unmixing_matrix_ and ica.pca_components_ are not really that useful, because it is not obvious how they should be used. Instead, the user has to rely on a transform method, which confusingly is called get_sources.

  8. agramfort commented on Jan 26, 2018

    @agramfort
    SponsorMember
  9. cbrnr commented on Jan 29, 2018

    @cbrnr
    ContributorAuthor

    It's not about how to store the matrices, but how to get them. My problem with the current approach is that currently you get ica.pca_components_ and ica.unmixing_matrix_ - but you cannot apply these matrices to the original data to do the transform, because you have to scale to unit variance, remove the mean, and whiten in addition. Ideally, I would like to replicate the transform that I get via get_sources with the transform matrices. Strangely, this is not possible with sklearn.decomposition.PCA either, because the whitening is not part of the PCA transform matrix. Of course this information is available with the explained_variance_ attribute, but I think it would be more convenient to get it in matrix form.

    What about adding a new method get_transform_matrices (is there a better/shorter name?) which returns all matrices required for the ICA transform?

    Alternatively, the user might only be interested in getting the final ICA transform matrix, so this might be another option (I would call this get_unmixing_matrix, but this would clash with ica.unmixing_matrix_).

    Finally, I suggest the following API changes.

    1. Let's rename get_sources to transform. This is more intuitive and in line with the sklearn API.
    2. Let's rename ica.unmixing_matrix_ to ica.ica_unmixing_matrix_. Then we could add the final unmixing matrix as ica.unmixing_matrix_ instead of a method get_unmixing_matrix. Similarly, we could add the other transform matrices as attributes.

    Either way, this needs to be documented so that it is clear what is going on. Once we've decided what to do I'll be happy to take care of that.

  10. agramfort commented on Jan 29, 2018

    @agramfort
    SponsorMember
  11. cbrnr commented on Jan 30, 2018

    @cbrnr
    ContributorAuthor

    how about we add get_unmixing_matrix method and add a param
    with_withening=True | False and if True the get the matrix you want?

    No need for a parameter, we're always whitening. I'm OK with adding a method get_unmixing_matrix if it returns the complete transformation matrix which takes you from the raw data to the sources (i.e. this unmixing matrix contains all transforms).

    I am -0.5 on changing yet again the ICA API...

    Yes, I don't want to do that either, so let's clearly document it.

    regarding sklearn API I would argue that we enforce this on the decoding
    module that works mostly with plain numpy arrays.
    get_sources is I think more explicit for non sklearn users.

    Well, ICA transforms the data too. What about adding a new alias method transform that just calls get_sources?

  12. agramfort commented on Jan 30, 2018

    @agramfort
    SponsorMember
  13. cbrnr commented on Jan 30, 2018

    @cbrnr
    ContributorAuthor

    from a user perspective why do you what to have the full matrix?
    you expect users to want to use directly this numpy array rather than
    calling get_sources?

    Yes. Partly also because you can get the full transform matrix in EEGLAB (where it is documented to be weights * sphere). Since you ask this question, I think the full matrix is much more useful for users than what MNE currently offers (ica.unmixing_matrix_ and ica.pca_components_). However, even better would be what I suggested previously, namely to have all matrices that compose the transform.

    I also think that we should not hide the full unmixing matrix from the user - what if someone wants to analyze its structure etc.?

    regarding the transform alias I would argue:

    $ python -c "import this" | grep "only one"
    There should be one-- and preferably only one --obvious way to do it.
    

    Right, but I think consistency is also important (the zen should really be updated to include that). But OK, I'm not Dutch, so get_sources was not really obvious to me since I was looking for transform.

  14. cbrnr commented on Jan 30, 2018

    @cbrnr
    ContributorAuthor

    Oh, and also, get_sources works with a raw instance, so yes, I think it would be very useful to be able to apply the ICA transform to a NumPy array.

  15. larsoner commented on Jan 30, 2018

    @larsoner
    Member

    I think it would be very useful to be able to apply the ICA transform to a NumPy array.

    A side note FYI -- this is a hack but for any of these linear transforms we have keep in mind you can hack it with something like:

    trans_mat = thing.transform(RawArray(np.eye(len(raw.ch_names)), raw.info)
    

    It sounds like you're saying that we want people to be able to do this without a hack workaround, though, so I'm fine with exposing the necessary parts and/or a method.

    Given that we already expose some of the components (but not all?), maybe we should just expose any remaining necessary components and tell people how to compose the transformation matrix in the docs (e.g., t = np.dot(a, b * c[:, np.newaxis) rather than just giving a method to obtain it. I think we should have all of these components exposed to the user in any case, so maybe we don't need to add a new method for such an advanced application.

  16. 13 remaining items

  17. cbrnr commented on Feb 9, 2018

    @cbrnr
    ContributorAuthor

    It is common practice to compute ICA on one dataset and apply it to another dataset. So in the first step, you solve s = U @ x for U. This transform now includes centering, i.e. it subtracts the channel means of x. But if you apply U to a new signal, you want to compute s1 = U @ x1. Therefore, U removes the mean of x1 - why should it remove the mean of x from x1?

    An option to assume zero-mean signals would be useful though.

  18. agramfort commented on Feb 9, 2018

    @agramfort
    SponsorMember
  19. cbrnr commented on Feb 9, 2018

    @cbrnr
    ContributorAuthor

    Yes, I get the point. You compute everything in fit and then apply that in transform. But this makes it hard to write the whole transform into one unmixing matrix - or am I missing something obvious?

  20. agramfort commented on Feb 9, 2018

    @agramfort
    SponsorMember
  21. cbrnr commented on Feb 9, 2018

    @cbrnr
    ContributorAuthor

    Currently we subtract the mean after scaling (pre-whitening), but before whitening and before PCA (after all, we use pca.mean_). But you can't write this is one matrix U then, because center uses a mean from a different signal. So the best solution would be to get rid of centering and assume that it is done separately (i can look into this some more to find out how it behaves). It is the same thing with filtering: ICA requires a highpass filter, but we don't incorporate the filter into the final unmixing matrix. We don't have to integrate centering either. I think if this is documented, we don't have to change code (except for renaming ica._pre_whitener to ica.pre_whitener_ for consistency), but "only" update the docs.

  22. agramfort commented on Feb 9, 2018

    @agramfort
    SponsorMember
  23. agramfort commented on Feb 9, 2018

    @agramfort
    SponsorMember
  24. cbrnr commented on Feb 9, 2018

    @cbrnr
    ContributorAuthor

    How does the centering matrix from the previous data look like then? I was using this definition here, and it only works on the new data.

  25. agramfort commented on Feb 9, 2018

    @agramfort
    SponsorMember
  26. cbrnr commented on Feb 9, 2018

    @cbrnr
    ContributorAuthor

    Hm, that's a pretty large difference (I assume because of the pseudo-inverse calculation), even with the same data X.

  27. agramfort commented on Feb 9, 2018

    @agramfort
    SponsorMember
  28. larsoner commented on Apr 4, 2018

    @larsoner
    Member

    @cbrnr do you have time to write a summary PR of this in the Notes section of the ICA object?

  29. added this to the 0.16 milestone on Apr 4, 2018
  30. cbrnr commented on Apr 11, 2018

    @cbrnr
    ContributorAuthor

    Yes, will do!

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions