Skip to content

Instantly share code, notes, and snippets.

@alex-hhh
Last active December 4, 2017 12:45
Show Gist options
  • Save alex-hhh/204be1d29e518689c9621ec9cdb5f4f2 to your computer and use it in GitHub Desktop.
Save alex-hhh/204be1d29e518689c9621ec9cdb5f4f2 to your computer and use it in GitHub Desktop.
#lang racket
;; Script used to generate plots for the blog post below:
;;
;; https://alex-hhh.github.io/2017/11/quantifying-fatigue.html
;; You will need to clone https://github.com/alex-hhh/ActivityLog2 and fix the
;; require below to point to the al-interactive.rkt file. You will also need
;; to update the session ids as they reference activities in my own database.
(require "../../ActivityLog2/etc/al-interactive.rkt"
plot
racket/draw)
(define (add-heartbeats-series df)
(define hb 0) ; holds accumulated heart beats
;; Called by the `data-frame%/map` method with two adjacent data points,
;; PREV and NEXT. The value returned by this function will be the data point
;; in the new series at the same index as NEXT
(define (accumulate-heart-beats prev next)
(when prev ; first time PREV is #f
(match-let (((vector e1 hr1) prev)
((vector e2 hr2) next))
(when (and e1 hr1 e2 hr2)
(let ((beats (/ (* 0.5 (+ hr1 hr2)) 60.0)))
(set! hb (+ hb (* beats (- e2 e1))))))))
(exact-truncate hb))
(unless (send df contains? "hb") ; might already be present due to caching!
(send df add-derived-series
"hb" ; new series name
'("elapsed" "hr") ; based on these series
accumulate-heart-beats ; generator function for the new series
)))
;; 1874 -- feb 16, 2017, 1:20, intervals
(define sid-hard 1874)
(define df-hard (sid->df sid-hard))
(add-heartbeats-series df-hard)
;; 1923 -- apr 25, 2017, 1:21, easy run
(define sid-easy 1923)
(define df-easy (sid->df sid-easy))
(add-heartbeats-series df-easy)
(define sd-hard (send df-hard select* "elapsed" "hb"))
(define sd-easy (send df-easy select* "elapsed" "hb"))
(define sd-hr-hard (send df-hard select* "elapsed" "hr"))
(define sd-hr-easy (send df-easy select* "elapsed" "hr"))
(define color1 (make-object color% #xdc #x14 #x3c 0.8))
(define color2 (make-object color% 0 148 255 0.8))
(define img-width 800)
(define img-height 300)
(define (generate-hb-plot)
(parameterize
([plot-x-label "Time"]
[plot-x-ticks (time-ticks)]
[plot-y-label "Heartbeats"])
(plot-file
(list
(tick-grid)
(lines sd-hard #:color color1 #:label "hard run" #:width 3)
(lines sd-easy #:color color2 #:label "easy run" #:width 3))
"hb-comparison.svg"
#:width img-width
#:height img-height)))
(define (generate-hr-plot)
(parameterize
([plot-x-label "Time"]
[plot-x-ticks (time-ticks)]
[plot-y-label "Hear Rate"])
(plot-file
(list
(lines sd-hr-hard #:color color1 #:label "hard run" #:width 3)
(lines sd-hr-easy #:color color2 #:label "easy run" #:width 3))
"hr-comparison.svg"
#:width img-width
#:height img-height)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment