Skip to content

don't difference rr beats across holes (irregular rhythm, cardio stager) - #81

Open
abdulsaheel wants to merge 3 commits into
mainfrom
fix/sweep-analytics-main-1
Open

abdulsaheel wants to merge 3 commits into
mainfrom
fix/sweep-analytics-main-1

Conversation

@abdulsaheel

@abdulsaheel abdulsaheel commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

irregularBeatScreen only broke pairs at its own 300-2000 filter. callers pass correctRr nn which is already compacted, so beats either side of a dropped run or a dropout got differenced and inflated sd1 / pnn70. now uses the same nnTimesMs seam test hrv_time does.

cardio stager window gather had the same thing: a step-rejected beat or an rr hole left its neighbours adjacent in beats, so window rmssd and r(k) took one big diff per seam (a 400ms jump got through a 200ms gate). gather now marks seams and both skip them.

staging and irregular rhythm outputs change, edge needs a kAlgoVersion bump on repin.

Summary by Sourcery

Skip RR differences across temporal seams so irregular-rhythm and cardio-staging outputs remain valid around dropped or rejected beats.

Bug Fixes:

  • Prevent irregular-rhythm metrics from differencing beats separated by dropped runs or time holes, avoiding inflated SD1 and pNN70 results.
  • Prevent cardio-staging RMSSD and R(k) metrics from including differences across rejected beats or RR-stream holes, and abstain when too few contiguous pairs remain.

Enhancements:

  • Share time-seam handling across irregular-rhythm and cardio-staging calculations and expose focused test helpers for window difference behavior.

Tests:

  • Add regression coverage for irregular-rhythm and cardio-staging seams caused by time holes, rejected beats, and fragmented windows.

Summary by CodeRabbit

  • Bug Fixes
    • Irregular rhythm and sleep cardio results now avoid comparing heartbeats across long gaps in the recorded data or across beats excluded from analysis. This helps prevent gaps or rejected readings from affecting rhythm indicators and related measurements.
    • Valid comparisons between successive recorded beats are preserved, including when a rejected beat is removed.

@sourcery-ai

sourcery-ai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Updates irregular-rhythm and cardio-stager HRV calculations to avoid differencing beats across dropped runs, rejected beats, and timestamp holes by deriving explicit continuity seams; adds regression tests for both paths. Resulting staging and irregular-rhythm outputs may change, requiring an edge kAlgoVersion bump when repinning.

Sequence diagram for seam-aware irregular rhythm screening

sequenceDiagram
    participant Input as Compacted RR input
    participant Screen as irregularBeatScreen
    participant Times as nnTimesMs
    participant Metrics as HRV metrics
    Input->>Screen: receive rrMs and keep mask
    Screen->>Times: check elapsed beat time
    alt contiguous successor
        Times-->>Screen: successive = true
        Screen->>Metrics: difference adjacent RR beats
    else dropped run or time hole
        Times-->>Screen: successive = false
        Screen->>Metrics: skip seam difference
    end
    Screen-->>Input: irregular rhythm result
Loading

Sequence diagram for seam-aware cardio staging metrics

sequenceDiagram
    participant Stager as Cardio stager
    participant Cleaner as _cleanBeatsInWindow
    participant RMSSD as _windowRmssd
    participant Rk as _websterRescore
    Stager->>Cleaner: clean beats in window
    Cleaner-->>Stager: beats and seam markers
    Stager->>RMSSD: calculate window differences
    RMSSD->>RMSSD: skip seam pairs
    Stager->>Rk: calculate R(k) differences
    Rk->>Rk: skip seam pairs
    RMSSD-->>Stager: seam-aware RMSSD
    Rk-->>Stager: seam-aware R(k)
Loading

Flow diagram for seam-aware HRV differencing

flowchart LR
    A[RR beats and timestamps] --> B{True successor?}
    B -->|Yes| C[Include successive difference]
    B -->|Dropped run, rejected beat, or time hole| D[Mark seam]
    D --> E[Skip difference]
    C --> F[HRV metrics]
    E --> F
Loading

File-Level Changes

Change Details Files
Prevent HRV differences from crossing dropped-beat and time-gap seams in irregular-rhythm analysis.
  • Build a shared successive-beat mask using keep status and timestamp continuity.
  • Exclude non-successive pairs from Poincaré differences while preserving compacted-beat processing.
  • Add regression coverage for compacted streams containing large time holes.
lib/src/onehz/clinical/irregular_rhythm.dart
test/onehz/irregular_rhythm_test.dart
Track and honor rejected-beat and RR-hole seams throughout cardio-stager window metrics.
  • Return seam metadata from cleaned window beats and mark seams caused by time gaps, range rejection, or step rejection.
  • Skip seam pairs when calculating window RMSSD and Webster R(k), with pair-count-aware RMSSD abstention.
  • Expose a test helper and add coverage for step-rejected beats and timestamp holes.
lib/src/onehz/sleep/cardio_stager.dart
test/onehz/real_night_cardio_stager_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

@coderabbitai

coderabbitai Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Warning

Review limit reached

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

Next included review available in 17 minutes.

Check out review usage here.

View limit details

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

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: d6be4ca4-9381-47df-8cb9-5abf770fe5a1

📥 Commits

Reviewing files that changed from the base of the PR and between e173fd9 and 209c526.

📒 Files selected for processing (2)
  • lib/src/onehz/sleep/cardio_stager.dart
  • test/onehz/real_night_cardio_stager_test.dart

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Advanced

Run ID: 1a32f241-5424-483b-b384-ecf8e4cff26d

📥 Commits

Reviewing files that changed from the base of the PR and between 0441ef9 and e173fd9.

📒 Files selected for processing (4)
  • lib/src/onehz/clinical/irregular_rhythm.dart
  • lib/src/onehz/sleep/cardio_stager.dart
  • test/onehz/irregular_rhythm_test.dart
  • test/onehz/real_night_cardio_stager_test.dart

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

Clinical rhythm screening and sleep cardio window calculations now exclude interval differences across timestamp gaps and rejected beats. Regression tests cover both conditions.

Changes

RR Continuity Filtering

Layer / File(s) Summary
Clinical rhythm continuity
lib/src/onehz/clinical/irregular_rhythm.dart, test/onehz/irregular_rhythm_test.dart
The continuity mask accounts for aligned timestamp gaps as well as filtered beats. Aggregate interval differences use this mask. A regression test checks results across 60-second gaps.
Sleep cardio window seams
lib/src/onehz/sleep/cardio_stager.dart, test/onehz/real_night_cardio_stager_test.dart
The window cleaner marks breaks after rejected beats and timestamp gaps. RMSSD and R(k) skip differences across seams. Tests cover rejected beats and gaps.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Merge Risk: 🔵 Low · up to e173f

Screening results can change incorrectly for some corrected beats. This is a bounded risk to accept or fix before merging; bump the edge algorithm version when deploying the changed metrics.

Security Architecture Review

Security architecture risk: 🔵 Low · up to e173f

The changes remain local to metric calculations and do not introduce new privileges or privileged operations. A small test helper becomes publicly accessible, and deployment of the corrected outputs depends on a separate algorithm-version update that was not verified.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The demonstrated exposure change is availability of an additional calculation helper to package consumers. Its inspected execution is confined to supplied samples and per-call results; the export does not itself expose another tenant, service, or data store. External consumer reachability was not established.

Resilience and Maintainability Implications

  • inferred — The new seam bookkeeping is per-call state, not a shared or persistent transition. Accepted beats and seam flags remain aligned, and interrupted calculation cannot leave partially published seam state through the inspected helper.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: preventing RR beat differences across time holes in irregular-rhythm screening and cardio-stager processing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Autopilot is currently an internal CodeRabbit preview.


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 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

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