So for years I’ve always felt excessive daytime sleepiness and fatigue. Once I finally got a job after school I now had health insurance and money to start looking into the issue. I visited a sleep doctor and was told “without even doing a sleep study I can already tell there’s a 75% chance you have sleep apnea”. Long story short after a $1000 sleep study the end result was almost zero sleep apnea and no issues identified at all. Basically I was told I’m fine.

I wasn’t happy with that answer since I knew there was something up. I decided to record some video to look into what I might be doing at night.

I took a $10 webcam and removed the IR filter from it. Any webcam will do but the $10 one worked great for this. I also got a $10 IR light from Amazon (48 led night vision illuminator). I set this up a few feet past the foot of my bed. I used some clear PVC pipe I had on hand to setup a 3d printed mount so that the camera was about 5 feet higher than where I slept.

Alright so after this I ended up with around 1.2 GB of video, but what do you do from there? I tried watching a little bit and scanning manually skipping around but it was tough to find anything interesting. So I decided to learn some Python and OpenCV to analyze what was happening.

The OpenCV part was pretty straightforward. I used a background subtract function which is normally used for motion tracking/highlighting. Rather than using the mask to highlight parts of the video, I used the mask to calculate how much activity was happening in a given frame. Simply summing up the mask array gives you a value that describes frame by frame activity. A high value means a lot of pixels have changed from one frame to the next. This condenses the data of every frame down to a single 2-byte or so value and we calculate an average every second to further reduce the data. So that condenses the data we need to interpret from from 1.2GB of information down to ~100kB. That’s a factor of 10,000 in reduction and makes our job a little easier. We can use that to generate the simple plot shown below.

Picture1

So this plot very generally shows us where there is activity throughout the night. Again, I found this kind of hard to interpret but it started pointing me in the right direction. It definitely looks like something’s going on here, but what? The next step was to make a compilation video so I could see exactly what this activity was. Another quick observation at this point was that the activity seems to happen in episodes throughout the night that last about an hour. At this point it made sense that the periods of low activity are probably deep REM sleep and that something is happening outside of that. The first compilation video I made is the youtube link below.

Based on the compilation video I could now see I have a lot of foot and leg movements in hour long episodes and there are a few of those episodes a night. At first I was searching about restless leg syndrome but that is something that affects people while awake. But I started to find mentions of “periodic limb movement disorder” alongside the RLS. This disorder is specifically involuntary during sleep and you will have hour long episodes where you have movements every minute or two with multiple episodes a night. That was the “AHA” moment. It felt good to finally have an explanation for my fatigue. The $1000 sleep study I had specifically had a section on PLMD and said I scored 0.0 – absolutely no movements. I asked the PA how they quantify that and apparently it’s based on leg electrodes – I don’t remember them putting leg electrodes on me and when I asked to see the data I never did get a clear answer on if it exists.

Another data reduction we can do on the activity plot is to count up the number of kicks/events in a given time interval and plot that (shown below). You could use this to determine PLM index since this is the # of distinct kicks/movements throughout the night.

So with a day’s work I was able to take an intimidating mountain of data and turn it into a simple plot of 32 data points that also has medical diagnostic relevance (PLMD index). While it’s kind of nice having disorder identified, I haven’t really found a fix yet. PLMD isn’t really the root cause, just a description of symptoms. Work in progress!

Here is a link to the jupyter notebook I put together that does all of the analysis described above:

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

For anyone who made it this far, here’s a funny dog tax that I caught on accident during this data analysis…

chewShort