Class PyPredictor
Base class for processing blocks in the MOSAIC pipeline.
JSON configuration
Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.
Block entry for a larger pipeline. Params: importable Python module name, class count, random seed, model save path. my_classifier is a placeholder for your module implementing the required Model interface. TrainingBuffer must be a TriggerBuffer. Configure Python and train before predicting.
{
"PyPredictor": {
"Type": "pypredictor",
"Inputs": ["Features", "TrainingBuffer"],
"Params": ["my_classifier", 2, 42, ""]
}
}
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.MachineLearning
Assembly: MOSAIC.dll
Syntax
public sealed class PyPredictor : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks
A BaseBlock represents one node in a processing graph. Blocks are typically created and linked based on a JSON configuration (pipeline definition).
Two Rate Properties:
| Property | Purpose |
|---|---|
| DesiredRate | Emission / display rate in Hz. Determines how often a block publishes output. Modified by windowing blocks (e.g. SlidingWindow: input rows/s divided by stride). Used for status monitoring and tick rate display. |
| SignalRate | True sample rate of the underlying signal in Hz. Used for DSP coefficient design (filter cutoffs, FFT bins, spectral resolution). Passes through windowing blocks unchanged — a SlidingWindow with stride 100 at 2000 Hz input still outputs data whose samples are at 2000 Hz, even though the windows arrive at 20 Hz. |
Rate Inheritance: Both rates propagate downstream automatically via TryInheritRate(BaseBlock). Publication rates initially fill unset values; use UpdateAndPropagateRate(double, bool) for runtime changes. Sample rates refresh downstream unless a block declares that it derives its own rate.
Publishing: The Publish(object) method is thread-safe and non-blocking. If a publish call is already in progress, the new value is dropped and the block enters a "stumbling" state reflected in Status.
Automatic CSV Logging: Every successful Publish(object) call feeds
Dumper whenever one is open. A dumper opens either because the config named a
Path — BlockFactory calls
InitDumper(IServiceProvider, string?) for every block it builds — or because IsRecording was
switched on, which writes to the shared session folder instead. Either way the file is named by
DumpFilePrefix. A block whose Path means something other than a recording
folder overrides PathIsDumpFolder.
Constructors
PyPredictor(string, double, string, int, int, string)
Declaration
public PyPredictor(string name, double desiredRate, string modulePath, int numClasses, int randomSeed = 42, string savePath = "")
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | |
| double | desiredRate | |
| string | modulePath | |
| int | numClasses | |
| int | randomSeed | |
| string | savePath |
Fields
_isTrained
Declaration
[ObservableProperty]
private bool _isTrained
Field Value
| Type | Description |
|---|---|
| bool |
_isTraining
Declaration
[ObservableProperty]
private bool _isTraining
Field Value
| Type | Description |
|---|---|
| bool |
_model
Declaration
private dynamic? _model
Field Value
| Type | Description |
|---|---|
| dynamic |
_modelDescription
Declaration
[ObservableProperty]
private string _modelDescription
Field Value
| Type | Description |
|---|---|
| string |
_modulePath
Declaration
[ObservableProperty]
private string _modulePath
Field Value
| Type | Description |
|---|---|
| string |
_np
Declaration
private dynamic? _np
Field Value
| Type | Description |
|---|---|
| dynamic |
_numClasses
Declaration
[ObservableProperty]
private int _numClasses
Field Value
| Type | Description |
|---|---|
| int |
_predictionCount
Declaration
[ObservableProperty]
private long _predictionCount
Field Value
| Type | Description |
|---|---|
| long |
_randomSeed
Declaration
[ObservableProperty]
private int _randomSeed
Field Value
| Type | Description |
|---|---|
| int |
_savePath
Declaration
private readonly string _savePath
Field Value
| Type | Description |
|---|---|
| string |
Properties
Buffer
Declaration
public TriggerBuffer? Buffer { get; set; }
Property Value
| Type | Description |
|---|---|
| TriggerBuffer |
DumpFilePrefix
Recordings from this block are named <block>_predictions.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
IsTrained
Declaration
[ExcludeFromCodeCoverage]
public bool IsTrained { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
IsTraining
Declaration
[ExcludeFromCodeCoverage]
public bool IsTraining { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
JsonTypeName
Block type identifier for JSON serialization. Override for a custom name.
Declaration
protected override string JsonTypeName { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
MaxInputs
Needs exactly two inputs (e.g. a data stream + a Trigger/TriggerBuffer).
Declaration
public override int MaxInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
MinInputs
Needs exactly two inputs (e.g. a data stream + a Trigger/TriggerBuffer).
Declaration
public override int MinInputs { get; }
Property Value
| Type | Description |
|---|---|
| int |
Overrides
ModelDescription
Declaration
[ExcludeFromCodeCoverage]
public string ModelDescription { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
ModulePath
Declaration
[ExcludeFromCodeCoverage]
public string ModulePath { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
NumClasses
Declaration
[ExcludeFromCodeCoverage]
public int NumClasses { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
PredictionCount
Declaration
[ExcludeFromCodeCoverage]
public long PredictionCount { get; set; }
Property Value
| Type | Description |
|---|---|
| long |
RandomSeed
Declaration
[ExcludeFromCodeCoverage]
public int RandomSeed { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
SavePath
Declaration
public string SavePath { get; }
Property Value
| Type | Description |
|---|---|
| string |
Visualization
This block's visualization, or null when it has none.
Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Overrides
Remarks
Blocks own their scope as a public Viz property rather than inheriting one, so the
base class has no way to reach it. Overriding this is what lets the rate below be wired up
automatically. With correct publication metadata, the bundle derives the time axis for
one vector per publication or fixed-size contiguous sample packets from the feed shape.
When a plot needs a rate that publication frequency and feed shape cannot express (for
example, variable-sized packets or overlapping windows), declare it with
Viz.UpdateSignalRate. An explicit declaration wins over the derived rate. A block
with several bundles must configure each one; this hook exposes only one bundle.
Viz
Declaration
public BlockVisualization Viz { get; }
Property Value
| Type | Description |
|---|---|
| BlockVisualization |
Methods
ConfigureInput(IServiceProvider, JsonModel)
Declaration
public static PyPredictor ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | sp | |
| JsonModel | m |
Returns
| Type | Description |
|---|---|
| PyPredictor |
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Declaration
public override void Dispose()
Overrides
FineTune(List<(string Label, Matrix<double> Targets, Matrix<double> Data)>, int, int, int, double, int)
Declaration
public void FineTune(List<(string Label, Matrix<double> Targets, Matrix<double> Data)> clusters, int epochs = 30, int hop = 100, int batchSize = 64, double lr = 0.0003, int oversampleFactor = 3)
Parameters
| Type | Name | Description |
|---|---|---|
| List<(string Label, Matrix<double> Targets, Matrix<double> Data)> | clusters | |
| int | epochs | |
| int | hop | |
| int | batchSize | |
| double | lr | |
| int | oversampleFactor |
GetJsonParams()
Block-specific parameters for JSON. Override to provide custom parameters.
Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
| Type | Description |
|---|---|
| IReadOnlyList<object> | The values for Params, in the order this block's |
Overrides
OnIsTrainedChanged(bool)
Executes the logic for when IsTrained just changed.
Declaration
private void OnIsTrainedChanged(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value that was set. |
Remarks
This method is invoked right after the value of IsTrained is changed.
OnIsTrainedChanged(bool, bool)
Executes the logic for when IsTrained just changed.
Declaration
private void OnIsTrainedChanged(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that was replaced. |
| bool | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of IsTrained is changed.
OnIsTrainedChanging(bool)
Executes the logic for when IsTrained is changing.
Declaration
private void OnIsTrainedChanging(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value being set. |
Remarks
This method is invoked right before the value of IsTrained is changed.
OnIsTrainedChanging(bool, bool)
Executes the logic for when IsTrained is changing.
Declaration
private void OnIsTrainedChanging(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that is being replaced. |
| bool | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of IsTrained is changed.
OnIsTrainingChanged(bool)
Executes the logic for when IsTraining just changed.
Declaration
private void OnIsTrainingChanged(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value that was set. |
Remarks
This method is invoked right after the value of IsTraining is changed.
OnIsTrainingChanged(bool, bool)
Executes the logic for when IsTraining just changed.
Declaration
private void OnIsTrainingChanged(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that was replaced. |
| bool | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of IsTraining is changed.
OnIsTrainingChanging(bool)
Executes the logic for when IsTraining is changing.
Declaration
private void OnIsTrainingChanging(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value being set. |
Remarks
This method is invoked right before the value of IsTraining is changed.
OnIsTrainingChanging(bool, bool)
Executes the logic for when IsTraining is changing.
Declaration
private void OnIsTrainingChanging(bool oldValue, bool newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | oldValue | The previous property value that is being replaced. |
| bool | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of IsTraining is changed.
OnModelDescriptionChanged(string)
Executes the logic for when ModelDescription just changed.
Declaration
private void OnModelDescriptionChanged(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The new property value that was set. |
Remarks
This method is invoked right after the value of ModelDescription is changed.
OnModelDescriptionChanged(string?, string)
Executes the logic for when ModelDescription just changed.
Declaration
private void OnModelDescriptionChanged(string? oldValue, string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | oldValue | The previous property value that was replaced. |
| string | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of ModelDescription is changed.
OnModelDescriptionChanging(string)
Executes the logic for when ModelDescription is changing.
Declaration
private void OnModelDescriptionChanging(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The new property value being set. |
Remarks
This method is invoked right before the value of ModelDescription is changed.
OnModelDescriptionChanging(string?, string)
Executes the logic for when ModelDescription is changing.
Declaration
private void OnModelDescriptionChanging(string? oldValue, string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | oldValue | The previous property value that is being replaced. |
| string | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of ModelDescription is changed.
OnModulePathChanged(string)
Executes the logic for when ModulePath just changed.
Declaration
private void OnModulePathChanged(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The new property value that was set. |
Remarks
This method is invoked right after the value of ModulePath is changed.
OnModulePathChanged(string?, string)
Executes the logic for when ModulePath just changed.
Declaration
private void OnModulePathChanged(string? oldValue, string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | oldValue | The previous property value that was replaced. |
| string | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of ModulePath is changed.
OnModulePathChanging(string)
Executes the logic for when ModulePath is changing.
Declaration
private void OnModulePathChanging(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The new property value being set. |
Remarks
This method is invoked right before the value of ModulePath is changed.
OnModulePathChanging(string?, string)
Executes the logic for when ModulePath is changing.
Declaration
private void OnModulePathChanging(string? oldValue, string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | oldValue | The previous property value that is being replaced. |
| string | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of ModulePath is changed.
OnNumClassesChanged(int)
Executes the logic for when NumClasses just changed.
Declaration
private void OnNumClassesChanged(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value that was set. |
Remarks
This method is invoked right after the value of NumClasses is changed.
OnNumClassesChanged(int, int)
Executes the logic for when NumClasses just changed.
Declaration
private void OnNumClassesChanged(int oldValue, int newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| int | oldValue | The previous property value that was replaced. |
| int | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of NumClasses is changed.
OnNumClassesChanging(int)
Executes the logic for when NumClasses is changing.
Declaration
private void OnNumClassesChanging(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value being set. |
Remarks
This method is invoked right before the value of NumClasses is changed.
OnNumClassesChanging(int, int)
Executes the logic for when NumClasses is changing.
Declaration
private void OnNumClassesChanging(int oldValue, int newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| int | oldValue | The previous property value that is being replaced. |
| int | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of NumClasses is changed.
OnPredictionCountChanged(long)
Executes the logic for when PredictionCount just changed.
Declaration
private void OnPredictionCountChanged(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value | The new property value that was set. |
Remarks
This method is invoked right after the value of PredictionCount is changed.
OnPredictionCountChanged(long, long)
Executes the logic for when PredictionCount just changed.
Declaration
private void OnPredictionCountChanged(long oldValue, long newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| long | oldValue | The previous property value that was replaced. |
| long | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of PredictionCount is changed.
OnPredictionCountChanging(long)
Executes the logic for when PredictionCount is changing.
Declaration
private void OnPredictionCountChanging(long value)
Parameters
| Type | Name | Description |
|---|---|---|
| long | value | The new property value being set. |
Remarks
This method is invoked right before the value of PredictionCount is changed.
OnPredictionCountChanging(long, long)
Executes the logic for when PredictionCount is changing.
Declaration
private void OnPredictionCountChanging(long oldValue, long newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| long | oldValue | The previous property value that is being replaced. |
| long | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of PredictionCount is changed.
OnRandomSeedChanged(int)
Executes the logic for when RandomSeed just changed.
Declaration
private void OnRandomSeedChanged(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value that was set. |
Remarks
This method is invoked right after the value of RandomSeed is changed.
OnRandomSeedChanged(int, int)
Executes the logic for when RandomSeed just changed.
Declaration
private void OnRandomSeedChanged(int oldValue, int newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| int | oldValue | The previous property value that was replaced. |
| int | newValue | The new property value that was set. |
Remarks
This method is invoked right after the value of RandomSeed is changed.
OnRandomSeedChanging(int)
Executes the logic for when RandomSeed is changing.
Declaration
private void OnRandomSeedChanging(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value being set. |
Remarks
This method is invoked right before the value of RandomSeed is changed.
OnRandomSeedChanging(int, int)
Executes the logic for when RandomSeed is changing.
Declaration
private void OnRandomSeedChanging(int oldValue, int newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| int | oldValue | The previous property value that is being replaced. |
| int | newValue | The new property value being set. |
Remarks
This method is invoked right before the value of RandomSeed is changed.
OnReceive(object, object)
Processes incoming data from an upstream block. Must be implemented by derived classes.
Declaration
protected override void OnReceive(object sender, object data)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | The publishing block, or whatever else delivered the value. |
| object | data |
Overrides
Remarks
Called on the dispatching thread, not the UI thread.
Predict(Matrix<double>)
Declaration
private void Predict(Matrix<double> inputMatrix)
Parameters
| Type | Name | Description |
|---|---|---|
| Matrix<double> | inputMatrix |
StackClusters(List<(string Label, Matrix<double> Targets, Matrix<double> Data)>)
Appends the class label (taken from each target row's first element) as the last column of each segment matrix. Because the buffer now stores one target row per data row, every output row gets the target that was active when that exact data row arrived — irrelevant for classic click-driven classification (every target row is identical) but future-proof against streaming-trigger sources that update mid-segment.
Declaration
private static Matrix<double> StackClusters(List<(string Label, Matrix<double> Targets, Matrix<double> Data)> clusters)
Parameters
| Type | Name | Description |
|---|---|---|
| List<(string Label, Matrix<double> Targets, Matrix<double> Data)> | clusters |
Returns
| Type | Description |
|---|---|
| Matrix<double> |
TrainIncremental(List<(string Label, Matrix<double> Targets, Matrix<double> Data)>, int, bool, double, bool, int, string?)
Declaration
public void TrainIncremental(List<(string Label, Matrix<double> Targets, Matrix<double> Data)> clusters, int epochs = 30, bool useDistillation = true, double replayRatio = 0.5, bool jointFinetune = true, int jointEpochs = 20, string? savePath = null)
Parameters
| Type | Name | Description |
|---|---|---|
| List<(string Label, Matrix<double> Targets, Matrix<double> Data)> | clusters | |
| int | epochs | |
| bool | useDistillation | |
| double | replayRatio | |
| bool | jointFinetune | |
| int | jointEpochs | |
| string | savePath |
UpdateDescription()
Declaration
private void UpdateDescription()
Events
OnBufferSegmentAdded
Declaration
public event Action? OnBufferSegmentAdded
Event Type
| Type | Description |
|---|---|
| Action |
OnPrediction
Declaration
public event Action<Vector<double>>? OnPrediction
Event Type
| Type | Description |
|---|---|
| Action<Vector<double>> |
OnTrainingStateChanged
Declaration
public event Action<bool>? OnTrainingStateChanged
Event Type
| Type | Description |
|---|---|
| Action<bool> |