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
3 changes: 2 additions & 1 deletion lib/src/onehz/incremental_minutes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ class IncrementalMinuteMetrics {
trimp: trimp,
strain: strain,
minutes: minutes,
energy: _basalPerMinute == null
energy: _basalPerMinute == null ||
!Calories.dayEnergyPlausible(_basalPerMinute!, basal + active)
? null
: (
total: basal + active,
Expand Down
2 changes: 1 addition & 1 deletion lib/src/onehz/registry/group_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const GroupRegistry kAnalyticsGroupRegistry = GroupRegistry(1, <GroupDecl>[
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'],
Expand Down
22 changes: 22 additions & 0 deletions lib/src/onehz/workout/calories.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,20 @@ class Calories {
? restingHr + activeHRRFraction * (hrmax - restingHr)
: null;

/// Day total above this multiple of basal is refused by [dailyEnergy]
/// (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 [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;

Expand Down Expand Up @@ -349,6 +363,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(basalPerMin, basal + active)) return null;
return (
total: basal + active,
active: active,
Expand Down
2 changes: 1 addition & 1 deletion test/algo_version_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
}
36 changes: 36 additions & 0 deletions test/onehz/daily_energy_artifact_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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<String, double>? day(List<double> 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<double> 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);
});
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);
});
}
2 changes: 1 addition & 1 deletion test/onehz/steps_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>.filled(1440, 111.0);
final hr = List<double>.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,
Expand Down
Loading