Sleep¶
Closed-loop sleep entrainment: circadian rhythm optimization, protocol library, session report generation, multi-objective sleep optimization, and EEG-based stage detection.
Circadian Optimizer¶
sc_neurocore.sleep.circadian_optimizer
¶
Chronotype
¶
Bases: Enum
Sleep chronotype labels (after Michael Breus' model).
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
31 32 33 34 35 36 37 | |
CircadianProfile
dataclass
¶
A chronotype's circadian parameters.
Attributes¶
chronotype : Chronotype The chronotype label. bedtime_hour : float Ideal bedtime in decimal hours (0-24, can exceed 24 for next-day). wake_hour : float Ideal wake time in decimal hours. default_protocol : str Name of the recommended sleep audio protocol. melatonin_peak_hour : float Hour at which endogenous melatonin peaks (typically ~2 h after bedtime onset for most chronotypes). core_body_temp_nadir_hour : float Hour of the core body temperature nadir (~2 h after melatonin peak).
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
CircadianOptimizer
¶
Chronotype-aware circadian rhythm optimizer.
Parameters¶
chronotype : Chronotype The user's chronotype.
Example::
opt = CircadianOptimizer(Chronotype.BEAR)
profile = opt.get_profile()
print(opt.melatonin_level(23.0)) # near-peak for a Bear
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
get_profile()
¶
Return the full circadian profile for the configured chronotype.
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
140 141 142 | |
get_sleep_window()
¶
Return (bedtime_hour, wake_hour).
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
144 145 146 | |
get_recommended_protocol()
¶
Return the default protocol name for this chronotype.
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
148 149 150 | |
is_in_sleep_window(hour)
¶
Check whether hour (0-24) falls inside the sleep window.
Handles windows that wrap past midnight (e.g. 23.5 -> 6.5).
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
152 153 154 155 156 157 158 159 160 161 162 163 164 | |
melatonin_level(hour)
¶
Estimate melatonin level at hour using a sinusoidal model.
Returns a value in [0, 1] where 1 is the peak (at
melatonin_peak_hour) and 0 is the trough (12 h offset).
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
166 167 168 169 170 171 172 173 174 175 176 | |
to_dict()
¶
Serialise the optimizer state to a plain dict.
Source code in src/sc_neurocore/sleep/circadian_optimizer.py
178 179 180 181 182 183 184 185 186 187 188 | |
Protocol Library¶
sc_neurocore.sleep.protocol_library
¶
StageAudioParams
dataclass
¶
Audio-entrainment parameters for a single sleep stage.
Attributes¶
binaural_hz : float
Binaural beat frequency (Hz).
noise_color : str
Background noise colour ("pink", "brown", "white", etc.).
base_freq_hz : float
Carrier / base tone frequency (Hz).
volume : float
Relative volume in [0, 1].
isochronic_hz : float
Isochronic pulse frequency (Hz); 0 disables.
spatial_rotation : float
Spatial audio rotation speed in degrees per second.
Source code in src/sc_neurocore/sleep/protocol_library.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
SleepProtocol
dataclass
¶
A named sleep-entrainment protocol.
Attributes¶
name : str Human-readable identifier (must match the registry key). description : str Short description of the protocol's therapeutic goal. stage_audio : Dict[SleepStage, StageAudioParams] Audio parameters keyed by target stage. stage_targets : Dict[SleepStage, float] Target fraction of total sleep time per stage (must sum to 1.0). total_duration_min : float Recommended session length in minutes.
Source code in src/sc_neurocore/sleep/protocol_library.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
get_audio_for_stage(stage)
¶
Return audio parameters for stage, falling back to WAKE params.
Source code in src/sc_neurocore/sleep/protocol_library.py
88 89 90 91 92 | |
get_target_stage(progress)
¶
Return the ideal stage for a given session progress in [0, 1].
Progress is mapped linearly through the cumulative stage-target fractions in stage order (WAKE, N1, N2, N3, REM).
Source code in src/sc_neurocore/sleep/protocol_library.py
94 95 96 97 98 99 100 101 102 103 104 105 106 | |
to_dict()
¶
Serialise the protocol to a plain dict.
Source code in src/sc_neurocore/sleep/protocol_library.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
get_protocol(name)
¶
Look up a protocol by name. Raises KeyError if not found.
Source code in src/sc_neurocore/sleep/protocol_library.py
495 496 497 | |
list_protocols()
¶
Return a sorted list of all available protocol names.
Source code in src/sc_neurocore/sleep/protocol_library.py
500 501 502 | |
Report Generator¶
sc_neurocore.sleep.report_generator
¶
SleepReport
dataclass
¶
Aggregate report for a completed sleep session.
Attributes¶
total_duration_min : float
Total session length in minutes.
sleep_onset_latency_min : float
Minutes from session start until the first non-WAKE epoch.
sleep_efficiency_pct : float
Percentage of total time spent asleep (non-WAKE).
quality_score : float
Composite quality score in [0, 100].
stage_durations_min : Dict[str, float]
Time (minutes) per stage.
stage_percentages : Dict[str, float]
Percentage of total time per stage.
stage_targets : Dict[str, float]
Protocol target percentages for comparison.
hypnogram : List[int]
Stage codes per epoch.
wakeups : int
Number of WAKE epochs that occurred after initial sleep onset.
reinductions : int
Number of re-induction sequences triggered.
recommendations : List[str]
Plain-language suggestions.
grade : str
Letter grade (A-F).
Source code in src/sc_neurocore/sleep/report_generator.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
SleepReportGenerator
¶
Generates a :class:SleepReport from a completed optimiser session.
Source code in src/sc_neurocore/sleep/report_generator.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
generate(optimizer)
staticmethod
¶
Analyse optimizer's tick history and return a report.
Source code in src/sc_neurocore/sleep/report_generator.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
Sleep Optimizer¶
sc_neurocore.sleep.sleep_optimizer
¶
SleepOptimizerConfig
dataclass
¶
Tuneable knobs for the optimiser loop.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
32 33 34 35 36 37 38 39 | |
SleepTick
dataclass
¶
Snapshot produced every stage_check_interval samples.
Attributes¶
tick : int Monotonic tick counter. elapsed_min : float Wall-clock minutes since session start. current_stage : SleepStage Detected stage at this tick. target_stage : SleepStage Protocol's ideal stage at this progress point. stage_match : bool Whether current == target. audio_params : StageAudioParams Audio parameters being delivered. band_powers : Dict[str, float] Most recent EEG band-power decomposition. reinduction_active : bool Whether a re-induction sequence is currently running.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
SleepOptimizer
¶
Closed-loop sleep optimiser.
Parameters¶
protocol : SleepProtocol or str The protocol instance (or its registry name) to follow. config : SleepOptimizerConfig, optional Operational parameters.
Example::
opt = SleepOptimizer("insomnia_relief")
opt.start_session()
for sample in eeg_stream:
opt.add_sample(sample)
tick = opt.check_and_adapt()
if tick is not None:
apply_audio(tick.audio_params)
report = opt.stop_session()
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
start_session()
¶
Begin a new optimisation session, resetting all state.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
135 136 137 138 139 140 141 142 143 144 | |
stop_session()
¶
End the current session and return the full tick history.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
146 147 148 149 | |
add_sample(sample)
¶
Feed a single EEG voltage sample.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
153 154 155 156 157 158 | |
add_samples(samples)
¶
Feed an array of EEG voltage samples.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
160 161 162 163 164 165 | |
check_and_adapt()
¶
Run stage detection and protocol adaptation.
Should be called after every stage_check_interval samples.
Returns a :class:SleepTick when a check is performed, or
None if the interval has not elapsed or the session is
inactive.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
get_history()
¶
Return a copy of all recorded ticks.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
231 232 233 | |
get_stage_durations()
¶
Compute time (minutes) spent in each detected stage.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
235 236 237 238 239 240 241 | |
get_hypnogram()
¶
Return the detected-stage sequence as a list of integer codes.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
243 244 245 | |
get_state()
¶
Return a summary dict of the optimiser's current state.
Source code in src/sc_neurocore/sleep/sleep_optimizer.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
Sleep Stage Detector¶
sc_neurocore.sleep.sleep_stage_detector
¶
SleepStage
¶
Bases: IntEnum
AASM sleep-stage labels.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
32 33 34 35 36 37 38 39 | |
DetectorConfig
dataclass
¶
Parameters for the sleep-stage detector.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
73 74 75 76 77 78 79 80 | |
SleepStageDetector
¶
Real-time sleep-stage detector from single-channel EEG.
Usage::
det = SleepStageDetector()
for sample in eeg_stream:
det.add_sample(sample)
stage = det.detect()
if stage is not None:
print(stage.name)
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | |
add_sample(sample)
¶
Append a single EEG voltage sample to the internal buffer.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
109 110 111 | |
add_samples(samples)
¶
Append an array of EEG voltage samples.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
113 114 115 116 | |
detect()
¶
Return the smoothed sleep-stage classification, or None if
insufficient data has been collected.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
get_band_powers()
¶
Return the most recently computed band-power dict, or None.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
134 135 136 | |
reset()
¶
Clear all internal state.
Source code in src/sc_neurocore/sleep/sleep_stage_detector.py
138 139 140 141 142 | |