Skip to content

MRG, ENH: Adding a function to import EEGLAB ICA. - #6938

Merged
agramfort merged 9 commits into
mne-tools:masterfrom
christian-oreilly:import_eeglab_ica
Jan 31, 2020
Merged

agramfort merged 9 commits into
mne-tools:masterfrom
christian-oreilly:import_eeglab_ica

Conversation

@christian-oreilly

@christian-oreilly christian-oreilly commented Oct 9, 2019 •

Copy link
Copy Markdown
Contributor

Reference issue

Fixes #6917.

What does this implement/fix?

Adding a function to import ica from EEGLAB .set files.

Todo:

  • Fix shapes of components using SVD

@codecov

codecov Bot commented Oct 9, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #6938 into master will increase coverage by 0.01%.
The diff coverage is 95%.

@@            Coverage Diff             @@
##           master    #6938      +/-   ##
==========================================
+ Coverage   89.75%   89.77%   +0.01%     
==========================================
  Files         445      445              
  Lines       79726    80070     +344     
  Branches    12754    12791      +37     
==========================================
+ Hits        71562    71883     +321     
- Misses       5370     5379       +9     
- Partials     2794     2808      +14

Comment thread mne/preprocessing/ica.py Resolved

@agramfort agramfort left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments to start

Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/tests/test_ica.py Resolved
@christian-oreilly

Copy link
Copy Markdown
Contributor Author

Note: I made corrections directly through GitHub interfaces so it made a bunch of small commits, but I take for granted that the person that will merge this PR will "squash and merge".

@christian-oreilly

Copy link
Copy Markdown
Contributor Author

@larsoner Do you know what could cause the Azure Pipelines to throw this kind of errors?

E           AssertionError: 
E           Found new public members missing from doc/python_reference.rst:
E           * read_ica_eeglab (mne.preprocessing.ica.read_ica_eeglab)

Do I need to add some doc somewhere besides the docstrings in the newly added function?

@cbrnr

cbrnr commented Oct 10, 2019

Copy link
Copy Markdown
Contributor

@christian-oreilly you need to add that function to doc/python_reference.rst because it is public.

Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/tests/test_ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
@larsoner

Copy link
Copy Markdown
Member

Diff looks remarkably clean, I expected it to be more painful. But I'm guessing the painful part was getting the math right :)

@christian-oreilly can you update doc/changes/latest.inc to include this? Then +1 for merge from me. @cbrnr are you also happy?

@cbrnr

cbrnr commented Oct 11, 2019

Copy link
Copy Markdown
Contributor

I didn't check the math so I trust that it works based on the test file. If we notice a bug later, we can always correct it then. So 👍 for merge.

Comment thread mne/preprocessing/tests/test_ica.py Resolved
Comment thread mne/preprocessing/ica.py Resolved
@christian-oreilly

Copy link
Copy Markdown
Contributor Author

Thanks @larsoner. I tried to keep it clean.

Yes, the math and understanding what is call what in EEGLAB versus MNE and how to do acceptable correspondances took most of the time.

I tested it with three datasets with ICA performed under different context. It might still break down when users will try it on ICA parametrized in ways I did not tested. Since the .set files are juste normal matlab files, users or different EEGLAB extension can modified them in ways I did not forseen. Also, I am also not terribly satisfied from rounding errors but the operations seems right as far as I can tell so I would attribute it matrix inversions or rounding when data are dumped to disk in .set and read back

Aside from the official tests, I did not see issues so far on the 100 hd-EEG recordings I used it in for database that I am currently analysing.

@agramfort

agramfort commented Oct 12, 2019 via email

Copy link
Copy Markdown
Member

@agramfort

agramfort commented Oct 17, 2019 •

Copy link
Copy Markdown
Member

rendering of docstring look wrong https://16261-1301584-gh.circle-artifacts.com/0/dev/generated/mne.preprocessing.ICA.html#mne.preprocessing.ICA not sure if it's your fault @christian-oreilly

@larsoner any idea?

btw I find the layout of the parameters from sklearn much more readable eg:

https://scikit-learn.org/stable/modules/generated/sklearn.cluster.AffinityPropagation.html#sklearn.cluster.AffinityPropagation

basically I would not expect the param description to be on the same line at the name + type that should stand out.

cc @drammock

@christian-oreilly

Copy link
Copy Markdown
Contributor Author

rendering of docstring look wrong https://16261-1301584-gh.circle-artifacts.com/0/dev/generated/mne.preprocessing.ICA.html#mne.preprocessing.ICA not sure if it's your fault @christian-oreilly

@agramfort Yes, it is probably my fault. CI checks worked before the last commit I made where I changed the docstrings.

just thinking out loud but could you compute an SVD of the mixing matrix to
obtain by the MNE pca_components and the mixing with the same shapes. It
would simplify API documentation.

I sure would. I had a look at it a few days ago, without much luck. I will look into it again when I get some time. Right now, I shifted to other duties.

@larsoner larsoner changed the title Adding a function to import EEGLAB ICA. WIP: Adding a function to import EEGLAB ICA. Oct 17, 2019
@agramfort

Copy link
Copy Markdown
Member

@christian-oreilly do you have time to make it green again? i can live with the current design

@larsoner

Copy link
Copy Markdown
Member

i can live with the current design

I would prefer if we could harmonize the two via svd. @christian-oreilly do you have time to look? If not I can try later this week.

@christian-oreilly

Copy link
Copy Markdown
Contributor Author

@larsoner No, I did not have much time to take a look at it again. Last time I did, I had the feeling that it might not be possible to do it cleanly. From memory, the issue was boiling down to the fact that we would want to decompose M=X@Y into U@S@V', with X, U and V' being rectangular and S and Y being square. So my problem was about how you get rid of the V' so that you can get the correspondences U=X and S=Y. I tried to transform data such that V' would become the identity matrix but without success. Feel free to have a look at it. Maybe a fresh set of eyes on this problem would be beneficial.

@agramfort I just looked at the CI issues more closely. It does not look like it was related to my last commit in the end. It was a socket error when running test_fetch_parcellations. No changes I made should have any effect on that test. Maybe just a transient network failure. I re-triggered the tests to check if they pass now.

@christian-oreilly

Copy link
Copy Markdown
Contributor Author

@larsoner Is this PR still on your radar? You were supposed to have a look at it around end of October.

@larsoner

larsoner commented Jan 6, 2020

Copy link
Copy Markdown
Member

Forgot about this, will try to look this week

@larsoner

larsoner commented Jan 8, 2020

Copy link
Copy Markdown
Member

@christian-oreilly okay for me to rebase and force push? I think I got this working but it would be good if you could double-check.

@larsoner
larsoner force-pushed the import_eeglab_ica branch 4 times, most recently from c534cc0 to df106aa Compare January 8, 2020 16:57
@larsoner

larsoner commented Jan 9, 2020

Copy link
Copy Markdown
Member

So, as far as I know, we will not manage to have something semantically (with respect to the definition of ica.unmixing_matrix and ica.pca_components_ in MNE) perfect here

I added a _assert_ica_attributes class that hopefully checks to make sure our PCA / unmixing / mixing matrices meet the definitions that MNE wants. But @christian-oreilly it would be good if you could check and see if your workflows using this function still work, or at least some basic use cases do. For example, if you could check to see that some component waveforms look the same in EEGLAB and MNE that would be very helpful.

Otherwise this should be good for review/merge now. Tests pass locally for me and the code has been squashed to by-author commits, so a rebase+merge rather than squash+merge should be done here if possible.

@larsoner larsoner changed the title WIP: Adding a function to import EEGLAB ICA. MRG, ENH: Adding a function to import EEGLAB ICA. Jan 9, 2020
@larsoner larsoner added this to the 0.20 milestone Jan 9, 2020
@christian-oreilly

Copy link
Copy Markdown
Contributor Author

@larsoner The test I added compare the signals transformed from applying the EEGLAB ICA to those obtained by applying the same ICA imported and applied to the signals in MNE. So I think it is a rather complete test that does verify that the waveform match between EEGLAB and MNE. I am in the middle of refactoring lots of code right now so I need to finish this refactoring before I can rerun my pipeline and see if everything works on my other uses cases. If you don't mind waiting something like a week before merging this PR, I can finish this refactoring, rerun the refactored pipeline with MNE patch with this PR and confirm that everything works fine on my end.

@larsoner

larsoner commented Jan 9, 2020

Copy link
Copy Markdown
Member

Sure, should be fine to wait

Comment thread mne/preprocessing/ica.py Resolved
@larsoner

Copy link
Copy Markdown
Member

@christian-oreilly does it work now?

@larsoner

Copy link
Copy Markdown
Member

(forgot the -1, force-pushed the fix)

@larsoner

Copy link
Copy Markdown
Member

@christian-oreilly can you try one more time? If all is well then we can merge

@christian-oreilly

Copy link
Copy Markdown
Contributor Author

Yes, sorry for the delay. What I tested worked well. I think it can be merged. Thanks.

@larsoner

Copy link
Copy Markdown
Member

@agramfort feel free to merge. Would be good to get a set of eyes on the additional checks that I put in place

@agramfort
agramfort merged commit ec63d98 into mne-tools:master Jan 31, 2020
@agramfort

Copy link
Copy Markdown
Member

thx @christian-oreilly and @larsoner !

@Federica-2

Copy link
Copy Markdown

Is there a documentation for mne.preprocessing.read_ica_eeglab() to understand the input to insert?
Also, can we use this function to add the projections to the EEG data that we need for source estimation analysis? Thanks!

@larsoner

larsoner commented Feb 5, 2020

Copy link
Copy Markdown
Member

@christian-oreilly

Copy link
Copy Markdown
Contributor Author

@dede3de You may want to have a look at test_read_ica_eeglab() in mne/preprocessing/tests/test_ica.py if you did not already. It would give you an example of use.

AdoNunes pushed a commit to AdoNunes/mne-python that referenced this pull request Apr 6, 2020
* Adding a function to import EEGLAB ICA.

* Update mne/preprocessing/ica.py

Typo correction.

Co-Authored-By: Alexandre Gramfort <[email protected]>

* Correct read_eeglab_ica to read_ica_eeglab.

* Update mne/preprocessing/ica.py

Co-Authored-By: Alexandre Gramfort <[email protected]>

* Corrections to the PR.

* Update mne/preprocessing/ica.py

Co-Authored-By: jona-sassenhagen <[email protected]>

* Adjusting docstrings for mixing/unmixing matrices.

* ENH: Unify

* FIX: Pick

Co-authored-by: Alexandre Gramfort <[email protected]>
Co-authored-by: Jona Sassenhagen <[email protected]>
Co-authored-by: Eric Larson <[email protected]>
AdoNunes pushed a commit to AdoNunes/mne-python that referenced this pull request Apr 6, 2020
* Adding a function to import EEGLAB ICA.

* Update mne/preprocessing/ica.py

Typo correction.

Co-Authored-By: Alexandre Gramfort <[email protected]>

* Correct read_eeglab_ica to read_ica_eeglab.

* Update mne/preprocessing/ica.py

Co-Authored-By: Alexandre Gramfort <[email protected]>

* Corrections to the PR.

* Update mne/preprocessing/ica.py

Co-Authored-By: jona-sassenhagen <[email protected]>

* Adjusting docstrings for mixing/unmixing matrices.

* ENH: Unify

* FIX: Pick

Co-authored-by: Alexandre Gramfort <[email protected]>
Co-authored-by: Jona Sassenhagen <[email protected]>
Co-authored-by: Eric Larson <[email protected]>
Comment thread mne/preprocessing/ica.py Outdated
ica.pre_whitener_ = np.ones((len(eeg.icachansind), 1))
ica.pca_mean_ = np.zeros(len(eeg.icachansind))

ica.unmixing_matrix_ = eeg.icaweights

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this specific change introduced a bug. I don't think the code for n_components < n_ch works as expected. I'll have to check, but it is possible that test_read_ica_eeglab just validates the "else" part of this if, which corresponds more or less to the behavior of this function before the introduction of this if-else statement. I'll create an issue to describe this bug and submit a corresponding PR.

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.

Import ICA results from EEGLAB

6 participants