Class OnlineLDA

Online Linear Discriminant Analysis using incremental learning. Maximizes between-class scatter while minimizing within-class scatter.

Key difference from PCA:

  • PCA finds directions of maximum variance (unsupervised)
  • LDA finds directions of maximum class separation (supervised)

Inputs (like IncrementalPredictor):

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

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: components (2 or 3), learning rate, re-orthonormalisation interval, stability sample count, regularization. Labels must come from a Trigger; provide labelled observations from multiple classes.

{
  "OnlineLDA": {
    "Type": "onlinelda",
    "Inputs": ["Features", "Labels"],
    "Params": [2, 0.1, 100, 500, 0.0001]
  }
}

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

Inheritance
OnlineLDA
Inherited Members
Namespace: MOSAIC.Models.Analytics
Assembly: MOSAIC.dll
Syntax
public class OnlineLDA : 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

OnlineLDA(string, double, int, double, int, int, double)

Declaration
public OnlineLDA(string name, double desiredRate = 0, int k = 2, double eta0 = 0.1, int reorthEvery = 100, int minStableCount = 500, double regularization = 0.0001)
Parameters
Type Name Description
string name
double desiredRate
int k
double eta0
int reorthEvery
int minStableCount
double regularization

Fields

_Sb

Declaration
private Matrix<double> _Sb
Field Value
Type Description
Matrix<double>

_Sw

Declaration
private Matrix<double> _Sw
Field Value
Type Description
Matrix<double>

_W

Declaration
private Matrix<double> _W
Field Value
Type Description
Matrix<double>

_availableColors

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

_classStats

Declaration
private readonly Dictionary<string, OnlineLDA.ClassStats> _classStats
Field Value
Type Description
Dictionary<string, OnlineLDA.ClassStats>

_colorIndex

Declaration
private int _colorIndex
Field Value
Type Description
int

_currentClassName

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

_currentLabel

Declaration
private string _currentLabel
Field Value
Type Description
string

_d

Declaration
private int _d
Field Value
Type Description
int

_diff

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

_eta0

Declaration
private readonly double _eta0
Field Value
Type Description
double

_globalCount

Declaration
private long _globalCount
Field Value
Type Description
long

_globalMean

Declaration
private Vector<double> _globalMean
Field Value
Type Description
Vector<double>

_isCapturing

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

_isStable

Declaration
private bool _isStable
Field Value
Type Description
bool

_k

Declaration
private readonly int _k
Field Value
Type Description
int

_minStableCount

Declaration
private readonly int _minStableCount
Field Value
Type Description
int

_n

Declaration
private long _n
Field Value
Type Description
long

_prevLD1

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

_prevLD2

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

_prevLD3

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

_regularization

Declaration
private readonly double _regularization
Field Value
Type Description
double

_reorthEvery

Declaration
private readonly int _reorthEvery
Field Value
Type Description
int

_separabilityScores

Declaration
private Vector<double> _separabilityScores
Field Value
Type Description
Vector<double>

_xc

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

_y

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

Properties

CapturedClusters

Declaration
public Dictionary<string, List<Vector<double>>> CapturedClusters { get; }
Property Value
Type Description
Dictionary<string, List<Vector<double>>>

ClassCount

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

ClusterColors

Declaration
public Dictionary<string, string> ClusterColors { get; }
Property Value
Type Description
Dictionary<string, string>

ComponentCount

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

CurrentClassName

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

DumpFilePrefix

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

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

GlobalMean

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

IsCapturing

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

IsStable

Declaration
public bool IsStable { get; }
Property Value
Type Description
bool

Loadings

Declaration
public Matrix<double> Loadings { get; }
Property Value
Type Description
Matrix<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

SampleCount

Declaration
public long SampleCount { get; }
Property Value
Type Description
long

SeparabilityScores

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

Methods

ClearAllClusters()

Declaration
public void ClearAllClusters()

ClearCluster(string)

Declaration
public void ClearCluster(string label)
Parameters
Type Name Description
string label

ConfigureInput(IServiceProvider, JsonModel)

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

CorrectLDSigns()

Declaration
private void CorrectLDSigns()

Dispose()

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

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

EnsureInit(int)

Declaration
private void EnsureInit(int d)
Parameters
Type Name Description
int d

GetJsonParams()

Exports the hyper-parameters in exactly the order ConfigureInput(IServiceProvider, JsonModel) reads them back out of Params: k, eta0, reorthEvery, minStableCount, regularization.

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

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.

OnIsCapturingChanged(bool)

Executes the logic for when IsCapturing just changed.

Declaration
private void OnIsCapturingChanged(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 IsCapturing is changed.

OnIsCapturingChanged(bool, bool)

Executes the logic for when IsCapturing just changed.

Declaration
private void OnIsCapturingChanged(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 IsCapturing is changed.

OnIsCapturingChanging(bool)

Executes the logic for when IsCapturing is changing.

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

The new property value being set.

Remarks

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

OnIsCapturingChanging(bool, bool)

Executes the logic for when IsCapturing is changing.

Declaration
private void OnIsCapturingChanging(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 IsCapturing 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.

Orthonormalize(ref Matrix<double>)

Declaration
private static void Orthonormalize(ref Matrix<double> W)
Parameters
Type Name Description
Matrix<double> W

ProcessLabeled(string, Vector<double>)

Declaration
private void ProcessLabeled(string label, Vector<double> x)
Parameters
Type Name Description
string label
Vector<double> x

Project(Vector<double>)

Declaration
public Vector<double> Project(Vector<double> x)
Parameters
Type Name Description
Vector<double> x
Returns
Type Description
Vector<double>

RecomputeBetweenClassScatter()

Declaration
private void RecomputeBetweenClassScatter()

Reset()

Declaration
public void Reset()

SetCurrentLabel(string)

Declaration
public void SetCurrentLabel(string label)
Parameters
Type Name Description
string label

TryDouble(IReadOnlyList<object>?, int, double)

Declaration
private static double TryDouble(IReadOnlyList<object>? p, int idx, double def)
Parameters
Type Name Description
IReadOnlyList<object> p
int idx
double def
Returns
Type Description
double

TryInt(IReadOnlyList<object>?, int, int)

Declaration
private static int TryInt(IReadOnlyList<object>? p, int idx, int def)
Parameters
Type Name Description
IReadOnlyList<object> p
int idx
int def
Returns
Type Description
int

UpdateProjection()

Declaration
private void UpdateProjection()

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.