ML pipeline¶
Pipeline¶
Pipeline
¶
ML lifecycle + state for an App.
Constructor registers the predict thread + cleanup on the App's hook
lists; they fire on app.run() start/exit. Decorators set the
callbacks. Transition methods flip app.ctx.state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
App
|
The myogestic App. |
required |
predict_hz
|
float
|
Maximum predict-loop tick rate. Set to 0 or negative to remove the cap (run at full speed). |
50.0
|
Examples:
>>> from myogestic import App
>>> from myogestic.ml import Pipeline
>>> pipeline = Pipeline(App("EMG demo"), predict_hz=20)
>>> @pipeline.extract
... def extract(windows):
... return windows["emg"].mean(axis=1)
Methods:
| Name | Description |
|---|---|
extract |
Decorator: register the feature-extraction callback. |
train |
Decorator: register the training callback. |
predict |
Decorator: register the predict callback. |
start_training |
Run the |
start_predicting |
Flip the state to |
stop_predicting |
Return to |
extract
¶
Decorator: register the feature-extraction callback.
The wrapped function receives windows: dict[str, np.ndarray]
keyed by stream name — each array is channels-first
(n_channels, n_samples). Return whatever shape your model
wants to consume. The same function is invoked from inside
train() (over recorded windows) and on the predict thread
(over live windows), so keep its return type stable.
train
¶
Decorator: register the training callback.
The wrapped function receives one TrainingData and
must return any object — it's stored on pipeline.model and
forwarded to every subsequent predict() call. If
pipeline.save_model is set, the Save Model button calls
it as save_model(pipeline.model, path).
predict
¶
Decorator: register the predict callback.
The wrapped function is called every 1/predict_hz seconds
with (model, features) where features is the return
value of the extract callback. Must return a
dict[str, Any] — non-dict returns are silently dropped
(the previous prediction stays in pipeline.predictions).
start_training
¶
Run the @pipeline.train callback on a worker thread.
No-op (sets ctx.status_message) unless the state is idle,
a train callback is registered, and non-empty
training_data is set. Flips the state to training for
the duration and stores the returned object on model.
start_predicting
¶
Flip the state to predicting so the predict thread runs.
No-op (sets ctx.status_message) unless the state is idle
and a model is loaded.
stop_predicting
¶
Return to idle, pausing the predict loop.
No-op (sets ctx.status_message) unless the state is currently
predicting.
PipelineState
¶
Bases: StrEnum
ML-side extension of AppState.
The core app only knows about "idle" and "recording";
attaching a Pipeline (via Pipeline(app)) adds two more
states for the ML lifecycle. Mutually exclusive with each other and
with the core states.
The enum is a StrEnum so it compares cleanly against the raw
string written to app.ctx.state by the transition methods.
Attributes:
| Name | Type | Description |
|---|---|---|
TRAINING |
|
|
PREDICTING |
The predict thread is calling |
Examples:
Persistence¶
save_pickle
¶
save_pickle(model: Any, path: str | Path, *, controls: ControlMap | None = None) -> str
Persist model to path via joblib, creating parent dirs as needed.
Returns the path as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Any
|
Any picklable object. |
required |
path
|
str | Path
|
Destination file. |
required |
controls
|
ControlMap | None
|
Optional |
None
|
Examples:
load_pickle
¶
load_pickle(path: str | Path, *, controls: ControlMap | None = None, allow_unverified: bool = False) -> Any
Inverse of save_pickle — load a joblib-saved model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
The model file. |
required |
controls
|
ControlMap | None
|
Optional |
None
|
allow_unverified
|
bool
|
Permit loading a model that carries no sidecar even though |
False
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sidecar disagrees with |
Examples:
Widgets¶
TrainButton
¶
PredictButton
¶
Predict/Stop toggle reflecting the pipeline's predict state.
Enabled to start only when the state is idle, a model is loaded, and
both the extract and predict callbacks are wired; shows a Stop button
while predicting and is disabled otherwise.
Examples:
>>> from myogestic.ml.widgets import PredictButton
>>> button = PredictButton(pipeline)
>>> button.ui()
TrainingLog
¶
Read-only view of pipeline.train_log.
The popout toggle isn't drawn here — it lives on
PipelinePanel's control row, next to Train/Predict.
Examples:
SaveModelButton
¶
LoadModelButton
¶
PipelinePanel
¶
Train + Predict + log as a single titled panel.
Matches the visual style of RecordingControls,
SessionManager, and PostProcessor.
The log lives in the ↗ popout, the same way
ProcessLauncher's does: a window you can move,
resize and leave open. Nothing is drawn inline unless you ask for a height — the panel
used to fill its whole cell with a box holding one line of output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipeline
|
Pipeline
|
The |
required |
log_height
|
float
|
Height in pixels of an optional inline log. |
0.0
|
widget_id
|
str
|
Unique ID for this panel, so two of them keep separate popout state. |
'ml'
|
Examples:
>>> from myogestic.ml.widgets import PipelinePanel
>>> panel = PipelinePanel(pipeline)
>>> panel.ui()
