Class BatchPredictor

Batch regression block that consumes data collected in a TriggerBuffer. Training is not automatic — the UI calls TrainFromBuffer() explicitly, at which point the model is fit on every segment currently held in dB. Predictions on incoming features run continuously while IsTraining is false.

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: regression model, input dimension, output dimension, regularization, RFF kernel width, RFF feature count. Features must have 8 values and training targets 2 values. TrainingBuffer must be a TriggerBuffer. Provide both inputs through JSON; the editor currently declares only one input.

{
  "BatchPredictor": {
    "Type": "batchpredictor",
    "Inputs": ["Features", "TrainingBuffer"],
    "Params": ["Ridge", 8, 2, 1.0, 1.0, 300]
  }
}

How Type, Inputs and Params work · Block catalogue and parameter reference

Inheritance
BatchPredictor
Namespace: MOSAIC.Models.MachineLearning
Assembly: MOSAIC.dll
Syntax
public sealed class BatchPredictor : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks

Mirrors the workflow of PyPredictorRegression: the Buffer is stored as a reference when it publishes (sender-type sniffing in OnReceive(object, object)), an OnBufferSegmentAdded event tells the ViewModel that a new segment is available, and training is gated behind a deliberate UI action. Reuses the regressor primitives in MOSAIC.Components.MachineLearning directly — sibling to IncrementalPredictor, not a subclass.

Targets shape: the buffer stores one target row per data row in a MathNet.Numerics.LinearAlgebra.Matrix<T>. For the classic single-vector Trigger block all rows are identical; for a streaming trigger (per-tick continuous label source) rows vary across the segment. TrainFromBuffer() reads targets.Row(r) when training so both behaviours are supported transparently.

Constructors

BatchPredictor(string, double, int, int)

Declaration
public BatchPredictor(string name, double desiredRate, int inputDim, int outputDim)
Parameters
Type Name Description
string name
double desiredRate
int inputDim
int outputDim

BatchPredictor(string, double, int, int, RegressorType, RegressorConfig)

Declaration
public BatchPredictor(string name, double desiredRate, int inputDim, int outputDim, RegressorType modelType, RegressorConfig config)
Parameters
Type Name Description
string name
double desiredRate
int inputDim
int outputDim
RegressorType modelType
RegressorConfig config

BatchPredictor(string, double, int, int, RegressorType, RegressorConfig, string?)

Declaration
public BatchPredictor(string name, double desiredRate, int inputDim, int outputDim, RegressorType modelType, RegressorConfig config, string? savePath)
Parameters
Type Name Description
string name
double desiredRate
int inputDim
int outputDim
RegressorType modelType
RegressorConfig config
string savePath

Fields

_confidence

Raw confidence value from the regressor for the latest prediction.

Declaration
[ObservableProperty]
private double _confidence
Field Value
Type Description
double

_currentTargetName

Human-readable label of the most recently trained segment.

Declaration
[ObservableProperty]
private string _currentTargetName
Field Value
Type Description
string

_isTrained

True after the first successful TrainFromBuffer() call.

Declaration
[ObservableProperty]
private bool _isTrained
Field Value
Type Description
bool

_isTraining

True while TrainFromBuffer() is running. Predictions are skipped during this window.

Declaration
[ObservableProperty]
private bool _isTraining
Field Value
Type Description
bool

_lastPrediction

Most recent prediction vector.

Declaration
private Vector<double>? _lastPrediction
Field Value
Type Description
Vector<double>

_lastVizFeedTicks

Timestamp of last viz feed for stall detection.

Declaration
private long _lastVizFeedTicks
Field Value
Type Description
long

_model

The active regression model.

Declaration
private IRegressor _model
Field Value
Type Description
IRegressor

_resetBeforeTraining

When true, TrainFromBuffer() resets the regressor before fitting. Default false — additive across multiple Train clicks.

Declaration
[ObservableProperty]
private bool _resetBeforeTraining
Field Value
Type Description
bool

_sampleCount

Total training samples processed across all TrainFromBuffer() calls.

Declaration
[ObservableProperty]
private int _sampleCount
Field Value
Type Description
int

_savePath

Directory for model auto-save (from Path).

Declaration
private readonly string? _savePath
Field Value
Type Description
string

_segmentsProcessed

Total Buffer segments trained on across all TrainFromBuffer() calls.

Declaration
[ObservableProperty]
private int _segmentsProcessed
Field Value
Type Description
int

Properties

Buffer

Reference to the upstream TriggerBuffer. Set automatically the first time the buffer publishes (OnReceive(object, object) identifies it by sender type).

Declaration
public TriggerBuffer? Buffer { get; set; }
Property Value
Type Description
TriggerBuffer

Confidence

Raw confidence value from the regressor for the latest prediction.

Declaration
[ExcludeFromCodeCoverage]
public double Confidence { get; set; }
Property Value
Type Description
double

Config

Hyperparameters for the active regressor.

Declaration
public RegressorConfig Config { get; private set; }
Property Value
Type Description
RegressorConfig

CurrentTargetName

Human-readable label of the most recently trained segment.

Declaration
[ExcludeFromCodeCoverage]
public string CurrentTargetName { get; set; }
Property Value
Type Description
string

DumpFilePrefix

Recordings from this block are named <block>_predictions.csv.

Declaration
protected override string DumpFilePrefix { get; }
Property Value
Type Description
string
Overrides
BaseBlock.DumpFilePrefix

InputDim

Expected length of incoming feature vectors.

Declaration
public int InputDim { get; }
Property Value
Type Description
int

IsTrained

True after the first successful TrainFromBuffer() call.

Declaration
[ExcludeFromCodeCoverage]
public bool IsTrained { get; set; }
Property Value
Type Description
bool

IsTraining

True while TrainFromBuffer() is running. Predictions are skipped during this window.

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
BaseBlock.JsonTypeName

LastPrediction

Most recent prediction vector.

Declaration
public Vector<double>? LastPrediction { get; }
Property Value
Type Description
Vector<double>

ModelType

Active regression algorithm type.

Declaration
public RegressorType ModelType { get; private set; }
Property Value
Type Description
RegressorType

OutputDim

Number of prediction output dimensions.

Declaration
public int OutputDim { get; }
Property Value
Type Description
int

ResetBeforeTraining

When true, TrainFromBuffer() resets the regressor before fitting. Default false — additive across multiple Train clicks.

Declaration
[ExcludeFromCodeCoverage]
public bool ResetBeforeTraining { get; set; }
Property Value
Type Description
bool

SampleCount

Total training samples processed across all TrainFromBuffer() calls.

Declaration
[ExcludeFromCodeCoverage]
public int SampleCount { get; set; }
Property Value
Type Description
int

SavePath

Save directory for model checkpoints (from Path).

Declaration
public string? SavePath { get; }
Property Value
Type Description
string

SegmentsProcessed

Total Buffer segments trained on across all TrainFromBuffer() calls.

Declaration
[ExcludeFromCodeCoverage]
public int SegmentsProcessed { get; set; }
Property Value
Type Description
int

Visualization

This block's visualization, or null when it has none.

Declaration
protected override BlockVisualization? Visualization { get; }
Property Value
Type Description
BlockVisualization
Overrides
BaseBlock.Visualization
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

Visualization bundle (Scope/Spider/Heatmap). Auto-initialized — bind a VisualizationPanel.Source directly to BatchPredictor.Viz from XAML.

Declaration
public BlockVisualization Viz { get; }
Property Value
Type Description
BlockVisualization

Methods

AutoSave()

Declaration
public void AutoSave()

ChangeModel(RegressorType, RegressorConfig?)

Declaration
public void ChangeModel(RegressorType type, RegressorConfig? config = null)
Parameters
Type Name Description
RegressorType type
RegressorConfig config

ConfigureInput(IServiceProvider, JsonModel)

Declaration
public static BatchPredictor ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
Type Name Description
IServiceProvider sp
JsonModel m
Returns
Type Description
BatchPredictor

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Declaration
public override void Dispose()
Overrides
BaseBlock.Dispose()

GetDefaultSavePath()

Declaration
private string? GetDefaultSavePath()
Returns
Type Description
string

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 ConfigureInput reads them, or null when the block takes no parameters.

Overrides
BaseBlock.GetJsonParams()

GetJsonPath()

Overrides what this block writes to Path.

Declaration
protected override string? GetJsonPath()
Returns
Type Description
string

The path to export, or null to fall back to the block's own recording folder.

Overrides
BaseBlock.GetJsonPath()
Remarks

Only for blocks that read Path as something other than a recording folder — a predictor's model directory, say. Such a block also overrides PathIsDumpFolder; without this override it would export null and saving the pipeline would silently drop the directory.

HandleFeatureInput(Vector<double>)

Declaration
private void HandleFeatureInput(Vector<double> x)
Parameters
Type Name Description
Vector<double> x

HandleFeatureInputSilent(Vector<double>)

Declaration
private void HandleFeatureInputSilent(Vector<double> x)
Parameters
Type Name Description
Vector<double> x

LoadModel(string)

Declaration
public void LoadModel(string path)
Parameters
Type Name Description
string path

OnConfidenceChanged(double)

Executes the logic for when Confidence just changed.

Declaration
private void OnConfidenceChanged(double value)
Parameters
Type Name Description
double value

The new property value that was set.

Remarks

This method is invoked right after the value of Confidence is changed.

OnConfidenceChanged(double, double)

Executes the logic for when Confidence just changed.

Declaration
private void OnConfidenceChanged(double oldValue, double newValue)
Parameters
Type Name Description
double oldValue

The previous property value that was replaced.

double newValue

The new property value that was set.

Remarks

This method is invoked right after the value of Confidence is changed.

OnConfidenceChanging(double)

Executes the logic for when Confidence is changing.

Declaration
private void OnConfidenceChanging(double value)
Parameters
Type Name Description
double value

The new property value being set.

Remarks

This method is invoked right before the value of Confidence is changed.

OnConfidenceChanging(double, double)

Executes the logic for when Confidence is changing.

Declaration
private void OnConfidenceChanging(double oldValue, double newValue)
Parameters
Type Name Description
double oldValue

The previous property value that is being replaced.

double newValue

The new property value being set.

Remarks

This method is invoked right before the value of Confidence is changed.

OnCurrentTargetNameChanged(string)

Executes the logic for when CurrentTargetName just changed.

Declaration
private void OnCurrentTargetNameChanged(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 CurrentTargetName is changed.

OnCurrentTargetNameChanged(string?, string)

Executes the logic for when CurrentTargetName just changed.

Declaration
private void OnCurrentTargetNameChanged(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 CurrentTargetName is changed.

OnCurrentTargetNameChanging(string)

Executes the logic for when CurrentTargetName is changing.

Declaration
private void OnCurrentTargetNameChanging(string value)
Parameters
Type Name Description
string value

The new property value being set.

Remarks

This method is invoked right before the value of CurrentTargetName is changed.

OnCurrentTargetNameChanging(string?, string)

Executes the logic for when CurrentTargetName is changing.

Declaration
private void OnCurrentTargetNameChanging(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 CurrentTargetName is changed.

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.

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
BaseBlock.OnReceive(object, object)
Remarks

Called on the dispatching thread, not the UI thread.

OnResetBeforeTrainingChanged(bool)

Executes the logic for when ResetBeforeTraining just changed.

Declaration
private void OnResetBeforeTrainingChanged(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 ResetBeforeTraining is changed.

OnResetBeforeTrainingChanged(bool, bool)

Executes the logic for when ResetBeforeTraining just changed.

Declaration
private void OnResetBeforeTrainingChanged(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 ResetBeforeTraining is changed.

OnResetBeforeTrainingChanging(bool)

Executes the logic for when ResetBeforeTraining is changing.

Declaration
private void OnResetBeforeTrainingChanging(bool value)
Parameters
Type Name Description
bool value

The new property value being set.

Remarks

This method is invoked right before the value of ResetBeforeTraining is changed.

OnResetBeforeTrainingChanging(bool, bool)

Executes the logic for when ResetBeforeTraining is changing.

Declaration
private void OnResetBeforeTrainingChanging(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 ResetBeforeTraining is changed.

OnSampleCountChanged(int)

Executes the logic for when SampleCount just changed.

Declaration
private void OnSampleCountChanged(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 SampleCount is changed.

OnSampleCountChanged(int, int)

Executes the logic for when SampleCount just changed.

Declaration
private void OnSampleCountChanged(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 SampleCount is changed.

OnSampleCountChanging(int)

Executes the logic for when SampleCount is changing.

Declaration
private void OnSampleCountChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of SampleCount is changed.

OnSampleCountChanging(int, int)

Executes the logic for when SampleCount is changing.

Declaration
private void OnSampleCountChanging(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 SampleCount is changed.

OnSegmentsProcessedChanged(int)

Executes the logic for when SegmentsProcessed just changed.

Declaration
private void OnSegmentsProcessedChanged(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 SegmentsProcessed is changed.

OnSegmentsProcessedChanged(int, int)

Executes the logic for when SegmentsProcessed just changed.

Declaration
private void OnSegmentsProcessedChanged(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 SegmentsProcessed is changed.

OnSegmentsProcessedChanging(int)

Executes the logic for when SegmentsProcessed is changing.

Declaration
private void OnSegmentsProcessedChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of SegmentsProcessed is changed.

OnSegmentsProcessedChanging(int, int)

Executes the logic for when SegmentsProcessed is changing.

Declaration
private void OnSegmentsProcessedChanging(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 SegmentsProcessed is changed.

ResetModel()

Declaration
public void ResetModel()

SaveModel(string?)

Declaration
public void SaveModel(string? path = null)
Parameters
Type Name Description
string path

TrainFromBuffer()

Trains the regressor on every segment currently held in the upstream dB. UI-driven — nothing in the data path triggers this.

Declaration
public void TrainFromBuffer()
Remarks

With the buffer's new per-row target shape, each data row r gets paired with targets.Row(r) — so the regressor sees correctly aligned (feature, target) pairs regardless of whether the trigger was a single-shot Trigger (identical rows) or a continuous StreamTrigger (varying rows).

If ResetBeforeTraining is true, the model is reset (and counters cleared) before fitting begins. Otherwise this call adds to whatever the model has already learned. Predictions are paused for the duration; dimension-mismatched segments are logged and skipped without throwing.

Events

OnBufferSegmentAdded

Fired when the upstream TriggerBuffer publishes (i.e. a new segment landed).

Declaration
public event Action? OnBufferSegmentAdded
Event Type
Type Description
Action

OnPrediction

Fired after every prediction. Args: (prediction, target, confidence, sampleCount).

Declaration
public event Action<Vector<double>, Vector<double>?, double, int>? OnPrediction
Event Type
Type Description
Action<Vector<double>, Vector<double>, double, int>
Remarks

The target argument is always null for BatchPredictor.

OnTrainingStateChanged

Fired when IsTraining changes. Args: new IsTraining value.

Declaration
public event Action<bool>? OnTrainingStateChanged
Event Type
Type Description
Action<bool>

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.