Skip to content

Implement EEGLAB-style interpolation in Reference for matlab_strict - #96

Merged
sappelhoff merged 10 commits into
sappelhoff:masterfrom
a-hurst:eeglab_interpolation
Jun 28, 2021
Merged

sappelhoff merged 10 commits into
sappelhoff:masterfrom
a-hurst:eeglab_interpolation

Conversation

@a-hurst

@a-hurst a-hurst commented Jun 27, 2021 •

Copy link
Copy Markdown
Collaborator

PR Description

Closes #77. This PR basically reimplements most of EEGLAB's eeg_interp method in Python so we can finally do MATLAB equivalence tests for the entirety of the Reference class (also added in this PR). This was... not easy to say the least, but it was necessary for the peace of mind that these MatPREP comparison CI tests allow (and also helped us identify a bunch of other small bugs/discrepancies along the way).

This is also the last numeric difference from MatPREP I'm aware of other than adaptive line noise removal (and differences between EEGLAB's montage-importing code and MNE's which result in differences in RANSAC, but that's not exactly a PREP divergence as much as an EEGLAB/MNE one).

Anyway, I've tried my best to make sense of and comment/document the re-implemented EEGLAB logic, but there are still a few areas I don't fully understand. If you have any insights, please let me know!

Merge Checklist

  • the PR has been reviewed and all comments are resolved
  • all CI checks pass
  • (if applicable): the PR description includes the phrase closes #<issue-number> to automatically close an issue
  • (if applicable): bug fixes, new features, or API changes are documented in whats_new.rst

@a-hurst

a-hurst commented Jun 27, 2021

Copy link
Copy Markdown
Collaborator Author

Note: if the docstring for _eeglab_calc_g seems a little opaque, that's because it's more-or-less a direct copy of the docstring for MNE's equivalent internal function. I'm not sure I understand the logic behind the actual math well enough to write anything more informative.

@codecov-commenter

codecov-commenter commented Jun 27, 2021 •

Copy link
Copy Markdown

Codecov Report

Merging #96 (98ac7c3) into master (3625b26) will increase coverage by 0.21%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #96      +/-   ##
==========================================
+ Coverage   98.83%   99.04%   +0.21%     
==========================================
  Files           7        7              
  Lines         687      736      +49     
==========================================
+ Hits          679      729      +50     
+ Misses          8        7       -1     
Impacted Files Coverage Δ
pyprep/reference.py 99.20% <100.00%> (+0.88%) ⬆️
pyprep/utils.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3625b26...98ac7c3. Read the comment docs.

@sappelhoff sappelhoff left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks! Nice to see all the equivalence tests :-)

One more general comments about the EEGLAB "translation" to Python --> i'd appreciate if you could go to the EEGLAB GitHub repo and create "permanent links" to the parts of the codebase that you used for the translation.

For example, you have these two lines of code:

  # Subtract all the Euclidian electrode distances from 1 (why?)
    EI = np.ones([n_to, n_from]) - elec_dists

This would be significantly enhanced (from a dev/docs perspective) if you add one more comment line like this (note, that's just an example):

# https://github.com/sccn/eeglab/blob/167dfc8766a253619fc586848c704665e39bce9f/Contents.m#L8

You can also shorten the hash to 7 characters, instead of using the full hash:

https://github.com/sccn/eeglab/blob/167dfc8/Contents.m#L8

Comment thread docs/matlab_differences.rst Resolved
Comment thread tests/test_matprep_compare.py Resolved
@a-hurst

a-hurst commented Jun 28, 2021

Copy link
Copy Markdown
Collaborator Author

One more general comments about the EEGLAB "translation" to Python --> i'd appreciate if you could go to the EEGLAB GitHub repo and create "permanent links" to the parts of the codebase that you used for the translation.

Do you mean per function, or per individual chunk of code (e.g. the EI = np.ones([n_to, n_from]) - elec_dists line you mentioned)? For the latter, it's a little messy since I refactored the code a bit for readability & consistency with MNE's versions of these functions, but I can certainly try!

@sappelhoff

Copy link
Copy Markdown
Owner

Do you mean per function, or per individual chunk of code (e.g. the EI = np.ones([n_to, n_from]) - elec_dists line you mentioned)? For the latter, it's a little messy since I refactored the code a bit for readability & consistency with MNE's versions of these functions, but I can certainly try!

I don't have a system in mind, so I'd leave this up to you and recommend to do it in a "reasonable way". For example if you take something from one file in EEGLAB, then put a link ... if the file you draw from changes further down your code, put in a new link.

The readers can then be expected to follow your link and pick up the pieces themselves. that is: not every line of code must be mapped 👍

Comment thread docs/matlab_differences.rst Resolved
@a-hurst

a-hurst commented Jun 28, 2021

Copy link
Copy Markdown
Collaborator Author

I don't have a system in mind, so I'd leave this up to you and recommend to do it in a "reasonable way". For example if you take something from one file in EEGLAB, then put a link ... if the file you draw from changes further down your code, put in a new link.

Okay, I put line references to the definitions of the functions I reimplemented. Hopefully that's enough for anyone curious in the future!

@sappelhoff sappelhoff left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

thanks! :)

@sappelhoff sappelhoff added this to the 0.4.0 milestone Jun 28, 2021
@sappelhoff
sappelhoff merged commit 1c9589c into sappelhoff:master Jun 28, 2021
@a-hurst a-hurst mentioned this pull request Jun 28, 2021
22 of 23 tasks
sappelhoff pushed a commit that referenced this pull request Jul 7, 2021
)

* Add initial implementation of EEGLAB interpolation

* Update matlab_differences.rst

* Add basic MATLAB comparison tests

* Fix spelling mistake

* Update whats_new.rst

* Make isort happy

* Improve wording around matlab_strict parameter

* Fix Perrin citation RST

* Add explicit refs to reimplemented EEGLAB code

* Add references header in matlab_differences
@sappelhoff

Copy link
Copy Markdown
Owner

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.

Interpolation of bad channels differs numerically between PyPREP and MatPREP

3 participants