BCI Studio¶
Brain-computer interface closed-loop control. Real-time neural decoding, closed-loop stimulation, and charge density safety limits.
Quick Start¶
from sc_neurocore.bci_studio.bci_primitives import BCIClosedLoopEngine
from sc_neurocore.bci_studio.bci_studio import BCIStudio
from sc_neurocore.interfaces import (
build_bci_hil_reference_manifest,
create_bci_hil_template,
)
HIL Reference Path¶
For deterministic hardware-in-the-loop prototyping, use the interface-layer template rather than the legacy studio loop:
manifest = build_bci_hil_reference_manifest("pynq_shd")
template = create_bci_hil_template("pynq_shd")
The reference path wires raw probe-like waveform windows through
WaveformCodec, AERSpikeCodec, rate decoding, feedback emission, and
DeviceTelemetry. The default sink is an implant emulator; physical PYNQ
feedback requires an explicit sink adapter and external bitstream artefacts.
Primitives¶
The primitive layer is the deterministic, auditable closed-loop path for research/HIL work:
from sc_neurocore.bci_studio.bci_primitives import (
BCIClosedLoopPrimitive,
BCIFrame,
BCIPrimitiveConfig,
)
primitive = BCIClosedLoopPrimitive(
BCIPrimitiveConfig(
channels=256,
sampling_rate_hz=30_000,
latency_budget_ms=10.0,
command_threshold_hz=75.0,
)
)
result = primitive.process_frame(BCIFrame(samples=window, reward=0.0, timestamp_us=1000))
packet = result.feedback_packet
trace = result.trace.as_dict()
The trace records schema version, frame id, input shape, spike count, active channels, score, command, latency, latency-budget status, adaptation status, and whether the optional native learning bridge was used. Feedback packets are fixed 24-byte little-endian records suitable for deterministic sink adapters.
Operational boundaries:
- This is research/HIL infrastructure, not medical-device control software.
- Physical feedback requires an explicit sink adapter and external safety case.
- The default command is bounded by
max_feedback_amplitudeand reports when clipping was applied. BCIClosedLoopEngineremains as a compatibility wrapper for older examples.
sc_neurocore.bci_studio.bci_primitives
¶
Deterministic BCI closed-loop primitives for HIL prototyping.
This module provides bounded raw-signal processing, reward-modulated adaptation, feedback packetisation, and an audit trace for each frame. It is research/HIL infrastructure, not medical-device control software.
BCIPrimitiveConfig
dataclass
¶
Configuration for the deterministic closed-loop primitive.
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
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 75 76 77 78 79 80 81 82 83 84 85 | |
BCIFrame
dataclass
¶
One raw neural signal frame.
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
88 89 90 91 92 93 94 95 96 | |
BCIFeedbackCommand
dataclass
¶
Feedback command emitted by the primitive.
Packet layout is 24 bytes: [schema:u16, command:u8, flags:u8,
channel:u16, reserved:u16, amplitude:f32, timestamp_us:u64, score:f32].
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
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 | |
BCIClosedLoopTrace
dataclass
¶
Audit trace for one processed frame.
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
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 | |
BCIPrimitiveResult
dataclass
¶
Result from one closed-loop primitive step.
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | |
BCIClosedLoopPrimitive
¶
Deterministic raw-signal to feedback primitive with audit trace.
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
BCIClosedLoopEngine
¶
Backward-compatible wrapper around :class:BCIClosedLoopPrimitive.
Source code in src/sc_neurocore/bci_studio/bci_primitives.py
| Python | |
|---|---|
394 395 396 397 398 399 400 401 402 403 404 405 406 407 | |
Studio¶
sc_neurocore.bci_studio.bci_studio
¶
BCI Studio orchestrator for real-time closed-loop brain-computer interfaces.
Pipeline: raw_ephys → codec → spike_extract → SC_decode → learner → feedback Includes SC-domain lossy compression, online STDP learning, FPGA feedback serialization, and latency profiling.
SpikeCodec
¶
SC-domain lossy compression for neural data streams.
Uses run-length encoding on spike trains with delta-time encoding.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
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 | |
encode(spikes)
¶
Compress boolean spike array to RLE byte stream.
Format: [total_len:u32_le] + N × [value:u8, count:u8]
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
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 | |
decode(data)
¶
Decompress RLE byte stream back to spike array.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
95 96 97 98 99 100 101 102 103 104 105 106 107 | |
compression_ratio(original)
¶
Return compression ratio (original_bytes / compressed_bytes).
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
109 110 111 112 113 114 | |
OnlineLearner
¶
Local STDP-inspired weight update rule (pure Python fallback).
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
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 | |
step(spikes, reward)
¶
Apply reward-modulated STDP update.
Spikes that contributed to a positive reward get potentiated; non-spiking channels get depressed toward baseline.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | |
FPGAFeedbackController
¶
Serializes BCI commands for DMA push to FPGA feedback register.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | |
serialize(command, channel=0, amplitude=1.0, timestamp_us=0.0)
¶
Pack a feedback command into a 16-byte DMA-aligned struct.
Layout: [cmd:u8, chan:u16, amp:f32, ts:f64, pad:1]
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
159 160 161 162 163 164 165 166 167 168 169 170 | |
deserialize(data)
¶
Unpack a feedback command.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
172 173 174 175 | |
LatencyProfiler
¶
Rolling window latency tracker with percentile reporting.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
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 | |
budget_met
property
¶
True if p95 latency is under 10 ms BCI hard real-time target.
BCIStudio
¶
End-to-end BCI closed-loop orchestrator.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
process_frame(raw_ephys, reward=0.0)
¶
Process a single BCI frame through the full pipeline.
Source code in src/sc_neurocore/bci_studio/bci_studio.py
| Python | |
|---|---|
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |