Category: programming

Pi day: Pi on the keyboard

On my computers I am using Windows, Linux, and sometimes MacOS. Regarding the keyboard layout, I prefer the US English layout – it is just so much better for programming, anything you do on the shell, or whatever. On my Windows Desktop, I have designed my own keyboard layout (using Microsoft Keyboard Layout Creator) that is based on the standard US English configuration: it adds the German special characters (ä, ö, ü, ß), as well as many mathematical and scientific symbols, including Greek letters, which are used very often in science.

Recently, I have bought a new used laptop, ordered a US English replacement keyboard on ebay. The machine is running Linux. So far, so good. However, I did not want to have to go over the hassle of assigning my own special functions to the keyboard keys again (even though there are tools such as Keyboard Layout Editor available). I have not found any program that can automatically convert a Windows keyboard layout into its Linux counterpart. That is why – with some creative help from these regular expressions – I have made my own python script to convert from Windows to Linux. It turns out, that the most annoying part is the conversion of the key identifiers – these have very different names in the two operating systems.

However, eventually I figured it out, merged the generated file with my /usr/share/X11/xkb/symbols/us file, and now I am enjoying the easy access to π (via AltGr-p) and also Π (via Shift-AltGr-p) on my keyboard. Happy Pi day! Ok, The article is a day late. Happy belated Pi day! Or how about this (in German): Fröhlichen Π Tag!

I admit, I might spend too much time on these things, but it just makes me happy when it all works out automatically – or at least semi-automatically. I dislike repetitive tasks.

xkcd - Automation
xkcd – Automation

If you want to check out the script, or my keyboard layout, you can download it from github: Keyboard Layout Converter (GPL v3).

Gmail statistics

On which days of the week do you send or receive emails? What time of the day? Well, let’s find out.

I have been somewhat inspired by Stephen Wolfram and used some of the fabulous open-source libraries that are available for data exploration: I have been playing around with imaplib, pandas, seaborn/matplotlib and came up with a script to analyze your email behavior. You can use it with any IMAP account (yes, that includes your Gmail account). Of course, only the emails that are not yet deleted are analyzed – but usually Gmail users archive most of the emails – they are then accessible in the “All Mail” folder. The typical output gives you bar plots as well as violin plots, such as here:

bar plots and violin plots for the sent and received emails.

Interestingly, the analysis here shows that a significant amount of emails is sent after the “classical” work hours (particularly after midnight). As expected, almost no emails are sent during the sleep time (between ~2am and ~8am). Also, the weekend is rather quiet, albeit with Monday approaching increased activity is observed.

The script is released under GPL and can be found at: https://github.com/alexriss/email-stats

Pythonic heart rate

Using python and the great matplotlib library, I have made a little app that measures the hart rate and computes various heart rate variability parameters (such as rMSSD, pNN50, LF, HF). On the hardware side, the pulse sensor connected to an Arduino Uno microcontroller is used. Parts of the code are based on other open source libraries. I am also planning to release the code as open source.

Here is a screenshot of the app:

HeartRateEx Screenshot
HeartRateEx Screenshot

A few things still need to be worked out. For instance, the heart beat detection (which is made by the Arduino) is not perfect. A few percent of beats are misclassified – depending on the positioning of the pulse sensor.

Update: I have now uploaded the source code. You can find it on GitHub.

Heart rate variability

Heart Rate Variability (HRV) describes the variation in the time interval between heartbeats and it is often calculated based on RR intervals (i.e. the interval between two successive R-peaks in the ECG wave). HRV is relevant for clinical monitoring of patients, in psychophysiology, as well as in sports.

There are several approaches to measure HRV “at home”, such as:

  • Mobile Apps that try to measure the HRV from a photoplethysmogram (similar to pulse oxymetry) using the integrated camera and LED light.
  • Apps that connect to heart rate monitors (capable of RR-interval measurement) and calculate HRV from the data.
  • Record an ECG, analyze the graph to find the positions of the R peaks and compute the HRV descriptors from there.

In the next months, I want to compare these methods. I have already played around with the first method (using the Android apps “Stress Check” and “Heartservice“), but I am quite concerned about the precision of this approach. For the second method an adequate heart rate monitor is needed, which are available from Viiiva, Garmin, Wahoo TICKR, and Polar. It seems like chest straps are needed for precise measurements. Connection to the mobile phone via ANT+ or Bluetooth LE is possible. While Bluetooth LE seems easier, I have set up ANT+ on my Nexus 5 relatively quickly using the “ANT+ enabler” and some of the official ANT+ apps. As I still need to buy a heart rate monitor capable of measuring RR intervals, I have not yet tested any of the apps.

For the third method (recording an ECG), I am planning to use an Arduino microcontroller and the Olimex EKG-EMG shield. An interesting alternative is the E-Health platform by cooking hacks that will work with  Arduino, Raspberry Pi and Intel Galileo boards, and can be connected to various other medical sensors.

Update: I found the Pulse Sensor, a lightsuccessful kickstarter project allowing you to measure the heart rate using a light sensor (it basically records a photoplethysmogram). The clip is connected to a microcontroller (such as an Arduino) and can be easily attached to your finger or ear lobe. That makes it one of the easiest methods to get HRV data.

Once the measurements are set up for robust everyday use, I want to study how certain HRV descriptors correlate with workout intensity, recovery, food intake, and so on.

Stay tuned for more.