From 6feab93727f771bec3743446807da32ebae60e48 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Thu, 3 Dec 2026 22:37:45 +0530 Subject: [PATCH 1/3] energy: refuse a day above 5x basal instead of billing artifact hr (#111) --- lib/src/onehz/incremental_minutes.dart | 3 ++- lib/src/onehz/registry/group_registry.dart | 2 +- lib/src/onehz/workout/calories.dart | 17 ++++++++++++ test/algo_version_test.dart | 2 +- test/onehz/daily_energy_artifact_test.dart | 31 ++++++++++++++++++++++ test/onehz/steps_test.dart | 2 +- 6 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 test/onehz/daily_energy_artifact_test.dart diff --git a/lib/src/onehz/incremental_minutes.dart b/lib/src/onehz/incremental_minutes.dart index a54a0e4..e33ce36 100644 --- a/lib/src/onehz/incremental_minutes.dart +++ b/lib/src/onehz/incremental_minutes.dart @@ -332,7 +332,8 @@ class IncrementalMinuteMetrics { trimp: trimp, strain: strain, minutes: minutes, - energy: _basalPerMinute == null + energy: _basalPerMinute == null || + !Calories.dayEnergyPlausible(basal, basal + active) ? null : ( total: basal + active, diff --git a/lib/src/onehz/registry/group_registry.dart b/lib/src/onehz/registry/group_registry.dart index 52887ea..ee2c717 100644 --- a/lib/src/onehz/registry/group_registry.dart +++ b/lib/src/onehz/registry/group_registry.dart @@ -118,7 +118,7 @@ const GroupRegistry kAnalyticsGroupRegistry = GroupRegistry(1, [ GroupDecl( id: 'found.energy', kind: GroupKind.foundation, - counter: 2, + counter: 3, members: ['Calories.dailyEnergy'], api: ['Calories.minuteEnergy', 'Calories.estimateBoutCalories'], files: ['lib/src/onehz/workout/calories.dart'], diff --git a/lib/src/onehz/workout/calories.dart b/lib/src/onehz/workout/calories.dart index 1786004..7339333 100644 --- a/lib/src/onehz/workout/calories.dart +++ b/lib/src/onehz/workout/calories.dart @@ -168,6 +168,15 @@ class Calories { ? restingHr + activeHRRFraction * (hrmax - restingHr) : null; + /// Day total above this multiple of basal is refused by [dailyEnergy] + /// (returns null). See the guard there. + static const double maxDailyEnergyBasalMultiple = 5.0; + + /// False when a day's [total] is above [maxDailyEnergyBasalMultiple] x + /// [basal]. Shared by [dailyEnergy] and the incremental path. + static bool dayEnergyPlausible(double basal, double total) => + !(basal > 0 && total > maxDailyEnergyBasalMultiple * basal); + /// 60 s/min × 4.184 kJ/kcal. static const double workoutDivisor = 251.04; @@ -349,6 +358,14 @@ class Calories { } active += walking; final basal = basalPerMin * dayMinutes; + // Implausible-day guard (issue #111): hours of motion-artifact HR above the + // gate bill 7-9k kcal days. Abstain, never cap (a cap is a substituted + // number). Ceiling: the highest multi-week means reported for elite + // cyclists in the Tour de France are ~4.3-5x BMR (Westerterp 1986, Saris + // 1989); a single day above 5x basal is not credible from a wrist HR + // stream. ponytail: one constant; a motion-corroborated gate needs accel + // in this signature. + if (!dayEnergyPlausible(basal, basal + active)) return null; return ( total: basal + active, active: active, diff --git a/test/algo_version_test.dart b/test/algo_version_test.dart index 1e7d53b..fe11508 100644 --- a/test/algo_version_test.dart +++ b/test/algo_version_test.dart @@ -16,6 +16,6 @@ void main() { }; expect({for (final g in kAnalyticsGroupRegistry.groups) if (g.counter != 1) g.id: g.counter}, - {for (final id in changed) id: 2}); + {for (final id in changed) id: id == 'found.energy' ? 3 : 2}); // 3: #111 implausible-day abstention }); } diff --git a/test/onehz/daily_energy_artifact_test.dart b/test/onehz/daily_energy_artifact_test.dart new file mode 100644 index 0000000..e80590a --- /dev/null +++ b/test/onehz/daily_energy_artifact_test.dart @@ -0,0 +1,31 @@ +// Issue #111: hours of HR above the flex gate must not bill an impossible day. +// Inputs are SYNTHETIC (made-up profile, constant HR), not any user's data. +import 'package:test/test.dart'; +import 'package:openstrap_analytics/src/onehz/workout/calories.dart'; + +void main() { + const p = WorkoutUserProfile( + weightKg: 80, heightCm: 180, age: 35, sex: 'male'); + // gate = 55 + 0.4 * (183.5 - 55) = 106.4 bpm + Map? day(List hr) { + final e = Calories.dailyEnergy(hr, + profile: p, hrmax: 183.5, restingHr: 55, dayMinutes: 1440); + return e == null ? null : {'total': e.total, 'active': e.active}; + } + + List hours(double bpm, int h) => List.filled(h * 60, bpm); + + test('14 h at 130 bpm abstains instead of billing ~10,700 kcal', () { + expect(day(hours(130, 14)), isNull); + }); + test('14 h at 115 bpm abstains', () { + expect(day(hours(115, 14)), isNull); + }); + test('plausible ski day (6 h at 125, 8 h at 80) still bills', () { + final d = day([...hours(125, 6), ...hours(80, 8)])!; + expect(d['total'], inInclusiveRange(4000, 7000)); + }); + test('quiet day below the gate unchanged', () { + expect(day(hours(100, 14))!['active'], 0); + }); +} diff --git a/test/onehz/steps_test.dart b/test/onehz/steps_test.dart index b9b413c..63b2027 100644 --- a/test/onehz/steps_test.dart +++ b/test/onehz/steps_test.dart @@ -849,7 +849,7 @@ void main() { // number in front of it is what decides. final profile = const WorkoutUserProfile( weightKg: 80, heightCm: 180, age: 30, sex: 'male'); - final hr = List.filled(1440, 111.0); + final hr = List.filled(480, 111.0); // 8 h: a 24 h day at 111 is refused (#111) final wide = Calories.dailyEnergy(hr, profile: profile, hrmax: 190, restingHr: 60)!; final tanaka = Calories.dailyEnergy(hr, From e89b35f227a7753e866872c1b6c7103f38068979 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 4 Dec 2026 02:16:47 +0530 Subject: [PATCH 2/3] energy: cite the 5x basal ceiling in the constant doc --- lib/src/onehz/workout/calories.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/src/onehz/workout/calories.dart b/lib/src/onehz/workout/calories.dart index 7339333..7bfa666 100644 --- a/lib/src/onehz/workout/calories.dart +++ b/lib/src/onehz/workout/calories.dart @@ -169,7 +169,10 @@ class Calories { : null; /// Day total above this multiple of basal is refused by [dailyEnergy] - /// (returns null). See the guard there. + /// (returns null). See the guard there. Sustained daily expenditure in + /// elite endurance athletes tops out around 4.3-5x BMR (Westerterp et al. + /// 1986, Int J Sports Med; Saris et al. 1989, Tour de France), so a single + /// wrist-HR day above 5x is treated as artifact, not billed. static const double maxDailyEnergyBasalMultiple = 5.0; /// False when a day's [total] is above [maxDailyEnergyBasalMultiple] x From f931073276615d7761da63bb75367eef79c77034 Mon Sep 17 00:00:00 2001 From: Mohammad Abdul Sahil <127765312+abdulsaheel@users.noreply.github.com> Date: Fri, 4 Dec 2026 02:27:26 +0530 Subject: [PATCH 3/3] energy: bound partial windows by the same daily ceiling --- lib/src/onehz/incremental_minutes.dart | 2 +- lib/src/onehz/workout/calories.dart | 12 +++++++----- test/onehz/daily_energy_artifact_test.dart | 5 +++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/src/onehz/incremental_minutes.dart b/lib/src/onehz/incremental_minutes.dart index e33ce36..0cc818a 100644 --- a/lib/src/onehz/incremental_minutes.dart +++ b/lib/src/onehz/incremental_minutes.dart @@ -333,7 +333,7 @@ class IncrementalMinuteMetrics { strain: strain, minutes: minutes, energy: _basalPerMinute == null || - !Calories.dayEnergyPlausible(basal, basal + active) + !Calories.dayEnergyPlausible(_basalPerMinute!, basal + active) ? null : ( total: basal + active, diff --git a/lib/src/onehz/workout/calories.dart b/lib/src/onehz/workout/calories.dart index 7bfa666..ff5a3e8 100644 --- a/lib/src/onehz/workout/calories.dart +++ b/lib/src/onehz/workout/calories.dart @@ -175,10 +175,12 @@ class Calories { /// wrist-HR day above 5x is treated as artifact, not billed. static const double maxDailyEnergyBasalMultiple = 5.0; - /// False when a day's [total] is above [maxDailyEnergyBasalMultiple] x - /// [basal]. Shared by [dailyEnergy] and the incremental path. - static bool dayEnergyPlausible(double basal, double total) => - !(basal > 0 && total > maxDailyEnergyBasalMultiple * basal); + /// False when [total] is above [maxDailyEnergyBasalMultiple] x a full + /// 1440-minute basal. The ceiling is a fixed daily number, not pro-rated by + /// the window: a 60 minute walk is judged the same as inside a full day. + /// Shared by [dailyEnergy] and the incremental path. + static bool dayEnergyPlausible(double basalPerMin, double total) => + !(basalPerMin > 0 && total > maxDailyEnergyBasalMultiple * basalPerMin * 1440); /// 60 s/min × 4.184 kJ/kcal. static const double workoutDivisor = 251.04; @@ -368,7 +370,7 @@ class Calories { // 1989); a single day above 5x basal is not credible from a wrist HR // stream. ponytail: one constant; a motion-corroborated gate needs accel // in this signature. - if (!dayEnergyPlausible(basal, basal + active)) return null; + if (!dayEnergyPlausible(basalPerMin, basal + active)) return null; return ( total: basal + active, active: active, diff --git a/test/onehz/daily_energy_artifact_test.dart b/test/onehz/daily_energy_artifact_test.dart index e80590a..fb627c2 100644 --- a/test/onehz/daily_energy_artifact_test.dart +++ b/test/onehz/daily_energy_artifact_test.dart @@ -28,4 +28,9 @@ void main() { test('quiet day below the gate unchanged', () { expect(day(hours(100, 14))!['active'], 0); }); + test('a short window is judged by the same daily ceiling', () { + final e = Calories.dailyEnergy(hours(130, 1), + profile: p, hrmax: 183.5, restingHr: 55, dayMinutes: 60); + expect(e, isNotNull); + }); }