Repository navigation
rmssd gate: give a refused low-heart-rate night a second look (offered for consideration) #114
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
Boyonbike
wants to merge
4
commits into
OpenStrap:main
Choose a base branch
from
Boyonbike:fix/low-hr-rmssd-gate
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
4 commits
Select commit
Hold shift + click to select a range
a0fb122
rmssd gate: let the breathing exemption see a low-heart-rate night
Boyonbike 315e53e
rmssd gate: make the low-HR change a second look after a refusal
Boyonbike 5e09654
rmssd gate: bump sleep.stage too; don't clean across a dropped window
Boyonbike a39d6a7
rmssd gate: second look only for a night main's path leaves blank
Boyonbike 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
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
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,125 @@ | ||
| // The jitter gate at low resting heart rate. | ||
| // | ||
| // Breathing at f cycles/beat puts the lag-1 ACF of the successive differences | ||
| // near cos 2πf: at ~14–15 breaths/min and 42–46 bpm, f ≈ 0.31–0.36 and ACF1 is | ||
| // past kNnDiffAcf1Floor with no jitter at all. The spectral exemption exists | ||
| // for that night but needs 20 unbroken 64-beat segments; beats an optical | ||
| // sensor places 15–28 % early or late trip the 20 % ectopic filter, and with | ||
| // dropouts and the 5-min window edges the stream never holds 20. A refused | ||
| // night now gets a second look at its contributing beats as one stream. | ||
| // | ||
| // The nights come from tool/rmssd_gate_corpus.dart (seeded). The pinned | ||
| // values were produced by main at b92686a. | ||
| import 'package:openstrap_analytics/onehz.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import '../../tool/rmssd_gate_corpus.dart'; | ||
|
|
||
| Metric<SessionRmssd> _session(({List<double> rr, List<double> ts}) n) => | ||
| sleepSessionRmssdDetail(n.rr, n.ts, | ||
| startSec: gateStartSec, endSec: gateStartSec + gateDurSec); | ||
|
|
||
| Metric<double> _nocturnal(({List<double> rr, List<double> ts}) n) { | ||
| final c = correctRr(n.rr, rrTsMs: n.ts); | ||
| final t0 = n.ts.first; | ||
| return nocturnalRmssd(c.nn, [for (final t in c.nnTimesMs) t + t0]); | ||
| } | ||
|
|
||
| void main() { | ||
| group('a refused low-HR night gets a second look', () { | ||
| // main refuses all of these. | ||
| final cases = <String, ({double hr, double p, double drop, int seed})>{ | ||
| '44 bpm, 4 % misplaced, 3 % dropped (seed 1)': | ||
| (hr: 44, p: 0.04, drop: 0.03, seed: 1), | ||
| '44 bpm, 4 % misplaced, 3 % dropped (seed 2)': | ||
| (hr: 44, p: 0.04, drop: 0.03, seed: 2), | ||
| '46 bpm, 8 % misplaced, 1 % dropped (seed 3)': | ||
| (hr: 46, p: 0.08, drop: 0.01, seed: 3), | ||
| }; | ||
| cases.forEach((label, c) { | ||
| test('session headline publishes: $label', () { | ||
| final m = _session(gateNight( | ||
| hr: c.hr, seed: c.seed, rsaMs: 110, drop: c.drop, misplaced: c.p)); | ||
| expect(m.present, isTrue, reason: m.note); | ||
| // Through the breathing line, not by ACF1 clearing the floor. | ||
| expect(m.value!.diffAcf1!, lessThan(kNnDiffAcf1Floor)); | ||
| // A 110 ms swing at f ≈ 0.33: RMSSD ≈ 110·√2·sin(πf) ≈ 135 ms. | ||
| expect(m.value!.rmssd, inInclusiveRange(115, 150)); | ||
| }); | ||
| }); | ||
|
|
||
| test('nocturnalRmssd publishes: 44 bpm, 4 % misplaced, 3 % dropped (seed 0)', | ||
| () { | ||
| final m = _nocturnal(gateNight( | ||
| hr: 44, seed: 0, rsaMs: 110, drop: 0.03, misplaced: 0.04)); | ||
| expect(m.present, isTrue, reason: m.note); | ||
| }); | ||
| }); | ||
|
|
||
| group('a night main already publishes keeps its value', () { | ||
| // The second look runs only after a refusal, so a night that cleared the | ||
| // gate before is untouched, including one that cleared it through the | ||
| // spectral exemption. | ||
| final pinned = <String, (({List<double> rr, List<double> ts}), double, double)>{ | ||
| '44 bpm, 4 % misplaced, 1 % dropped (seed 0)': ( | ||
| gateNight(hr: 44, seed: 0, rsaMs: 110, drop: 0.01, misplaced: 0.04), | ||
| 130.6, 150.5), | ||
| '44 bpm, 4 % misplaced, 1 % dropped (seed 2)': ( | ||
| gateNight(hr: 44, seed: 2, rsaMs: 110, drop: 0.01, misplaced: 0.04), | ||
| 129.2, 149.1), | ||
| '46 bpm, 15 breaths/min, 1 % dropped (seed 0)': ( | ||
| gateNight(hr: 46, seed: 0, breathsPerMin: 15, rsaMs: 130, drop: 0.01), | ||
| 152.6, 158.2), | ||
| }; | ||
| pinned.forEach((label, p) { | ||
| test(label, () { | ||
| final (night, session, nocturnal) = p; | ||
| final s = _session(night); | ||
| expect(s.present, isTrue, reason: s.note); | ||
| expect(s.value!.diffAcf1!, lessThan(kNnDiffAcf1Floor)); | ||
| expect(s.value!.rmssd, closeTo(session, 0.05)); | ||
| expect(_nocturnal(night).value, closeTo(nocturnal, 0.05)); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| group('the second look still refuses noise', () { | ||
| const artifacts = [(0.0, 0.0), (0.01, 0.01), (0.03, 0.02)]; | ||
| for (final hr in [42.0, 55.0, 75.0]) { | ||
| for (final noise in [20.0, 60.0]) { | ||
| for (final (ect, miss) in artifacts) { | ||
| for (final drop in [0.0, 0.05]) { | ||
| test('white ${noise.toInt()} ms at ${hr.toInt()} bpm, ' | ||
| 'ectopic $ect + missed $miss, drop $drop', () { | ||
| final n = gateNight( | ||
| hr: hr, | ||
| seed: 0, | ||
| noiseMs: noise, | ||
| lfWander: false, | ||
| drop: drop, | ||
| ectopic: ect, | ||
| missed: miss); | ||
| final s = _session(n); | ||
| expect(s.present, isFalse, | ||
| reason: 'published ${s.value?.rmssd} from white noise'); | ||
| final nr = _nocturnal(n); | ||
| expect(nr.present, isFalse, | ||
| reason: 'nocturnalRmssd published ${nr.value} from white noise'); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| group('typical heart rates clear the floor outright', () { | ||
| for (final hr in [60.0, 75.0]) { | ||
| test('$hr bpm, 1 % ectopic + 1 % missed, 5 % dropped', () { | ||
| final m = _session(gateNight( | ||
| hr: hr, seed: 1, rsaMs: 40, drop: 0.05, ectopic: 0.01, missed: 0.01)); | ||
| expect(m.present, isTrue, reason: m.note); | ||
| expect(m.value!.diffAcf1!, greaterThanOrEqualTo(kNnDiffAcf1Floor)); | ||
| }); | ||
| } | ||
| }); | ||
| } |
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.