cbrnr / sleepecg

Sleep stage detection using ECG
BSD 3-Clause "New" or "Revised" License
86 stars 22 forks source link

Heart rate data #199

Open MashRiza opened 8 months ago

MashRiza commented 8 months ago

I have introduced Changes to the SleepECG package, primarily focusing on enabling sleep stage classification using heart rate data when EEG signals are not available. Key features of this update include:

Heart Rate to R-Interval Conversion:

Implemented an algorithm to convert heart rate data into R-intervals. This crucial development allows the use of heart rate data, which is more readily available in many scenarios, for sleep stage classification. Handling Data Gaps:

Added robustness to the system by integrating code that effectively handles scenarios where data might be missing for extended periods. This ensures more reliable performance and resilience in real-world applications. Web-Based and Local Application:

Originally developed for a web-based project, these enhancements are equally applicable for local deployments, thereby broadening the usability scope of the SleepECG package.

cbrnr commented 7 months ago

Thank you for your PR. Could you please explain what you added that wasn't available in SleepECG before? We already compute RRI, and based on that, we have implemented many different features (see https://sleepecg.readthedocs.io/en/stable/feature_extraction/).

Regarding robustness, there is an ongoing discussion in #14, maybe you want to chime in there?

MashRiza commented 7 months ago

Thanks for taking the time to review my pull request. In this update, I focused on enhancing the SleepECG package's capability to classify sleep stages using heart rate data (a heart rate with corresponding timestamp )when EEG signals are unavailable. example data : data = [ {'TimeStamp': '1690409900', 'HeartRate': '55'}, {'TimeStamp': '1690410000', 'HeartRate': '60'}, {'TimeStamp': '1690410120', 'HeartRate': '65'} ...] Specifically, I introduced an algorithm to convert heart rate data into R-intervals, providing a valuable addition to the existing features. While I acknowledge that RRI computation is already part of SleepECG, my contribution lies in showcasing a practical example in the example folder. This example illustrates how to calculate R-intervals from heart rate and perform subsequent sleep stage classification, catering to scenarios where only heart rate data is accessible.

Additionally, I addressed the robustness of the system by implementing code to handle missing data for extended periods. This improvement aims to ensure more reliable performance in real-world applications.

Considering the ongoing discussion in #14 about robustness, I'll certainly try my best to chime in to contribute to the discourse and provide further insights into the implemented changes.

Moreover, these enhancements were initially developed for a web-based project, but I made sure they are equally applicable for local deployments, thereby expanding the SleepECG package's usability scope .

cbrnr commented 7 months ago

It looks like the main addition is an option to convert RRI back to time stamps, correct? I think this could be quite useful, but in order to integrate it into SleepECG, you should put it into a dedicated function. I'm not sure if it belongs to the heartbeats or feature_extraction module, probably in the former. The function should receive an array of RRIs and return an array of heart beats.

Please remove your changes from README.md. Also, the example needs to be adapted in order to use the dedicated function. We can decide later how to best create an example (currently I think it is too long).

@hofaflo WDYT?

hofaflo commented 7 months ago

If I understand correctly, the input data would not be RR intervals, but an irregularly sampled heart rate. But I generally agree, a function which takes heart rate and corresponding timestamps as input and produces (estimated) heartbeat times which can then be used as input to the existing feature extraction functions would be a useful addition!

cbrnr commented 7 months ago

Definitely! RRI is the inverse of the HR, so the function would support both measures.

MashRiza commented 7 months ago

Definitely! RRI is the inverse of the HR, so the function would support both measures.

The idea for this usage came up as I was working on a current project. I analyze heart rate data gathered from smart bands, and I need to deploy it on the web using JSON files as input. Thanks for your review; I will make the changes you mentioned earlier (wrap it in dedicated function and delete changes in README.md) and ask for more help if it's possible. I also recommend checking a competition on Kaggle, which has a relatively useful topic. kaggle.com/competitions/child-mind-institute-detect-sleep-states

MashRiza commented 7 months ago

Definitely! RRI is the inverse of the HR, so the function would support both measures.

The idea for this usage came up as I was working on a current project. I analyze heart rate data gathered from smart bands, and I need to deploy it on the web using JSON files as input. Thanks for your review; I will make the changes you mentioned earlier (wrap it in dedicated function and delete changes in README.md) and ask for more help if it's possible. I also recommend checking a competition on Kaggle, which has a relatively useful topic. kaggle.com/competitions/child-mind-institute-detect-sleep-states

i have added HeartRateSleep function as last function in feature_extraction.py . the function also has some codes for handling missing intervals as it is common in smart band/watch . i would love to read your reviews