diff --git a/lib/src/onehz/sleep/hmm_stager.dart b/lib/src/onehz/sleep/hmm_stager.dart new file mode 100644 index 00000000..31c05751 --- /dev/null +++ b/lib/src/onehz/sleep/hmm_stager.dart @@ -0,0 +1,414 @@ +// DISCLAIMER: EXPERIMENTAL RESEARCH FEATURE ONLY. +// +// This classifier is NOT a medical device, NOT a validated sleep staging tool, +// and NOT a substitute for clinical polysomnography (PSG). +// +// The sleep staging methodology used here is for informational and research +// purposes only. It is based on autonomic signatures derived from wrist-worn +// PPG sensors and accelerometry. These signatures are estimates and not +// clinically diagnostic. +// +// IF YOU ARE EXPERIENCING SLEEP PROBLEMS (e.g. apnea, chronic insomnia, +// excessive daytime fatigue), please consult a physician or a qualified +// sleep specialist. Do NOT rely on these analytics for medical decisions. + +import 'dart:math'; + +enum SleepState { wake, nremLight, nremDeep, rem } + +// The transition table and the emission sigmoid centres/scales below are +// hand-set ESTIMATES (per 30 s epoch), not fitted to or validated against PSG. +class HMMConfig { + static const states = [ + SleepState.wake, + SleepState.nremLight, + SleepState.nremDeep, + SleepState.rem + ]; + + static final Map> transitionLogProb = { + SleepState.wake: { + SleepState.wake: log(0.90), + SleepState.nremLight: log(0.09), + SleepState.nremDeep: log(0.005), + SleepState.rem: log(0.005) + }, + SleepState.nremLight: { + SleepState.wake: log(0.12), + SleepState.nremLight: log(0.73), + SleepState.nremDeep: log(0.10), + SleepState.rem: log(0.05) + }, + SleepState.nremDeep: { + SleepState.wake: log(0.06), + SleepState.nremLight: log(0.11), // rows must sum to 1.0 (CodeRabbit #756) + SleepState.nremDeep: log(0.80), + SleepState.rem: log(0.03) + }, + SleepState.rem: { + SleepState.wake: log(0.10), + SleepState.nremLight: log(0.10), + // INTENTIONAL HARD FORBID: REM never goes straight to deep (it passes + // through light). log(0) is -infinity; the decoder only ever ADDS it to + // finite emissions and compares, so it rules the move out without ever + // producing NaN. + SleepState.nremDeep: double.negativeInfinity, + SleepState.rem: log(0.80) // rows must sum to 1.0 (CodeRabbit #756) + }, + }; +} + +class SleepStageAnalyzer { + double? hrMinNight; + double? rmssdMeanNight; + // HYBRID baselines: motion (ENMO, g) and skin temp (centi-°C) medians from + // the opening window. 0 = absent sentinel and is EXCLUDED — a missing sensor + // must never set the baseline to "perfectly still" / "cold". + double? enmoMedianNight; + double? skinTempMedianNight; + + // THREE-LEVEL self-calibration (per user, across nights): + // - nightly baseline (computeBaseline, first 120 min) — primary, always there + // - 7-day rolling baseline — smooths single restless nights out + // - long-term baseline — the user's personal reference; a nightly baseline + // that deviates strongly from it marks the night as ATYPICAL (illness, + // alcohol, travel) instead of silently bending the thresholds. + double? hrMin7d; + double? rmssdMean7d; + double? hrMinLong; + double? rmssdMeanLong; + /// True when tonight's baseline deviates from the long-term baseline by more + /// than [atypicalZThreshold] robust z — an outlier night, lower confidence. + bool atypicalNight = false; + static const double atypicalZThreshold = 2.0; + + /// Feed the multi-night histories (trailing nightly series, oldest first). + /// [rhrHistory]/[rmssdHistory] are the persisted per-night resting-HR / + /// nocturnal-RMSSD series; the last 7 form the rolling baseline, all of them + /// the long-term one. Missing/empty history simply leaves the level unset. + void computeMultiNightBaselines({ + List rhrHistory = const [], + List rmssdHistory = const [], + List skinTempHistory = const [], + List enmoHistory = const [], + }) { + // Describes tonight only: an analyzer run for a later night starts clear. + atypicalNight = false; + double? medianOf(List xs) { + final v = xs.where((x) => x.isFinite && x > 0).toList()..sort(); + if (v.isEmpty) return null; + return v[v.length ~/ 2]; + } + double? madOf(List xs, double? med) { + if (med == null) return null; + final dev = xs.where((x) => x.isFinite).map((x) => (x - med).abs()).toList()..sort(); + if (dev.isEmpty) return null; + return dev[dev.length ~/ 2]; + } + List last7(List xs) => xs.length > 7 ? xs.sublist(xs.length - 7) : xs; + hrMin7d = medianOf(last7(rhrHistory)); + rmssdMean7d = medianOf(last7(rmssdHistory)); + hrMinLong = medianOf(rhrHistory); + rmssdMeanLong = medianOf(rmssdHistory); + // Motion + thermal channels self-calibrate too when a nightly history + // exists (skinTempAdcHistory); without one they stay nightly-only. + skinTempMedian7d = medianOf(last7(skinTempHistory)); + skinTempMedianLong = medianOf(skinTempHistory); + // Motion history: persisted per-night ENMO medians (stable-core), so the + // motion channel self-calibrates across nights like the others. Absent + // history (pre-feature nights) simply leaves the levels unset. + enmoMedian7d = medianOfEnmo(last7(enmoHistory)); + enmoMedianLong = medianOfEnmo(enmoHistory); + // Outlier check: nightly vs long-term, robust z via MAD (1.4826*MAD ~ sd). + if (hrMinNight != null && hrMinLong != null) { + final mad = madOf(rhrHistory, hrMinLong); + final sd = mad != null && mad > 0 ? 1.4826 * mad : 3.0; // floor: 3 bpm + final z = (hrMinNight! - hrMinLong!) / sd; + if (z.abs() > atypicalZThreshold) atypicalNight = true; + } + } + + double? skinTempMedian7d; + double? skinTempMedianLong; + double? enmoMedian7d; + double? enmoMedianLong; + // ENMO medians use >= 0 as valid (a measured still night is a real 0); + // the -1 absent sentinel is excluded. + double? medianOfEnmo(List xs) { + final v = xs.where((x) => x.isFinite && x >= 0).toList()..sort(); + if (v.isEmpty) return null; + return v[v.length ~/ 2]; + } + /// Effective thermal baseline: blend nightly with 7d rolling when both exist. + double? get effectiveSkinTempMedian => + skinTempMedian7d != null && skinTempMedianNight != null + ? (skinTempMedianNight! + skinTempMedian7d!) / 2 + : (skinTempMedianNight ?? skinTempMedian7d); + /// Effective motion baseline: blend nightly (stable core) with 7d rolling. + double? get effectiveEnmoMedian => + enmoMedian7d != null && enmoMedianNight != null + ? (enmoMedianNight! + enmoMedian7d!) / 2 + : (enmoMedianNight ?? enmoMedian7d); + + /// Effective HR baseline for the emission: blend nightly with the 7-day + /// rolling level when both exist (half/half) — the night's own physiology + /// anchors it, the rolling window keeps single odd nights from dominating. + double? get effectiveHrMin => + hrMin7d != null && hrMinNight != null ? (hrMinNight! + hrMin7d!) / 2 : (hrMinNight ?? hrMin7d); + double? get effectiveRmssdMean => + rmssdMean7d != null && rmssdMeanNight != null ? (rmssdMeanNight! + rmssdMean7d!) / 2 : (rmssdMeanNight ?? rmssdMean7d); + + // The same plausibility window decides which samples form the baseline and + // which observations get scored. A value outside it (a 250 bpm spike, a NaN + // from a dropped beat) is a measurement fault, not physiology, and scoring it + // would push the decoder towards wake. + static bool _plausibleHr(double? h) => h != null && h.isFinite && h > 30.0 && h < 200.0; + static bool _plausibleRmssd(double? r) => r != null && r.isFinite && r > 0.0 && r < 500.0; + static double _sigmoid(double x, double center, double scale) => + 1.0 / (1.0 + exp(-(x - center) / scale)); + + void computeBaseline(List> first120minData) { + if (first120minData.isEmpty) return; + final hrs = first120minData + .map((d) => (d['hr'] as num?)?.toDouble()) + .where(_plausibleHr) + .cast() + .toList(); + final rmssds = first120minData + .map((d) => (d['rmssd'] as num?)?.toDouble()) + .where(_plausibleRmssd) + .cast() + .toList(); + final enmos = first120minData + .map((d) => (d['enmo'] as num?)?.toDouble() ?? -1.0) + .where((e) => e.isFinite && e >= 0.0) + .toList(); + final temps = first120minData + .map((d) => (d['skin_temp'] as num?)?.toDouble() ?? 0.0) + .where((t) => t.isFinite && t > 0.0) + .toList(); + + if (hrs.isNotEmpty) { + hrs.sort(); + hrMinNight = hrs[(hrs.length * 0.1).floor()]; + } + if (rmssds.isNotEmpty) rmssdMeanNight = rmssds.reduce((a, b) => a + b) / rmssds.length; + if (enmos.isNotEmpty) { + enmos.sort(); + enmoMedianNight = enmos[enmos.length ~/ 2]; + } + if (temps.isNotEmpty) { + temps.sort(); + skinTempMedianNight = temps[temps.length ~/ 2]; + } + } + + /// Motion baseline from the STABLE CORE of the night — the + /// middle 50 % by elapsed time — NOT the first 120 min. The opening window + /// contains settling-in / turning over and is systematically more active, so + /// a median taken from it would overstate the person's normal sleep motion + /// and the wake-vs-arousal threshold would sit too high. HR/RMSSD keep the + /// opening-window baseline (autonomic settling is exactly what we want there), + /// and so does skin temperature ([computeBaseline] sets it). + /// Missing ENMO (0 sentinel) is excluded — never "still". + void computeMotionBaseline(List> nightData) { + if (nightData.length < 10) return; + final t0 = (nightData.first['ts'] as num?)?.toDouble() ?? 0.0; + final t1 = (nightData.last['ts'] as num?)?.toDouble() ?? t0; + final span = t1 - t0; + if (span <= 0) return; + final core = nightData + .where((e) => ((e['ts'] as num?)?.toDouble() ?? 0.0) - t0 > span * 0.25 && + ((e['ts'] as num?)?.toDouble() ?? 0.0) - t0 < span * 0.75) + .toList(); + if (core.isEmpty) return; + final enmos = core + .map((d) => (d['enmo'] as num?)?.toDouble() ?? -1.0) + .where((e) => e.isFinite && e >= 0.0) + .toList(); + if (enmos.isNotEmpty) { + enmos.sort(); + enmoMedianNight = enmos[enmos.length ~/ 2]; + } + } + + /// HYBRID emission over ALL available channels: HR, RMSSD, ENMO (motion), + /// skin temp. Absent channels (0 sentinel) contribute a NEUTRAL factor — + /// never evidence. Wake is boosted by ANY of: elevated HR + low RMSSD + /// (autonomic arousal), high movement, warm skin (peripheral vasodilation + /// on arousal); sleep states are penalised by movement. + double _getEmissionLogProb(SleepState state, double hr, double rmssd, { + double enmo = -1.0, // -1 = absent (see viterbiDecode) + double skinTemp = 0.0, + bool isContactLost = false, + }) { + if (isContactLost || + effectiveHrMin == null || + effectiveRmssdMean == null) { + return log(0.25); + } + if (!_plausibleHr(hr) || !_plausibleRmssd(rmssd)) { + // HR/RR unusable: motion-only evidence. A clearly moving wrist cannot + // be deep sleep. If motion is ALSO absent (0 sentinel), the epoch is + // fully unmeasured — return the uniform neutral for every state so the + // gap cannot bias adjacent measured epochs towards wake (CodeRabbit #756). + if (enmo < 0) return log(0.25); // absent sentinel: fully unmeasured epoch + final motionWake = _sigmoid(enmo, 0.08, 0.03); + switch (state) { + case SleepState.wake: + return log(max(0.01, 0.2 + 0.8 * motionWake)); + case SleepState.nremDeep: + return log(max(0.01, 0.1 * (1.0 - motionWake))); + case SleepState.rem: + return log(max(0.01, 0.15 * (1.0 - motionWake))); + case SleepState.nremLight: + return log(max(0.01, 0.3)); + } + } + + final hrNorm = hr / effectiveHrMin!; + final rmssdNorm = rmssd / effectiveRmssdMean!; + // Motion relative to the night's own median (robust for restless + // sleepers). Absent ENMO (0 sentinel) = NO evidence either way: wake + // factor 0 (does not boost wake), sleep factor 1.0 (does not penalise). + // A measured-zero baseline (very still night) is real: floor the divisor so + // movement still scores instead of silently disabling the channel. + final motionMeasured = enmo >= 0 && effectiveEnmoMedian != null; + final motion = + motionMeasured ? enmo / max(effectiveEnmoMedian!, 0.005) : 1.0; + final motionWake = motionMeasured ? _sigmoid(motion, 5.0, 1.0) : 0.0; + final motionSleep = motionMeasured ? 1.0 - _sigmoid(motion, 4.0, 1.5) : 1.0; + // Skin temp vs the night median, normalised so ~100 centi-°C = 1 °C. + // Absent temp = no thermal evidence (excluded from the noisy-OR, i.e. + // factor 0, never the 0.5 that would silently boost wake) — CodeRabbit #756. + final tempMeasured = skinTemp > 0 && effectiveSkinTempMedian != null; + final tempWake = tempMeasured + ? _sigmoid((skinTemp - effectiveSkinTempMedian!) / 100.0, 1.5, 0.3) + : 0.0; + + double prob; + switch (state) { + case SleepState.wake: + // Noisy-OR over autonomic, motion and thermal arousal cues — any one + // strong cue can carry wake, so brief awakenings with movement but + // barely-elevated HR are still caught. + final autonomic = + _sigmoid(hrNorm, 1.15, 0.05) * (1.0 - _sigmoid(rmssdNorm, 0.8, 0.05)); + // Weak HR-only bonus for "quiet wakefulness" (issue #593 pattern: + // lying still, HR clearly above the personal sleep baseline, RMSSD + // barely moved). Capped at 0.5 and centred high (1.25×) so REM — + // whose HR/HRV is wake-like (Zemaityte et al.) — is not flipped to + // wake; REM's own emission + self-transition guard it instead. + final hrOnlyWake = _sigmoid(hrNorm, 1.25, 0.04) * 0.5; + prob = 1.0 - + (1.0 - autonomic) * + (1.0 - motionWake) * + (1.0 - tempWake) * + (1.0 - hrOnlyWake); + break; + case SleepState.nremDeep: + prob = (1.0 - _sigmoid(hrNorm, 1.0, 0.05)) * + _sigmoid(rmssdNorm, 1.1, 0.05) * + motionSleep; + break; + case SleepState.rem: + prob = _sigmoid(hrNorm, 1.05, 0.05) * + _sigmoid(rmssdNorm, 1.0, 0.05) * + (0.5 + 0.5 * motionSleep); + break; + case SleepState.nremLight: + prob = 0.4 * (0.5 + 0.5 * motionSleep); + break; + } + return log(max(0.01, prob)); + } + + /// True when the emission carries no information at all (contact lost, no + /// baseline, or no usable HR/RR AND no motion). + bool _unmeasured(double hr, double rmssd, double enmo, bool contactLost) => + contactLost || + effectiveHrMin == null || + effectiveRmssdMean == null || + ((!_plausibleHr(hr) || !_plausibleRmssd(rmssd)) && enmo < 0); + + List viterbiDecode(List> nightData) { + if (nightData.isEmpty) return []; + + final T = nightData.length; + final states = HMMConfig.states; + final N = states.length; + + final viterbi = List.generate(T, (_) => List.filled(N, double.negativeInfinity)); + final backpointer = List.generate(T, (_) => List.filled(N, 0)); + + for (var i = 0; i < N; i++) { + final state = states[i]; + final hr = (nightData[0]['hr'] as num?)?.toDouble() ?? 0.0; + final rmssd = (nightData[0]['rmssd'] as num?)?.toDouble() ?? 0.0; + final initProb = (state == SleepState.wake || state == SleepState.nremLight) ? log(0.5) : double.negativeInfinity; + viterbi[0][i] = initProb + + _getEmissionLogProb(state, hr, rmssd, + enmo: (nightData[0]['enmo'] as num?)?.toDouble() ?? -1.0, + skinTemp: (nightData[0]['skin_temp'] as num?)?.toDouble() ?? 0.0, + isContactLost: nightData[0]['isContactLost'] == true); + } + + for (var t = 1; t < T; t++) { + final hr = (nightData[t]['hr'] as num?)?.toDouble() ?? 0.0; + final rmssd = (nightData[t]['rmssd'] as num?)?.toDouble() ?? 0.0; + final contactLost = nightData[t]['isContactLost'] == true; + final enmo = (nightData[t]['enmo'] as num?)?.toDouble() ?? -1.0; + final skinTemp = (nightData[t]['skin_temp'] as num?)?.toDouble() ?? 0.0; + if (_unmeasured(hr, rmssd, enmo, contactLost)) { + // A gap says nothing: hold the state. Running the sticky transition + // table over uniform emissions let wake absorb the whole gap. + for (var j = 0; j < N; j++) { + viterbi[t][j] = viterbi[t - 1][j]; + backpointer[t][j] = j; + } + continue; + } + for (var j = 0; j < N; j++) { + final nextState = states[j]; + final emission = + _getEmissionLogProb(nextState, hr, rmssd, + enmo: enmo, skinTemp: skinTemp, isContactLost: contactLost); + var maxLogProb = double.negativeInfinity; + var bestPrevState = 0; + for (var i = 0; i < N; i++) { + final prevState = states[i]; + final trans = HMMConfig.transitionLogProb[prevState]![nextState]!; + final logProb = viterbi[t - 1][i] + trans + emission; + if (logProb > maxLogProb) { + maxLogProb = logProb; + bestPrevState = i; + } + } + viterbi[t][j] = maxLogProb; + backpointer[t][j] = bestPrevState; + } + } + + var bestLastState = 0; + var maxFinalProb = double.negativeInfinity; + for (var i = 0; i < N; i++) { + if (viterbi[T - 1][i] > maxFinalProb) { + maxFinalProb = viterbi[T - 1][i]; + bestLastState = i; + } + } + // Wake and light are always reachable with finite emissions, so a path + // always exists; if one ever does not (or a score is NaN), decode + // nothing and let the caller keep the shipped labels. + if (!maxFinalProb.isFinite) return []; + + final result = List.filled(T, SleepState.nremLight); + var currentStateIndex = bestLastState; + result[T - 1] = states[currentStateIndex]; + for (var t = T - 1; t > 0; t--) { + currentStateIndex = backpointer[t][currentStateIndex]; + result[t - 1] = states[currentStateIndex]; + } + return result; + } +} diff --git a/lib/src/onehz/sleep/sleep.dart b/lib/src/onehz/sleep/sleep.dart index 34170220..c8eed15b 100644 --- a/lib/src/onehz/sleep/sleep.dart +++ b/lib/src/onehz/sleep/sleep.dart @@ -42,6 +42,7 @@ export 'cardio_stager.dart' cleanBeatDiffsInWindowForTest, windowRmssdRkForTest; export 'stage_ranges.dart'; +export 'hmm_stager.dart'; export 'cardio_row_cache.dart'; export 'epoch_rows.dart'; export 'causal_stager.dart'; diff --git a/lib/src/onehz/wellness/temp_health.dart b/lib/src/onehz/wellness/temp_health.dart index 61b81c2f..aeecc7c2 100644 --- a/lib/src/onehz/wellness/temp_health.dart +++ b/lib/src/onehz/wellness/temp_health.dart @@ -265,3 +265,57 @@ Metric> menstrualCoverline( 'RELATIVE temp ADC). NEVER a forward fertility prediction.', ); } + +/// Per-night "is this date in the SECOND HALF of the cycle it belongs to", +/// parallel to [dates]. All-false when there is nothing to count from. +/// +/// Each date walks back to its NEAREST PRECEDING logged start — NOT through +/// `getCycle`, which counts from the LAST start only and therefore produces +/// nonsense for every historical day. The cycle's length is the gap to the NEXT +/// logged start; the still-open cycle uses the median of her own completed +/// gaps (15 to 60 days), and with no completed cycle there is no length to use, +/// so the open cycle contributes nothing rather than a guess. One completed +/// cycle is enough to give the open one a length. +/// +/// A date more than 1.5 cycles past its start is a lapse in logging, not a +/// four-week luteal phase — it stops counting. +List lutealFlags(List dates, List cycleStartDates) { + final out = List.filled(dates.length, false); + // Labels parsed as UTC midnight, as in [_isNextDay]: at local midnight a + // span holding the 23 h spring-forward day floors one day short in `inDays`, + // which shortens that cycle and moves its midpoint night. + DateTime? day(String s) => DateTime.tryParse('${s}T00:00:00Z'); + final starts = [ + for (final s in cycleStartDates) + if (day(s) != null) day(s)!, + ]..sort(); + if (starts.isEmpty) return out; + + final gaps = [ + for (var i = 1; i < starts.length; i++) + starts[i].difference(starts[i - 1]).inDays.toDouble(), + ].where((g) => g >= 15 && g <= 60).toList(); + final medianGap = median(gaps); + + for (var i = 0; i < dates.length; i++) { + final d = day(dates[i]); + if (d == null) continue; + // Nearest PRECEDING start (inclusive: the start day is cycle day 1). + var si = -1; + for (var k = 0; k < starts.length; k++) { + if (starts[k].isAfter(d)) break; + si = k; + } + if (si < 0) continue; // before she ever logged — nothing to count from + final elapsed = d.difference(starts[si]).inDays; + // Closed cycle → its own measured length. Open cycle → her median, if she + // has one. + final len = si + 1 < starts.length + ? starts[si + 1].difference(starts[si]).inDays.toDouble() + : medianGap; + if (len == null || len < 15) continue; + if (elapsed > 1.5 * len) continue; // stale log, not a long luteal phase + if (elapsed >= len / 2) out[i] = true; + } + return out; +} diff --git a/test/luteal_flags_test.dart b/test/luteal_flags_test.dart new file mode 100644 index 00000000..03e89452 --- /dev/null +++ b/test/luteal_flags_test.dart @@ -0,0 +1,37 @@ +import 'package:openstrap_analytics/onehz.dart'; +import 'package:test/test.dart'; + +void main() { + test('nothing logged: nothing is luteal', () { + expect(lutealFlags(['2026-01-10', '2026-01-20'], []), [false, false]); + }); + + test('second half of a closed 28-day cycle counts, first half does not', () { + final flags = lutealFlags( + ['2026-01-02', '2026-01-14', '2026-01-15', '2026-01-28'], + ['2026-01-01', '2026-01-29']); + expect(flags, [false, false, true, true]); + }); + + test('before the first logged start counts for nothing', () { + expect(lutealFlags(['2025-12-20'], ['2026-01-01', '2026-01-29']), [false]); + }); + + test('an open cycle uses her median gap, and a stale log stops counting', () { + final starts = ['2026-01-01', '2026-01-29', '2026-02-26']; + // open cycle from 02-26, median gap 28: 13 days in is not luteal, 14 is, + // and 48 days in (past 1.5 cycles) is a lapse in logging + expect(lutealFlags(['2026-03-11', '2026-03-12', '2026-04-15'], starts), + [false, true, false]); + }); + + test('one logged start has no length to count with', () { + expect(lutealFlags(['2026-02-20'], ['2026-01-01']), [false]); + }); + + test('one completed cycle is enough to give the open cycle a length', () { + // starts 01-01 and 01-29: one 28-day gap, open cycle from 01-29 + expect(lutealFlags(['2026-02-11', '2026-02-12'], ['2026-01-01', '2026-01-29']), + [false, true]); + }); +} diff --git a/test/sleep_hmm_aasm_wake_test.dart b/test/sleep_hmm_aasm_wake_test.dart new file mode 100644 index 00000000..699f864c --- /dev/null +++ b/test/sleep_hmm_aasm_wake_test.dart @@ -0,0 +1,90 @@ +import 'package:openstrap_analytics/onehz.dart'; +import 'package:test/test.dart'; + +Map epoch(double hr, double rmssd, {double enmo = 0.0, double skinTemp = 0.0}) => + {'hr': hr, 'rmssd': rmssd, 'enmo': enmo, 'skin_temp': skinTemp}; + +SleepStageAnalyzer baselined() { + final a = SleepStageAnalyzer(); + a.computeBaseline(List.generate(7200, (_) => epoch(55, 45, enmo: 0.01, skinTemp: 3350))); + return a; +} + +/// Mirror of the AASM 15 s minimum-wake-run rule applied in _hmmRestage. +List applyAasmWakeRule(List labels, {int minWakeRunSec = 15}) { + final merged = [...labels]; + var i = 0; + while (i < merged.length) { + if (merged[i] != 'wake') { + i++; + continue; + } + var j = i; + while (j < merged.length && merged[j] == 'wake') { + j++; + } + if (j - i < minWakeRunSec) { + final surrounding = i > 0 ? merged[i - 1] : 'light'; + for (var k = i; k < j; k++) { + merged[k] = surrounding == 'unobserved' ? 'light' : surrounding; + } + } + i = j; + } + return merged; +} + +void main() { + test('AASM: short wake run (<15 s) is an arousal, reverts to sleep stage', () { + // 10 s of wake between deep phases must NOT stay wake (AASM: arousal + // >=3 s but < half a 30 s epoch is not stage W). + final a = baselined(); + final night = >[ + ...List.generate(30, (_) => epoch(53, 50, enmo: 0.005, skinTemp: 3350)), // deep + ...List.generate(10, (_) => epoch(70, 25, enmo: 0.3, skinTemp: 3450)), // brief arousal + ...List.generate(30, (_) => epoch(53, 50, enmo: 0.005, skinTemp: 3350)), // deep + ]; + final states = a.viterbiDecode(night); + final labels = states.map((s) => switch (s) { + SleepState.wake => 'wake', + SleepState.nremLight => 'light', + SleepState.nremDeep => 'deep', + SleepState.rem => 'rem', + }).toList(); + final cleaned = applyAasmWakeRule(labels); + expect(cleaned.sublist(30, 40).where((l) => l == 'wake').length, 0, + reason: 'a 10 s wake burst is an arousal, not wake'); + }); + + test('AASM: long wake run (>=15 s) stays wake', () { + final a = baselined(); + final night = >[ + ...List.generate(30, (_) => epoch(53, 50, enmo: 0.005, skinTemp: 3350)), + ...List.generate(60, (_) => epoch(85, 15, enmo: 0.4, skinTemp: 3500)), // real awakening + ...List.generate(30, (_) => epoch(53, 50, enmo: 0.005, skinTemp: 3350)), + ]; + final states = a.viterbiDecode(night); + final labels = states.map((s) => switch (s) { + SleepState.wake => 'wake', + SleepState.nremLight => 'light', + SleepState.nremDeep => 'deep', + SleepState.rem => 'rem', + }).toList(); + final cleaned = applyAasmWakeRule(labels); + expect(cleaned.sublist(30, 90).where((l) => l == 'wake').length, greaterThan(30), + reason: 'a sustained awakening must remain wake (counts to WASO)'); + }); + + test('AASM: staging continues after wake — no restart (sleep resumes)', () { + final a = baselined(); + final night = >[ + ...List.generate(30, (_) => epoch(53, 50, enmo: 0.005, skinTemp: 3350)), + ...List.generate(30, (_) => epoch(85, 15, enmo: 0.4, skinTemp: 3500)), + ...List.generate(30, (_) => epoch(53, 50, enmo: 0.005, skinTemp: 3350)), + ]; + final states = a.viterbiDecode(night); + // After the wake block the decoder resumes sleep (via light, wake->deep is + // a 0.5% transition) instead of staying latched in wake. + expect(states[75], isNot(SleepState.wake)); + }); +}