You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Current »

Edge AI with ML.NET (Tutorial) demonstrates using ML.NET 4.0 for real-time anomaly detection on sensor data using Script Tasks.

Prerequisites:

  • Complete Scripts & Data Enrichment (Tutorial)
  • Tags for sensor monitoring
  • ML.NET 4.0 references (included in FrameworX)

In this page:



Scripts → Tutorial | Concept | How-to Guide | Reference



Step 1: Create Monitoring Tags

  1. Navigate to Unified Namespace → Tags
  2. Create tags:
    • Tag.SensorValue (Double) - Current reading
    • Tag.AnomalyScore (Double) - Detection score
    • Tag.IsAnomaly (Boolean) - Alert flag
    • Tag.Threshold (Double) - Detection threshold (default: 0.3)

Step 2: Create ML Detection Task

  1. Go to Scripts → Tasks
  2. Create task: AnomalyDetector
  3. Set trigger: Period = 1000ms
  4. Add code:

csharp

// Simple spike detection using ML.NET
using Microsoft.ML;
using Microsoft.ML.Data;

// Static ML context (initialized once)
if (@Tag.MLContext == null)
{
    @Tag.MLContext = new MLContext(seed: 0);
    @Tag.DetectionEngine = InitializeDetector();
}

// Data class for ML model
public class SensorData
{
    public float Value { get; set; }
}

public class AnomalyPrediction
{
    [VectorType(3)]
    public double[] Prediction { get; set; }
}

// Initialize detector (runs once)
private ITransformer InitializeDetector()
{
    var dataView = @Tag.MLContext.Data.LoadFromEnumerable(new List<SensorData>());
    
    var pipeline = @Tag.MLContext.Transforms
        .DetectSpikeBySsa(
            outputColumnName: "Prediction",
            inputColumnName: "Value",
            confidence: 95,
            pvalueHistoryLength: 30,
            trainingWindowSize: 90,
            seasonalityWindowSize: 30);
    
    return pipeline.Fit(dataView);
}

// Detection logic (runs every second)
var currentValue = (float)@Tag.SensorValue;

var data = new SensorData { Value = currentValue };
var prediction = @Tag.DetectionEngine.Transform(
    @Tag.MLContext.Data.LoadFromEnumerable(new[] { data }));

var result = @Tag.MLContext.Data
    .CreateEnumerable<AnomalyPrediction>(prediction, false)
    .First();

// Update tags with results
@Tag.AnomalyScore = result.Prediction[0];  // Spike score
@Tag.IsAnomaly = result.Prediction[0] > @Tag.Threshold;

// Log anomalies
if (@Tag.IsAnomaly)
{
    @Alarm.GlobalSettings.AuditTrail.AddCustomMessage(
        $"Anomaly detected: Sensor={currentValue:F2}, Score={result.Prediction[0]:F3}");
}

Step 3: Create Simple Simulator

  1. Create task: SensorSimulator
  2. Set trigger: Period = 500ms
  3. Add simulation code:

csharp

// Simulate normal sensor data with occasional spikes
Random rand = new Random();
double baseValue = 50.0;
double noise = rand.NextDouble() * 5 - 2.5;

// Inject anomaly occasionally (5% chance)
if (rand.NextDouble() < 0.05)
{
    @Tag.SensorValue = baseValue + (rand.NextDouble() * 30 + 20);  // Spike
}
else
{
    @Tag.SensorValue = baseValue + noise;  // Normal variation
}

Step 4: Create Monitoring Display

  1. Create display with:
    • Trend chart for SensorValue
    • Indicator for IsAnomaly
    • Text display for AnomalyScore
    • Threshold adjustment slider

Step 5: Test ML Detection

  1. Start runtime
  2. Observe sensor simulation
  3. Watch for anomaly detection
  4. Adjust threshold as needed
  5. Check audit trail for logged anomalies

Next Steps

  • Advanced MCP Tools → Complex multi-tool scenarios
  • ML.NET Models → Regression and classification
  • Edge Computing → Deploy to field devices

These tutorials provide simple, practical starting points for both MCP Tools and ML.NET integration, focusing on real industrial scenarios while keeping complexity minimal for learning purposes.


In this section...

The root page @parent could not be found in space 93Draft.



  • No labels