ONNX Converter#

Export fitted Iguanas rule strings to a self-contained ONNX binary classifier that can be served by any ONNX-compatible runtime (e.g. onnxruntime, ONNX.js, Triton, Azure ML, etc.).

Functions#

rules_to_onnx#

iguanas.onnx_converter.rules_to_onnx(rules: str | list[str], dtype: str = 'f32') → onnx.onnx_ml_pb2.ModelProto[source]#

Convert Iguanas rule strings to an ONNX binary classifier.

Parameters:
  • rules (str | list[str]) – One rule string or a list of rule strings. When a list is supplied, the rules are OR’d together — the model outputs 1 if any rule fires. Each rule must use the X["col"] op val notation produced by Iguanas, where op is one of >=, >, <=, <, ==, !=, and conditions may be combined with & / |.

  • dtype (str, default "f32") – Numeric dtype for the input tensor and thresholds. "f32" → float32, "f64" → float64.

Returns:

ONNX model with:

  • input X shape [N, num_features] (dtype as requested)

  • output prediction shape [N] (int64, values 0 or 1)

Feature names are stored in metadata_props as "feature_0", "feature_1", … in first-appearance order.

Return type:

onnx.ModelProto

Raises:

ValueError – If rules is empty, dtype is not "f32" or "f64", or a rule string is syntactically invalid or uses unsupported node types.

Notes

Security. This conversion parses each rule with ast and emits a static ONNX graph; it never calls eval(), and the resulting model executes no Python at scoring time. Only a whitelist of AST nodes (subscripted column access, numeric-literal comparisons, and &/|) is accepted — anything else raises ValueError rather than being executed. This makes ONNX export the recommended production deployment path, in contrast to apply_rules(), which compiles rule strings with eval() and therefore requires trusted rule sources.

See also

iguanas.rule_evaluation.apply_rules

In-process evaluation via eval(); requires trusted rule strings.

iguanas.rule_evaluation.apply_rules_lazy

Lazy in-process evaluation, same eval() caveat.