Skip to content

Compute the sleep robust-z scales once per night, not per epoch - #31

Merged
abdulsaheel merged 1 commit into
mainfrom
feat/optimizations
Jul 29, 2026
Merged

Compute the sleep robust-z scales once per night, not per epoch#31
abdulsaheel merged 1 commit into
mainfrom
feat/optimizations

Conversation

@abdulsaheel

@abdulsaheel abdulsaheel commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

cardioStager scored three fixed samples (sleepRmssd, sleepLfhf,
sleepRk) with robustZ inside the per-epoch classify loop. Each call
re-derived the same median and MAD, sorting the sample twice — about 2,880
times over a night, on lists that never change inside the loop.

Hoisted to a RobustScale built once after the samples are assembled.
robustZ now delegates to it, so the null semantics are shared rather than
duplicated: absent for a sample under two values or a MAD of zero.

Output is unchanged. Verified by running both versions over an 8-hour
synthetic night and comparing the stage sequence, deep flags and confidence —
identical. 765 ms -> 408 ms per run.

Tests: 383 pass.

Summary by CodeRabbit

  • Performance
    • Improved sleep-stage analysis efficiency by reusing statistical calculations across sleep epochs.
    • Reduced repeated processing during REM-related detection without changing results or public interfaces.

cardioStager scored three fixed samples with robustZ inside the classify loop,
so each epoch re-sorted them to re-derive the same median and MAD. Hoisted to
a RobustScale built once; robustZ now delegates to it.

Output is unchanged — same stages, deep flags and confidence on an 8-hour
synthetic night. 765 ms -> 408 ms per run.
@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 8d42cb08-0461-4da0-91c5-802a0903f15e

📥 Commits

Reviewing files that changed from the base of the PR and between f5ccae6 and 832d8fe.

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

📝 Walkthrough

Walkthrough

RobustScale precomputes median and MAD-derived values for reusable robust z-score calculations. cardioStager creates cached scales for sleep baselines and uses them during REM autonomic classification.

Changes

Cached Robust Scaling

Layer / File(s) Summary
RobustScale abstraction
lib/src/onehz/util.dart
Adds RobustScale and updates robustZ to use precomputed median and MAD-derived scale values.
Cardio staging scale reuse
lib/src/onehz/sleep/cardio_stager.dart
Precomputes RMSSD, LF/HF, and R(k) scales once per staging run and reuses them in REM gating calculations.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

🚥 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 accurately summarizes the main change: caching sleep robust-z scales once per night instead of recomputing them each epoch.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/optimizations

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.

@abdulsaheel
abdulsaheel merged commit 5d42191 into main Jul 29, 2026
3 checks passed
@abdulsaheel
abdulsaheel deleted the feat/optimizations branch July 29, 2026 18:42
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