Class ClassifierBlock

Classification block supporting both batch training from a TriggerBuffer and incremental online learning from a Trigger.

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, class count, training mode. Features must have one value; TrainingBuffer must be a TriggerBuffer containing labelled segments for two classes. Capture and train before interpreting predictions.

{
  "Classifier": {
    "Type": "classifier",
    "Inputs": ["Features", "TrainingBuffer"],
    "Params": ["KNN", 1, 2, "Buffer"]
  }
}

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

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

ClassifierBlock(string, double, int, int, ClassifierType, ClassifierConfig, ClassifierTrainingMode)

Declaration
public ClassifierBlock(string name, double desiredRate, int inputDim, int numClasses, ClassifierType modelType, ClassifierConfig config, ClassifierTrainingMode trainingMode = ClassifierTrainingMode.Buffer)
Parameters
Type Name Description
string name
double desiredRate
int inputDim
int numClasses
ClassifierType modelType
ClassifierConfig config
ClassifierTrainingMode trainingMode

Fields

_confidence

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

_currentLabel

Declaration
private int _currentLabel
Field Value
Type Description
int

_currentTargetName

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

_isTrained

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

_isTraining

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

_lastKnownSegmentCount

Declaration
private int _lastKnownSegmentCount
Field Value
Type Description
int

_lastPredictedClass

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

_lastVizFeedTicks

Declaration
private long _lastVizFeedTicks
Field Value
Type Description
long

_model

Declaration
private IClassifier _model
Field Value
Type Description
IClassifier

_numClasses

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

_sampleCount

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

_segmentCount

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

_trainingMode

Declaration
[ObservableProperty]
private ClassifierTrainingMode _trainingMode
Field Value
Type Description
ClassifierTrainingMode

_triggerBuffer

Declaration
private TriggerBuffer? _triggerBuffer
Field Value
Type Description
TriggerBuffer

_viz

Declaration
private BlockVisualization? _viz
Field Value
Type Description
BlockVisualization

Properties

ClassLabels

Declaration
public List<string> ClassLabels { get; private set; }
Property Value
Type Description
List<string>

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

CurrentTargetName

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

DumpFilePrefix

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

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

InputDim

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

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

LastPredictedClass

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

LastProbabilities

Declaration
public Vector<double>? LastProbabilities { get; private set; }
Property Value
Type Description
Vector<double>

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

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

ModelType

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

NumClasses

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

SampleCount

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

SegmentCount

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

TrainingMode

Declaration
[ExcludeFromCodeCoverage]
public ClassifierTrainingMode TrainingMode { get; set; }
Property Value
Type Description
ClassifierTrainingMode

TrainingStatusText

Declaration
public string TrainingStatusText { 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
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

Live plot for this block. Assigned by the ViewModel, which owns the scope.

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

Setting this re-pushes the publish rate: the rate is normally pushed when it changes, which for these blocks happens during construction — before the ViewModel has handed over the scope — so without this the scope would never learn its time base.

Methods

ArgMax(Vector<double>)

Inline argmax for a Vector — index of the largest element.

Declaration
private static int ArgMax(Vector<double> v)
Parameters
Type Name Description
Vector<double> v
Returns
Type Description
int

ArgMaxRow(Matrix<double>, int)

Inline argmax across a Matrix row — index of the largest element in that row.

Declaration
private static int ArgMaxRow(Matrix<double> m, int row)
Parameters
Type Name Description
Matrix<double> m
int row
Returns
Type Description
int

ChangeModel(ClassifierType, ClassifierConfig?)

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

ConfigureInput(IServiceProvider, JsonModel)

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

Dispose()

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

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

ForceRetrain()

Declaration
public void ForceRetrain()

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()

HandleFeatureInput(Vector<double>)

Declaration
private void HandleFeatureInput(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

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.

OnLastPredictedClassChanged(int)

Executes the logic for when LastPredictedClass just changed.

Declaration
private void OnLastPredictedClassChanged(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 LastPredictedClass is changed.

OnLastPredictedClassChanged(int, int)

Executes the logic for when LastPredictedClass just changed.

Declaration
private void OnLastPredictedClassChanged(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 LastPredictedClass is changed.

OnLastPredictedClassChanging(int)

Executes the logic for when LastPredictedClass is changing.

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

The new property value being set.

Remarks

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

OnLastPredictedClassChanging(int, int)

Executes the logic for when LastPredictedClass is changing.

Declaration
private void OnLastPredictedClassChanging(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 LastPredictedClass 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.

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.

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.

OnSegmentCountChanged(int)

Executes the logic for when SegmentCount just changed.

Declaration
private void OnSegmentCountChanged(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 SegmentCount is changed.

OnSegmentCountChanged(int, int)

Executes the logic for when SegmentCount just changed.

Declaration
private void OnSegmentCountChanged(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 SegmentCount is changed.

OnSegmentCountChanging(int)

Executes the logic for when SegmentCount is changing.

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

The new property value being set.

Remarks

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

OnSegmentCountChanging(int, int)

Executes the logic for when SegmentCount is changing.

Declaration
private void OnSegmentCountChanging(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 SegmentCount is changed.

OnTrainingModeChanged(ClassifierTrainingMode)

Executes the logic for when TrainingMode just changed.

Declaration
private void OnTrainingModeChanged(ClassifierTrainingMode value)
Parameters
Type Name Description
ClassifierTrainingMode value

The new property value that was set.

Remarks

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

OnTrainingModeChanged(ClassifierTrainingMode, ClassifierTrainingMode)

Executes the logic for when TrainingMode just changed.

Declaration
private void OnTrainingModeChanged(ClassifierTrainingMode oldValue, ClassifierTrainingMode newValue)
Parameters
Type Name Description
ClassifierTrainingMode oldValue

The previous property value that was replaced.

ClassifierTrainingMode newValue

The new property value that was set.

Remarks

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

OnTrainingModeChanging(ClassifierTrainingMode)

Executes the logic for when TrainingMode is changing.

Declaration
private void OnTrainingModeChanging(ClassifierTrainingMode value)
Parameters
Type Name Description
ClassifierTrainingMode value

The new property value being set.

Remarks

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

OnTrainingModeChanging(ClassifierTrainingMode, ClassifierTrainingMode)

Executes the logic for when TrainingMode is changing.

Declaration
private void OnTrainingModeChanging(ClassifierTrainingMode oldValue, ClassifierTrainingMode newValue)
Parameters
Type Name Description
ClassifierTrainingMode oldValue

The previous property value that is being replaced.

ClassifierTrainingMode newValue

The new property value being set.

Remarks

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

ResetModel()

Declaration
public void ResetModel()

RetrainFromBuffer()

Reads all segments from the TriggerBuffer, flattens them into (feature, label) pairs, and retrains the active classifier. With the new buffer protocol, target rows are per-data-row; for classification every row of a segment's Targets matrix is identical (the trigger held a single class for the whole capture), so we read row 0 and argmax across columns to recover the integer class index.

Declaration
private void RetrainFromBuffer()

SetTrainingMode(ClassifierTrainingMode)

Declaration
public void SetTrainingMode(ClassifierTrainingMode mode)
Parameters
Type Name Description
ClassifierTrainingMode mode

Events

OnLabelsUpdated

Declaration
public event Action<List<string>>? OnLabelsUpdated
Event Type
Type Description
Action<List<string>>

OnPrediction

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

OnRetrained

Declaration
public event Action<int, int>? OnRetrained
Event Type
Type Description
Action<int, int>

OnTrainingStateChanged

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.