Repository navigation
Closed
Description
This is just a rough sketchpad to help organize and keep track a little of the ICA construction site, because the big picture is fading quickly for me…
-
We now support floats inmax_pca_components, and we will keep this parameter -
We will dropICA.n_pca_components, as typically, users can achieve similar results by controllingmax_pca_componentsandn_components. This will close ICA should not touch init parameter n_pca_components during fit #8327 - Ensure that projectors are handled better and that
n_pca_components=Nonework (MRG, BUG: Fix ICA with projectors #8343) - Selection of number of components via
n_componentsas a float should follow the "greater than" approach, not "less or equal than" (Change interval of selected components for ICA.n_components #8336, MRG, FIX: Test round-trip, fix EEGLAB ICA #8326) - Even in cases where we don't warn, display the estimated rank of the and potentially applied projectors, as suggested by @cbrnr (WIP: Warn if ICA was performed on potentially rank-deficient data #8328)
- Next PR:
- Drop
ICA.max_pca_componentsvia deprecation - Keep
ICA.n_componentsandICA.n_pca_components, keepapply(..., n_pca_components)to select a sub-set of all PCs when reconstructing the signal - Warn if
n_pca_componentsn_componentsis likely to operate on rank-deficient data (produce ~zero-valued principal components) by checking the variance division values used when computing mixing_matrix_ from the unmixing_matrix_ (WIP: Warn if ICA was performed on potentially rank-deficient data #8328) - Make sure
n_pca_componentsis not changed during fit (should become n_pca_components_; ICA should not touch init parameter n_pca_components during fit #8327)
- Drop
- Next PR: make n_pca_components and apply_baseline keyword-only by fixing decorator
How would we handle the deprecation ofmax_pca_components? Will we even need a deprecation cycle, since while we're changing the API, we're not removing functionality, and we could emit a fat helpful error message if sb tries to use this param?
Metadata
Metadata
Assignees
Labels
No labels
Activity
hoechenberger commentedon Oct 3, 2020
@larsoner @agramfort Let's move ICA discussions here :)
agramfort commentedon Oct 4, 2020
sounds like a plan !
larsoner commentedon Oct 4, 2020
Although it might be slightly cleaner to do:
I can live with the API:
it makes it clear that the value only matters during reconstruction, not during fitting. But if we consider the constructor
n_pca_componentsa sort of "default value" that can be overridden it makes sense, especially since it will be an attribute of the object and survive a save-load round-trip.One thing I think we should no longer do is remove any components from the PCA decomposition -- there is no speed gain so no advantage to doing so beyond a negligible memory and I/O space savings. This means this would now be permitted:
In other words, the
n_pca_componentsin the constructor provide the default number to reconstruct with, but you can always choose up ton_channelsto use.-1 on this. I actually think it won't matter or break anything -- you'll only need to be careful about rank deficiency for the
n_componentsstep (to keep theself.mixing_matrix_ = pinv(self.unmixing_matrix_)stable because we divide the latter by the exp var). If you do the lines I pasted above on 10-channel EEG data with an average projector I think it will reconstruct just fine. There will be a noise component reconstructed but it will be 1e-14 (or maybe 1e-7) or so down so should be fine.From a practical standpoint, I think we should merge #8326, then a follow-up PR using the
nooptest there should helpassertthe behaviors above about not needing to warn about rank-deficiency ofn_pca_components, justn_components.larsoner commentedon Oct 4, 2020
... and then for defaults we can just have:
and everything should be safe.
hoechenberger commentedon Oct 4, 2020
Ok so let's go ahead and merge #8326 then, and then move on from there.
hoechenberger commentedon Oct 4, 2020
#8326 has been merged. I want to test this with some of our previously problematic data before we proceed. But out for a walk now – will test sometime tonight.
hoechenberger commentedon Oct 4, 2020
Why not set
n_componentsto a bearable value by default, like, "70" or "100" or so… And warn and use fewer components if there are not as many in the datahoechenberger commentedon Oct 4, 2020
@larsoner Sorry to break this to you but #8326 is not doing the trick. The ICA decomposition seems to be working fine, but the signal reconstruction step with
n_pca_components = max_pca_components = None(i.e., all channels) screws up the results (check out the "cleaned" magnetometer data in the last plot; gradiometers are not affected. Magnetometers, on the other hand, have projectors applied, i.e. their data is rank-deficient, and removal of the ECG-related ICs doesn't seem to really do much of anything.)Setting
n_pca_components = 0.9999, however, yields a sensible reconstruction, with the ECG artifact being removed from both, gradiometers and magnetometers.This is exactly the same behavior @agramfort and I witnessed before.
larsoner commentedon Oct 4, 2020
Because this depends on the number of channels which is a bit wacky. It seems like going by the variance explained and just fitting all the components by default is better.
Okay I'm not sure why this would be the case -- I'll take a look tomorrow and see why it breaks
larsoner commentedon Oct 4, 2020
If it turns out there is some bug / rank deficiency is problematic, then I still think
max_pca_componentscan go away and we can just haven_components=None, n_pca_components=0.9999or something as defaults.hoechenberger commentedon Oct 4, 2020
+1 on that.
Thanks for taking the time to look into this, I really appreciate it.
hoechenberger commentedon Oct 4, 2020
But if you have hundreds of sensors like in MEG it may seem ICA never finishes. I guess what I'm getting at is: could we maybe add a progress bar somehow?
larsoner commentedon Oct 4, 2020
For infomax maybe because we write the code for it. For FastICA and Picard you'll have to look into seeing if those libraries provide some interface for a callback to call at the end of each loop. Then we could use that plus the
max_iterto get it into a ProgressBar (which might terminate early because of convergence criteria, but this might be okay).hoechenberger commentedon Oct 4, 2020
I could also just add a progress bar upstream in
picard? We do passmax_iterto it, after all13 remaining items
cbrnr commentedon Oct 5, 2020
This doesn't change anything, because all 0.9999 means all components:
Which means that the ECG detection algorithm still identifies all components as ECG-related:
hoechenberger commentedon Oct 5, 2020
When you say "means all components", are you referring to this part in the output:
?
Because that only tells you how many components were selected based on
n_components, which is "all" forn_components=NoneStill,
n_pca_components=0.9999should not keep 305, but 302 PCs. Are you absolutely sure you're using the latestmasterto run this code?cbrnr commentedon Oct 5, 2020
I was using 0.21, will retry with the latest master...
hoechenberger commentedon Oct 5, 2020
Oh!!! Yes, sorry, I assumed you're on
master! You're missing the fixes @larsoner has added since the release.larsoner commentedon Oct 5, 2020
It looks like the real problem with this example is that the data are fit on Epochs that have been projected (rank 302) and then apply is run on Epochs that have not been projected (rank 305).
In your example if you:
proj=Falseto theEpochsconstructor, orica.fit(raw)instead of epochs, orecg_evoked.apply_proj()It works. Here is a faster version you can more quickly test with:
Details
To me the fix is to make sure that whatever projections are applied (present and active) during the
fitstep are present and active during theapplystep / on that instance, or maybe better, just apply them directly using theica.infoinstance since they are stored there.larsoner commentedon Oct 5, 2020
Working on a fix now
cbrnr commentedon Oct 5, 2020
So does this mean that the initial problem is also solved? Was it caused by computing ICA on different data than applying it to?
larsoner commentedon Oct 5, 2020
It was caused by fitting on data that had projections applied but applying it to data that didn't have them applied (and not applying them in the process)
cbrnr commentedon Oct 5, 2020
OK then, I'm glad we don't have to use
n_pca_components=0.9999then!hoechenberger commentedon Oct 5, 2020
But this will only fix things in cases where we know about projectors, right? What about e.g. #7727, where we might deal with EEGLAB-preprocessed data that has been average-referenced, hence is now rank-deficient, but we don't know this beforehand? (I know #7727 was originally about covariance estimation, but I'd expect to run into similar issues with ICA).
hoechenberger commentedon Oct 5, 2020
But why not? It wouldn't hurt – or would it? I cannot see any drawbacks here, neither with EEG nor with MEG data. Am I missing something?
hoechenberger commentedon Oct 5, 2020
Although, in this case, we'd fit and apply on the same data, i.e., same level of rank deficiency… still. Would that work?
larsoner commentedon Oct 5, 2020
No, the average ref (not projector, applied) works fine in
masteralready in thenooptest IIRC, and should be fine. The critical thing is just that the data you fit should have the same rank deficiency / linear operators applied to it as the data you apply to. So in the case of projections we can just force this to be the case. People with custom refs or rank deficiencies from elsewhere will just have to be careful about it.Any time you have to numerically estimate rank you have to trade off the risk of blowing up some meaningless/numerically zero component against accidentally zeroing out some meaningful component. So we should only add it if we need to.
Yes as above
larsoner commentedon Oct 5, 2020
We should just emit a warning saying it will be removed and they should use
n_pca_componentsinstead. Then in our.fitwe setn_pca_components_using the smaller of then_pca_componentsvalue andmax_pca_componentsvalue (for example if one is int and one is float). And we have to be careful that when reading an ICA object we properly handle old ICA objects in a similar way.