Skip to content

Commit

Permalink
Altitude series will use imperial system correctly (#9)
Browse files Browse the repository at this point in the history
New "altitude" and "corrected-altitude" series now track altitude in the
selected measurement unit (meters or feet), the "alt" and "calt" series remain
in meters only, for other calculations.  This makes the elevation plot use the
correct values when the user has the Imperial units selected.
  • Loading branch information
alex-hhh committed Nov 5, 2022
1 parent 2109a13 commit fe22284
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 64 deletions.
4 changes: 3 additions & 1 deletion docs/session-df.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ session. To check if a data frame contains some series, use the

* **lat**, **lon** are the latitude, longitude coordinates for the point
* **alt**, **calt** are the altitude (as recorded by the device) and corrected
altitude, in meters
altitude, in meters (see notes on elevation correction)
* **altitude**, **corrected-altitude** are the altitude/corrected altitude
series in a convenient metric (either Meters or Yards).
* **grade** is grade (incline) at the current point, as a percentage
* **dst** is distance traveled in meters since the start of the activity
* **distance** is traveled since the start of the activity using the most
Expand Down
11 changes: 9 additions & 2 deletions rkt/fmt-util.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
;; (: m->ft (-> Real Real))
(define (m->ft m) (* m 3.2808))

(define (ft->m m) (/ m 3.2808))

;; (: celsius->fahrenheit (-> Real Real))
(define (celsius->fahrenheit c) (+ 32 (* c 1.8)))

Expand Down Expand Up @@ -161,6 +163,7 @@
(define vertical-oscillation-label "in")
(define m->weight kg->lb)
(define weight-label "kg")
(define vertical-distance->m values)

;; Export converters as functions, we cannot export the defines above
;; directly, because any code that uses them will not pick up any changes.
Expand All @@ -170,12 +173,14 @@
(define (convert-pace->m/s val) (pace->m/s val))
(define (convert-swim-pace->m/s val) (swim-pace->m/s val))
(define (convert-m/s->swim-pace val) (m/s->swim-pace val))
(define (convert-vertical-distance->m val) (vertical-distance->m val))
(provide convert-m/s->speed
convert-speed->m/s
convert-m/s->pace
convert-pace->m/s
convert-swim-pace->m/s
convert-m/s->swim-pace)
convert-m/s->swim-pace
convert-vertical-distance->m)

;; (: setup-measurement-system (-> Symbol Void))
(define (setup-measurement-system mu)
Expand All @@ -201,7 +206,8 @@
(set! m->vertical-oscillation mm->inch)
(set! vertical-oscillation-label "in")
(set! m->weight kg->lb)
(set! weight-label "lb"))
(set! weight-label "lb")
(set! vertical-distance->m ft->m))

(begin
(set! m/s->speed m/s->km/h)
Expand All @@ -225,6 +231,7 @@
(set! vertical-oscillation-label "mm")
(set! m->weight values)
(set! weight-label "kg")
(set! vertical-distance->m values)
)))

(define ms-tag 'activity-log:measurement-system)
Expand Down
7 changes: 4 additions & 3 deletions rkt/metrics.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
;;; metrics.rkt -- calculate aggregate metrics for activities

;; This file is part of ActivityLog2, an fitness activity tracker
;; Copyright (C) 2016, 2018 Alex Harsányi <[email protected]>
;; Copyright (C) 2016, 2018, 2022 Alex Harsányi <[email protected]>
;;
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the Free
Expand Down Expand Up @@ -408,7 +408,7 @@
(define (scatter/jsexpr df series1 series2)

(define is-lap-swim? (df-get-property df 'is-lap-swim?))

(define (extract)
(if (df-contains? df series1 series2)
(df-select* df series1 series2 #:filter valid-only)
Expand Down Expand Up @@ -778,6 +778,8 @@ select X.session_id
(clear-saved-metrics-for-series "pace")
(clear-saved-metrics-for-series "speed")
(clear-saved-metrics-for-series "stride")
(clear-saved-metrics-for-series "altitude")
(clear-saved-metrics-for-series "corrected-altitude")
;; (clear-saved-metrics-for-series "alt")
;; (clear-saved-metrics-for-series "calt")
;; (clear-saved-metrics-for-series "vosc")
Expand Down Expand Up @@ -867,4 +869,3 @@ select X.session_id
(aggregate-scatter-bounds (-> aggregate-scatter/c number? number? bounds/c))
(aggregate-scatter-slr (-> aggregate-scatter/c (or/c #f slr?)))
(clear-metrics-cache (-> any/c)))

2 changes: 1 addition & 1 deletion rkt/models/cp-util.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"../dbapp.rkt"
"../dbutil.rkt")

(struct cp-validity (id sport sub-sport valid-from valid-until))
(struct cp-validity (id sport sub-sport valid-from valid-until) #:transparent)

(define scp-query
(virtual-statement
Expand Down
14 changes: 9 additions & 5 deletions rkt/session-df/native-series.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; built into the application (like heart rate or power).
;;
;; This file is part of ActivityLog2, an fitness activity tracker
;; Copyright (C) 2015, 2018, 2020, 2021 Alex Harsányi <[email protected]>
;; Copyright (C) 2015, 2018, 2020, 2021, 2022 Alex Harsányi <[email protected]>
;;
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the Free
Expand Down Expand Up @@ -437,16 +437,18 @@
(if (eq? (al-pref-measurement-system) 'metric)
"Elevation (m)" "Elevation (ft)"))
(define/override (should-filter?) #f)
(define/override (series-name) "alt")
(define/override (series-name) "altitude")
(define/override (name) "Elevation")
(define/override (fractional-digits) 1)
;; Don't replace missing values with anything, strip them out.
(define/override (missing-value) #f)
(define/override (value-formatter sport (sid #f) #:show-unit-label? (label? #f))
(lambda (v)
(vertical-distance->string v label?)))
(define v1 (convert-vertical-distance->m v))
(vertical-distance->string v1 label?)))
)))
(register-series-metadata axis-elevation)
(register-series-metadata axis-elevation #:series-name "alt")
(provide axis-elevation)


Expand All @@ -458,16 +460,18 @@
(if (eq? (al-pref-measurement-system) 'metric)
"Elevation (m)" "Elevation (ft)"))
(define/override (should-filter?) #f)
(define/override (series-name) "calt")
(define/override (series-name) "corrected-altitude")
(define/override (name) "Elevation")
(define/override (fractional-digits) 1)
;; Don't replace missing values with anything, strip them out.
(define/override (missing-value) #f)
(define/override (value-formatter sport (sid #f) #:show-unit-label? (label? #f))
(lambda (v)
(vertical-distance->string v label?)))
(define v1 (convert-vertical-distance->m v))
(vertical-distance->string v1 label?)))
)))
(register-series-metadata axis-corrected-elevation)
(register-series-metadata axis-corrected-elevation #:series-name "calt")
(provide axis-corrected-elevation)


Expand Down
28 changes: 18 additions & 10 deletions rkt/session-df/series-metadata.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; series-metadata.rkt --meta data about data series in session data frames
;;
;; This file is part of ActivityLog2 -- https://github.com/alex-hhh/ActivityLog2
;; Copyright (c) 2018, 2019, 2020, 2021 Alex Harsányi <[email protected]>
;; Copyright (c) 2018, 2019, 2020, 2021, 2022 Alex Harsányi <[email protected]>
;;
;; This program is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by the Free
Expand Down Expand Up @@ -156,22 +156,24 @@
(define the-metadata-registry (make-hash))
(define the-swim-metadata-registry (make-hash))

(define (register-series-metadata m (is-lap-swimming? #f))
(define sn (send m series-name))
(define (register-series-metadata m
(is-lap-swimming? #f)
#:series-name (sn (send m series-name)))
(define registry (if is-lap-swimming?
the-swim-metadata-registry
the-metadata-registry))
(when (hash-ref registry sn #f)
(dbglog "register-series-metadata: overriding metadata for ~a" sn))
(hash-set! registry (send m series-name) m)
(hash-set! registry sn m)
(void))

(define (unregister-series-metadata m (is-lap-swimming? #f))
(define sn (send m series-name))
(define (unregister-series-metadata
m (is-lap-swimming? #f)
#:series-name (sn (send m series-name)))
(define registry (if is-lap-swimming?
the-swim-metadata-registry
the-metadata-registry))
(hash-remove! registry (send m series-name))
(hash-remove! registry sn)
(void))

(define (find-series-metadata series-name (is-lap-swimming? #f))
Expand All @@ -192,6 +194,12 @@
(provide series-metadata%)

(provide/contract
(register-series-metadata (->* ((is-a?/c series-metadata%)) (boolean?) void?))
(unregister-series-metadata (->* ((is-a?/c series-metadata%)) (boolean?) void?))
(find-series-metadata (->* (string?) (boolean?) (is-a?/c series-metadata%))))
(register-series-metadata (->* ((is-a?/c series-metadata%))
(boolean? #:series-name string?)
void?))
(unregister-series-metadata (->* ((is-a?/c series-metadata%))
(boolean? #:series-name string?)
void?))
(find-series-metadata (->* (string?)
(boolean?)
(is-a?/c series-metadata%))))
60 changes: 41 additions & 19 deletions rkt/session-df/session-df.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"dst"
'("active")
(lambda (v)
(define active (list-ref v 0))
(define active (car v))
(set! dst (+ dst (* active pool-length)))
dst))))

Expand Down Expand Up @@ -226,6 +226,7 @@
(smooth-start-of-series df "vosc")
(smooth-start-of-series df "cad")
(add-speed-series df)
(add-altitude-series df)
(add-pace-series df)
(add-speed-zone-series df)
(maybe-add-grade-series! df)
Expand Down Expand Up @@ -284,8 +285,8 @@
(let ((timer 0))
(lambda (prev-val val)
(when prev-val
(match-define (list ptimestamp) prev-val)
(match-define (list timestamp) val)
(define ptimestamp (car prev-val))
(define timestamp (car val))
(define dt (- timestamp ptimestamp))
;; Keep track of the average time between samples (open water swims
;; can have 40-70 seconds between samples) and consider a stop
Expand Down Expand Up @@ -336,26 +337,26 @@
"elapsed"
'("timestamp")
(lambda (val)
(define timestamp (list-ref val 0))
(define timestamp (car val))
(- timestamp timestamp0))))
(df-set-sorted! df "elapsed" <)))

(define (add-distance-series df)

(define (distance-km val)
(define dst (list-ref val 0))
(define dst (car val))
(if dst (m->km dst) #f))

(define (distance-mi val)
(define dst (list-ref val 0))
(define dst (car val))
(if dst (m->mi dst) #f))

(define (distance-yards val)
(define dst (list-ref val 0))
(define dst (car val))
(if dst (m->yd dst) #f))

(define (distance-meters val)
(list-ref val 0))
(car val))

(when (df-contains? df "dst")

Expand Down Expand Up @@ -390,10 +391,10 @@
(define (add-speed-series df)

(define (speed-km/h val)
(match-define (list spd) val)
(define spd (car val))
(if spd (m/s->km/h spd) spd))
(define (speed-mi/h val)
(match-define (list spd) val)
(define spd (car val))
(if spd (m/s->mi/h spd) #f))
(when (df-contains? df "spd")
(df-add-lazy!
Expand All @@ -403,6 +404,29 @@
(if (eq? (al-pref-measurement-system) 'metric)
speed-km/h speed-mi/h))))

(define (add-altitude-series df)
(define (altitude-meters val)
(car val))
(define (altitude-feet val)
(define alt (car val))
(and alt (m->ft alt)))
(when (df-contains? df "alt")
(df-add-lazy!
df
"altitude"
'("alt")
(if (eq? (al-pref-measurement-system) 'metric)
altitude-meters
altitude-feet)))
(when (df-contains? df "calt")
(df-add-lazy!
df
"corrected-altitude"
'("calt")
(if (eq? (al-pref-measurement-system) 'metric)
altitude-meters
altitude-feet))))

(define (add-temperature-series df)
(when (df-contains? df "tempe")
(df-add-lazy!
Expand Down Expand Up @@ -476,11 +500,11 @@
(if (and spd (> spd 0.6)) (m/s->sec/mi spd) #f))

(define (pace-sec/100m val)
(match-define (list spd) val)
(define spd (car val))
(if (and spd (> spd 0.1)) (m/s->sec/100m spd) #f))

(define (pace-sec/100yd val)
(match-define (list spd) val)
(define spd (car val))
(if (and spd (> spd 0.1)) (m/s->sec/100yd spd) #f))

(when (df-contains? df "spd" "elapsed")
Expand Down Expand Up @@ -514,11 +538,9 @@
"speed-zone"
'("spd")
(lambda (val)
(match-define (list spd) val)
(define spd (car val))
(if spd (value->zone zones spd) #f)))))



(define (add-hr-pct-series df)
(define sid (df-get-property df 'session-id))
(define zones (sport-zones-for-session sid 'heart-rate))
Expand All @@ -528,7 +550,7 @@
"hr-pct"
'("hr")
(lambda (val)
(match-define (list hr) val)
(define hr (car val))
(if hr (value->pct-of-max zones hr) #f)))))

(define (add-hr-zone-series df)
Expand All @@ -540,7 +562,7 @@
"hr-zone"
'("hr")
(lambda (val)
(match-define (list hr) val)
(define hr (car val))
(if hr (value->zone zones hr) #f)))))

(define (add-stride-series df)
Expand Down Expand Up @@ -597,7 +619,7 @@
"pwr-zone"
'("pwr")
(lambda (val)
(match-define (list pwr) val)
(define pwr (car val))
(if pwr (value->zone zones pwr) #f)))))

(define (add-lppa-series df)
Expand Down Expand Up @@ -625,7 +647,7 @@
series-name
(list series-name)
(lambda (val)
(define a (list-ref val 0))
(define a (car val))
(if a (if (> a 180.0) (- a 360) a) #f)))
;; Get rid if this series if it became empty
(unless (df-has-non-na? df series-name)
Expand Down
2 changes: 2 additions & 0 deletions sql/default-theme.rktd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
(speed-zone 0 148 255)
(alt 38 127 0)
(calt 0 153 136)
(altitude 38 127 0)
(corrected-altitude 0 153 136)
; (grade #x2f #x4f #x4f)
; (grade 51 187 238)
(grade 187 187 187)
Expand Down
Loading

0 comments on commit fe22284

Please sign in to comment.