Skip to content

WIP: Warn if ICA was performed on potentially rank-deficient data - #8328

Closed
hoechenberger wants to merge 8 commits into
mne-tools:masterfrom
hoechenberger:ica-rank-deficiency
Closed

hoechenberger wants to merge 8 commits into
mne-tools:masterfrom
hoechenberger:ica-rank-deficiency

Conversation

@hoechenberger

@hoechenberger hoechenberger commented Oct 1, 2020 •

Copy link
Copy Markdown
Member

Reference issue

Closes #8313, #8316, #7727

What does this implement/fix?

  • Emits a warning if we have reason to believe ICA operated on rank-deficient data.
  • plus some small cleanups

Additional information

WIP because working on this PR exposed that the default for max_pca_components, currently n_channels, might not be the best choice. Lots of tests fail now because we get the new warning, typically:

E   RuntimeWarning: It is likely that we operated on rank-deficient data, which is known to be problematic. The estimated rank of the data is 363, which is smaller than the number of retained PCA components (366).
E   
E   Rank-deficiency  can be caused through SSP projections or certain referencing schemes, e.g. an EEG average reference.It is STRONGLY advised to perform rank reduction using PCA prior to ICA in order to ensure proper operation of the ICA algorithm.
E   
E   Since the rank of your data was estimated to be 363, we recommend instantiating the ICA object via:
E   
E       ICA(..., max_pca_components=363)
E   
E   (or using an even smaller number).

This is because currently, I calculate rank with SSP projectors applied, so the rank of the data would effectively be n_channels - len(info['projs']), which is obviously smaller than n_channels if there is any projection vectors stored.

Now this got me thinking, should the default of max_pca_components be changed from n_channels to n_channels - len(info['projs'])?

Applied SSP projectors leading to rank-deficient data and thereby screwing up ICA was exactly the problem @agramfort and I witnessed in the past.

@hoechenberger

Copy link
Copy Markdown
Member Author

@agramfort @larsoner @cbrnr
Would like to hear your thoughts on this

@hoechenberger

Copy link
Copy Markdown
Member Author

If #8326 becomes a success, we can also close this PR…

@larsoner

larsoner commented Oct 1, 2020

Copy link
Copy Markdown
Member

WIP because working on this PR exposed that the default for max_pca_components, currently n_channels, might not be the best choice. Lots of tests fail now because we get the new warning, typically:

Do we fit ICA on the data with projectors applied, or not? If it varies, then we can probably compute rank only taking into account projectors marked as "active" (i.e., applied to the data). I imagine most raw instances have not had the projectors applied, and hence shouldn't be counted when computing the rank.

@hoechenberger

Copy link
Copy Markdown
Member Author

Do we fit ICA on the data with projectors applied, or not?

I'm not sure, would need to step through the code. Does Raw/Epochs.get_data() apply projectors before returning the data?

@larsoner

larsoner commented Oct 1, 2020

Copy link
Copy Markdown
Member

Does Raw/Epochs.get_data() apply projectors before returning the data?

Only if apply_proj or proj=True was done, in which case active=True should be set in info['projs']. Hence why I think we need to make use of that property.

And you should be able to check this in a test that uses it just by doing a quick np.linalg.matrix_rank on the data, at least if it's MEG (or EEG) only.

@hoechenberger

Copy link
Copy Markdown
Member Author

Ok, sounds good!

@hoechenberger

Copy link
Copy Markdown
Member Author

@larsoner
I've added handling for projectors, but haven't extensively tested.

Also needed to add an option to include EOG channels in rank computation via compute_rank, similarly to how we handle ref_meg. Probably will need to do the same with ECG too…

@larsoner

larsoner commented Oct 1, 2020

Copy link
Copy Markdown
Member

Also needed to add an option to include EOG channels in rank computation via compute_rank, similarly to how we handle ref_meg. Probably will need to do the same with ECG too…

Why? If these channels are included in the ICA, they should be included in the rank calculation, no? We need the ICA to be stable, so if those channels are included in ICA they should affect the rank calculation.

@hoechenberger

hoechenberger commented Oct 2, 2020 •

Copy link
Copy Markdown
Member Author

Why? If these channels are included in the ICA, they should be included in the rank calculation, no? We need the ICA to be stable, so if those channels are included in ICA they should affect the rank calculation.

Yes, exactly. By default on master, EOG channels are are not included in rank calculation. But they are included in ICA.

I discovered this because some ICA unit tests that do have an EOG channel started throwing a warning about rank deficiency, because only the M/EEG channels were considered by the rank calculation. I can also revert the last changes that add EOG to rank calculation so you can see for yourself.

My current approach of adjusting _estimate_rank_meeg_signals is probably not the best idea anyway, because EOG is clearly not M/EEG? Or is it, in MNE-speak? ;))

@hoechenberger

hoechenberger commented Oct 2, 2020 •

Copy link
Copy Markdown
Member Author

@larsoner I've reverted the "EOG changes" so you can see for yourself what the problem was
Two ICA tests or so are now expected to fail.

@cbrnr

cbrnr commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

I'm OK with issuing a warning if we detect rank deficiency. However, the rank needs to be computed on the same data that ICA uses (and not just by assuming certain types of channels). Furthermore, I have never used projectors, so I hope these do not interfere with ICA - if they do (e.g. if projectors are not only available but also active) we should also issue a warning.

@hoechenberger

Copy link
Copy Markdown
Member Author

I'm OK with issuing a warning if we detect rank deficiency. However, the rank needs to be computed on the same data that ICA uses (and not just by assuming certain types of channels).

Yes, this is precisely what we're / I'm trying to achieve here :)

Furthermore, I have never used projectors, so I hope these do not interfere with ICA - if they do (e.g. if projectors are not only available but also active) we should also issue a warning.

If they're applied, each projector will reduce the rank of the data by 1.

@cbrnr

cbrnr commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

If they're applied, each projector will reduce the rank of the data by 1.

Yes, but at least issue an info. I could imagine myself having applied projectors accidentally, or people might want to compute ICA with the original data even if they have projectors applied. That way, they can at least see what's going on.

@hoechenberger

Copy link
Copy Markdown
Member Author

Yes, but at least issue an info. I could imagine myself having applied projectors accidentally, or people might want to compute ICA with the original data even if they have projectors applied. That way, they can at least see what's going on.

You mean something like

INFO Performing ICA on data with rank X (Y channels, Z projectors applied).

?

@cbrnr

cbrnr commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

Exactly!

@hoechenberger

Copy link
Copy Markdown
Member Author

@cbrnr Ok, that seems reasonable.

@larsoner
For your convenience, without the EOG-picking adjustments in compute_rank(), the following test fails (see CI logs: estimated rank is 4, but number of channels was 5 – 4 MEG + 1 EOG):

================================== FAILURES ===================================
__________________________ test_eog_channel[fastica] __________________________
mne\preprocessing\tests\test_ica.py:931: in test_eog_channel
    ica.fit(inst, picks=picks1)
<decorator-gen-384>:21: in fit
    ???
mne\preprocessing\ica.py:570: in fit
    warn(f'It is likely that we operated on rank-deficient data, '
mne\utils\_logging.py:348: in warn
    warnings.warn_explicit(
E   RuntimeWarning: It is likely that we operated on rank-deficient data, which is known to be problematic. The estimated rank of the data is 4, which is smaller than the number of retained PCA components (5).
E   
E   Rank-deficiency  can be caused through SSP projections or certain referencing schemes, e.g. an EEG average reference. It is STRONGLY advised to perform rank reduction using PCA prior to ICA in order to ensure proper operation of the ICA procedure.
E   
E   Since the rank of your data was estimated to be 4, we recommend instantiating the ICA object via:
E   
E       ICA(..., max_pca_components=4)
E   
E   (or using an even smaller number).

@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member

IIRC the only reason it's really named _estimate_rank_meeg_signals is because typically we only want data channels for noise cov + inversion. I don't see a problem with making it work for other channel types, too -- we just need to make sure our default scaling factors do something reasonable for EOG and ECG channels for example, and that we don't start accidentally returning EOG/ECG in places in the code that currently use that function. This should be fixable via some picking arguments of some sort.

@hoechenberger

hoechenberger commented Oct 2, 2020 via email

Copy link
Copy Markdown
Member Author

@larsoner

larsoner commented Oct 2, 2020

Copy link
Copy Markdown
Member

Yes

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

larsoner commented Oct 5, 2020

Copy link
Copy Markdown
Member

This PR should be updated so that only n_components is checked this way. It should be fairly easy to do at the same point in the code where we do the pinv to get the mixing_matrix_ from the unmixing_matrix_ (after dividing by the sqrt of the variances).

@larsoner larsoner mentioned this pull request Oct 7, 2020
8 tasks done
@hoechenberger
hoechenberger deleted the ica-rank-deficiency branch January 13, 2021 22:59
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.

3 participants