Apple Watch, Cardiogram, and Atrial Fibrillation

Cardiogram app (iOS, WatchOS).
Image source:

Fascinating news today that the app Cardiogram (iOS, WatchOS) has used machine learning algorithms to differentiate between regular cardiac rhythms and irregular rhythms such as atrial fibrillation (AF) from data collected by the Apple Watch. The study in which this finding was presented states the app picked up 97% of AF episodes. While real-world usage may give different results (e.g. if the Apple Watch wasn’t worn correctly), and consumers should be cautioned about false negatives and the risk in trusting any device absolutely – this is still very exciting news.

AF is a key concern in heart disease. The heart is made of four chambers, two atria and two ventricles. Blood normally flows from the atria to the ventricles and from there to the lungs and the rest of the body. For the atria to transfer the blood over to the ventricles, they need to pump well. In AF, the atria do not pump in a coordinated manner and, instead, “fibrillate”. This causes two key problems. First, the ventricles aren’t filled with blood well enough. This means that when the ventricles contract to send blood to the rest of the body, they are pumping out a lesser amount of blood. One of the organs that gets this reduced amount of blood is the brain – and this is why syncope (fainting) or pre-syncope (feeling as if you’re about to faint) can be associated with AF. The other, potentially more serious, issue with AF is that because the blood isn’t moving along from the atria well enough, it increases the likelihood of clots forming in the atria. These clots can be pumped by the heart into circulation and go on to block blood vessels, such as in the brain causing a stroke. There are other reasons why AF is something to take seriously, but this is the gist of it.

For those who are already in known AF, the cornerstone of management is rate control and anticoagulation medication. It would be interesting to see if the Cardiogram app not only recognizes when the patient has gone into AF, but also accurately notes the actual heart rate during the AF event. Atrial fibrillation where the rate is within accepted range, is a completely different beast to “fast AF” which can cause life-threatening hemodynamic compromise (critically inadequate amount of blood pumped to the body). In cases where rhythm control is also a treatment aim (e.g. when the patient experiences troublesome symptoms despite rate control), I wonder if the Cardiogram app could help give an idea of how often the rhythm is irregular. It would also be very helpful if AF detection by the app automatically fired off an alert to caregivers, particularly in the elderly who may not always be able to signal for help immediately.

Another use I can think of for AF detection is when a patient with known AF presents with syncope. In the absence of witnesses to the syncopal event, history of the presenting complaint can be tricky to elicit. This can make it difficult to determine if the syncope was due to AF (poor pumping by the heart), or a different reason. If the Apple Watch showed an AF event coinciding with the syncopal event (perhaps by measuring a sudden drop in altitude?), that could be pretty handy information for the clinicians. I can think of so many great ways to use the sensors on Apple Watch to help with serious medical conditions, but I’ll leave that for a separate blog post.

I feel Apple is well suited to push a wearable into serious healthcare territory, beyond mere step tracking. Building trust will be key but I think Apple could just pull it off.

Notes on AngularJS


Most of AngularJS is straightforward and there’s good documentation elsewhere online.

Some important/interesting bits 

  • filters are very cool e.g. {{ lastname | uppercase }}. Particularly useful with ng-repeat e.g. ng-repeat="x in names | orderBy:'country'" or ng-repeat="x in names | filter : 'i'" . Wham! So convenient. And you can write your own filters.
  • ng-src for images
  • ng-bind is one-way binding ($scope to view); ng-model is two-way binding.
  • easy form validation with using the required attribute, .$valid or .$error​, and possibly some cool CSS magic using ng-dirty/pristine/valid/invalid. Good to disable default browser (novalidate) validation to have consistency.
  • when working with tables, there are handy properties like $odd , $even, $index you can use, say, with ng-if
  • creating dropdown lists connected to a model is easy with ng-options (while you could also use ng-repeat, with ng-options the objects don’t have to be strings)
<select ng-model="selectedName" ng-options="x for x in names">
// or using any Object, where x is the key, y is the value.
<select ng-model="selectedCar" ng-options="x for (x, y) in cars">
  • angular events could be used for some pretty cool stuff with animation
  • ng-include lets you refactor out snippets of html, which is loaded via ajax
  • consider using a custom directive instead of ng-include – makes the HTML more expressive.
  • custom directives can be of type element, attribute, class, or comment. Default is element and attribute (i.e. restrict: 'EA').
  • you can move controller logic into a custom directive using the returned object’s controller property.
app.directive("productDetails", function() {
   return {
      restrict: 'E',
      templateUrl: 'product-details.html',
      controller: function(..){/*controller logic can be moved here*/},
      controllerAs: 'someAlias'
  • $scope belongs to the controller, $rootScope is for the whole app.
  • while ng-init lets you initialize some values, should really think whether this initialization belongs in a controller.
  • there are angular equivalents (as services that start with $) of native things like window and location – and it’s best practice to use these angular services. You can write your own services.
app.service('hexafy', function() {
   this.myFunc = function (x) {
      return x.toString(16);
  • cannot call this inside the success call on $http service to refer to the containing controller’s this. Makes sense.
  • angular handles asynchronicity using promises (so no callback hell).
  • config(..) runs before run(..), which runs before controller(..).
  • remember to use closure for javascript modules, good practice.

Thinking about MVC and TDD in AngularJS

AngularJS lends itself very well to an MVC approach.

Keep the view stuff in the HTML, the controller logic in .controller(..)s, and the model (likely tied to a backend service/database) in variables in the angular module (bound to the view with the intention of exposing the minimum information needed).

Split view/controller/model components so that each model entity (e.g. product) corresponds to isolated controller(s) (e.g. productController), and view snippets (e.g. productsList HTML element or custom directive).

This way each component has a small focus of what it is trying to do and it can do it well. This also makes it easier to test-drive the application, small careful steps.

Modularity and testability can be further improved by using proper Angular services (e.g. $window, ​$location instead of the native elements). Using dependency injection, you can inject mock objects and check that the methods you expect are being called on the injected (mock) services in unit tests.

“What’s next?” – James Whittaker

James Whittaker of Microsoft gives a stimulating talk on how thinking about “what’s next?” is critical to sustained success. He posits that a brand new thing comes and revolutionizes everything every 10 years, and that technology matures over the preceding 10 years. It does make sense when you think about PC -> mobile -> cloud. He then talks about what he thinks the next big thing will be: machine learning + the concept of microservices in a purchase economy. In other words, the app store is dead! Just a brilliant talk!


[Work in progress] A handy little app I’m making for myself that makes it easy to track textual information on any given webpage. It uses the page’s URL and the text element’s XPath to perform a harvest using the external htmlunit library. Lets me keep track of things like the price of an item I’m interested in, or a sports score, all without needing to go to the page.

Can handle pages that have AJAX; however, does not support pages that require authentication or pagination.

The code was developed in a test-driven way and employs the MVC design pattern.

Uses the following external libraries:

  • htmlunit by Gargoyle Software, Inc. licensed under Apache License, Version 2.0.
  • GSON by Google, Inc. licensed under Apache License, Version 2.0.

Full code can be found GitHub.

Update: 24 April, 2017

Added a background thread that periodically updates all the entries at their required update frequency.


Artificial Intelligence is the New Electricity

Great talk by Prof. Andrew Ng of Stanford/Baidu on how AI will permeate almost every conceivable industry (except hairdressing!). I’ve been interested in AI for a long time and it’s so great to see it finally coming into “the eternal spring”.

Key points:

  • Machine learning used to plateau despite increasing amount of training data – this is no longer the case. Now the more (good) data you have, the better your AI performs. This needs not only AI/ML expertise but also hardware expertise to handle next-level computation with an increasing amount of training data.
  • AI progresses fastest when it’s attempting to do something a human can do; after reaching the human-level of accuracy, progress tends to slow down.
  • If a typical human can do a task with less than a second of thought, then AI can automate it now or in the near future.
  • While programming methodologies such as Agile have had the time to mature into their present forms, there remains a need for an effective method for communication between project managers and engineers in AI projects.
  • Particular domains where AI is very likely to take off are:
    • speech recognition;
    • computer vision, e.g. facial recognition; and
    • healthcare