pdstools.infinity.resources.prediction_studio.local_model_utils¶
Attributes¶
Exceptions¶
Exception for errors during ONNX conversion and save. |
|
Exception for errors during ONNX validation. |
Classes¶
Supported outcome types for local Prediction Studio model metadata. |
|
A single predictor (feature) in an ONNX model. |
|
Model output metadata stored in the embedded Pega metadata block. |
|
Top-level Pega metadata embedded in an ONNX model. |
|
Wrapper around a PMML file path. |
|
Wrapper around an H2O model file path. |
|
Wrapper around an in-memory ONNX model plus Pega metadata helpers. |
Module Contents¶
- logger¶
- PEGA_METADATA = 'pegaMetadata'¶
- class OutcomeType(*args, **kwds)¶
Bases:
enum.EnumSupported outcome types for local Prediction Studio model metadata.
- BINARY = 'binary'¶
- CATEGORICAL = 'categorical'¶
- CONTINUOUS = 'continuous'¶
- class Predictor(/, **data: Any)¶
Bases:
pydantic.BaseModelA single predictor (feature) in an ONNX model.
Automatic name derivation — When pega_property is supplied (e.g.
".Customer.Age") and name is omitted, the predictornameis automatically set to the leaf segment of the property path ("Age"). Pega Prediction Studio auto‑maps predictors by matchingnameagainst its data‑model properties, so this guarantees correct field mapping on upload without any manual work.If name is provided explicitly it is always used as‑is.
See also
Metadata.build_predictor_listBatch‑build predictors from a list of Pega property paths or plain feature names.
- Parameters:
data (Any)
- validate_input_name(v)¶
- validate_name(v)¶
- validate_index(v)¶
- validate_data_type(v)¶
- class Output(/, **data: Any)¶
Bases:
pydantic.BaseModelModel output metadata stored in the embedded Pega metadata block.
- Parameters:
data (Any)
- validate_label_name(v)¶
- class Metadata(/, **data: Any)¶
Bases:
pydantic.BaseModelTop-level Pega metadata embedded in an ONNX model.
- Parameters:
data (Any)
- type: OutcomeType | None = None¶
- validate_type(v, values)¶
- validate_output(v, values)¶
- static build_predictor_list(features: list[str], input_name: str = 'features', *, data_types: list[str] | dict[str, str] | None = None) list[Predictor]¶
Build a predictor list from feature names or Pega property paths.
This is the recommended one‑liner for constructing predictors.
Each entry in
featurescan be a Pega property path starting with"."(for example".Customer.Age"). The leaf segment becomes the predictorname("Age") and the full path is stored aspega_propertyso Pega Prediction Studio auto-maps the field on upload.Each entry can also be a plain feature name (for example
"Age"). In that case the value is used as-is fornameandpega_propertyis left unset.Indices are assigned automatically (1‑based) in the order the features appear.
- Parameters:
features (list[str]) – Ordered list of feature identifiers. The order must match the column order in the ONNX input tensor. Accepts any mix of plain names and Pega property paths.
input_name (str) – Name of the ONNX input node. Default
"features".data_types (list[str] | dict[str, str] | None) –
Optional type annotations for features.
Accepts either a list of
"Numeric"/"Symbolic"values, one per feature and matchingfeatureslength, or a dict mapping feature names (the leaf segment for Pega paths) to"Numeric"or"Symbolic". Only the features present in the dict are overridden; the rest default to"Numeric".When
Noneevery feature defaults to"Numeric".
- Return type:
Examples
>>> # Using Pega property paths (recommended for auto-mapping): >>> Metadata.build_predictor_list( ... [".Customer.Age", ".Customer.Tenure", ".Customer.MonthlyCharges"], ... )
>>> # Using plain feature names: >>> Metadata.build_predictor_list(["Age", "Tenure", "MonthlyCharges"]) ...
>>> # Sparse overrides via dict: >>> Metadata.build_predictor_list( ... [".Customer.Age", ".Customer.ContractType"], ... data_types={"ContractType": "Symbolic"}, ... )
- exception ONNXModelCreationError¶
Bases:
ExceptionException for errors during ONNX conversion and save.
- exception ONNXModelValidationError¶
Bases:
pdstools.infinity.resources.prediction_studio.base.ModelValidationErrorException for errors during ONNX validation.
- class PMMLModel(file_path: str)¶
Bases:
pdstools.infinity.resources.prediction_studio.base.LocalModelWrapper around a PMML file path.
- Parameters:
file_path (str)
- class H2OModel(file_path: str)¶
Bases:
pdstools.infinity.resources.prediction_studio.base.LocalModelWrapper around an H2O model file path.
- Parameters:
file_path (str)
- class ONNXModel(model: onnx.ModelProto)¶
Bases:
pdstools.infinity.resources.prediction_studio.base.LocalModelWrapper around an in-memory ONNX model plus Pega metadata helpers.
- Parameters:
model (onnx.ModelProto)
- classmethod from_onnx_proto(model: onnx.ModelProto) ONNXModel¶
Creates an ONNXModel object.
- Parameters:
model (ModelProto) – An onnx ModelProto object
- Returns:
An instance of the ONNXModel class.
- Return type:
- Raises:
ImportError – If the optional dependencies for ONNX Conversion are not installed.
ONNXModelCreationError – If the ONNXModel object cannot be created.
- classmethod from_sklearn_pipeline(model: sklearn.pipeline.Pipeline, initial_types: list) ONNXModel¶
Creates an ONNXModel object.
- Parameters:
model (Pipeline) – A sklearn Pipeline object
initial_types (list) – A list of initial types for the model’s input variables if the model is a Sklearn Pipeline object.
- Returns:
An instance of the ONNXModel class.
- Return type:
- Raises:
ImportError – If the optional dependencies for ONNX Conversion are not installed.
ONNXModelCreationError – If the ONNXModel object cannot be created.
- classmethod from_pytorch(model, dummy_input, *, input_names: list[str] | None = None, output_names: list[str] | None = None, opset_version: int = 17, fixed_batch_size: bool = True) ONNXModel¶
Create an ONNXModel from a PyTorch
nn.Module.The model is exported via
torch.onnx.exportwith static shapes. When fixed_batch_size isTrue(the default), any remaining dynamic dimensions are replaced with a batch size of 1 so that the resulting ONNX file is accepted by Pega Prediction Studio.- Parameters:
model – A PyTorch
nn.Module(already in eval mode is recommended).dummy_input – Example input tensor(s) matching the model’s forward signature.
input_names (list[str] | None) – Optional list of ONNX input node names. Defaults to
["input"].output_names (list[str] | None) – Optional list of ONNX output node names. Defaults to
["output"].opset_version (int) – ONNX opset version. Default
17.fixed_batch_size (bool) – Replace dynamic dimensions with batch size 1.
- Return type:
- Raises:
ONNXModelCreationError – If PyTorch is not installed or the export fails.
- get_metadata() Metadata | None¶
Return the embedded
MetadataorNoneif absent.- Return type:
Metadata | None
- add_metadata(metadata: Metadata) ONNXModel¶
Adds metadata to the ONNX model.
- Parameters:
metadata (Meta) – The metadata to be added.
- Returns:
The ONNXModel object with the added metadata.
- Return type:
- Raises:
ImportError – If the optional dependencies for ONNX Metadata addition are not installed.
- validate() bool¶
Validates an ONNX model.
- Raises:
ImportError – If the optional dependencies for ONNX Validation are not installed.
ONNXModelValidationError – If the model is invalid or if the validation process fails.
- Return type:
- run(test_data: dict)¶
Run the prediction using the provided test data.
- Parameters:
test_data (dict) – The test data to be used for prediction. It is a dictionary where each key is a column name from the dataset, and each value is a NumPy array representing the column data as a vector.
- Returns:
The prediction result.
- Return type:
Any
Examples
test_datashould look like:{ "column1": array([[value1], [value2], [value3]]), "column2": array([[value4], [value5], [value6]]), "column3": array([[value7], [value8], [value9]]), }
- save(onnx_file_path: str)¶
Saves the ONNX model to the specified file path.
- Parameters:
onnx_file_path (str) – The file path where the ONNX model should be saved.
- Raises:
ImportError – If the optional dependencies for ONNX Conversion are not installed.