Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of channels with dropouts (intermittent flat regions) within NoisyChannels #81

Merged
merged 7 commits into from
May 7, 2021

Conversation

a-hurst
Copy link
Collaborator

@a-hurst a-hurst commented May 6, 2021

PR Description

Closes #80. Because Numpy's quantile function handles NaN values differently than MATLAB, finding bad-by-correlation channels in NoisyChannels broke completely if there were any correlation windows where a channel had a flat signal. Also, the code that was meant to replace NaN correlation values with zeros was accidentally doing so in the wrong matrix. Both of these are fixed by this PR!

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
Copy link
Collaborator Author

a-hurst commented May 6, 2021

Ah whoops, I accidentally based this off the MATLAB comparison test branch. Rebasing on master now.

Copy link
Owner

@sappelhoff sappelhoff left a comment

Choose a reason for hiding this comment

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

Ah thanks, now I see what you meant before. You solved it well, I think. Just one comment.

Comment on lines +369 to +370
with np.errstate(invalid='ignore'): # suppress divide-by-zero warnings
window_correlation = np.corrcoef(np.transpose(eeg_portion))
Copy link
Owner

Choose a reason for hiding this comment

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

in which cases would the np.errstate(invalid='ignore') be necessary? And could it results in window_correlation being NaN?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, inside np.corrcoef it uses the standard deviation of the inputs at some point in the calculation, and since for a dropout channel the SD is going to be 0, we get NaNs because of the division-by-zero error (and a corresponding RuntimeWarning message). However, the current code uses those NaNs to determine which channels are dropout channels for each window so things would need to be refactored a fair bit to prevent the NaNs in the first place.

I'm thinking of doing a proper refactor of NoisyChannels at some point once MATLAB comparison is merged, so I went with the path of least resistance for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

To clarify, the with np.errstate is just to suppress some division-by-zero runtime warnings that are currently inevitable/expected based on how the code is structured.

Copy link
Owner

Choose a reason for hiding this comment

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

that makes sense, thanks for clarifying.

@codecov-commenter
Copy link

Codecov Report

Merging #81 (967845c) into master (ffa6346) will increase coverage by 0.17%.
The diff coverage is 94.11%.

❗ Current head 967845c differs from pull request most recent head 5e784ea. Consider uploading reports for the commit 5e784ea to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master      #81      +/-   ##
==========================================
+ Coverage   97.49%   97.66%   +0.17%     
==========================================
  Files           7        7              
  Lines         678      686       +8     
==========================================
+ Hits          661      670       +9     
+ Misses         17       16       -1     
Impacted Files Coverage Δ
pyprep/utils.py 98.42% <91.66%> (-0.75%) ⬇️
pyprep/find_noisy_channels.py 97.71% <100.00%> (+1.18%) ⬆️

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 ffa6346...5e784ea. Read the comment docs.

@sappelhoff sappelhoff added this to the 0.4.0 milestone May 7, 2021
@sappelhoff sappelhoff merged commit 20e2941 into sappelhoff:master May 7, 2021
@a-hurst a-hurst mentioned this pull request May 7, 2021
23 tasks
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.

Dropout channel detection broken in PyPREP
3 participants