Class SupervisedPCA
Supervised version of OnlinePCA with manual cluster labelling and capture.
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.
JSON configuration:
{
"MySupervisedPCA": {
"Type": "SupervisedPCA",
"Inputs": [ "FeatureExtractor" ],
"Params": [ 2, 0.2, 100, 500 ],
"DesiredRate": 100,
"Path": "output/spca_log"
}
}
Params: Identical to OnlinePCA:
| Index | Description |
|---|---|
| 0 | k (int) — Number of principal components (2 or 3). Default: 2. |
| 1 | eta0 (double) — Initial learning rate η₀. Default: 0.2. |
| 2 | reorthEvery (int) — QR orthonormalisation frequency. Default: 100. |
| 3 | minStableCount (int) — Minimum samples before stable. Default: 500. |
Path: Optional. If provided, CSV logs of projected outputs are written to this directory.
JSON configuration:
{
"MyPCA": {
"Type": "OnlinePCA",
"Inputs": [ "SlidingWindow1" ],
"Params": [ 2, 0.2, 100, 500 ],
"DesiredRate": 100,
"Path": "output/pca_log"
}
}
Params:
| Index | Description |
|---|---|
| 0 | k (int) — Number of principal components (2 or 3). Default: 2. |
| 1 | eta0 (double) — Initial learning rate η₀. Default: 0.2. |
| 2 | reorthEvery (int) — QR orthonormalisation frequency in samples. Default: 100. |
| 3 | minStableCount (int) — Minimum samples before model is stable. Default: 500. |
Path: Optional. If provided, CSV logs of projected outputs are written to this directory.
How Type, Inputs and Params work · Block catalogue and parameter reference
Implements
Inherited Members
Namespace: MOSAIC.Models.Analytics
Assembly: MOSAIC.dll
Syntax
public class SupervisedPCA : OnlinePCA, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks
Extends OnlinePCA by intercepting projections via PublishProjection(Vector<double>) and storing them in labelled clusters for supervised learning or visualisation purposes.
Usage:
- Call StartCapture(string) with a label to begin capturing.
- Process data normally — projections are automatically stored.
- Call StopCapture() to end the capture session.
- Access captured data via CapturedClusters.
All PCA parameters (learning rate, orthonormalisation, etc.) are inherited from OnlinePCA. See the base class for algorithmic details.
Constructors
SupervisedPCA(string, double, int, double, int, int)
Initializes a new SupervisedPCA instance.
Declaration
public SupervisedPCA(string name, double desiredRate = 0, int k = 2, double eta0 = 0.2, int reorthEvery = 100, int minStableCount = 500)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | Display name of the block. |
| double | desiredRate | Desired processing rate in Hz. 0 = inherited from upstream. |
| int | k | Number of principal components (must be 2 or 3 for visualisation). |
| double | eta0 | Initial learning rate η₀. |
| int | reorthEvery | QR orthonormalisation frequency in samples. |
| int | minStableCount | Minimum samples before the model is considered stable. |
Fields
_availableColors
Palette of distinct colours cycled through as new clusters appear.
Declaration
private readonly List<string> _availableColors
Field Value
| Type | Description |
|---|---|
| List<string> |
_colorIndex
Next colour index in _availableColors.
Declaration
private int _colorIndex
Field Value
| Type | Description |
|---|---|
| int |
_currentCaptureCount
Gets or sets the number of points captured in the current session.
Declaration
[ObservableProperty]
private int _currentCaptureCount
Field Value
| Type | Description |
|---|---|
| int |
_currentLabel
Gets or sets the class label currently being captured.
Declaration
[ObservableProperty]
private string _currentLabel
Field Value
| Type | Description |
|---|---|
| string |
_isCapturing
Gets or sets whether the block is currently capturing data into a labelled cluster.
Declaration
[ObservableProperty]
private bool _isCapturing
Field Value
| Type | Description |
|---|---|
| bool |
_preserveClustersOnReset
Gets or sets whether to preserve captured clusters when Reset() is called.
Declaration
[ObservableProperty]
private bool _preserveClustersOnReset
Field Value
| Type | Description |
|---|---|
| bool |
Remarks
When true (default), calling Reset() preserves all captured cluster data while resetting only the PCA model parameters.
Properties
CapturedClusters
Stores captured cluster points keyed by class label, used for UI scatter-plot visualisation.
Declaration
public Dictionary<string, List<Vector<double>>> CapturedClusters { get; }
Property Value
| Type | Description |
|---|---|
| Dictionary<string, List<Vector<double>>> | Dictionary mapping cluster labels to lists of k-dimensional projection vectors. |
ClusterColors
Hex colour strings assigned to each cluster label for consistent UI rendering.
Declaration
public Dictionary<string, string> ClusterColors { get; }
Property Value
| Type | Description |
|---|---|
| Dictionary<string, string> | Dictionary mapping cluster labels to hex color strings (e.g., |
CurrentCaptureCount
Gets or sets the number of points captured in the current session.
Declaration
[ExcludeFromCodeCoverage]
public int CurrentCaptureCount { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
CurrentLabel
Gets or sets the class label currently being captured.
Declaration
[ExcludeFromCodeCoverage]
public string CurrentLabel { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
DumpFilePrefix
Recordings from this block are named <block>_projection.csv.
Declaration
protected override string DumpFilePrefix { get; }
Property Value
| Type | Description |
|---|---|
| string |
Overrides
IsCapturing
Gets or sets whether the block is currently capturing data into a labelled cluster.
Declaration
[ExcludeFromCodeCoverage]
public bool IsCapturing { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
PreserveClustersOnReset
Gets or sets whether to preserve captured clusters when Reset() is called.
Declaration
[ExcludeFromCodeCoverage]
public bool PreserveClustersOnReset { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Remarks
When true (default), calling Reset() preserves all captured cluster data while resetting only the PCA model parameters.
Methods
ClearAllClusters()
Removes all captured clusters and resets the colour index.
Declaration
public void ClearAllClusters()
ClearCluster(string)
Removes a single cluster and its colour assignment.
Declaration
public void ClearCluster(string label)
Parameters
| Type | Name | Description |
|---|---|---|
| string | label | The label of the cluster to remove. |
ConfigureInput(IServiceProvider, JsonModel)
Creates a SupervisedPCA instance from a JSON model definition.
Declaration
public static SupervisedPCA ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
| Type | Name | Description |
|---|---|---|
| IServiceProvider | sp | Service provider for dependency injection. |
| JsonModel | m | The JSON model. See class-level |
Returns
| Type | Description |
|---|---|
| SupervisedPCA | A fully configured SupervisedPCA instance. |
OnCurrentCaptureCountChanged(int)
Executes the logic for when CurrentCaptureCount just changed.
Declaration
private void OnCurrentCaptureCountChanged(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 CurrentCaptureCount is changed.
OnCurrentCaptureCountChanged(int, int)
Executes the logic for when CurrentCaptureCount just changed.
Declaration
private void OnCurrentCaptureCountChanged(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 CurrentCaptureCount is changed.
OnCurrentCaptureCountChanging(int)
Executes the logic for when CurrentCaptureCount is changing.
Declaration
private void OnCurrentCaptureCountChanging(int value)
Parameters
| Type | Name | Description |
|---|---|---|
| int | value | The new property value being set. |
Remarks
This method is invoked right before the value of CurrentCaptureCount is changed.
OnCurrentCaptureCountChanging(int, int)
Executes the logic for when CurrentCaptureCount is changing.
Declaration
private void OnCurrentCaptureCountChanging(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 CurrentCaptureCount is changed.
OnCurrentLabelChanged(string)
Executes the logic for when CurrentLabel just changed.
Declaration
private void OnCurrentLabelChanged(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 CurrentLabel is changed.
OnCurrentLabelChanged(string?, string)
Executes the logic for when CurrentLabel just changed.
Declaration
private void OnCurrentLabelChanged(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 CurrentLabel is changed.
OnCurrentLabelChanging(string)
Executes the logic for when CurrentLabel is changing.
Declaration
private void OnCurrentLabelChanging(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The new property value being set. |
Remarks
This method is invoked right before the value of CurrentLabel is changed.
OnCurrentLabelChanging(string?, string)
Executes the logic for when CurrentLabel is changing.
Declaration
private void OnCurrentLabelChanging(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 CurrentLabel 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.
OnPreserveClustersOnResetChanged(bool)
Executes the logic for when PreserveClustersOnReset just changed.
Declaration
private void OnPreserveClustersOnResetChanged(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 PreserveClustersOnReset is changed.
OnPreserveClustersOnResetChanged(bool, bool)
Executes the logic for when PreserveClustersOnReset just changed.
Declaration
private void OnPreserveClustersOnResetChanged(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 PreserveClustersOnReset is changed.
OnPreserveClustersOnResetChanging(bool)
Executes the logic for when PreserveClustersOnReset is changing.
Declaration
private void OnPreserveClustersOnResetChanging(bool value)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | value | The new property value being set. |
Remarks
This method is invoked right before the value of PreserveClustersOnReset is changed.
OnPreserveClustersOnResetChanging(bool, bool)
Executes the logic for when PreserveClustersOnReset is changing.
Declaration
private void OnPreserveClustersOnResetChanging(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 PreserveClustersOnReset is changed.
PublishProjection(Vector<double>)
Intercepts projections from the base OnlinePCA to capture them into the current cluster when capture is active, then publishes downstream.
Declaration
protected override void PublishProjection(Vector<double> projection)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector<double> | projection | The k-dimensional principal component projection vector. |
Overrides
Remarks
A clone of the projection is stored in CapturedClusters under CurrentLabel. The original vector is always forwarded to downstream subscribers via Publish(object).
Reset()
Resets the PCA model, optionally preserving captured clusters.
Declaration
public void Reset()
Remarks
Behaviour depends on PreserveClustersOnReset:
StartCapture(string)
Starts capturing projected data with the given label.
Declaration
public void StartCapture(string label)
Parameters
| Type | Name | Description |
|---|---|---|
| string | label | The cluster label for captured points. |
Remarks
If already capturing, the current session is stopped first. If the label already exists, new points are appended to the existing cluster.
A unique colour is automatically assigned to new clusters from the internal palette.
StopCapture()
Stops the current capture session.
Declaration
public void StopCapture()
Remarks
If not currently capturing, this method is a no-op (logged as a warning).
TryDouble(IReadOnlyList<object>?, int, double)
Attempts to parse a double from a parameter list.
Declaration
private static double TryDouble(IReadOnlyList<object>? p, int idx, double def)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<object> | p | Parameter list. |
| int | idx | Index to read. |
| double | def | Default value if parsing fails. |
Returns
| Type | Description |
|---|---|
| double | Parsed double or default. |
TryInt(IReadOnlyList<object>?, int, int)
Attempts to parse an integer from a parameter list.
Declaration
private static int TryInt(IReadOnlyList<object>? p, int idx, int def)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<object> | p | Parameter list. |
| int | idx | Index to read. |
| int | def | Default value if parsing fails. |
Returns
| Type | Description |
|---|---|
| int | Parsed integer or default. |