scadable add model

Generate an ONNX model wrapper in models/.

scadable add model <name>

Example

scadable add model anomaly-detector

Creates models/anomaly_detector.py:

"""TODO: describe this model."""
from scadable import ONNXModel


class AnomalyDetector(ONNXModel):
    id = "anomaly-detector"
    name = "Anomaly Detector"
    version = "0.1.0"
    file = "models/anomaly-detector.onnx"

    def preprocess(self, *args):
        """Transform raw sensor values into model input tensor."""
        return list(args)

    def inference(self, prediction):
        """Interpret model output into actionable result."""
        return {"score": prediction[0]}

What you fill in

Three things make this real:

  1. Drop the actual ONNX file into models/anomaly-detector.onnx (or wherever file = points).
  2. Implement preprocess(*args) to turn raw sensor values into the tensor the model expects.
  3. Implement inference(prediction) to convert model output into a dict your controllers can use.

Calling from a controller

from models.anomaly_detector import AnomalyDetector

class PredictiveMaintenance(Controller):
    detector = AnomalyDetector()

    @on.interval(1, MINUTES)
    def predict(self):
        result = self.detector.run(
            MotorDrive.vibration,
            MotorDrive.current,
            MotorDrive.temperature,
        )
        if result["score"] > 0.8:
            self.alert("warning", f"Anomaly score: {result['score']}")

The runtime ships the ONNX file inside the bundle, loads it on gateway boot, and runs inference locally. No cloud round-trips.

Required methods

MethodWhat it doesRequired
preprocess(*args)Raw values to model input tensorYes
inference(prediction)Model output to dictYes
run(*args)Chains preprocess to predict to inferenceNo (inherited)

Exit codes

CodeMeaning
0File created
1Model file already exists

Next steps