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
19 changes: 18 additions & 1 deletion lib/src/onehz/sleep/cardio_stager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,15 @@ class SleepUserProfile {

/// One night's observed sleep baselines, emitted by [cardioStager] for folding
/// into a [SleepUserProfile]. [epochs] = staged epoch count (lets the caller
/// pick the MAIN sleep among a night's sessions/naps, and reject short naps).
/// reject short naps). [startTsSec]/[endTsSec] = the staged window in epoch
/// seconds, which is what ties an observation to ITS night: among the sessions
/// a search window stages, the epoch count alone cannot tell the night the
/// caller chose from a longer session of the day before. Null when the caller
/// did not know the window (the fold then has nothing to match on).
class SleepNightObservation {
final int epochs;
final int? startTsSec;
final int? endTsSec;
final double? hrFloorP5;
final double? hrFloorP25;
final double? hrSleepMedian;
Expand All @@ -353,6 +359,8 @@ class SleepNightObservation {
final double? rkMed;
const SleepNightObservation({
required this.epochs,
this.startTsSec,
this.endTsSec,
this.hrFloorP5,
this.hrFloorP25,
this.hrSleepMedian,
Expand Down Expand Up @@ -431,9 +439,14 @@ CardioStagerResult cardioStager(
final cadence = runStarts.isEmpty ? null : sampleCadenceSeconds(
[for (var i = 0; i < n; i++) accel[i].tsMs / 1000.0]);
final perEpoch = math.max(1, (epochSec / (cadence ?? 1)).round());
// The staged window, for the observation this call may record.
final firstMs = n > 0 ? accel[0].tsMs : double.nan;
final lastMs = n > 0 ? accel[n - 1].tsMs : double.nan;
return classifyCardioEpochs(
features,
epochSec: epochSec,
startTsSec: firstMs.isFinite ? (firstMs / 1000).floor() : null,
endTsSec: lastMs.isFinite ? (lastMs / 1000).ceil() : null,
segmentStarts: [for (final i in runStarts)
if (i > 0 && i ~/ perEpoch < features.length) i ~/ perEpoch],
userProfile: userProfile ?? cardioUserProfile,
Expand Down Expand Up @@ -997,6 +1010,8 @@ void _removeEpochSorted(List<double> sorted, double value) {
CardioStagerResult classifyCardioEpochs(
CardioEpochFeatures f, {
int epochSec = _epochSec,
int? startTsSec,
int? endTsSec,
List<int> segmentStarts = const [],
SleepUserProfile? userProfile,
double remScoreCut = _remScoreCut,
Expand Down Expand Up @@ -1344,6 +1359,8 @@ CardioStagerResult classifyCardioEpochs(
final hrSleepMed = median(sleepHr);
_cardioObservations.add(SleepNightObservation(
epochs: nEpoch,
startTsSec: startTsSec,
endTsSec: endTsSec,
hrFloorP5: percentile(sleepHr, 5),
hrFloorP25: percentile(sleepHr, 25),
hrSleepMedian: hrSleepMed,
Expand Down
4 changes: 3 additions & 1 deletion lib/src/onehz/sleep/epoch_rows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,9 @@ class EpochRows {
lfhf: [for (final r in pooled) r.lfhf],
rk: [for (final r in pooled) r.rk],
sdnn: [for (final r in pooled) r.sdnn]),
segmentStarts: segmentStarts);
segmentStarts: segmentStarts,
startTsSec: runs.isEmpty ? null : runs.first.lo,
endTsSec: runs.isEmpty ? null : runs.last.hi);
final labels = result.base.stages;
var offset = 0;
for (final run in runs) {
Expand Down
22 changes: 22 additions & 0 deletions test/onehz/sleep_user_profile_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ void main() {
}
});

test('an observation carries the window it was staged over', () {
// Which night an observation belongs to cannot be read from its epoch
// count: a longer session of the day before looks the same. The window
// is what the caller matches against the night it chose.
const n = 4 * 3600, t0 = 1780000000;
final hr = [for (var i = 0; i < n; i++) 55.0 + (i ~/ 60) % 5];
final accel = <AccelSample>[
for (var i = 0; i < n; i++) AccelSample((t0 + i) * 1000.0, 0, 0, 1.0),
];
cardioRecordObservations = true;
resetCardioObservations();
try {
cardioStager(hr, accel);
final o = takeCardioObservations().single;
expect(o.startTsSec, t0);
expect(o.endTsSec, t0 + n - 1);
} finally {
cardioRecordObservations = false;
resetCardioObservations();
}
});

test('a profile consistent with the night does not destabilize staging',
() {
// Synthetic still, low-HR night (should read as overwhelmingly asleep).
Expand Down
Loading