Clinical Grade Sleep Tracking using Neurobit-HRV API

Consumer sleep trackers are woefully inaccurate and lab-based sleep measurement is prohibitively expensive and ridiculously uncomfortable. A key barrier to progress is the tech to extract high-quality sleep information from heart rate. Not anymore…

Dr. Amiya Patanaik
Neurobit Technologies
6 min readMay 27, 2020

--

Your heart is a window to your mind (Original Photo by Kinga Cichewicz on Unsplash)

It has been 7 weeks since the announcement of the circuit-breaker by the Singapore government. While offices remain closed, we at Neurobit have been working tirelessly (at home), pushing the boundaries of AI in the pursuit of better sleep for everyone. We are proud to announce the release of Neurobit-HRV API. A set of tools to extract very high-quality sleep information from heart rate data that may come from an ECG chest belt, a wearable device, or a medical device with just two lines of code. Before we go into the details, let us see why this has far-reaching consequences.

As sleep clinics across the world remain closed and patients avoid Hospitals even in emergencies, the importance of remote health monitoring cannot be overstated. For a long time, consumers have tried to take their health into their own hands with wearable and fitness trackers. But when it comes to sleep, it has been a story of disappointments. As Dr. Robert S Rosenberg, a board-certified sleep physician and author of The Doctor’s Guide to Sleep Solutions for Stress & Anxiety puts it about current sleep trackers:

Their algorithms assume with motion you are awake and without it you are asleep. They tend to overestimate total sleep time. They are very poor at detecting true awakening during the night. They purport to be able to tell if you are in a light or deep sleep. — Dr. Robert S Rosenberg (source)

Sleep is measured from your brain activity, so EEG (brain waves) remains the gold standard of sleep measurement. Multiple scientific studies involving simultaneous measurement from EEG and sleep trackers support Dr. Rosenberg’s view. But what if there was a way to get a glimpse of your mind without actually measuring EEG?

Introducing Heart Rate (Variability)

Everyone should be familiar with the heart rate. Thanks to the rapid rise in interest in fitness trackers, commoditization of hardware and R&D into completely new ways of measuring bio-signals, heart rate measurement is slowly becoming integral to most trackers. Much more interesting than heart rate is heart rate variability or HRV. In simple terms, it is the fluctuations in your heart rate which is controlled by your autonomic nervous system (ANS). The ANS can be further split into the parasympathetic nervous system (PNS) and the sympathetic nervous system (SNS). The PNS is referred to as “rest and digest” while the SNS is referred to as “fight or flight” and reflects responds to stress and exercise. The HRV reflects the intricate dance between these two systems and truly is a gateway to your mind.

Lower HRV is associated with both higher mortality and morbidity. It’s an excellent proxy for your fitness levels and biological age. The importance of HRV in the fitness industry is well recognized. The reason why this is relevant for sleep is because HRV might be a cost-effective and scalable way to measure sleep with very high accuracy. So what is the issue?

Measuring sleep from HRV is a challenging problem

There is an increasing trend of consumer companies trying to come towards health-care while health-care companies are trying to be more consumer-friendly. While you can get away with showing pretty but inaccurate graphs to consumers, to bridge this gap between consumer tech and clinical grade, high accuracy and reliability are a must.

The hardware gap between consumer devices and clinical devices are slowly fading away. Soon what will differentiate the two will be software — the ability to extract real value from the same measurements.

The relationship between HRV and sleep is intricate and a major data science problem. Despite combining HRV and motion, the accuracy of sleep-tracking remains poor. Solving this requires deep domain expertise, a thorough understanding of AI and machine learning technologies, and access to a massive amount of simultaneous EEG/HRV data.

This is where Neurobit’s razor-sharp focus and experience in developing state-of-the-art sleep analytics technologies come into play. Neurobit-HRV is part of a suite of sleep analytics technologies under the Neurobit’s health analytics umbrella. We have spent 6 years of R&D refining our technologies and working with our customers and partners to push the boundaries of what is possible. Let’s now move to the details of the tech.

Tech behind Neurobit-HRV

Our HRV module was trained on 2720 nights (approx 22,000 hours) of simultaneous EEG and ECG which was painstakingly sleep scored by expert technicians to serve as ground truth. This data came from every corner of the world from Japan to the US, with a spread of age and disease conditions. The AI models behind the scene are state of the art deep-learning models. A detailed validation report will be published soon. But for now, we will show how easy it is to use on a third-party open sleep dataset. Keep in mind that our models have never seen this data.

Neurobit-HRV in action

This section is meant for developers.

Update: We have also added support for real-time staging using HRV. Here is a Jupyter Notebook with a demo.

Scoring using HRV is as simple as these two lines of python code:

stream = create_ndf_from_ecg(ecg_signal, sampling_rate)
response = post('https://z3score.com/api/v4/hrv', files={'file': ('stream.ndf', stream)}, data={'email':email, 'key':key})

NDF or Neurobit Data Format is our proprietary format to communicate with our servers. It ensures security, anonymity, and reduces data overhead by 100X or more. You are not only limited to ECG. You can extract your own rr peaks from photoplethysmogram or any other modality and directly feed it to our API. There is no need to do any artifact correction as we do it at our end.

stream = create_ndf_from_rr(rr_loc, sampling_rate)

Here rr_loc is the index of the rr peaks. So if the rr peaks are located at index 128 and 256 at a sampling rate of 128 Hz, the peaks are located at 1 sec and 2 sec respectively. This format of the API makes it easy to accommodate any sampling rate without increasing data overhead.

Restrictions:

The API is meant for offline use. You are expected to feed it a complete sleep episode. The minimum duration of the data is at least 36 minutes. If you have access to motion/position, you should use it to sensor data that is obviously wake. This can further boost the accuracy of our API.

Complete example with ISRUC public dataset

Start off by installing the latest version of our pyndf SDK located here: https://bitbucket.org/NeurobitTech/pyndf/src/master/

or you can also do a pip install: pip install pyndf

git clone https://NeurobitTech@bitbucket.org/NeurobitTech/pyndf.git
cd pyndf
python setup.py install

Install all dependencies

pip install numpy numba msgpack msgpack_numpy py-ecg-detectors scikit-image scipy pyedflib pandas tqdm

Here is the complete Jupyter notebook to programmatically download the ISRUC subgroup 3 dataset comprising 10 healthy subjects simultaneous EEG/ECG recordings which were scored by two experts and run it through our API and finally compare it with expert scores.

We achieve an impressive average accuracy of 83.5% on Wake, Non-REM, REM classification and a 72.6% accuracy on Wake, Light Sleep, Deep Sleep and REM classification. This is incredible as even expert to expert agreements are only 80 to 90%.

Performance summary of Neurobit-HRV on ISRUC subgroup 3 dataset comprising of 10 healthy subjects manually scored by two experts.

Final Words

Neurobit-HRV API is currently in Beta and is already accessible to existing Neurobit customers. It is strategically designed to work across demographics and disease conditions and is not limited to a healthy adult population. It is a key technology behind Pulse (more info at https://www.mysleep.study), a highly cost-effective and scalable way of diagnosing sleep apnea. If you are interested in our technologies and would like to test them out, please fill out the demo form at z3score.com or contact us at support@neurobit.com

--

--

Dr. Amiya Patanaik
Neurobit Technologies

CEO & Co-founder of #Neurobit, leverage technology to make high-quality sleep health accessible and affordable https://www.neurobit.io