Sources¶
A Source wraps a device, file, or transport behind a uniform interface. Built-in sources live here; custom sources implement the Source protocol below. See Add a custom source for the recipe.
The protocol¶
Source ¶
Bases: Protocol
Protocol every data source must implement.
Three methods, no inheritance: connect opens the device or file
and returns a :class:StreamInfo describing the data, read
polls non-blockingly for the next chunk, disconnect releases
the device. The framework's :class:Stream wraps any object
matching this Protocol and runs it on a daemon acquisition thread.
See Add a custom source for worked examples and the full contract.
connect ¶
connect() -> StreamInfo
read ¶
Poll for the next chunk of samples.
Return (data, ts) where data is sample-major
(n_samples, n_channels) and ts is (n_samples,)
float64 LSL clock timestamps. Return (None, None) if no
new data is available.
Source code in myogestic/stream.py
Built-in sources¶
LSLSource ¶
Pull samples from a Lab Streaming Layer outlet by name.
The default real-time source for MyoGestic: name the outlet you want
on your local LSL network, drop the source into a :class:Stream,
and the framework's acquisition thread handles the rest. Uses
mne_lsl under the hood.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stream_name
|
str
|
LSL outlet name to subscribe to (e.g. |
required |
dtype
|
DTypeLike | None
|
Dtype the samples are stored as (one of
:data: |
'float32'
|
Examples:
>>> from myogestic import Stream
>>> from myogestic.sources import LSLSource
>>> stream = Stream("emg", source=LSLSource("TestEMG1"),
... window_ms=1000)
>>> # keep a 16-bit amp's native format to halve memory / disk:
>>> raw = LSLSource("TestEMG1", dtype=None)
The source is non-blocking: :meth:read pulls whatever is
immediately available from the inlet and returns (None, None)
when the outlet has produced nothing new since the last call. The
acquisition thread paces itself by waiting for the inlet to fill,
so a fast spin loop is harmless.
Source code in myogestic/sources/lsl.py
connect ¶
connect() -> StreamInfo
Resolve the outlet by name and open an inlet.
Returns a :class:StreamInfo whose channel count and sample rate
come from the outlet's metadata. dtype is the value requested
at construction (default float32), or the outlet's native wire
format when constructed with dtype=None. Blocks up to 10 s
waiting for the outlet to appear on the network.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if no outlet with |
Source code in myogestic/sources/lsl.py
read ¶
Pull whatever samples are immediately available.
Non-blocking. Returns (data, timestamps) where data is
(n_samples, n_channels) in the configured dtype (default
float32) and timestamps is a 1-D float64 array of LSL clock
seconds. Returns (None, None) if the inlet hasn't been opened
or no new samples are pending.
Source code in myogestic/sources/lsl.py
disconnect ¶
discover ¶
Scan for available LSL streams on the network.
reconnect ¶
reconnect(target: str | None = None) -> StreamInfo
Reconnect to the same or a different LSL stream.
ReplaySource ¶
Replays a recorded session as if it were a live stream.
Accepts either a folder-format session or a .session.zip archive —
delegates to open_session_store so both layouts work transparently.
Source code in myogestic/sources/replay.py
connect ¶
connect() -> StreamInfo
Open the recorded session and return its :class:StreamInfo.
Raises :class:ValueError if the requested stream name is not
present in the session.
Source code in myogestic/sources/replay.py
read ¶
Return the next recorded chunk, paced to wall-clock time x speed.
Returns (None, None) when no samples are due yet; loops back
to the start of the recording once the end is reached.
Source code in myogestic/sources/replay.py
disconnect ¶
Close the session and rewind the replay position.
Closing releases the ZipStore — on Windows an open handle keeps the
.session.zip locked, so the file couldn't be deleted or re-recorded
until the source was garbage-collected.
Source code in myogestic/sources/replay.py
SerialSource¶
Opt-in: lives at myogestic.sources.serial_source.SerialSource. Direct import only (requires the serial extra for pyserial).
SerialSource ¶
Reads fixed-width binary frames from a serial port.
Each frame is n_channels float32 values (little-endian). The source self-paces via serial blocking reads; timestamps are stamped on arrival with mne_lsl.lsl.local_clock().
Source code in myogestic/sources/serial_source.py
connect ¶
connect() -> StreamInfo
Open the serial port and return the configured :class:StreamInfo.
Source code in myogestic/sources/serial_source.py
read ¶
Read one n_channels-float32 frame, timestamped on arrival.
Returns (None, None) when the port is closed or a short read
yields fewer than n_channels values.
Source code in myogestic/sources/serial_source.py
disconnect ¶
discover ¶
List available serial ports.
Source code in myogestic/sources/serial_source.py
reconnect ¶
reconnect(target: str | None = None) -> StreamInfo
Reconnect to the same or a different serial port.