From abbacdaab4d18a3695c615f2db6ab2615e9dc076 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:33:00 +0530 Subject: [PATCH] strain: score against the nights the quiet level came from, not this morning's rhr --- lib/src/onehz/clinical/load_trimp.dart | 23 ++++++++++++++++ test/onehz/steady_resting_hr_test.dart | 38 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/onehz/steady_resting_hr_test.dart diff --git a/lib/src/onehz/clinical/load_trimp.dart b/lib/src/onehz/clinical/load_trimp.dart index 6b8b583..9b78a23 100644 --- a/lib/src/onehz/clinical/load_trimp.dart +++ b/lib/src/onehz/clinical/load_trimp.dart @@ -567,6 +567,29 @@ const int quietHrrWindowDays = 28; /// A 1-hour window measures that hour, not ordinary living. const int quietHrrTraitMinMinutes = 360; +/// The resting HR to score a day's strain against: the median of the trailing +/// [quietHrrWindowDays] prior nights, or [today] when fewer than +/// [quietHrrMinDays] nights exist. +/// +/// The quiet level is stored as a fraction of each prior day's own reserve, so +/// scoring today against THIS MORNING's resting HR compares two different +/// rulers: a night 3 bpm higher shrinks every minute of the day while the +/// level stays put, and an ordinary day scored 3.3 reads 0.0 (edge#471). Using +/// the same slow reference on both sides keeps a raised night from erasing the +/// day. Null only when there is neither a usable history nor a [today]. +double? steadyRestingHr(double? today, List priorNights) { + final valid = [ + for (final v in priorNights) + if (v.isFinite && v > 0) v, + ]; + if (valid.length < quietHrrMinDays) { + return today != null && today.isFinite && today > 0 ? today : null; + } + return median(valid.length > quietHrrWindowDays + ? valid.sublist(valid.length - quietHrrWindowDays) + : valid); +} + /// A personal quiet-waking level and how many prior days back it. class QuietLevel { final double hrr; diff --git a/test/onehz/steady_resting_hr_test.dart b/test/onehz/steady_resting_hr_test.dart new file mode 100644 index 0000000..a71fffd --- /dev/null +++ b/test/onehz/steady_resting_hr_test.dart @@ -0,0 +1,38 @@ +// A raised night must not erase an ordinary day (edge#471). +import 'package:test/test.dart'; +import 'package:openstrap_analytics/onehz.dart'; + +void main() { + // 16 h awake at ~78 bpm with an hour's walk at ~100, max HR 185. + final day = [ + for (var i = 0; i < 900; i++) 78.0, + for (var i = 0; i < 60; i++) 100.0, + ]; + double strainAt(double? rhr, double quiet) => strainScoreFromSeries(day, + restingHr: rhr, + maxHr: 185, + quietHrr: quiet, + sex: Sex.male, + quietSettled: true) + .value!; + + test('quiet level measured at rhr 55, day scored at 58: 0.0 without the fix', + () { + final quiet = dailyQuietWakingHrr(day, restingHr: 55, maxHr: 185)!; + expect(strainAt(55, quiet), greaterThan(2.0)); + expect(strainAt(58, quiet), 0.0); // the bug + }); + + test('steadyRestingHr keeps the day on the nights the level came from', () { + final quiet = dailyQuietWakingHrr(day, restingHr: 55, maxHr: 185)!; + final rhr = steadyRestingHr(58, List.filled(28, 55.0)); + expect(rhr, 55); + expect(strainAt(rhr, quiet), strainAt(55, quiet)); + }); + + test('cold start falls back to today, and nothing at all is null', () { + expect(steadyRestingHr(58, [55, 55]), 58); + expect(steadyRestingHr(null, const []), isNull); + expect(steadyRestingHr(58, [55, double.nan, 0, 56, 54]), 55); + }); +}