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

Add two new functions: a) find periods in hypnogram and b) heart rate by stage #68

Merged
merged 16 commits into from
Jun 3, 2022

Conversation

raphaelvallat
Copy link
Owner

Hi,

This PR adds two new functions to YASA, which will hopefully be released in YASA 0.6.2.

  • The yasa.hypno_find_periods is a flexible function that allows to find sequences of consecutive values exceeding a certain duration in hypnogram. See the "Examples" section of the documentation of the function for some concrete examples.
  • The yasa.hrv_stage is the first function of the new "Heart rate analysis" module. Specifically, it allows to calculate heart rate and heart rate variability (HRV) features from an ECG. By default, the cardiac features are calculated for each period of N2, N3 or REM sleep that are longer than 2 minutes. It uses the yasa.hypno_find_periods to find the periods (and thus inherits from the flexibility of that function), and the SleepECG library to detect the heartbeats in the ECG. It also returns, for each epoch, the indices of the detected heartbeats. This can be used to calculate more advanced heart rate variability metrics (e.g. frequency domain), or do some EEG-ECG coupling analysis.

I am looking for 1-2 reviewers to review this PR. (Suggestions @olmurillo @remrama, who else?)

Thanks!

@raphaelvallat raphaelvallat self-assigned this May 20, 2022
@raphaelvallat raphaelvallat added the enhancement 🚧 New feature or request label May 20, 2022
@codecov-commenter
Copy link

codecov-commenter commented May 20, 2022

Codecov Report

Merging #68 (6565dd5) into master (71c0a82) will increase coverage by 0.16%.
The diff coverage is 92.71%.

❗ Current head 6565dd5 differs from pull request most recent head 7a55e76. Consider uploading reports for the commit 7a55e76 to get more accurate results

@@            Coverage Diff             @@
##           master      #68      +/-   ##
==========================================
+ Coverage   90.35%   90.51%   +0.16%     
==========================================
  Files          20       22       +2     
  Lines        2342     2489     +147     
==========================================
+ Hits         2116     2253     +137     
- Misses        226      236      +10     
Impacted Files Coverage Δ
yasa/heart.py 87.50% <87.50%> (ø)
yasa/hypno.py 95.55% <89.18%> (-4.45%) ⬇️
yasa/__init__.py 100.00% <100.00%> (ø)
yasa/io.py 100.00% <100.00%> (ø)
yasa/tests/test_heart.py 100.00% <100.00%> (ø)
yasa/tests/test_hypno.py 100.00% <100.00%> (ø)
yasa/tests/test_io.py 100.00% <100.00%> (ø)
yasa/detection.py 96.37% <0.00%> (+0.13%) ⬆️

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 71c0a82...7a55e76. Read the comment docs.

@remrama
Copy link
Collaborator

remrama commented May 21, 2022

@raphaelvallat I'd love to help here but you should know I've never reviewed a PR before. I wouldn't be asking for any direction -- I'll dig that up on my own -- but I guess I'd ask for some leeway on ignorance/mistakes and it might take longer than you're expecting. So I understand completely if you'd rather go with someone else. If you want me to go through with it let me know what kind of time frame you'd expect or at least prefer.

@olmurillo
Copy link

olmurillo commented May 22, 2022 via email

@raphaelvallat
Copy link
Owner Author

Hi @olmurillo and @remrama!

Thank you both for replying! To give more context, I want to start making changes in YASA as "peer-reviewed" pull requests (PR) instead of just me pushing commits to the main branch that no one will ever check. I couldn't think of anyone else to help with the review, but please feel free to just say no and/or recommend someone else!

If you have the time to help, here are some guidelines for reviewing the PR:

  • Please only focus on the heart.py and hypno.py files. You can discard the other files (i.e. mark them as "Viewed").
  • I think I mostly need help with making sure that the documentation of the two new functions is clear and understandable (especially since you are both native English speakers).
  • As for the actual code, I hope that it is commented and written in such a way that you should be able to understand the overall pipeline. If it's not the case, please let me know. You should mostly try to a) spot blatant mistakes that I may have missed, b) see if the outputs of the function make sense or if there is something we can do that would be more useful/generalizable.
  • The best way to review the PR is simply to try the new functions and make sure that they work as expected. You will need to git fork YASA on your local computer, switch to the PR branch and then install the develop version of YASA by typing python setup.py develop in the YASA folder. Let me know if you need help with this.

As for timeline, I would say within the next couple of weeks would be great but totally understand if that's not possible. There's no urgency at all.

Thanks again!
Raphael

@remrama
Copy link
Collaborator

remrama commented May 24, 2022

Ok this sounds great @raphaelvallat I'd love to contribute with a PR review. I expect to be able to have this done within the "couple of weeks" timeframe. I'll let you know otherwise, or if anything comes up.

@olmurillo
Copy link

olmurillo commented May 31, 2022

One place to improve flexibility in hypno_find_periods() (line 408 of hypno.py):
np.not_equal() does not work with strings (unless np.char.not_equal is used) and there may be cases where the hypnogram contains strings, e.g., hypno = np.array(['W', 'W', 'R', 'R']) . Currently testing, but looks like a simple switch to: loc_run_start[1:] = x[:-1] != x[1:] instead of np.not_equal(x[:-1], x[1:], out=loc_run_start[1:]) should do the trick! It should also work if hypnograms are a combination of strings and ints.

@raphaelvallat
Copy link
Owner Author

@olmurillo Thanks for catching that! I just pushed a commit implementing your changes :):
6565dd5

Copy link
Collaborator

@remrama remrama left a comment

Choose a reason for hiding this comment

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

Amazing work as always @raphaelvallat

My comments are all incredibly minor and the PR could be merged with them all ignored. I was mostly just thinking out loud.

Making ECG accessible through yasa was a great idea and I look forward to utilizing this module a ton :)

yasa/hypno.py Outdated Show resolved Hide resolved
yasa/heart.py Show resolved Hide resolved
yasa/heart.py Outdated Show resolved Hide resolved
yasa/heart.py Show resolved Hide resolved
yasa/heart.py Outdated Show resolved Hide resolved
@raphaelvallat
Copy link
Owner Author

raphaelvallat commented Jun 3, 2022

Thank you @remrama! Really appreciate the time and effort you put in this review :) I have just pushed a commit to implement your changes, and I have left two conversations unresolved.

@raphaelvallat raphaelvallat merged commit 34e40b6 into master Jun 3, 2022
@raphaelvallat raphaelvallat deleted the hrv_stage branch June 3, 2022 19:09
This was referenced Jun 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 🚧 New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants