Skip to content

Instantly share code, notes, and snippets.

@alex-hhh
Created October 20, 2017 23:47
Show Gist options
  • Save alex-hhh/fbf966f52cfbed6812c785b58e5d86a9 to your computer and use it in GitHub Desktop.
Save alex-hhh/fbf966f52cfbed6812c785b58e5d86a9 to your computer and use it in GitHub Desktop.
sample code for issue 17
#lang racket
;; https://github.com/alex-hhh/ActivityLog2/issues/17
(require plot
racket/draw)
(require "al-interactive.rkt"
"../rkt/metrics.rkt"
"../rkt/series-meta.rkt")
(define candidates
(query-list
(current-database)
"select session_id
from V_ACTIVITY_LIST VAL, SEASON S
where S.name = 'Tri 2017 / Marathon'
and VAL.start_time between S.start_date and S.end_date
and VAL.sport = 1"))
;; Obtain BAVG for "pace" by calculating it for the "spd" series than
;; converting the result to pace values
(define (get-aggregate-bavg-1 candidates axis)
(let* ((sname (send axis series-name))
(actual-sname (if (string=? sname "pace") "spd" sname))
(inverted? (if (string=? sname "pace") #f (send axis inverted-best-avg?)))
(data (aggregate-bavg candidates
actual-sname
#:inverted? inverted?)))
(if (string=? sname "pace")
(for/list ((item data))
(match-define (list sid timestamp duration value) item)
(list sid timestamp duration (convert-m/s->pace value)))
data)))
;; Obtain BAVG for "pace" by calculating it for "pace"
(define (get-aggregate-bavg-2 candidates axis)
(aggregate-bavg candidates
(send axis series-name)
#:inverted? (send axis inverted-best-avg?)))
(define set1 (get-aggregate-bavg-1 candidates axis-pace))
(define set2 (get-aggregate-bavg-2 candidates axis-pace))
(define plot-fn1 (aggregate-bavg->spline-fn set1))
(define plot-fn2 (aggregate-bavg->spline-fn set2))
(define color1 (make-object color% #xdc #x14 #x3c 0.8))
(define color2 (make-object color% 0 148 255 0.8))
(define rt
(list
(tick-grid)
(function plot-fn2 #:color color2 #:width 4 #:alpha 0.7 #:label "before (pace series)")
(function plot-fn1 #:color color1 #:width 4 #:alpha 0.7 #:label "after (spd series)")))
(define (make-plot/frame rt)
(parameterize ([plot-x-ticks (best-avg-ticks)]
[plot-x-label "Duration"]
[plot-x-transform log-transform]
[plot-y-ticks (send axis-pace plot-ticks)]
[plot-x-tick-label-anchor 'top-right]
[plot-x-tick-label-angle 30]
[plot-y-label (send axis-pace axis-label)])
(let-values (((min-x max-x min-y max-y) (aggregate-bavg-bounds set1)))
(send (plot-frame rt #:x-min min-x #:x-max max-x #:y-min min-y #:y-max max-y) show #t))))
(define (make-plot/file rt file-name)
(parameterize ([plot-x-ticks (best-avg-ticks)]
[plot-x-label "Duration"]
[plot-x-transform log-transform]
[plot-y-ticks (send axis-pace plot-ticks)]
[plot-x-tick-label-anchor 'top-right]
[plot-x-tick-label-angle 30]
[plot-y-label (send axis-pace axis-label)])
(let-values (((min-x max-x min-y max-y) (aggregate-bavg-bounds set1)))
(plot-file rt
file-name
#:width 800 #:height 600
#:x-min min-x #:x-max max-x #:y-min min-y #:y-max max-y))))
;; (make-plot/frame rt)
;; (make-plot/file rt "bavg-pace-comparison.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment