Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/src/onehz/clinical/load_trimp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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;
Expand Down
38 changes: 38 additions & 0 deletions test/onehz/steady_resting_hr_test.dart
Original file line number Diff line number Diff line change
@@ -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);
});
}
Loading