Stats: read the effort back — trend, distribution, hard sets, per exercise
Rating a set only paid off inside the set label until now: Stats never looked at rir/rpe at all. It reads them now, in four places. lib/effort.js aggregates everything in RIR and converts back to whichever scale the profile reads. A history that mixes the two — own logs in RIR, an imported file in RPE — has to average as one series or every chart draws half the data, and RIR is the internal unit because its zero is real where RPE's floor of 6 is a convention. - an Effort card: average and share of sets taken near failure, the week-by-week curve with the week's volume in the tooltip, and where the sets land on the scale - the muscle map gains a hard-sets mode: a muscle can lead on volume and still never be trained near failure - Exercise progress carries effort on the top-set dots and as a curve of its own — the same weight with more left in the tank is progress a weight-only line draws flat - LineChart learns an inverted axis (RIR counts down as it gets harder) and per-point marks Every number carries how much of the training it speaks for: rating is optional and off by default, so an unrated set is left out rather than read as failure, and an average waits for five rated sets before it claims to be one.
This commit is contained in:
1 parent
4c829b6d1f
commit
caaf61d91e
7 files changed
+503
-21
No files matched your search
@@ -0,0 +1,154 @@
|
||||
// Effort as a statistic: one internal scale, both display scales.
|
||||
//
|
||||
// A set carries either `rir` or `rpe` and is never rewritten — switching the setting changes
|
||||
// what new sets ask for, nothing else (see history.js). For a *chart* that is a problem: a
|
||||
// history that mixes the two (own logs in RIR, an imported file in RPE) would draw two
|
||||
// half-empty series. So everything aggregates in RIR and is converted back for display.
|
||||
// RIR is the internal unit because it has a real zero — a set taken to failure — where RPE's
|
||||
// floor of 6 is only a convention about which sets are worth rating. RPE 8 == RIR 2.
|
||||
import { EFFORT, effortOf } from './history.js'
|
||||
import { weekKey } from './format.js'
|
||||
|
||||
// At or below this a set is close enough to failure to be the kind that drives adaptation.
|
||||
// 3 rather than 2: the line is a convention, and drawn one rep too generously it still
|
||||
// separates working sets from the ones left in the warm-up range.
|
||||
export const HARD_RIR = 3
|
||||
// Below this many rated sets an average is noise. Showing "RIR 1.0" off a single set reads
|
||||
// like a finding when it is one tap, so the callers show a dash instead.
|
||||
export const MIN_RATED = 5
|
||||
|
||||
/** A set's effort in RIR, or null when it was never rated. 0 is a rating, not "empty". */
|
||||
export const rirOf = s =>
|
||||
!s ? null : s.rir != null ? s.rir : s.rpe != null ? 10 - s.rpe : null
|
||||
|
||||
/** RIR → the scale being displayed. The reverse of rirOf, for one number. */
|
||||
export const toScale = (kind, rir) =>
|
||||
rir == null ? null : Math.round((kind === 'rpe' ? 10 - rir : rir) * 10) / 10
|
||||
|
||||
/**
|
||||
* Which scale to *label* aggregates with. The profile's own setting wins; a profile that
|
||||
* logs nothing itself but carries rated history (the imported-from-Hevy case) is shown the
|
||||
* scale that history is actually written in, rather than an RIR it has never seen.
|
||||
*/
|
||||
export function displayScale(S) {
|
||||
const k = effortOf(S)
|
||||
if (EFFORT[k]) return k
|
||||
let rir = 0, rpe = 0
|
||||
eachDoneSet(S, s => { if (s.rir != null) rir++; else if (s.rpe != null) rpe++ })
|
||||
return rpe > rir ? 'rpe' : 'rir'
|
||||
}
|
||||
export const scaleName = kind => EFFORT[kind].hd
|
||||
|
||||
// Every finished set in the profile, oldest first. `fn` gets the set plus the workout it
|
||||
// belongs to, which is what the windowed and per-week views need.
|
||||
function eachDoneSet(S, fn) {
|
||||
;(S.workouts || []).forEach(w =>
|
||||
(w.entries || []).forEach(e =>
|
||||
(e.sets || []).forEach(s => { if (s.done) fn(s, w, e) })))
|
||||
}
|
||||
|
||||
// A window in days, counted back from now. 0 = everything, which is also what an empty
|
||||
// history means for every caller here.
|
||||
const inWindow = (w, days) =>
|
||||
!days || (w.start || new Date(w.d).getTime()) > Date.now() - days * 86400000
|
||||
|
||||
export const avgRir = sets => {
|
||||
const vs = (sets || []).map(rirOf).filter(v => v != null)
|
||||
return vs.length ? vs.reduce((a, b) => a + b, 0) / vs.length : null
|
||||
}
|
||||
|
||||
/**
|
||||
* The headline numbers for a window: how hard, how much of it was hard, and — the part that
|
||||
* keeps the rest honest — how much of the training was rated at all. Effort is optional and
|
||||
* off by default, so partial coverage is the normal case; an average without its denominator
|
||||
* would quietly speak for sets that were never rated.
|
||||
*/
|
||||
export function effortSummary(S, days) {
|
||||
let done = 0, rated = 0, sum = 0, hard = 0
|
||||
eachDoneSet(S, (s, w) => {
|
||||
if (!inWindow(w, days)) return
|
||||
done++
|
||||
const r = rirOf(s)
|
||||
if (r == null) return
|
||||
rated++; sum += r
|
||||
if (r <= HARD_RIR) hard++
|
||||
})
|
||||
return {
|
||||
done, rated, hard,
|
||||
avg: rated >= MIN_RATED ? sum / rated : null,
|
||||
hardPct: rated >= MIN_RATED ? hard / rated : null
|
||||
}
|
||||
}
|
||||
|
||||
/** Does this profile hold any rated set at all? Decides whether the effort UI exists. */
|
||||
export function hasEffort(S) {
|
||||
let any = false
|
||||
eachDoneSet(S, s => { if (!any && rirOf(s) != null) any = true })
|
||||
return any
|
||||
}
|
||||
|
||||
/**
|
||||
* Average effort per calendar week, with the week's set count alongside: the pair is the
|
||||
* point. Volume up with effort up is fatigue accumulating; volume up with effort flat is the
|
||||
* adaptation you were training for. Weeks with a single rated set are dropped rather than
|
||||
* drawn — one tap should not become a peak in the curve.
|
||||
*/
|
||||
export function effortWeeks(S, days) {
|
||||
const wk = new Map()
|
||||
eachDoneSet(S, (s, w) => {
|
||||
if (!inWindow(w, days)) return
|
||||
const k = weekKey(w.d)
|
||||
let e = wk.get(k)
|
||||
if (!e) wk.set(k, e = { k, t: mondayOf(w.d), sum: 0, n: 0, sets: 0 })
|
||||
e.sets++
|
||||
const r = rirOf(s)
|
||||
if (r != null) { e.sum += r; e.n++ }
|
||||
})
|
||||
return [...wk.values()].filter(e => e.n >= 2).sort((a, b) => a.t - b.t)
|
||||
.map(e => ({ t: e.t, rir: e.sum / e.n, n: e.n, sets: e.sets }))
|
||||
}
|
||||
// The Monday of an ISO date, as ms — the x position a week's point sits at.
|
||||
function mondayOf(iso) {
|
||||
const d = new Date(iso + 'T12:00:00')
|
||||
d.setDate(d.getDate() - ((d.getDay() + 6) % 7))
|
||||
d.setHours(12, 0, 0, 0)
|
||||
return +d
|
||||
}
|
||||
|
||||
/**
|
||||
* How the rated sets spread across the scale, in whole steps with everything past the top
|
||||
* bucket collapsed into it. This is the chart that answers "am I training too far from
|
||||
* failure, or leaving nothing for the next session" — an average alone hides both, because
|
||||
* half the sets at 0 and half at 4 average to a healthy-looking 2.
|
||||
*/
|
||||
export const BUCKETS = 4 // 0,1,2,3 and a "4+" tail
|
||||
export function effortHistogram(S, days) {
|
||||
const bins = new Array(BUCKETS + 1).fill(0)
|
||||
let rated = 0
|
||||
eachDoneSet(S, (s, w) => {
|
||||
if (!inWindow(w, days)) return
|
||||
const r = rirOf(s)
|
||||
if (r == null) return
|
||||
rated++
|
||||
bins[Math.min(BUCKETS, Math.max(0, Math.floor(r)))]++
|
||||
})
|
||||
return bins.map((n, i) => ({ rir: i, tail: i === BUCKETS, n, pct: rated ? n / rated : 0 }))
|
||||
}
|
||||
|
||||
/**
|
||||
* Average effort per session for one exercise, oldest first. Sessions where the exercise was
|
||||
* logged but never rated are absent, not zero — an unrated set must never read as failure.
|
||||
*/
|
||||
export function exerciseEffort(S, exId) {
|
||||
const pts = []
|
||||
;(S.workouts || []).forEach(w => {
|
||||
const en = (w.entries || []).find(e => e.id === exId)
|
||||
if (!en) return
|
||||
const r = avgRir((en.sets || []).filter(s => s.done))
|
||||
if (r != null) pts.push({ t: w.start || new Date(w.d).getTime(), d: w.d, rir: r })
|
||||
})
|
||||
return pts
|
||||
}
|
||||
|
||||
/** A set that counts as hard — the filter behind the muscle map's "hard sets" mode. */
|
||||
export const isHardSet = s => { const r = rirOf(s); return r != null && r <= HARD_RIR }
|
||||
@@ -0,0 +1,191 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
rirOf, toScale, displayScale, avgRir, effortSummary, hasEffort, effortWeeks,
|
||||
effortHistogram, exerciseEffort, isHardSet, HARD_RIR, MIN_RATED
|
||||
} from './effort.js'
|
||||
import { isoOf } from './format.js'
|
||||
|
||||
const daysAgo = n => { const d = new Date(); d.setDate(d.getDate() - n); return d }
|
||||
// One workout on a day, with the sets given. Everything here is a finished set unless a set
|
||||
// says otherwise, because that is what the stats read.
|
||||
const W = (n, sets, id = '0025') => {
|
||||
const d = daysAgo(n)
|
||||
return { id: 'w' + n, d: isoOf(d), start: +d, entries: [{ id, sets: sets.map(s => ({ w: 60, r: 8, done: true, ...s })) }] }
|
||||
}
|
||||
const S = (...workouts) => ({ workouts })
|
||||
|
||||
describe('rirOf', () => {
|
||||
it('reads RIR straight and RPE as its mirror — RPE 8 is RIR 2', () => {
|
||||
expect(rirOf({ rir: 2 })).toBe(2)
|
||||
expect(rirOf({ rpe: 8 })).toBe(2)
|
||||
expect(rirOf({ rpe: 9.5 })).toBe(0.5)
|
||||
})
|
||||
|
||||
it('keeps a rated 0 and rejects everything that only looks like one', () => {
|
||||
expect(rirOf({ rir: 0 })).toBe(0) // taken to failure — a rating, not "empty"
|
||||
expect(rirOf({ rpe: 10 })).toBe(0)
|
||||
expect(rirOf({})).toBe(null)
|
||||
expect(rirOf({ rir: null })).toBe(null)
|
||||
expect(rirOf(null)).toBe(null)
|
||||
})
|
||||
|
||||
it('prefers the scale the set itself was logged on when a file wrote both', () => {
|
||||
expect(rirOf({ rir: 3, rpe: 9 })).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe('toScale', () => {
|
||||
it('converts back to whichever scale is on screen', () => {
|
||||
expect(toScale('rir', 2)).toBe(2)
|
||||
expect(toScale('rpe', 2)).toBe(8)
|
||||
expect(toScale('rpe', 0)).toBe(10)
|
||||
expect(toScale('rir', null)).toBe(null)
|
||||
})
|
||||
|
||||
it('does not let the round trip produce float dust', () => {
|
||||
expect(toScale('rpe', 10 - 7.5)).toBe(7.5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('displayScale', () => {
|
||||
it('follows the profile setting when it has one', () => {
|
||||
expect(displayScale({ effort: 'rpe', workouts: [] })).toBe('rpe')
|
||||
expect(displayScale({ effort: 'rir', workouts: [] })).toBe('rir')
|
||||
})
|
||||
|
||||
it('shows an unrated profile the scale its imported history is written in', () => {
|
||||
// Effort off, but a file brought RPE with it: labelling that history "RIR" would show a
|
||||
// number the profile has never entered and mean the opposite of what it says.
|
||||
expect(displayScale({ effort: 'none', ...S(W(3, [{ rpe: 8 }, { rpe: 9 }])) })).toBe('rpe')
|
||||
expect(displayScale({ effort: 'none', ...S(W(3, [{ rir: 2 }])) })).toBe('rir')
|
||||
expect(displayScale({ effort: 'none', workouts: [] })).toBe('rir')
|
||||
})
|
||||
})
|
||||
|
||||
describe('avgRir', () => {
|
||||
it('averages the rated sets and ignores the rest', () => {
|
||||
expect(avgRir([{ rir: 1 }, { rir: 3 }])).toBe(2)
|
||||
expect(avgRir([{ rir: 1 }, {}, { rpe: 7 }])).toBe(2) // RPE 7 = RIR 3
|
||||
})
|
||||
|
||||
it('is null rather than 0 when nothing was rated', () => {
|
||||
expect(avgRir([{}, { rir: null }])).toBe(null)
|
||||
expect(avgRir([])).toBe(null)
|
||||
expect(avgRir(null)).toBe(null)
|
||||
})
|
||||
})
|
||||
|
||||
describe('effortSummary', () => {
|
||||
const st = S(
|
||||
W(2, [{ rir: 1 }, { rir: 3 }, { rir: 2 }]),
|
||||
W(4, [{ rir: 0 }, { rpe: 6 }, {}]), // RPE 6 = RIR 4, one set unrated
|
||||
W(40, [{ rir: 5 }, { rir: 5 }])
|
||||
)
|
||||
|
||||
it('counts rated sets against every finished set, not against itself', () => {
|
||||
const r = effortSummary(st, 0)
|
||||
expect(r.done).toBe(8)
|
||||
expect(r.rated).toBe(7)
|
||||
})
|
||||
|
||||
it('leaves unrated sets out of the average instead of reading them as failure', () => {
|
||||
const r = effortSummary(S(W(2, [{ rir: 4 }, {}, {}, {}, {}, {}])), 0)
|
||||
expect(r.rated).toBe(1)
|
||||
expect(r.avg).toBe(null) // one rating is not an average
|
||||
})
|
||||
|
||||
it('waits for a real sample before reporting a number', () => {
|
||||
const few = S(W(2, new Array(MIN_RATED - 1).fill({ rir: 2 })))
|
||||
expect(effortSummary(few, 0).avg).toBe(null)
|
||||
const enough = S(W(2, new Array(MIN_RATED).fill({ rir: 2 })))
|
||||
expect(effortSummary(enough, 0).avg).toBe(2)
|
||||
})
|
||||
|
||||
it('counts the sets taken close to failure', () => {
|
||||
const r = effortSummary(st, 0)
|
||||
expect(r.hard).toBe(4) // 1, 3, 2, 0 — the 4 and the two 5s are not
|
||||
expect(r.hardPct).toBeCloseTo(4 / 7)
|
||||
})
|
||||
|
||||
it('honours the window', () => {
|
||||
const r = effortSummary(st, 7)
|
||||
expect(r.rated).toBe(5) // the 40-day-old session is out
|
||||
expect(effortSummary(st, 3).rated).toBe(3)
|
||||
})
|
||||
|
||||
it('survives a profile with no training at all', () => {
|
||||
expect(effortSummary({ workouts: [] }, 30)).toEqual({ done: 0, rated: 0, hard: 0, avg: null, hardPct: null })
|
||||
})
|
||||
})
|
||||
|
||||
describe('hasEffort', () => {
|
||||
it('decides whether the effort UI exists at all', () => {
|
||||
expect(hasEffort(S(W(2, [{ rir: 0 }])))).toBe(true)
|
||||
expect(hasEffort(S(W(2, [{ rpe: 8 }])))).toBe(true)
|
||||
expect(hasEffort(S(W(2, [{}, { rir: null }])))).toBe(false)
|
||||
expect(hasEffort({ workouts: [] })).toBe(false)
|
||||
})
|
||||
|
||||
it('ignores sets that were never finished', () => {
|
||||
expect(hasEffort(S(W(2, [{ rir: 2, done: false }])))).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('effortWeeks', () => {
|
||||
it('averages per calendar week and carries the week volume alongside', () => {
|
||||
const pts = effortWeeks(S(W(1, [{ rir: 1 }, { rir: 3 }, {}])), 0)
|
||||
expect(pts).toHaveLength(1)
|
||||
expect(pts[0].rir).toBe(2)
|
||||
expect(pts[0].n).toBe(2) // rated
|
||||
expect(pts[0].sets).toBe(3) // trained
|
||||
})
|
||||
|
||||
it('drops a week that rests on a single tap', () => {
|
||||
expect(effortWeeks(S(W(1, [{ rir: 1 }])), 0)).toEqual([])
|
||||
})
|
||||
|
||||
it('comes back oldest first, whatever order the workouts arrived in', () => {
|
||||
const pts = effortWeeks(S(W(2, [{ rir: 1 }, { rir: 1 }]), W(30, [{ rir: 3 }, { rir: 3 }])), 0)
|
||||
expect(pts.map(p => p.rir)).toEqual([3, 1])
|
||||
expect(pts[0].t).toBeLessThan(pts[1].t)
|
||||
})
|
||||
})
|
||||
|
||||
describe('effortHistogram', () => {
|
||||
it('bins by whole steps and collapses the far end into a tail', () => {
|
||||
const h = effortHistogram(S(W(2, [{ rir: 0 }, { rir: 1.5 }, { rir: 4 }, { rir: 7 }])), 0)
|
||||
expect(h.map(b => b.n)).toEqual([1, 1, 0, 0, 2])
|
||||
expect(h[4].tail).toBe(true)
|
||||
expect(h[0].pct).toBe(0.25)
|
||||
})
|
||||
|
||||
it('is all zeroes, not NaN, when nothing is rated', () => {
|
||||
const h = effortHistogram(S(W(2, [{}])), 0)
|
||||
expect(h.every(b => b.n === 0 && b.pct === 0)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('exerciseEffort', () => {
|
||||
it('gives one point per session, in order', () => {
|
||||
const st = S(W(20, [{ rir: 4 }, { rir: 2 }]), W(2, [{ rir: 1 }]))
|
||||
expect(exerciseEffort(st, '0025').map(p => p.rir)).toEqual([3, 1])
|
||||
})
|
||||
|
||||
it('skips a session where the exercise was logged but never rated', () => {
|
||||
const st = S(W(20, [{}]), W(2, [{ rir: 1 }]))
|
||||
expect(exerciseEffort(st, '0025')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('does not pick up another exercise', () => {
|
||||
expect(exerciseEffort(S(W(2, [{ rir: 1 }], '0047')), '0025')).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('isHardSet', () => {
|
||||
it('draws the line at the effort that actually drives adaptation', () => {
|
||||
expect(isHardSet({ rir: HARD_RIR })).toBe(true)
|
||||
expect(isHardSet({ rir: HARD_RIR + 0.5 })).toBe(false)
|
||||
expect(isHardSet({ rpe: 10 })).toBe(true)
|
||||
expect(isHardSet({})).toBe(false) // unrated is not hard, and not easy either
|
||||
})
|
||||
})
|
||||
@@ -100,10 +100,15 @@ export function loadOf(items) {
|
||||
return load
|
||||
}
|
||||
|
||||
/** Load for finished workouts (only sets actually ticked off count). */
|
||||
export const loadOfWorkouts = workouts =>
|
||||
/**
|
||||
* Load for finished workouts (only sets actually ticked off count). `pick` narrows that
|
||||
* further — the map can then answer "where did the *hard* sets go", which is a different
|
||||
* question from where the sets went: a muscle can lead on volume and still never be trained
|
||||
* near failure.
|
||||
*/
|
||||
export const loadOfWorkouts = (workouts, pick) =>
|
||||
loadOf((workouts || []).flatMap(w =>
|
||||
(w.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done).length }))))
|
||||
(w.entries || []).map(e => ({ id: e.id, sets: (e.sets || []).filter(s => s.done && (!pick || pick(s))).length }))))
|
||||
|
||||
/** Load a routine *would* produce, from its planned set counts. */
|
||||
export const loadOfRoutine = routine =>
|
||||
|
||||
Reference in new issue
Block a user