Skip to content

fix sleep-session rmssd not catching timestamp gaps - #71

Merged
abdulsaheel merged 1 commit into
mainfrom
fix/sleep-rmssd-gap-aware
Sep 19, 2026
Merged

abdulsaheel merged 1 commit into
mainfrom
fix/sleep-rmssd-gap-aware

Conversation

@abdulsaheel

@abdulsaheel abdulsaheel commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

sleepSessionWindowedRmssd buckets rr values into 5-min windows but only keeps the value, not the beat timestamp, before handing the bucket to _cleanWindowRuns. that means it has no way to tell a real sensor dropout (loose strap, brief skin-contact loss) from two beats that just happen to sit next to each other in the bucket. nocturnalRmssd already does this via nnTimesMs, this was the one producer still missing it.

threaded the timestamps through to _cleanWindowRuns and applied the same gap check nocturnalRmssd uses, but with a wider tolerance (1000ms vs 0.5ms) since the real caller (_sessionAvgHRV in advanced_stager.dart) quantizes beat times to whole seconds before calling this, not sub-second precision like the synthetic tests use elsewhere. verified the tighter tolerance broke 3 existing tests due to that quantization before landing on 1000ms.

added two tests: one with a genuine ~2min gap inside a bucket (rmssd should not manufacture a cross-gap diff), one control with no gap (rmssd unchanged).

analytics-output change, downstream edge needs a kAlgoVersion bump once this is pinned — not doing that here, flagging as follow-up.

Summary by Sourcery

Preserve beat timestamps during sleep-session RMSSD cleanup so sensor dropouts do not produce artificial HRV differences.

Bug Fixes:

  • Prevent sleep-session RMSSD from calculating differences across real sensor timestamp gaps within a five-minute window.

Enhancements:

  • Thread beat timestamps through window cleanup with tolerance for whole-second timestamp quantization.

Tests:

  • Add coverage for rejecting cross-gap RMSSD differences and preserving results when no gap exists.

sleepSessionWindowedRmssd buckets rr values but drops the timestamps
before handing the bucket to _cleanWindowRuns, so it can't tell a real
sensor dropout (loose strap, momentary skin-contact loss) from two
beats that are actually adjacent. nocturnalRmssd already does this gap
check via nnTimesMs, this was the one producer that couldn't.

threaded timestamps through to _cleanWindowRuns and added the same gap
check, with a wider tolerance than nocturnalRmssd since the real caller
quantizes beat times to whole seconds (RrTs.ts rounds), not sub-second.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @abdulsaheel, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 6 days and 7 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@coderabbitai

coderabbitai Bot commented Sep 19, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 22 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 48fc32ba-1711-4038-8724-65239b2b8b8b

📥 Commits

Reviewing files that changed from the base of the PR and between 9db819b and 82bd686.

📒 Files selected for processing (2)
  • lib/src/onehz/clinical/hrv_time.dart
  • test/onehz/clinical_test.dart

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai

sourcery-ai Bot commented Sep 19, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fix sleep-session windowed RMSSD inflation from sensor dropouts by threading beat timestamps into run cleaning and rejecting differences across real gaps, while using a 1000 ms tolerance to account for whole-second timestamp quantization. Regression tests cover both dropout and control cases; the noted downstream algorithm-version bump remains follow-up work.

Sequence diagram for sleep-session RMSSD gap filtering

sequenceDiagram
    participant Caller as _sessionAvgHRV
    participant RMSSD as sleepSessionWindowedRmssd
    participant Cleaner as _cleanWindowRuns
    participant Metric as RMSSD calculation

    Caller->>RMSSD: sleepSessionWindowedRmssd(rrMs, rrTsMs)
    RMSSD->>RMSSD: Bucket RR values and beat timestamps
    RMSSD->>Cleaner: _cleanWindowRuns(rrBucket, timestampBucket)
    Cleaner->>Cleaner: Apply range and median filters
    alt Timestamp gap > nn[i] + 1000ms
        Cleaner-->>RMSSD: Split into separate runs
    else No real gap
        Cleaner-->>RMSSD: Preserve run
    end
    RMSSD->>Metric: Difference values within each run
    Metric-->>Caller: Windowed RMSSD
Loading

File-Level Changes

Change Details Files
Preserve beat timestamps through five-minute RMSSD bucketing and split cleaned runs at real temporal gaps.
  • Collect timestamps in parallel with RR values while assigning bucket entries.
  • Extend _cleanWindowRuns to retain survivor timestamps and detect gaps beyond the expected RR interval plus a 1000 ms quantization tolerance.
  • Pass timestamp-aware runs into RMSSD differencing so cross-dropout differences are excluded without changing contiguous data behavior.
lib/src/onehz/clinical/hrv_time.dart
Add regression coverage for dropout handling and unchanged contiguous-session behavior.
  • Verify a roughly two-minute dropout does not create a cross-gap RMSSD difference.
  • Verify a no-gap control case retains the expected RMSSD value.
test/onehz/clinical_test.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@abdulsaheel
abdulsaheel merged commit 1bf9b62 into main Sep 19, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant