You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
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
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?)
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_.
This is a very old API on which lots of user code relies and I see no
reason why it is broken or needs to be changed.
It is an important feature that our API allows us to precisely control the
relationship between ICA and PCA components used and discarded. I'd just
suggest to improve the documentation if needed.
On Thu, Jan 11, 2018 at 10:18 AM Clemens Brunner ***@***.***> wrote:
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_.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#4856 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AB0fiqwuK-peWm_9N1t32qsw8ZgQOTU3ks5tJdHNgaJpZM4RX9Zb>
.
We used this a lot historically in several research projects and
developmentally it helped us to come up with the final API here that allows
you to run ICA on few PCA components while not throwing away anything when
reconstructing the signal. Another potential use case maybe additional
denoising by discarding PCA.
That would be great. The important is to make the examples clear-cut and
just not advertise the more expert arguments. Additional reasons why I am
reluctant to change this API is that we discussed it in several papers and
it really makes explicit the relationship between ICA and PCA, which is
educational.
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.
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.
@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).
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).
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.
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_.
Like @cbrnr, I have issues with the ICA API and would appreciate a quick check on my intuition about it:
My current understanding is:
calculate PCA on data X
retain max_pca_components and project X to X_reduced
at this point, probably n_components is doing something ... but what?
Using X_reduced, we compute an ICA and have X_reduced_ica
From X_reduced_ica, we retain/reject components
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)
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)
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.
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? :-)
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.)
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)
Activity
mmagnuski commentedon Jan 9, 2018
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_channelsif Nonen_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
mmagnuski commentedon Jan 9, 2018
Generally when going from data to ICs:
data --[ PCA and
max_pca_components]--> PCs --[ICA andn_components] --> ICsn_pca_componentsis used when going in the reverse order (IIRC to drop some PCs - ignore them in reconstructing the data?)cbrnr commentedon Jan 11, 2018
This is confusing. Can we combine
max_pca_componentsandn_componentsinto one argument (calledn_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 then_componentsargument insklearn.decomposition.FastICAor thepcaargument inrunicado. 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 letn_componentsaccept bothintandfloat.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 thisweightsand defines the unmixing matrix asweights @ sphere. And I still don't see the need for an_pca_componentsargument - if someone really needs to drop PCA components in the reverse case, one could always do this manually withpca_components_andunmixing_matrix_.dengemann commentedon Jan 11, 2018
cbrnr commentedon Jan 11, 2018
Where is this feature (controlling ICA and PCA components separately) actually used?
dengemann commentedon Jan 11, 2018
cbrnr commentedon Jan 11, 2018
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.
dengemann commentedon Jan 11, 2018
mmagnuski commentedon Jan 11, 2018
I'm also in favor of improving the docs and not changing the API.
n_componentsalready allows that - this is actually clearly stated in the docs. I mosly use onlyn_components- you don't need the other params for basic use cases.cbrnr commentedon Jan 11, 2018
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_componentsargument).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_componentsandmax_pca_componentsactually do the same thing? If yes, this would be great in a way, because everyone else is using then_componentsargument 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.mmagnuski commentedon Jan 11, 2018
@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).cbrnr commentedon Jan 12, 2018
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
xis (64, N). Let's further assume we want to compute only 60 ICs.In EEGLAB, I'd specify
pca=60for 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 @ icasphereand is of shape (60, 64). This unmixing matrix can then be applied to the data to get the sources asunmixing @ x, which results in the expected shape (60, N).How do we achieve the same thing with MNE? First I thought that
n_components=60does 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_ @ xhas incompatible dimensions. Apparently, I have to dounmixing_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=60actually 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 matricesweightsandsphere(and their product isunmixing).Finally, I have still no idea why
n_pca_componentsis 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_componentsis the counterpart to EEGLAB'sn_components(and scikit-learn'sn_components).cbrnr commentedon Jan 12, 2018
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.
mmagnuski commentedon Jan 12, 2018
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.cbrnr commentedon Jan 12, 2018
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 viaunmixing_matrix_ @ pca_components_[:60]. Settingmax_pca_components=60seems to be more what I expect, because then the unmixing matrix isunmixing_matrix_ @ pca_components_.51 remaining items
cbrnr commentedon Jan 19, 2018
Do you have such a large file for me to test? Is there something in the sample data?
dengemann commentedon Jan 19, 2018
dengemann commentedon Jan 19, 2018
dengemann commentedon Jan 19, 2018
cbrnr commentedon Jan 19, 2018
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.
sappelhoff commentedon Mar 29, 2018
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:
Xmax_pca_componentsand projectXtoX_reducedn_componentsis doing something ... but what?X_reduced, we compute an ICA and haveX_reduced_icaX_reduced_ica, we retain/reject componentsX_reduced_icausing the ICA weights and haveX_reduced''indicating that some components were rejected in the ICA ... but the dimensions are the same)X_reduced'can now be back-projected toX'using the PCA weights of dimensionmax_pca_componentsX'instead of the originalXbecause of two reasons: (a) some ICA components were missing (rejected) to get the sameX_reducedand (b)X_reduceddoes not contain all pca components so we cannot recover all the data)n_pca_componentscomes into play by using more than justmax_pca_componentsto constructX'fromX_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? :-)
jeythekey commentedon Oct 17, 2018
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.)
larsoner commentedon Oct 17, 2018
@jeythekey can you make a PR to fix the problems you've identified?
jeythekey commentedon Oct 17, 2018
OK, done so (my first one ;): #5620
larsoner commentedon Nov 13, 2018
Closing as (optimistically!) fixed, @sappelhoff or others if it's not, feel free to reopen
cbrnr commentedon Nov 13, 2018
That's indeed optimistic 😄 - when I have more time I'll come back to this.
sappelhoff commentedon Nov 13, 2018
+1, same here :-)
... at the latest the next time I want to use it. Fair enough to close it for now.
cbrunner commentedon Nov 13, 2018
larsoner commentedon Nov 13, 2018
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)