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.

Declaring the Anomaly Detection Provider in AppSettings.json

Add an entry for the newly created anomaly detection provider in the AppSettings.json file. Since we used the reference name “Overheating” in the Training Tool, we accomplish this by adding the following:

"AnomalyDetectionProviders" : [ "Overheating" ],

This will be used at deploy time to load the trained algorithm and make it available for the C# digital model.

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 ProcessingResult ProcessMessages(ProcessingContext context, SensorsRTModel digitalTwin, IEnumerable<DigitalTwinMessage> newMessages)
{
  // 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)] = newReading.Temperature;
  properties[nameof(SensorsRTModel.RPM)] = newReading.RPM;
  properties[nameof(SensorsRTModel.Friction)] = newReading.Friction;

  // Run the anomaly detection on the new provided values
  if (algorithm.DetectAnomaly(properties) == true)
  {
      // In this sample, log a message
      context.LogMessage(LogSeverity.Error, $"An anomaly has been detected : {DisplayProperties(properties)}");
  }
  else
  {
      context.LogMessage(LogSeverity.Informational, $"No anomaly: {DisplayProperties(properties)}");
  }
}