Reference
ML pipeline
Pipeline
Pipeline
Pipeline(
app: App,
predict_hz: float = 50.0,
*,
input_streams: Sequence[str] | None = None,
)
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. | |
predict_hz | float |
Maximum predict-loop tick rate. Set to 0 or negative to remove the cap (run at full speed). | 50.0 |
input_streams | sequence of str or None |
Streams passed to | None |
Attributes
| Name | Type | Description | Default |
|---|---|---|---|
autosave_dir | str or None |
Data root to save each successfully trained model under, as
| |
model_name | str or None |
Names the model's type in its file name, such as | |
last_saved_model | str or None |
Path of the most recent successful auto-save, or |
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)extract
extract(fn: Callable) -> Callable
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
train(fn: Callable) -> Callable
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). Setting autosave_dir
also calls it automatically after successful training.
predict
predict(fn: Callable) -> Callable
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
start_training() -> None
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. When
autosave_dir and save_model are set, save under the participant
label and publish the path as last_saved_model. Completion and
failures notify the user; a failed save keeps the trained model.
start_predicting
start_predicting() -> None
Flip the state to predicting so the predict thread runs.
No-op (sets ctx.status_message) unless the state is idle or recording
and a model is loaded. A take that is open keeps recording; the pipeline writes
into it.
stop_predicting
stop_predicting() -> None
Return to idle — or to recording when a take is open — 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. A
recording is a flag beside the state (ctx.recording), so a take
can run under predicting; stop_predicting returns to
recording while one is open.
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 | Default |
|---|---|---|---|
TRAINING | |
| |
PREDICTING | |
The predict thread is calling |
Examples
>>> from myogestic.ml import PipelineState>>> PipelineState.PREDICTING.value'predicting'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. | |
path | str | Path |
Destination file. | |
controls | ControlMap | None |
Optional | None |
Examples
>>> from myogestic.ml import save_pickle>>> save_pickle({"classes": 2}, "models/demo.joblib")'models/demo.joblib'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. | |
controls | ControlMap | None |
Optional | None |
allow_unverified | bool |
Permit loading a model that carries no sidecar even though | False |
Raises
| Name | Type | Description | Default |
|---|---|---|---|
| ValueError |
If the sidecar disagrees with |
Examples
>>> from myogestic.ml import load_pickle>>> model = load_pickle("models/demo.joblib")>>> model["classes"]2Widgets
TrainButton
TrainButton(pipeline: Pipeline, *, size: tuple[float, float] = (92, 0)) -> None
Primary Train action — calls [Pipeline.start_training][] on click.
Disabled until idle with a training callback and selected takes.
size uses logical pixels and follows the UI scale.
Examples
>>> from myogestic.ml.widgets import TrainButton>>> button = TrainButton(pipeline)>>> button.ui()ui
ui() -> None
Render the Train button. Call once per frame inside @app.ui.
PredictButton
PredictButton(
pipeline: Pipeline,
*,
size: tuple[float, float] = (92, 0),
) -> None
Predict/Stop toggle reflecting the pipeline's predict state.
Enabled to start only when the state is idle or recording, a model is loaded, and
both the extract and predict callbacks are wired. Click again to stop.
size uses logical pixels and follows the UI scale.
Examples
>>> from myogestic.ml.widgets import PredictButton>>> button = PredictButton(pipeline)>>> button.ui()ui
ui() -> None
Render the Predict/Stop button. Call once per frame.
TrainingLog
TrainingLog(
pipeline: Pipeline,
*,
height: float = 100.0,
widget_id: str = 'ml',
) -> None
Read-only view of pipeline.train_log.
The popout toggle isn't drawn here — it lives on
[PipelinePanel][]'s header. height follows the UI scale.
Examples
>>> from myogestic.ml.widgets import TrainingLog>>> log = TrainingLog(pipeline)>>> log.ui()ui
ui() -> None
Render the training log. Call once per frame.
SaveModelButton
SaveModelButton(
pipeline: Pipeline,
path: str,
*,
size: tuple[float, float] = (92, 0),
) -> None
Save icon action — writes the model to path via pipeline.save_model.
Disabled unless both pipeline.save_model and pipeline.model are set.
size uses logical pixels and follows the UI scale.
Examples
>>> from myogestic.ml.widgets import SaveModelButton>>> button = SaveModelButton(pipeline, "models/demo.joblib")>>> button.ui()ui
ui() -> None
Render the Save button. Call once per frame.
LoadModelButton
LoadModelButton(
pipeline: Pipeline,
path: str,
*,
size: tuple[float, float] = (92, 0),
) -> None
Load icon action — reads a model from path via pipeline.load_model.
Disabled unless pipeline.load_model is set. size uses logical pixels
and follows the UI scale.
Examples
>>> from myogestic.ml.widgets import LoadModelButton>>> button = LoadModelButton(pipeline, "models/demo.joblib")>>> button.ui()ui
ui() -> None
Render the Load button. Call once per frame.
PipelinePanel
PipelinePanel(
pipeline: Pipeline,
*,
log_height: float = 0.0,
widget_id: str = 'ml',
) -> None
Model training, prediction controls and logs.
Shows pipeline state, prediction rate and training errors.
The header's icon opens the log in a popout, the same way
ProcessLauncher's does: a window you can move,
resize and leave open. Set log_height to show the log inline.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
pipeline | Pipeline |
The | |
log_height | float |
Logical pixel height of an optional inline log, scaled with the UI.
| 0.0 |
widget_id | str |
Unique ID for this panel, so controls and popout state stay independent. | 'ml' |
Examples
>>> from myogestic.ml.widgets import PipelinePanel>>> panel = PipelinePanel(pipeline)>>> panel.ui()ui
ui() -> None
Render the pipeline panel. Call once per frame inside @app.ui.
