Making Predictions From a C# Digital Twin Model
Trained machine learning algorithms are accessible in C# digital twin models via an interface called IAnomalyDetectionProvider.
namespace Scaleout.Streaming.DigitalTwin.Core
{
/// <summary>
/// Encapsulates the capabilities of a ScaleOut real-time digital twin
/// anomaly detection provider
/// </summary>
public interface IAnomalyDetectionProvider
{
/// <summary>
/// Detects anomalies by using the trained algorithm and the provided property values
/// </summary>
/// <param name="properties">A dictionary of the properties to use for the prediction</param>
/// <returns>True if an anomaly is detected, False otherwise</returns>
bool DetectAnomaly(Dictionary<string, float> properties);
}
}
Each IAnomalyDetectionProvider is associated with a trained machine learning algorithm, which is provided when deploying the digital twin model. The DetectAnomaly API takes one parameter: a dictionary containing property names and their associated values. Those properties must correspond to the properties defined in the trained machine learning algorithm.
Associate a Machine Learning Algorithm with your Model
At deployment time, you will have the option to use pre-trained machine learning algorithms in your digital twin model. You will provide the zip file you created with the ScaleOut Machine Learning Training Tool along with your digital twin zip file. This will make the algorithm available to obtain predictions from any instance.
See the ScaleOut Digital Twins UI Guide for more information on how to deploy digital twins with machine learning algorithms.
Looking up the Anomaly Detection Provider from Code and Get a Prediction
To access an IAnomalyDetectionProvider, the C# code will use the AnomalyDetectionProviders property of the ProcessingContext instance. The ProcessingContext keeps track of all trained algorithms provided at deployment. The model’s MessageProcessor implementation will look it up by name, using the name assigned when exporting the machine learning algorithm in the Machine Learning Training Tool.
The sample below demonstrates how to look up the algorithm trained by the Machine Learning Training Tool named “Overheating”.
public override async Task<ProcessingResult> ProcessMessageAsync(ProcessingContext<SensorsRTModel> context, SensorsRTModel digitalTwin, byte[] msgBytes)
{
// Deserialize the message
ExampleMessage? message = System.Text.Json.JsonSerializer.Deserialize<ExampleMessage>(msgBytes);
if (message is null)
{
_logger.LogWarning("Message deserialization failed.");
return ProcessingResult.NoUpdate;
}
// Look up the Anomaly Detection Provider by name from the context
var algorithm = context.AnomalyDetectionProviders["Overheating"];
// ...
After acquiring a reference to the trained algorithm, generate a prediction by calling the DetectAnomaly API with a dictionary with all properties that the algorithm was trained on. The API will return TRUE if the machine learning algorithm determines that the provided values constitute an anomaly, and FALSE otherwise.
For this sample, log a message indicating whether the provided values are detected as anomalies.
Dictionary<string, Single> properties = new Dictionary<string, float>();
if (algorithm != null)
{
// Build the dictionary of properties with the values we want to get a prediction for,
// in this case, values obtained from the new message.
properties[nameof(SensorsRTModel.Temperature)] = message.Temperature;
properties[nameof(SensorsRTModel.RPM)] = message.RPM;
properties[nameof(SensorsRTModel.Friction)] = message.Friction;
// Run the anomaly detection on the new provided values
if (algorithm.DetectAnomaly(properties) == true)
{
// In this sample, log a message
await context.LogMessageAsync(LogSeverity.Error, $"An anomaly has been detected : {DisplayProperties(properties)}");
}
else
{
await context.LogMessageAsync(LogSeverity.Informational, $"No anomaly: {DisplayProperties(properties)}");
}
}