Class HybridPredictorBlock

Hybrid Predictor Block - combines classifier training with continuous vector output.

Training: Like a classifier - learns from discrete class labels (action names from Trigger) Output: Continuous vector - weighted sum of class target vectors by predicted probabilities

Example: Classes defined in Trigger: rest: [0,0,0,0,0,0] power: [1,1,1,1,0,0]
pinch: [0,0,0,0,1,1]

Predicted probabilities: [0.1, 0.7, 0.2]

Output = 0.1rest + 0.7power + 0.2*pinch = [0.7, 0.7, 0.7, 0.7, 0.2, 0.2]

This gives smooth, continuous control signals for prosthesis control while training with simple discrete gesture labels.

Inputs:

  • Input[0]: Data source (feature vectors)
  • Input[1]: Trigger (provides class labels AND target vectors)

JSON Config: { "Type": "HybridPredictor", "Inputs": ["DataSource", "Trigger"], "DesiredRate": 200, "Params": ["SoftmaxRFF", 8, 0.01, 0.001, 1.0, 300], "Path": "models/" }

Params: [modelType, inputDim, learningRate?, lambda?, sigma?, featureDim?] Note: Classes are automatically learned from Trigger actions!

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: classifier model, feature dimension, learning rate, regularization, RFF kernel width, RFF feature count. Classes are discovered from Trigger action names; there is no numClasses parameter. Define an 8-value feature stream and a Trigger supplying targets.

{
  "HybridPredictor": {
    "Type": "hybridpredictor",
    "Inputs": ["Features", "Capture"],
    "Params": ["Softmax", 8, 0.01, 0.001, 1.0, 300]
  }
}

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

Inheritance
HybridPredictorBlock
Namespace: MOSAIC.Models.Learning
Assembly: MOSAIC.dll
Syntax
public sealed class HybridPredictorBlock : 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:

PropertyPurpose
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

HybridPredictorBlock(string, double, int, ClassifierType, ClassifierConfig?, string?)

Declaration
public HybridPredictorBlock(string name, double desiredRate, int inputDim, ClassifierType modelType = ClassifierType.Softmax, ClassifierConfig? config = null, string? savePath = null)
Parameters
Type Name Description
string name
double desiredRate
int inputDim
ClassifierType modelType
ClassifierConfig config
string savePath

Fields

_classIndices

Declaration
private readonly Dictionary<string, int> _classIndices
Field Value
Type Description
Dictionary<string, int>

_classNames

Declaration
private readonly List<string> _classNames
Field Value
Type Description
List<string>

_classProbabilities

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

_classVectors

Declaration
private readonly List<Vector<double>> _classVectors
Field Value
Type Description
List<Vector<double>>

_confidence

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

_currentClassName

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

_currentLabel

Declaration
private int _currentLabel
Field Value
Type Description
int

_isInitialized

Declaration
private bool _isInitialized
Field Value
Type Description
bool

_isTraining

Declaration
[ObservableProperty]
[NotifyPropertyChangedFor("TrainingStatusText")]
private bool _isTraining
Field Value
Type Description
bool

_lastOutput

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

_model

Declaration
private IClassifier _model
Field Value
Type Description
IClassifier

_predictedClassName

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

_sampleCount

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

_savePath

Declaration
private readonly string? _savePath
Field Value
Type Description
string

_trainingMessage

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

Properties

ClassNames

Declaration
public IReadOnlyList<string> ClassNames { get; }
Property Value
Type Description
IReadOnlyList<string>

ClassProbabilities

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

Confidence

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

Config

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

CurrentClassName

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

InputDim

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

IsTraining

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

LastOutput

Gets the last output vector.

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

MaxInputs

Gets the maximum number of input connections allowed for this block.

Declaration
public override int MaxInputs { get; }
Property Value
Type Description
int
Overrides
BaseBlock.MaxInputs
Remarks

Override in derived classes to specify input requirements. Default is 1 (most blocks accept exactly one input). Use MaxValue for blocks that accept unlimited inputs (e.g., Joiner).

MinInputs

Gets the minimum number of input connections required for this block.

Declaration
public override int MinInputs { get; }
Property Value
Type Description
int
Overrides
BaseBlock.MinInputs
Remarks

Override in derived classes to specify input requirements. Default is 1 (most blocks require exactly one input). Source blocks (devices, generators) should return 0.

ModelType

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

NumClasses

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

OutputDim

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

PathIsDumpFolder

Path is this block's model directory, not a CSV destination.

Declaration
protected override bool PathIsDumpFolder { get; }
Property Value
Type Description
bool
Overrides
BaseBlock.PathIsDumpFolder
Remarks

The only block in the pipeline that reads Path as something other than a recording folder without also recording there. Without this the factory would drop a CSV in among the saved model files.

PredictedClassName

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

SampleCount

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

TrainingMessage

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

TrainingStatusText

Declaration
public string TrainingStatusText { get; }
Property Value
Type Description
string

Methods

AddClass(string, Vector<double>)

Manually add a class with its target vector.

Declaration
public void AddClass(string name, Vector<double> targetVector)
Parameters
Type Name Description
string name
Vector<double> targetVector

AutoSave()

Declaration
public void AutoSave()

ChangeModel(ClassifierType, ClassifierConfig?)

Changes the model type. Resets the model but keeps learned classes.

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

ClearClasses()

Clears all learned classes and resets everything.

Declaration
public void ClearClasses()

ComputeWeightedOutput(double[])

Computes weighted sum of class vectors based on probabilities. Output = sum(prob[i] * classVector[i])

Declaration
private Vector<double> ComputeWeightedOutput(double[] probabilities)
Parameters
Type Name Description
double[] probabilities
Returns
Type Description
Vector<double>

ConfigureInput(IServiceProvider, JsonModel)

Creates block from JSON configuration.

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

Dispose()

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

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

GetClassVector(int)

Gets the target vector for a class index.

Declaration
public Vector<double>? GetClassVector(int index)
Parameters
Type Name Description
int index
Returns
Type Description
Vector<double>

GetClassVector(string)

Gets the target vector for a class.

Declaration
public Vector<double>? GetClassVector(string name)
Parameters
Type Name Description
string name
Returns
Type Description
Vector<double>

GetDefaultSavePath()

Declaration
private string? GetDefaultSavePath()
Returns
Type Description
string

GetDouble(object?, double)

Declaration
private static double GetDouble(object? obj, double defaultValue = 0)
Parameters
Type Name Description
object obj
double defaultValue
Returns
Type Description
double

GetInt(object?, int)

Declaration
private static int GetInt(object? obj, int defaultValue = 0)
Parameters
Type Name Description
object obj
int defaultValue
Returns
Type Description
int

GetJsonParams()

The six values ConfigureInput reads, in its order. Taken from the live model and config rather than the constructor arguments, so a model swapped at run time is what gets saved.

Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
Type Description
IReadOnlyList<object>
Overrides
BaseBlock.GetJsonParams()

GetJsonPath()

Puts the model directory back in the config on save.

Declaration
protected override string? GetJsonPath()
Returns
Type Description
string
Overrides
BaseBlock.GetJsonPath()
Remarks

ToJsonModel() exports GetJsonPath() ?? _explicitDumpFolder, and this block opens no dumper, so without the override both halves were null and saving a pipeline silently dropped the model directory.

GetString(object?, string)

Declaration
private static string GetString(object? obj, string defaultValue = "")
Parameters
Type Name Description
object obj
string defaultValue
Returns
Type Description
string

HandleDataInput(Vector<double>)

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

HandleTriggerInput(object, object?)

Declaration
private void HandleTriggerInput(object sender, object? data)
Parameters
Type Name Description
object sender
object data

LearnNewClass(string, Vector<double>)

Declaration
private void LearnNewClass(string name, Vector<double> targetVector)
Parameters
Type Name Description
string name
Vector<double> targetVector

LoadModel(string)

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

OnClassProbabilitiesChanged(double[])

Executes the logic for when ClassProbabilities just changed.

Declaration
private void OnClassProbabilitiesChanged(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 ClassProbabilities is changed.

OnClassProbabilitiesChanged(double[]?, double[])

Executes the logic for when ClassProbabilities just changed.

Declaration
private void OnClassProbabilitiesChanged(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 ClassProbabilities is changed.

OnClassProbabilitiesChanging(double[])

Executes the logic for when ClassProbabilities is changing.

Declaration
private void OnClassProbabilitiesChanging(double[] value)
Parameters
Type Name Description
double[] value

The new property value being set.

Remarks

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

OnClassProbabilitiesChanging(double[]?, double[])

Executes the logic for when ClassProbabilities is changing.

Declaration
private void OnClassProbabilitiesChanging(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 ClassProbabilities is changed.

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.

OnCurrentClassNameChanged(string)

Executes the logic for when CurrentClassName just changed.

Declaration
private void OnCurrentClassNameChanged(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 CurrentClassName is changed.

OnCurrentClassNameChanged(string?, string)

Executes the logic for when CurrentClassName just changed.

Declaration
private void OnCurrentClassNameChanged(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 CurrentClassName is changed.

OnCurrentClassNameChanging(string)

Executes the logic for when CurrentClassName is changing.

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

The new property value being set.

Remarks

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

OnCurrentClassNameChanging(string?, string)

Executes the logic for when CurrentClassName is changing.

Declaration
private void OnCurrentClassNameChanging(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 CurrentClassName 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.

OnPredictedClassNameChanged(string)

Executes the logic for when PredictedClassName just changed.

Declaration
private void OnPredictedClassNameChanged(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 PredictedClassName is changed.

OnPredictedClassNameChanged(string?, string)

Executes the logic for when PredictedClassName just changed.

Declaration
private void OnPredictedClassNameChanged(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 PredictedClassName is changed.

OnPredictedClassNameChanging(string)

Executes the logic for when PredictedClassName is changing.

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

The new property value being set.

Remarks

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

OnPredictedClassNameChanging(string?, string)

Executes the logic for when PredictedClassName is changing.

Declaration
private void OnPredictedClassNameChanging(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 PredictedClassName is changed.

OnReceive(object, object)

Handles incoming data from connected blocks.

Declaration
protected override void OnReceive(object sender, object data)
Parameters
Type Name Description
object sender
object data
Overrides
BaseBlock.OnReceive(object, object)

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.

OnTrainingMessageChanged(string)

Executes the logic for when TrainingMessage just changed.

Declaration
private void OnTrainingMessageChanged(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 TrainingMessage is changed.

OnTrainingMessageChanged(string?, string)

Executes the logic for when TrainingMessage just changed.

Declaration
private void OnTrainingMessageChanged(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 TrainingMessage is changed.

OnTrainingMessageChanging(string)

Executes the logic for when TrainingMessage is changing.

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

The new property value being set.

Remarks

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

OnTrainingMessageChanging(string?, string)

Executes the logic for when TrainingMessage is changing.

Declaration
private void OnTrainingMessageChanging(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 TrainingMessage is changed.

ReinitializeClassifier()

Declaration
private void ReinitializeClassifier()

ResetModel()

Resets the model but keeps learned classes.

Declaration
public void ResetModel()

SaveModel(string?)

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

Events

OnClassLearned

Event fired when a new class is learned.

Declaration
public event Action<string, Vector<double>>? OnClassLearned
Event Type
Type Description
Action<string, Vector<double>>

OnPrediction

Event fired when a new prediction is made. Args: (outputVector, predictedClass, probabilities, confidence, sampleCount)

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

OnTrainingStateChanged

Event fired when training state changes.

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

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.