Repository navigation
fix: retain interrupted main sleep across accepted continuation gaps #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DropTabl
wants to merge
2
commits into
OpenStrap:main
Choose a base branch
from
DropTabl:fix/interrupted-sleep
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import 'dart:math' as math; | ||
|
|
||
| import 'package:openstrap_analytics/onehz.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| // Synthetic nights; no personal sensor data. A 70-minute awakening fits the | ||
| // inclusive 90-minute continuation limit; the gap remains awake or unobserved. | ||
| void main() { | ||
| for (final scenario in [ | ||
| (gapMinutes: 70, observed: true), | ||
| (gapMinutes: 100, observed: true), | ||
| (gapMinutes: 70, observed: false), | ||
| ]) { | ||
| final gapMinutes = scenario.gapMinutes; | ||
| test( | ||
| 'main sleep after a $gapMinutes-minute break (observed: ${scenario.observed})', | ||
| () { | ||
| final data = _Recording(); | ||
| data.block(120 * 60, awake: true); | ||
| data.block(240 * 60, awake: false); // 23:00–03:00 | ||
| data.block(gapMinutes * 60, awake: true, observed: scenario.observed); | ||
| data.block(180 * 60, awake: false); | ||
| data.block(120 * 60, awake: true); | ||
|
|
||
| final sleep = segmentSleep(data.accel, data.hr, tzOffsetSec: 0); | ||
| expect(sleep.present, isTrue); | ||
| if (gapMinutes == 70) { | ||
| expect(sleep.inBedSec, greaterThan(8 * 3600), | ||
| reason: 'both overnight blocks belong to the main sleep'); | ||
| expect(sleep.tstSec, greaterThan(6 * 3600)); | ||
| expect(sleep.tstSec, lessThanOrEqualTo(7 * 3600), | ||
| reason: 'the awake gap must not inflate time asleep'); | ||
| if (scenario.observed) { | ||
| expect(sleep.wasoSec, greaterThanOrEqualTo(65 * 60)); | ||
| } else { | ||
| expect(sleep.unobservedSec, greaterThanOrEqualTo(70 * 60)); | ||
| expect(sleep.wasoSec, lessThan(10 * 60), | ||
| reason: 'missing measurements must not become measured wake'); | ||
| } | ||
| } else { | ||
| expect(sleep.inBedSec, lessThan(5 * 3600), | ||
| reason: 'a break beyond the continuation limit stays separate'); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| for (final gap in [90 * 60, 90 * 60 + 1]) { | ||
| test('detected session gap of $gap seconds respects the inclusive limit', | ||
| () { | ||
| final data = _Recording(); | ||
| data.block(2 * 3600, awake: true); | ||
| data.block(4 * 3600, awake: false); | ||
| // The still-window filter and last-sample run endpoints add 342 | ||
| // seconds for this fixture. Verify the detected gap, not the raw one. | ||
| data.block(gap - 342, awake: true); | ||
| data.block(3 * 3600, awake: false); | ||
| data.block(2 * 3600, awake: true); | ||
| final sessions = data.detect(); | ||
| expect(sessions, hasLength(2)); | ||
| expect(sessions[1].start - sessions[0].end, gap); | ||
| final sleep = segmentSleep(data.accel, data.hr, tzOffsetSec: 0); | ||
| expect(sleep.present, isTrue); | ||
| expect(sleep.inBedSec, | ||
| gap == 90 * 60 ? greaterThan(8 * 3600) : lessThan(5 * 3600)); | ||
| }); | ||
| } | ||
|
|
||
| for (final hours in [ | ||
| <int>[8, 8], | ||
| <int>[6, 5, 5] | ||
| ]) { | ||
| test('fragmented night $hours cannot bypass the 16-hour span cap', () { | ||
| final data = _Recording(startHour: 18); | ||
| data.block(2 * 3600, awake: true); | ||
| for (var i = 0; i < hours.length; i++) { | ||
| if (i > 0) data.block(70 * 60, awake: true); | ||
| data.block(hours[i] * 3600, awake: false); | ||
| } | ||
| data.block(2 * 3600, awake: true); | ||
| final sessions = data.detect(); | ||
| expect(sessions, hasLength(hours.length)); | ||
| expect(sessions.last.end - sessions.first.start, greaterThan(16 * 3600)); | ||
| final sleep = segmentSleep(data.accel, data.hr, tzOffsetSec: 0); | ||
| expect(sleep.present, isTrue); | ||
| expect(sleep.inBedSec, lessThanOrEqualTo(16 * 3600)); | ||
| expect(sleep.tstSec, greaterThan(5 * 3600), | ||
| reason: | ||
| 'keep a valid main group rather than discard the whole night'); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| class _Recording { | ||
| final accel = <AccelSample>[]; | ||
| final hr = <double>[]; | ||
| final int start; | ||
| int elapsedSeconds = 0; | ||
|
|
||
| _Recording({int startHour = 21}) | ||
| : start = DateTime.utc(2024, 1, 1, startHour).millisecondsSinceEpoch; | ||
|
|
||
| void block(int seconds, {required bool awake, bool observed = true}) { | ||
| for (var i = 0; i < seconds; i++, elapsedSeconds++) { | ||
| if (!observed) continue; | ||
| final phase = math.sin(i * 0.5); | ||
| accel.add(AccelSample( | ||
| start + elapsedSeconds * 1000.0, | ||
| awake ? 0.3 * phase : 0.02, | ||
| awake ? 0.3 : 0.02, | ||
| awake ? 0.9 * (1 - 0.2 * phase) : 1.0, | ||
| )); | ||
| hr.add(awake ? 85 : 49 + 1.5 * math.sin(i / 1800)); | ||
| } | ||
| } | ||
|
|
||
| List<SleepSession> detect() => AdvancedSleepStager.detectSleep( | ||
| [for (final a in accel) GravTs(a.tsMs ~/ 1000, a.x, a.y, a.z)], | ||
| [ | ||
| for (var i = 0; i < accel.length; i++) | ||
| HrTs(accel[i].tsMs ~/ 1000, hr[i]) | ||
| ], | ||
| tzOffsetSec: 0, | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.